com.sap.dirigible.ide.ui.rap.entry.DirigibleActionBarAdvisor.java Source code

Java tutorial

Introduction

Here is the source code for com.sap.dirigible.ide.ui.rap.entry.DirigibleActionBarAdvisor.java

Source

/*******************************************************************************
 * Copyright (c) 2014 SAP AG or an SAP affiliate company. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *******************************************************************************/

package com.sap.dirigible.ide.ui.rap.entry;

import java.net.URL;
import java.util.Dictionary;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;

import com.sap.dirigible.ide.common.CommonParameters;
import com.sap.dirigible.ide.logging.Logger;

public class DirigibleActionBarAdvisor extends ActionBarAdvisor {

    private static final String HELP = Messages.DirigibleActionBarAdvisor_HELP;

    private static final String WINDOW = Messages.DirigibleActionBarAdvisor_WINDOW;

    private static final String RUNNING_ON_RAP_VERSION = Messages.DirigibleActionBarAdvisor_RUNNING_ON_RAP_VERSION;

    private static final String WORKBENCH = Messages.DirigibleActionBarAdvisor_WORKBENCH;

    private static final String HTTP_DIRIGIBLE_HOME = "http://www.dirigible.io"; //$NON-NLS-1$

    private static final String HTTP_DIRIGIBLE_HELP = "http://help.dirigible.io"; //$NON-NLS-1$

    private static final String HTTP_DIRIGIBLE_SAMPLES = "http://samples.dirigible.io"; //$NON-NLS-1$

    private static final String HTTP_DIRIGIBLE_FORUM = "http://forum.dirigible.io"; //$NON-NLS-1$

    private static final String ABOUT = Messages.DirigibleActionBarAdvisor_ABOUT;

    private static final String DIRIGIBLE_HOME = Messages.DirigibleActionBarAdvisor_DIRIGIBLE_HOME;

    private static final String DIRIGIBLE_HELP = Messages.DirigibleActionBarAdvisor_DIRIGIBLE_HELP;

    private static final String DIRIGIBLE_SAMPLES = Messages.DirigibleActionBarAdvisor_DIRIGIBLE_SAMPLES;

    private static final String DIRIGIBLE_FORUM = Messages.DirigibleActionBarAdvisor_DIRIGIBLE_FORUM;

    private static final String SHOW_VIEW = Messages.DirigibleActionBarAdvisor_SHOW_VIEW;

    private static final String SHOW_PERSPECTIVE = Messages.DirigibleActionBarAdvisor_SHOW_PERSPECTIVE;

    private static final Logger logger = Logger.getLogger(DirigibleActionBarAdvisor.class);

    private static final String WEB_PAGE_ERROR = Messages.DirigibleActionBarAdvisor_WEB_PAGE_ERROR;
    private static final String COULD_NOT_OPEN_WEB_PAGE = Messages.DirigibleActionBarAdvisor_COULD_NOT_OPEN_WEB_PAGE;

    private IWorkbenchAction newAction;
    private IWorkbenchAction saveAction;
    private IWorkbenchAction saveAllAction;
    private IWorkbenchAction importAction;
    private IWorkbenchAction exportAction;
    private IWorkbenchAction exitAction;

    private IWorkbenchAction undoAction;
    private IWorkbenchAction redoAction;

    private Action dirigibleHomeAction;
    private Action dirigibleHelpAction;
    private Action dirigibleSamplesAction;
    private Action dirigibleForumAction;
    private Action aboutAction;
    private MenuManager showPerspectiveMenuMgr;
    private MenuManager showViewMenuMgr;
    private IWorkbenchAction preferencesAction;

    // private Action cheatSheetsAction;

    public DirigibleActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }

    protected void makeActions(final IWorkbenchWindow window) {
        newAction = ActionFactory.NEW.create(window);
        register(newAction);

        saveAction = ActionFactory.SAVE.create(window);
        register(saveAction);

        saveAllAction = ActionFactory.SAVE_ALL.create(window);
        register(saveAllAction);

        importAction = ActionFactory.IMPORT.create(window);
        register(importAction);

        exportAction = ActionFactory.EXPORT.create(window);
        register(exportAction);

        exitAction = ActionFactory.QUIT.create(window);
        register(exitAction);

        undoAction = ActionFactory.UNDO.create(window);
        register(undoAction);

        redoAction = ActionFactory.REDO.create(window);
        register(redoAction);

        showPerspectiveMenuMgr = new MenuManager(SHOW_PERSPECTIVE, "showPerspective"); //$NON-NLS-1$

        showViewMenuMgr = new MenuManager(SHOW_VIEW, "showView"); //$NON-NLS-1$
        IContributionItem showViewMenu = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
        showViewMenuMgr.add(showViewMenu);

        preferencesAction = ActionFactory.PREFERENCES.create(window);
        register(preferencesAction);

        // helpAction = ActionFactory.HELP_CONTENTS.create(window);
        // register(helpAction);
        //
        // cheatSheetsAction = new Open Action() {
        // public void run() {
        // onWebPageAction(window.getWorkbench());
        // }
        // };
        // cheatSheetsAction.setText("Cheat Sheets");
        // cheatSheetsAction.setId("org.eclipse.ui.cheatsheets.openCheatSheet");
        // register(cheatSheetsAction);

        // Home
        dirigibleHomeAction = new Action() {
            private static final long serialVersionUID = 7112545561507879756L;

            public void run() {
                onWebPageAction(window.getWorkbench(), dirigibleHomeAction, HTTP_DIRIGIBLE_HOME);
            }
        };
        dirigibleHomeAction.setText(DIRIGIBLE_HOME);
        dirigibleHomeAction.setId("com.sap.dirigible.ide.home"); //$NON-NLS-1$
        register(dirigibleHomeAction);

        // Help
        dirigibleHelpAction = new Action() {
            private static final long serialVersionUID = 7112545561507879756L;

            public void run() {
                onWebPageAction(window.getWorkbench(), dirigibleHelpAction, HTTP_DIRIGIBLE_HELP);
            }
        };
        dirigibleHelpAction.setText(DIRIGIBLE_HELP);
        dirigibleHelpAction.setId("com.sap.dirigible.ide.help"); //$NON-NLS-1$
        register(dirigibleHelpAction);

        // Samples
        dirigibleSamplesAction = new Action() {
            private static final long serialVersionUID = 7112545561507879756L;

            public void run() {
                onWebPageAction(window.getWorkbench(), dirigibleSamplesAction, HTTP_DIRIGIBLE_SAMPLES);
            }
        };
        dirigibleSamplesAction.setText(DIRIGIBLE_SAMPLES);
        dirigibleSamplesAction.setId("com.sap.dirigible.ide.samples"); //$NON-NLS-1$
        register(dirigibleSamplesAction);

        // Forum
        dirigibleForumAction = new Action() {
            private static final long serialVersionUID = 7112545561507879756L;

            public void run() {
                onWebPageAction(window.getWorkbench(), dirigibleForumAction, HTTP_DIRIGIBLE_FORUM);
            }
        };
        dirigibleForumAction.setText(DIRIGIBLE_FORUM);
        dirigibleForumAction.setId("com.sap.dirigible.ide.forum"); //$NON-NLS-1$
        register(dirigibleForumAction);

        // About
        aboutAction = new Action() {
            private static final long serialVersionUID = 8477239924815783883L;

            public void run() {
                onAboutAction(window.getShell());
            }
        };
        aboutAction.setText(ABOUT);
        aboutAction.setId("com.sap.dirigible.ide.about"); //$NON-NLS-1$
        register(aboutAction);
    }

    private void onWebPageAction(IWorkbench workbench, Action action, String url) {
        IWorkbenchBrowserSupport browserSupport = workbench.getBrowserSupport();
        int style = IWorkbenchBrowserSupport.AS_EXTERNAL;
        try {
            IWebBrowser browser = browserSupport.createBrowser(style, action.getId(), "", ""); //$NON-NLS-1$ //$NON-NLS-2$
            browser.openURL(new URL(url));
        } catch (Exception e) {
            logger.error(COULD_NOT_OPEN_WEB_PAGE, e);
            MessageDialog.openError(null, WEB_PAGE_ERROR, COULD_NOT_OPEN_WEB_PAGE);
        }
    }

    private void onAboutAction(Shell shell) {
        Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
        Dictionary<?, ?> headers = bundle.getHeaders();
        Object version = headers.get(Constants.BUNDLE_VERSION);
        MessageDialog.openInformation(shell, CommonParameters.DIRIGIBLE_PRODUCT_NAME + WORKBENCH,
                CommonParameters.DIRIGIBLE_PRODUCT_NAME + " " //$NON-NLS-1$
                        + CommonParameters.DIRIGIBLE_PRODUCT_VERSION + "\n" //$NON-NLS-1$
                        + RUNNING_ON_RAP_VERSION + version);

    }

    protected void fillMenuBar(IMenuManager menuBar) {
        // MenuManager newMenu = new MenuManager("New", "id.file");
        // newMenu.add(newAction);

        // File menu
        MenuManager fileMenu = new MenuManager(Messages.DirigibleActionBarAdvisor_FILE, "id.file"); //$NON-NLS-2$
        // Other plug-ins can contribute there actions here
        fileMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

        // fileMenu.add(newMenu);
        fileMenu.add(new Separator());
        fileMenu.add(saveAction);
        fileMenu.add(saveAllAction);
        // fileMenu.add(new Separator());
        // fileMenu.add(importAction);
        // fileMenu.add(exportAction);
        fileMenu.add(new Separator());
        fileMenu.add(exitAction);
        menuBar.add(fileMenu);

        // Edit menu
        // MenuManager editMenu = new MenuManager("Edit", "id.edit");
        // editMenu.add(undoAction);
        // editMenu.add(redoAction);
        // editMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
        // menuBar.add(editMenu);

        // Window menu
        MenuManager windowMenu = new MenuManager(WINDOW, IWorkbenchActionConstants.M_WINDOW);
        windowMenu.add(showPerspectiveMenuMgr);
        windowMenu.add(showViewMenuMgr);
        windowMenu.add(new Separator());
        windowMenu.add(preferencesAction);
        menuBar.add(windowMenu);

        // Help menu
        MenuManager helpMenu = new MenuManager(HELP, IWorkbenchActionConstants.M_HELP);
        helpMenu.add(dirigibleHelpAction);
        helpMenu.add(new Separator());
        helpMenu.add(dirigibleHomeAction);
        helpMenu.add(dirigibleSamplesAction);
        helpMenu.add(dirigibleForumAction);
        // helpMenu.add(helpAction);
        helpMenu.add(new Separator("about")); //$NON-NLS-1$
        helpMenu.add(aboutAction);
        menuBar.add(helpMenu);
    }

    protected void fillCoolBar(ICoolBarManager coolBar) {
        IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
        toolbar.add(undoAction);
        toolbar.add(redoAction);
        toolbar.add(saveAction);
        toolbar.add(saveAllAction);
        toolbar.add(new Separator("control")); //$NON-NLS-1$
        coolBar.add(new ToolBarContributionItem(toolbar, "main")); //$NON-NLS-1$
    }

    protected void fillStatusLine(IStatusLineManager statusLine) {
        statusLine.add(dirigibleHomeAction);
        statusLine.add(dirigibleForumAction);
        statusLine.add(aboutAction);
    }
}