/*******************************************************************************
 * Copyright (c) 2010 Neil Bartlett.
 * 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
 *
 * Contributors:
 *     Neil Bartlett - initial API and implementation
 *******************************************************************************/
package bndtools.jareditor.internal;

import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;

/**
 * Manages the installation/deinstallation of global actions for multi-page editors. Responsible for the redirection of
 * global actions to the active editor. Multi-page contributor replaces the contributors for the individual editors in
 * the multi-page editor.
 */
public class JAREditorContributor extends MultiPageEditorActionBarContributor {
    private IEditorPart activeEditorPart;
    private Action sampleAction;

    /**
     * Creates a multi-page contributor.
     */
    public JAREditorContributor() {
        super();
        createActions();
    }

    /**
     * Returns the action registed with the given text editor.
     * 
     * @return IAction or null if editor is null.
     */
    protected static IAction getAction(ITextEditor editor, String actionID) {
        return (editor == null ? null : editor.getAction(actionID));
    }

    /*
     * (non-JavaDoc) Method declared in AbstractMultiPageEditorActionBarContributor.
     */

    @Override
    public void setActivePage(IEditorPart part) {
        if (activeEditorPart == part)
            return;

        activeEditorPart = part;

        IActionBars actionBars = getActionBars();
        if (actionBars != null) {

            ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null;

            actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), getAction(editor, ITextEditorActionConstants.DELETE));
            actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), getAction(editor, ITextEditorActionConstants.UNDO));
            actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), getAction(editor, ITextEditorActionConstants.REDO));
            actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), getAction(editor, ITextEditorActionConstants.CUT));
            actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), getAction(editor, ITextEditorActionConstants.COPY));
            actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), getAction(editor, ITextEditorActionConstants.PASTE));
            actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), getAction(editor, ITextEditorActionConstants.SELECT_ALL));
            actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), getAction(editor, ITextEditorActionConstants.FIND));
            actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(editor, IDEActionFactory.BOOKMARK.getId()));
            actionBars.updateActionBars();
        }
    }

    private void createActions() {
        sampleAction = new Action() {
            @Override
            public void run() {
                MessageDialog.openInformation(null, "JAR Editor Plug-in", "Sample Action Executed");
            }
        };
        sampleAction.setText("Sample Action");
        sampleAction.setToolTipText("Sample Action tool tip");
        sampleAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK));
    }

    @Override
    public void contributeToMenu(IMenuManager manager) {
        IMenuManager menu = new MenuManager("Editor &Menu");
        manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu);
        menu.add(sampleAction);
    }

    @Override
    public void contributeToToolBar(IToolBarManager manager) {
        manager.add(new Separator());
        manager.add(sampleAction);
    }
}

@@ -587,57 +587,15 @@
 ion.
-*;%0Aimport org.eclipse.jface.dialogs.MessageDialog
+IAction
 ;%0Aim
@@ -666,91 +666,8 @@
 rt;%0A
-import org.eclipse.ui.IWorkbenchActionConstants;%0Aimport org.eclipse.ui.PlatformUI;%0A
 impo
@@ -711,39 +711,8 @@
 ry;%0A
-import org.eclipse.ui.ide.IDE;%0A
 impo
@@ -1321,41 +1321,8 @@
 art;
-%0A    private Action sampleAction;
 %0A%0A  
@@ -1433,33 +1433,8 @@
 ();%0A
-        createActions();%0A
     
@@ -3366,32 +3366,32 @@
 teActionBars();%0A
+
         %7D%0A    %7D%0A
@@ -3393,924 +3393,7 @@
    %7D
-%0A%0A    private void createActions() %7B%0A        sampleAction = new Action() %7B%0A            @Override%0A            public void run() %7B%0A                MessageDialog.openInformation(null, %22JAR Editor Plug-in%22, %22Sample Action Executed%22);%0A            %7D%0A        %7D;%0A        sampleAction.setText(%22Sample Action%22);%0A        sampleAction.setToolTipText(%22Sample Action tool tip%22);%0A        sampleAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK));%0A    %7D%0A%0A    @Override%0A    public void contributeToMenu(IMenuManager manager) %7B%0A        IMenuManager menu = new MenuManager(%22Editor &Menu%22);%0A        manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu);%0A        menu.add(sampleAction);%0A    %7D%0A%0A    @Override%0A    public void contributeToToolBar(IToolBarManager manager) %7B%0A        manager.add(new Separator());%0A        manager.add(sampleAction);%0A    %7D
 %0A%7D%0A

remove bogus 'Sample Action'