package org.eclipse.ui.examples.multipageeditor; /* * (c) Copyright IBM Corp. 2000, 2001. * All Rights Reserved. */ import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.SubMenuManager; import org.eclipse.jface.action.SubToolBarManager; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchActionConstants; 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 MultiPageContributor extends MultiPageEditorActionBarContributor { private IEditorPart activeEditorPart; private EditorAction action1; private EditorAction action2; private EditorAction action3; SubMenuManager smm1; SubMenuManager smm2; SubMenuManager smm3; SubToolBarManager stm1; SubToolBarManager stm2; SubToolBarManager stm3; class EditorAction extends Action { private Shell shell; private IEditorPart activeEditor; public EditorAction(String label) { super(label); } public void setShell(Shell shell) { this.shell = shell; } public void run() { } public void setActiveEditor(IEditorPart part) { activeEditor = part; } } /** * Creates a multi-page contributor. */ public MultiPageContributor() { super(); action1 = new EditorAction("Editor_Action1"); //$NON-NLS-1$ action1.setToolTipText("Readme_Editor_Action1"); //$NON-NLS-1$ action2 = new EditorAction("Editor_Action2"); //$NON-NLS-1$ action2.setToolTipText("Readme_Editor_Action2"); //$NON-NLS-1$ action3 = new EditorAction("Editor_Action3"); //$NON-NLS-1$ action3.setToolTipText("Readme_Editor_Action3"); //$NON-NLS-1$ } /** * Returns the action registed with the given text editor. * @return IAction or null if editor is null. */ protected IAction getAction(ITextEditor editor, String actionID) { return (editor == null ? null : editor.getAction(actionID)); } /* (non-JavaDoc) * Method declared in AbstractMultiPageEditorActionBarContributor. */ public void setActivePage(IEditorPart part) { if (activeEditorPart == part) return; stm1.setVisible(part instanceof ITextEditor); stm2.setVisible(!(part instanceof ITextEditor)); stm3.setVisible(false); smm1.setVisible(part instanceof ITextEditor); smm2.setVisible(!(part instanceof ITextEditor)); smm3.setVisible(false); activeEditorPart = part; IActionBars actionBars = getActionBars(); if (actionBars != null) { ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null; actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, getAction(editor, ITextEditorActionConstants.DELETE)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.UNDO, getAction(editor, ITextEditorActionConstants.UNDO)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.REDO, getAction(editor, ITextEditorActionConstants.REDO)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, getAction(editor, ITextEditorActionConstants.CUT)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, getAction(editor, ITextEditorActionConstants.COPY)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, getAction(editor, ITextEditorActionConstants.PASTE)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.SELECT_ALL, getAction(editor, ITextEditorActionConstants.SELECT_ALL)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.FIND, getAction(editor, ITextEditorActionConstants.FIND)); actionBars.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, getAction(editor, ITextEditorActionConstants.BOOKMARK)); } actionBars.updateActionBars(); } public void contributeToToolBar(IToolBarManager toolBarManager) { super.contributeToToolBar(toolBarManager); stm1 = new SubToolBarManager(toolBarManager); stm2 = new SubToolBarManager(toolBarManager); stm3 = new SubToolBarManager(toolBarManager); stm1.add(action1); stm2.add(action2); stm3.add(action3); } /** * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(IMenuManager) */ public void contributeToMenu(IMenuManager menuManager) { super.contributeToMenu(menuManager); smm1 = new SubMenuManager(menuManager); smm2 = new SubMenuManager(menuManager); smm3 = new SubMenuManager(menuManager); smm1.add(action1); smm2.add(action2); smm3.add(action3); } }