kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | 8e296ca | 2009-01-21 00:09:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2009 IBM Corporation and others. |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 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 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 13 | import java.util.ArrayList; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 14 | import java.util.Iterator; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.eclipse.gef.editparts.ZoomManager; |
| 18 | import org.eclipse.gef.ui.actions.ActionRegistry; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 19 | import org.eclipse.gef.ui.actions.GEFActionConstants; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 20 | import org.eclipse.gef.ui.actions.ZoomComboContributionItem; |
| 21 | import org.eclipse.gef.ui.actions.ZoomInRetargetAction; |
| 22 | import org.eclipse.gef.ui.actions.ZoomOutRetargetAction; |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 23 | import org.eclipse.jface.action.Action; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 24 | import org.eclipse.jface.action.IAction; |
| 25 | import org.eclipse.jface.action.IMenuManager; |
| 26 | import org.eclipse.jface.action.IToolBarManager; |
| 27 | import org.eclipse.jface.action.MenuManager; |
| 28 | import org.eclipse.jface.action.Separator; |
| 29 | import org.eclipse.ui.IActionBars; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 30 | import org.eclipse.ui.IEditorActionBarContributor; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 31 | import org.eclipse.ui.IEditorPart; |
csalter | 143154d | 2006-07-06 05:55:09 +0000 | [diff] [blame] | 32 | import org.eclipse.ui.IEditorSite; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 33 | import org.eclipse.ui.IWorkbenchActionConstants; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 34 | import org.eclipse.ui.IWorkbenchPage; |
csalter | 143154d | 2006-07-06 05:55:09 +0000 | [diff] [blame] | 35 | import org.eclipse.ui.IWorkbenchPartSite; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 36 | import org.eclipse.ui.actions.ActionFactory; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 37 | import org.eclipse.ui.actions.RetargetAction; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 38 | import org.eclipse.ui.part.MultiPageEditorActionBarContributor; |
| 39 | import org.eclipse.ui.part.MultiPageEditorPart; |
| 40 | import org.eclipse.ui.texteditor.ITextEditor; |
csalter | 143154d | 2006-07-06 05:55:09 +0000 | [diff] [blame] | 41 | import org.eclipse.ui.texteditor.ITextEditorActionConstants; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 42 | import org.eclipse.wst.sse.ui.internal.ISourceViewerActionBarContributor; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 43 | import org.eclipse.wst.xsd.ui.internal.actions.IXSDToolbarAction; |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 44 | import org.eclipse.wst.xsd.ui.internal.adt.actions.CaptureScreenAction; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 45 | import org.eclipse.wst.xsd.ui.internal.adt.actions.DeleteAction; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Manages the installation/deinstallation of global actions for multi-page |
| 49 | * editors. Responsible for the redirection of global actions to the active |
| 50 | * editor. Multi-page contributor replaces the contributors for the individual |
| 51 | * editors in the multi-page editor. |
| 52 | */ |
| 53 | public class XSDMultiPageEditorContributor extends MultiPageEditorActionBarContributor |
| 54 | { |
| 55 | private IEditorPart activeEditorPart; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 56 | private InternalXSDMultiPageEditor xsdEditor; |
| 57 | protected ITextEditor textEditor; |
| 58 | protected IEditorActionBarContributor sourceViewerActionContributor = null; |
| 59 | protected List fPartListeners= new ArrayList(); |
| 60 | ZoomInRetargetAction zoomInRetargetAction; |
| 61 | ZoomOutRetargetAction zoomOutRetargetAction; |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 62 | Action captureScreenAction; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 63 | ZoomComboContributionItem zoomComboContributionItem; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Creates a multi-page contributor. |
| 66 | */ |
| 67 | public XSDMultiPageEditorContributor() |
| 68 | { |
| 69 | super(); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 70 | sourceViewerActionContributor = new SourcePageActionContributor(); |
| 71 | zoomInRetargetAction = new ZoomInRetargetAction(); |
kchong | bce9e9a | 2008-04-03 20:05:18 +0000 | [diff] [blame] | 72 | zoomInRetargetAction.setImageDescriptor(XSDEditorPlugin.getImageDescriptor("icons/etool16/zoomplus.gif")); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 73 | zoomOutRetargetAction = new ZoomOutRetargetAction(); |
kchong | bce9e9a | 2008-04-03 20:05:18 +0000 | [diff] [blame] | 74 | zoomOutRetargetAction.setImageDescriptor(XSDEditorPlugin.getImageDescriptor("icons/etool16/zoomminus.gif")); |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 75 | captureScreenAction = new CaptureScreenAction(); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 76 | fPartListeners.add(zoomInRetargetAction); |
| 77 | fPartListeners.add(zoomOutRetargetAction); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the action registed with the given text editor. |
| 82 | * |
| 83 | * @return IAction or null if editor is null. |
| 84 | */ |
| 85 | protected IAction getAction(ITextEditor editor, String actionID) |
| 86 | { |
| 87 | return (editor == null ? null : editor.getAction(actionID)); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * (non-JavaDoc) Method declared in |
| 92 | * AbstractMultiPageEditorActionBarContributor. |
| 93 | */ |
| 94 | |
| 95 | public void setActivePage(IEditorPart part) |
| 96 | { |
| 97 | if (activeEditorPart == part) |
| 98 | return; |
| 99 | |
| 100 | activeEditorPart = part; |
| 101 | |
| 102 | IActionBars actionBars = getActionBars(); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 103 | boolean isSource = false; |
| 104 | |
| 105 | if (activeEditorPart != null && activeEditorPart instanceof ITextEditor) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 106 | { |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 107 | isSource = true; |
| 108 | zoomInRetargetAction.setEnabled(false); |
| 109 | zoomOutRetargetAction.setEnabled(false); |
kchong | 62b5f1e | 2007-03-07 20:32:52 +0000 | [diff] [blame] | 110 | captureScreenAction.setEnabled(false); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 111 | activateSourcePage(activeEditorPart, true); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | activateSourcePage(xsdEditor, false); |
| 116 | if (part instanceof InternalXSDMultiPageEditor) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 117 | { |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 118 | xsdEditor = (InternalXSDMultiPageEditor) part; |
| 119 | } |
| 120 | if (xsdEditor != null) |
csalter | 143154d | 2006-07-06 05:55:09 +0000 | [diff] [blame] | 121 | { |
| 122 | // cs: here's we ensure the UNDO and REDO actions are available when |
| 123 | // the design view is active |
| 124 | IWorkbenchPartSite site = xsdEditor.getSite(); |
| 125 | if (site instanceof IEditorSite) |
| 126 | { |
| 127 | ITextEditor textEditor = xsdEditor.getTextEditor(); |
| 128 | IActionBars siteActionBars = ((IEditorSite) site).getActionBars(); |
| 129 | siteActionBars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, getAction(textEditor, ITextEditorActionConstants.UNDO)); |
| 130 | siteActionBars.setGlobalActionHandler(ITextEditorActionConstants.REDO, getAction(textEditor, ITextEditorActionConstants.REDO)); |
| 131 | siteActionBars.updateActionBars(); |
| 132 | } |
| 133 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 134 | Object adapter = xsdEditor.getAdapter(ActionRegistry.class); |
| 135 | if (adapter instanceof ActionRegistry) |
| 136 | { |
| 137 | ActionRegistry registry = (ActionRegistry) adapter; |
| 138 | actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), registry.getAction(DeleteAction.ID)); |
| 139 | actionBars.setGlobalActionHandler(GEFActionConstants.ZOOM_IN, registry.getAction(GEFActionConstants.ZOOM_IN)); |
| 140 | actionBars.setGlobalActionHandler(GEFActionConstants.ZOOM_OUT, registry.getAction(GEFActionConstants.ZOOM_OUT)); |
kchong | c9c08cb | 2006-07-19 19:11:27 +0000 | [diff] [blame] | 141 | actionBars.setGlobalActionHandler(ActionFactory.PRINT.getId(), registry.getAction(ActionFactory.PRINT.getId())); |
kchong | 8e296ca | 2009-01-21 00:09:54 +0000 | [diff] [blame] | 142 | actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), registry.getAction(ActionFactory.SELECT_ALL.getId())); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 143 | zoomInRetargetAction.setEnabled(true); |
| 144 | zoomOutRetargetAction.setEnabled(true); |
kchong | 62b5f1e | 2007-03-07 20:32:52 +0000 | [diff] [blame] | 145 | captureScreenAction.setEnabled(true); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 146 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 150 | if (actionBars != null) { |
| 151 | // update menu bar and tool bar |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 152 | actionBars.updateActionBars(); |
| 153 | } |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 154 | |
| 155 | if (zoomComboContributionItem != null) |
| 156 | { |
| 157 | zoomComboContributionItem.setVisible(!isSource); |
| 158 | zoomComboContributionItem.update(); |
kchong | 6673ee2 | 2008-11-20 05:50:50 +0000 | [diff] [blame] | 159 | // Bug 254772 - parent contribution manager should not be null. We added this item already. |
| 160 | // Force the ToolBarManager to update/redraw the items |
| 161 | zoomComboContributionItem.getParent().update(true); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | protected void activateSourcePage(IEditorPart activeEditor, boolean state) |
| 166 | { |
| 167 | if (sourceViewerActionContributor != null && sourceViewerActionContributor instanceof ISourceViewerActionBarContributor) |
| 168 | { |
| 169 | sourceViewerActionContributor.setActiveEditor(activeEditor); |
| 170 | ((ISourceViewerActionBarContributor) sourceViewerActionContributor).setViewerSpecificContributionsEnabled(state); |
| 171 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | public void setActiveEditor(IEditorPart part) |
| 175 | { |
| 176 | IEditorPart activeNestedEditor = null; |
| 177 | if (part instanceof MultiPageEditorPart) |
| 178 | { |
kchong | 3fab7d5 | 2006-04-19 19:16:06 +0000 | [diff] [blame] | 179 | activeNestedEditor = part; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 180 | } |
| 181 | setActivePage(activeNestedEditor); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 182 | |
| 183 | if (part instanceof InternalXSDMultiPageEditor) |
| 184 | { |
| 185 | xsdEditor = (InternalXSDMultiPageEditor) part; |
| 186 | |
| 187 | textEditor = xsdEditor.getTextEditor(); |
| 188 | if (textEditor != null) |
| 189 | { |
| 190 | // updateActions(); |
| 191 | getActionBars().updateActionBars(); |
| 192 | } |
| 193 | } |
| 194 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 195 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 196 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 197 | { |
| 198 | ((IXSDToolbarAction)i.next()).setEditorPart(activeNestedEditor); |
| 199 | } |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 200 | |
| 201 | super.setActiveEditor(part); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public void contributeToMenu(IMenuManager manager) |
| 205 | { |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 206 | IMenuManager menu = new MenuManager(Messages._UI_MENU_XSD_EDITOR); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 207 | manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu); |
| 208 | |
| 209 | // Add extension menu actions |
| 210 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 211 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 212 | { |
| 213 | menu.add((IXSDToolbarAction)i.next()); |
| 214 | } |
| 215 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 216 | menu.add(zoomInRetargetAction); |
| 217 | menu.add(zoomOutRetargetAction); |
kchong | 62b5f1e | 2007-03-07 20:32:52 +0000 | [diff] [blame] | 218 | menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 219 | menu.add(captureScreenAction); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 220 | |
| 221 | menu.updateAll(true); |
| 222 | } |
| 223 | |
| 224 | public void contributeToToolBar(IToolBarManager manager) |
| 225 | { |
| 226 | manager.add(new Separator()); |
| 227 | // Add extension toolbar actions |
| 228 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 229 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 230 | { |
| 231 | manager.add((IXSDToolbarAction)i.next()); |
| 232 | } |
| 233 | |
| 234 | manager.add(new Separator()); |
| 235 | String[] zoomStrings = new String[] { ZoomManager.FIT_ALL, ZoomManager.FIT_HEIGHT, ZoomManager.FIT_WIDTH }; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 236 | zoomComboContributionItem = new ZoomComboContributionItem(getPage(), zoomStrings); |
| 237 | manager.add(zoomComboContributionItem); |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 238 | manager.add(captureScreenAction); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 239 | } |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | public void init(IActionBars bars, IWorkbenchPage page) |
| 243 | { |
| 244 | Iterator e = fPartListeners.iterator(); |
| 245 | while (e.hasNext()) |
| 246 | { |
| 247 | page.addPartListener((RetargetAction) e.next()); |
| 248 | } |
| 249 | |
| 250 | initSourceViewerActionContributor(bars); |
| 251 | |
| 252 | super.init(bars, page); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | protected void initSourceViewerActionContributor(IActionBars actionBars) { |
| 257 | if (sourceViewerActionContributor != null) |
| 258 | sourceViewerActionContributor.init(actionBars, getPage()); |
| 259 | } |
| 260 | |
| 261 | public void dispose() |
| 262 | { |
| 263 | fPartListeners = null; |
| 264 | if (sourceViewerActionContributor != null) |
| 265 | sourceViewerActionContributor.dispose(); |
| 266 | super.dispose(); |
| 267 | } |
| 268 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 269 | } |