/*******************************************************************************
 * 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.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.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
 */
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 lockToolBarAction;

	private IWorkbenchAction editActionSetAction;

    public ApplicationActionBarAdvisor(final IActionBarConfigurer configurer)
    {
        super(configurer);
        window = configurer.getWindowConfigurer().getWindow();
    }

    /** {@inheritDoc} */
	@Override
    protected void makeActions(final IWorkbenchWindow window)
    {
        lockToolBarAction = ActionFactory.LOCK_TOOL_BAR.create(window);
        register(lockToolBarAction);

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

        // The help menu tries to invoke the into(welcome)
        // and help commands, but by default no handler is
        // available. Registering these actions also
        // registers the handlers.
        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(lockToolBarAction));
        coolbarPopupMenuManager.add(new ActionContributionItem(editActionSetAction));
        coolbar.setContextMenuManager(coolbarPopupMenuManager);
        final IMenuService menuService = (IMenuService) window.getService(IMenuService.class);
        menuService.populateContributionManager(coolbarPopupMenuManager, "popup:windowCoolbarContextMenu"); //$NON-NLS-1$

        // 'File' and 'User' sections of the cool bar
        IToolBarManager file_bar = new ToolBarManager();
        IToolBarManager user_bar = new ToolBarManager();
        coolbar.add(new ToolBarContributionItem(file_bar, IWorkbenchActionConstants.M_FILE));
        coolbar.add(new ToolBarContributionItem(user_bar, TOOLBAR_USER));

        // File 'new' and 'save' actions
        file_bar.add(ActionFactory.NEW.create(window));
        file_bar.add(ActionFactory.SAVE.create(window));
        file_bar.add(new CoolItemGroupMarker(IWorkbenchActionConstants.FILE_END));
    }
}

@@ -2064,71 +2064,60 @@
 lock
-ToolBarAction;%0A%0A%09private IWorkbenchAction editActionSetAction;%0A
+_toolbar, edit_actionsets, save;%0A%0A%09/** Initialize */
 %0A   
@@ -2293,32 +2293,456 @@
 ;%0A    %7D%0A%0A    /**
+ Create actions.%0A     *%0A     *  %3Cp%3ESome of these actions are created to programmatically%0A     *  add them to the toolbar.%0A     *  Other actions like save_as are not used at all in here,%0A     *  but they need to be created because the plugin.xml%0A     *  registers their command ID in the menu bar,%0A     *  and the action actually implements the handler.%0A     *  The actions also provide the dynamic enablement.%0A     *%0A     * 
  %7B@inheritDoc%7D *
@@ -2735,24 +2735,29 @@
 @inheritDoc%7D
+%0A    
  */%0A%09@Overri
@@ -2839,29 +2839,24 @@
     lock
-ToolB
+_toolb
 ar
-Action
  = Actio
@@ -2915,29 +2915,24 @@
 ter(lock
-ToolB
+_toolb
 ar
-Action
 );%0A%0A    
@@ -2939,31 +2939,27 @@
     edit
-A
+_a
 ction
-SetAction
+sets
  = Actio
@@ -3021,31 +3021,27 @@
 ter(edit
-A
+_a
 ction
-SetAction
+sets
 );%0A%0A    
@@ -3048,205 +3048,131 @@
     
-// The help menu tries to invoke the into(welcome)%0A        // and help commands, but by default no handler is%0A        // available. Registering these actions also%0A        // registers the handlers.
+save = ActionFactory.SAVE.create(window);%0A        register(save);%0A%0A        register(ActionFactory.SAVE_AS.create(window));%0A
 %0A   
@@ -3282,32 +3282,33 @@
 reate(window));%0A
+%0A
         register
@@ -3908,21 +3908,16 @@
 lock
-ToolB
+_toolb
 ar
-Action
 ));%0A
@@ -3987,23 +3987,19 @@
 edit
-A
+_a
 ction
-SetAction
+sets
 ));%0A
@@ -4739,41 +4739,12 @@
 add(
-ActionFactory.SAVE.create(window)
+save
 );%0A 

Create save, save_as actions to enable the corresponding command IDs in menu