kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2007 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(); |
| 72 | zoomOutRetargetAction = new ZoomOutRetargetAction(); |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 73 | captureScreenAction = new CaptureScreenAction(); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 74 | fPartListeners.add(zoomInRetargetAction); |
| 75 | fPartListeners.add(zoomOutRetargetAction); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the action registed with the given text editor. |
| 80 | * |
| 81 | * @return IAction or null if editor is null. |
| 82 | */ |
| 83 | protected IAction getAction(ITextEditor editor, String actionID) |
| 84 | { |
| 85 | return (editor == null ? null : editor.getAction(actionID)); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * (non-JavaDoc) Method declared in |
| 90 | * AbstractMultiPageEditorActionBarContributor. |
| 91 | */ |
| 92 | |
| 93 | public void setActivePage(IEditorPart part) |
| 94 | { |
| 95 | if (activeEditorPart == part) |
| 96 | return; |
| 97 | |
| 98 | activeEditorPart = part; |
| 99 | |
| 100 | IActionBars actionBars = getActionBars(); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 101 | boolean isSource = false; |
| 102 | |
| 103 | if (activeEditorPart != null && activeEditorPart instanceof ITextEditor) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 104 | { |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 105 | isSource = true; |
| 106 | zoomInRetargetAction.setEnabled(false); |
| 107 | zoomOutRetargetAction.setEnabled(false); |
kchong | 62b5f1e | 2007-03-07 20:32:52 +0000 | [diff] [blame^] | 108 | captureScreenAction.setEnabled(false); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 109 | activateSourcePage(activeEditorPart, true); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | activateSourcePage(xsdEditor, false); |
| 114 | if (part instanceof InternalXSDMultiPageEditor) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 115 | { |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 116 | xsdEditor = (InternalXSDMultiPageEditor) part; |
| 117 | } |
| 118 | if (xsdEditor != null) |
csalter | 143154d | 2006-07-06 05:55:09 +0000 | [diff] [blame] | 119 | { |
| 120 | // cs: here's we ensure the UNDO and REDO actions are available when |
| 121 | // the design view is active |
| 122 | IWorkbenchPartSite site = xsdEditor.getSite(); |
| 123 | if (site instanceof IEditorSite) |
| 124 | { |
| 125 | ITextEditor textEditor = xsdEditor.getTextEditor(); |
| 126 | IActionBars siteActionBars = ((IEditorSite) site).getActionBars(); |
| 127 | siteActionBars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, getAction(textEditor, ITextEditorActionConstants.UNDO)); |
| 128 | siteActionBars.setGlobalActionHandler(ITextEditorActionConstants.REDO, getAction(textEditor, ITextEditorActionConstants.REDO)); |
| 129 | siteActionBars.updateActionBars(); |
| 130 | } |
| 131 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 132 | Object adapter = xsdEditor.getAdapter(ActionRegistry.class); |
| 133 | if (adapter instanceof ActionRegistry) |
| 134 | { |
| 135 | ActionRegistry registry = (ActionRegistry) adapter; |
| 136 | actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), registry.getAction(DeleteAction.ID)); |
| 137 | actionBars.setGlobalActionHandler(GEFActionConstants.ZOOM_IN, registry.getAction(GEFActionConstants.ZOOM_IN)); |
| 138 | actionBars.setGlobalActionHandler(GEFActionConstants.ZOOM_OUT, registry.getAction(GEFActionConstants.ZOOM_OUT)); |
kchong | c9c08cb | 2006-07-19 19:11:27 +0000 | [diff] [blame] | 139 | actionBars.setGlobalActionHandler(ActionFactory.PRINT.getId(), registry.getAction(ActionFactory.PRINT.getId())); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 140 | zoomInRetargetAction.setEnabled(true); |
| 141 | zoomOutRetargetAction.setEnabled(true); |
kchong | 62b5f1e | 2007-03-07 20:32:52 +0000 | [diff] [blame^] | 142 | captureScreenAction.setEnabled(true); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 143 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 147 | if (actionBars != null) { |
| 148 | // update menu bar and tool bar |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 149 | actionBars.updateActionBars(); |
| 150 | } |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 151 | |
| 152 | if (zoomComboContributionItem != null) |
| 153 | { |
| 154 | zoomComboContributionItem.setVisible(!isSource); |
| 155 | zoomComboContributionItem.update(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | protected void activateSourcePage(IEditorPart activeEditor, boolean state) |
| 160 | { |
| 161 | if (sourceViewerActionContributor != null && sourceViewerActionContributor instanceof ISourceViewerActionBarContributor) |
| 162 | { |
| 163 | sourceViewerActionContributor.setActiveEditor(activeEditor); |
| 164 | ((ISourceViewerActionBarContributor) sourceViewerActionContributor).setViewerSpecificContributionsEnabled(state); |
| 165 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | public void setActiveEditor(IEditorPart part) |
| 169 | { |
| 170 | IEditorPart activeNestedEditor = null; |
| 171 | if (part instanceof MultiPageEditorPart) |
| 172 | { |
kchong | 3fab7d5 | 2006-04-19 19:16:06 +0000 | [diff] [blame] | 173 | activeNestedEditor = part; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 174 | } |
| 175 | setActivePage(activeNestedEditor); |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 176 | |
| 177 | if (part instanceof InternalXSDMultiPageEditor) |
| 178 | { |
| 179 | xsdEditor = (InternalXSDMultiPageEditor) part; |
| 180 | |
| 181 | textEditor = xsdEditor.getTextEditor(); |
| 182 | if (textEditor != null) |
| 183 | { |
| 184 | // updateActions(); |
| 185 | getActionBars().updateActionBars(); |
| 186 | } |
| 187 | } |
| 188 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 189 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 190 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 191 | { |
| 192 | ((IXSDToolbarAction)i.next()).setEditorPart(activeNestedEditor); |
| 193 | } |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 194 | |
| 195 | super.setActiveEditor(part); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | public void contributeToMenu(IMenuManager manager) |
| 199 | { |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 200 | IMenuManager menu = new MenuManager(Messages._UI_MENU_XSD_EDITOR); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 201 | manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu); |
| 202 | |
| 203 | // Add extension menu actions |
| 204 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 205 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 206 | { |
| 207 | menu.add((IXSDToolbarAction)i.next()); |
| 208 | } |
| 209 | |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 210 | menu.add(zoomInRetargetAction); |
| 211 | menu.add(zoomOutRetargetAction); |
kchong | 62b5f1e | 2007-03-07 20:32:52 +0000 | [diff] [blame^] | 212 | menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 213 | menu.add(captureScreenAction); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 214 | |
| 215 | menu.updateAll(true); |
| 216 | } |
| 217 | |
| 218 | public void contributeToToolBar(IToolBarManager manager) |
| 219 | { |
| 220 | manager.add(new Separator()); |
| 221 | // Add extension toolbar actions |
| 222 | List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions(); |
| 223 | for (Iterator i = list.iterator(); i.hasNext(); ) |
| 224 | { |
| 225 | manager.add((IXSDToolbarAction)i.next()); |
| 226 | } |
| 227 | |
| 228 | manager.add(new Separator()); |
| 229 | String[] zoomStrings = new String[] { ZoomManager.FIT_ALL, ZoomManager.FIT_HEIGHT, ZoomManager.FIT_WIDTH }; |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 230 | zoomComboContributionItem = new ZoomComboContributionItem(getPage(), zoomStrings); |
| 231 | manager.add(zoomComboContributionItem); |
kchong | 66e32b7 | 2007-02-26 20:28:43 +0000 | [diff] [blame] | 232 | manager.add(captureScreenAction); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 233 | } |
kchong | 6e0ae09 | 2006-05-09 02:44:20 +0000 | [diff] [blame] | 234 | |
| 235 | |
| 236 | public void init(IActionBars bars, IWorkbenchPage page) |
| 237 | { |
| 238 | Iterator e = fPartListeners.iterator(); |
| 239 | while (e.hasNext()) |
| 240 | { |
| 241 | page.addPartListener((RetargetAction) e.next()); |
| 242 | } |
| 243 | |
| 244 | initSourceViewerActionContributor(bars); |
| 245 | |
| 246 | super.init(bars, page); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | protected void initSourceViewerActionContributor(IActionBars actionBars) { |
| 251 | if (sourceViewerActionContributor != null) |
| 252 | sourceViewerActionContributor.init(actionBars, getPage()); |
| 253 | } |
| 254 | |
| 255 | public void dispose() |
| 256 | { |
| 257 | fPartListeners = null; |
| 258 | if (sourceViewerActionContributor != null) |
| 259 | sourceViewerActionContributor.dispose(); |
| 260 | super.dispose(); |
| 261 | } |
| 262 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 263 | } |