kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2006 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.editor; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
| 13 | import java.util.Iterator; |
| 14 | import java.util.List; |
| 15 | |
| 16 | import org.eclipse.gef.editparts.ZoomManager; |
| 17 | import org.eclipse.gef.ui.actions.ActionRegistry; |
| 18 | import org.eclipse.gef.ui.actions.ZoomComboContributionItem; |
| 19 | import org.eclipse.gef.ui.actions.ZoomInRetargetAction; |
| 20 | import org.eclipse.gef.ui.actions.ZoomOutRetargetAction; |
| 21 | import org.eclipse.jface.action.IAction; |
| 22 | import org.eclipse.jface.action.IMenuManager; |
| 23 | import org.eclipse.jface.action.IToolBarManager; |
| 24 | import org.eclipse.jface.action.MenuManager; |
| 25 | import org.eclipse.jface.action.Separator; |
| 26 | import org.eclipse.ui.IActionBars; |
| 27 | import org.eclipse.ui.IEditorPart; |
| 28 | import org.eclipse.ui.IWorkbenchActionConstants; |
| 29 | import org.eclipse.ui.actions.ActionFactory; |
| 30 | import org.eclipse.ui.ide.IDEActionFactory; |
| 31 | import org.eclipse.ui.part.MultiPageEditorActionBarContributor; |
| 32 | import org.eclipse.ui.part.MultiPageEditorPart; |
| 33 | import org.eclipse.ui.texteditor.ITextEditor; |
| 34 | import org.eclipse.ui.texteditor.ITextEditorActionConstants; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 35 | import org.eclipse.wst.xsd.ui.internal.actions.IXSDToolbarAction; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Manages the installation/deinstallation of global actions for multi-page |
| 39 | * editors. Responsible for the redirection of global actions to the active |
| 40 | * editor. Multi-page contributor replaces the contributors for the individual |
| 41 | * editors in the multi-page editor. |
| 42 | */ |
| 43 | public class XSDMultiPageEditorContributor extends MultiPageEditorActionBarContributor |
| 44 | { |
| 45 | private IEditorPart activeEditorPart; |
| 46 | |
| 47 | /** |
| 48 | * Creates a multi-page contributor. |
| 49 | */ |
| 50 | public XSDMultiPageEditorContributor() |
| 51 | { |
| 52 | super(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the action registed with the given text editor. |
| 57 | * |
| 58 | * @return IAction or null if editor is null. |
| 59 | */ |
| 60 | protected IAction getAction(ITextEditor editor, String actionID) |
| 61 | { |
| 62 | return (editor == null ? null : editor.getAction(actionID)); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * (non-JavaDoc) Method declared in |
| 67 | * AbstractMultiPageEditorActionBarContributor. |
| 68 | */ |
| 69 | |
| 70 | public void setActivePage(IEditorPart part) |
| 71 | { |
| 72 | if (activeEditorPart == part) |
| 73 | return; |
| 74 | |
| 75 | activeEditorPart = part; |
| 76 | |
| 77 | IActionBars actionBars = getActionBars(); |
| 78 | |
| 79 | if (part != null) |
| 80 | { |
| 81 | Object adapter = part.getAdapter(ActionRegistry.class); |
| 82 | if (adapter instanceof ActionRegistry) |
| 83 | { |
| 84 | ActionRegistry registry = (ActionRegistry) adapter; |
| 85 | actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), registry.getAction(ActionFactory.UNDO.getId())); |
| 86 | actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), registry.getAction(ActionFactory.REDO.getId())); |
| 87 | actionBars.updateActionBars(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (actionBars != null) |
| 92 | { |
| 93 | |
| 94 | ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null; |
| 95 | |
| 96 | actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), getAction(editor, ITextEditorActionConstants.DELETE)); |
| 97 | actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), getAction(editor, ITextEditorActionConstants.UNDO)); |
| 98 | actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), getAction(editor, ITextEditorActionConstants.REDO)); |
| 99 | actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), getAction(editor, ITextEditorActionConstants.CUT)); |
| 100 | actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), getAction(editor, ITextEditorActionConstants.COPY)); |
| 101 | actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), getAction(editor, ITextEditorActionConstants.PASTE)); |
| 102 | actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), getAction(editor, ITextEditorActionConstants.SELECT_ALL)); |
| 103 | actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(), getAction(editor, ITextEditorActionConstants.FIND)); |
| 104 | actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(editor, IDEActionFactory.BOOKMARK.getId())); |
| 105 | |
| 106 | actionBars.updateActionBars(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public void setActiveEditor(IEditorPart part) |
| 111 | { |
| 112 | IEditorPart activeNestedEditor = null; |
| 113 | if (part instanceof MultiPageEditorPart) |
| 114 | { |
kchong | 3fab7d5 | 2006-04-19 19:16:06 +0000 | [diff] [blame^] | 115 | activeNestedEditor = part; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 116 | } |
| 117 | setActivePage(activeNestedEditor); |
| 118 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 119 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 120 | { |
| 121 | ((IXSDToolbarAction)i.next()).setEditorPart(activeNestedEditor); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public void contributeToMenu(IMenuManager manager) |
| 126 | { |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 127 | IMenuManager menu = new MenuManager(Messages._UI_MENU_XSD_EDITOR); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 128 | manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu); |
| 129 | |
| 130 | // Add extension menu actions |
| 131 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 132 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 133 | { |
| 134 | menu.add((IXSDToolbarAction)i.next()); |
| 135 | } |
| 136 | |
| 137 | menu.add((new ZoomInRetargetAction())); |
| 138 | menu.add((new ZoomOutRetargetAction())); |
| 139 | |
| 140 | menu.updateAll(true); |
| 141 | } |
| 142 | |
| 143 | public void contributeToToolBar(IToolBarManager manager) |
| 144 | { |
| 145 | manager.add(new Separator()); |
| 146 | // Add extension toolbar actions |
| 147 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 148 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 149 | { |
| 150 | manager.add((IXSDToolbarAction)i.next()); |
| 151 | } |
| 152 | |
| 153 | manager.add(new Separator()); |
| 154 | String[] zoomStrings = new String[] { ZoomManager.FIT_ALL, ZoomManager.FIT_HEIGHT, ZoomManager.FIT_WIDTH }; |
| 155 | manager.add(new ZoomComboContributionItem(getPage(), zoomStrings)); |
| 156 | } |
| 157 | } |