org.csstudio.ui.menu.app.ApplicationActionBarAdvisor.java Source code

Java tutorial

Introduction

Here is the source code for org.csstudio.ui.menu.app.ApplicationActionBarAdvisor.java

Source

/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.ui.menu.app;

import org.eclipse.core.runtime.IExtension;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;
import org.eclipse.ui.menus.IMenuService;
import org.eclipse.ui.part.CoolItemGroupMarker;

/** {@link ActionBarAdvisor} that can be called by CSS
 *  application startup code to create the menu and tool bar.
 *
 *  <p>The menu bar is mostly empty, only providing the "additions"
 *  section that is used by the contributions in plugin.xml.
 *
 *  <p>The toolbar also mostly defines sections used by contributions
 *  from plugin.xml.
 *
 *  <p>Some actions are created for Eclipse command names
 *  in the help menu that have no default implementation.
 *
 *  @author Kay Kasemir
 *  @author Xihui Chen
 *  @author mfurseman Remove Eclipse menu items that don't apply
 */
@SuppressWarnings("restriction") // For removing internal actions
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
    /** Toolbar ID of switch user and logout toolbar */
    private static final String TOOLBAR_USER = "user"; //$NON-NLS-1$

    final private IWorkbenchWindow window;

    private IWorkbenchAction lock_toolbar, edit_actionsets, save;

    /** Initialize */
    @SuppressWarnings("nls")
    public ApplicationActionBarAdvisor(final IActionBarConfigurer configurer) {
        super(configurer);
        window = configurer.getWindowConfigurer().getWindow();

        // Remove menu items that are not suitable in CS-Studio
        removeActionById("org.eclipse.ui.edit.text.actionSet.navigation");
        removeActionById("org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo");
        removeActionById("org.eclipse.ui.edit.text.actionSet.annotationNavigation");
    }

    /** Create actions.
     *
     *  <p>Some of these actions are created to programmatically
     *  add them to the toolbar.
     *  Other actions like save_as are not used at all in here,
     *  but they need to be created because the plugin.xml
     *  registers their command ID in the menu bar,
     *  and the action actually implements the handler.
     *  The actions also provide the dynamic enablement.
     *
     *  {@inheritDoc}
     */
    @Override
    protected void makeActions(final IWorkbenchWindow window) {
        lock_toolbar = ActionFactory.LOCK_TOOL_BAR.create(window);
        register(lock_toolbar);

        edit_actionsets = ActionFactory.EDIT_ACTION_SETS.create(window);
        register(edit_actionsets);

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

        register(ActionFactory.SAVE_AS.create(window));

        if (window.getWorkbench().getIntroManager().hasIntro())
            register(ActionFactory.INTRO.create(window));

        register(ActionFactory.HELP_CONTENTS.create(window));
    }

    /** {@inheritDoc} */
    @Override
    protected void fillMenuBar(final IMenuManager menubar) {
        // Placeholder for possible additions, rest filled from plugin.xml
        menubar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
    }

    /** {@inheritDoc} */
    @Override
    protected void fillCoolBar(final ICoolBarManager coolbar) {
        // Set up the context Menu
        final MenuManager coolbarPopupMenuManager = new MenuManager();
        coolbarPopupMenuManager.add(new ActionContributionItem(lock_toolbar));
        coolbarPopupMenuManager.add(new ActionContributionItem(edit_actionsets));
        coolbar.setContextMenuManager(coolbarPopupMenuManager);
        final IMenuService menuService = (IMenuService) window.getService(IMenuService.class);
        menuService.populateContributionManager(coolbarPopupMenuManager, "popup:windowCoolbarContextMenu"); //$NON-NLS-1$

        // 'File' section of the cool bar
        final IToolBarManager file_bar = new ToolBarManager();
        // File 'new' and 'save' actions
        file_bar.add(ActionFactory.NEW.create(window));
        file_bar.add(save);
        file_bar.add(new CoolItemGroupMarker(IWorkbenchActionConstants.FILE_END));
        coolbar.add(new ToolBarContributionItem(file_bar, IWorkbenchActionConstants.M_FILE));

        // 'User' section of the cool bar
        final IToolBarManager user_bar = new ToolBarManager();
        coolbar.add(new ToolBarContributionItem(user_bar, TOOLBAR_USER));

        // Explicitly add "additions" and "editor" to work around https://bugs.eclipse.org/bugs/show_bug.cgi?id=422651
        // After a restart, merging of persisted model, PerspectiveSpacer, contributions resulted in re-arranged toolbar with E4.
        coolbar.add(new CoolItemGroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        coolbar.add(new CoolItemGroupMarker(IWorkbenchActionConstants.GROUP_EDITOR));
    }

    /**
     * Remove the menu item specified.
     * @param actionSetId The ID of the action to be removed.
     */
    private void removeActionById(String actionSetId) {
        // Use of an internal API is required to remove actions that are provided
        // by including Eclipse bundles.
        ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
        IActionSetDescriptor[] actionSets = reg.getActionSets();
        for (int i = 0; i < actionSets.length; i++) {
            if (actionSets[i].getId().equals(actionSetId)) {
                IExtension ext = actionSets[i].getConfigurationElement().getDeclaringExtension();
                reg.removeExtension(ext, new Object[] { actionSets[i] });
                return;
            }
        }
    }

}