blob: 6025e2d117aa8653e52fe1a296bb6f6f3eb737a2 [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
kchong66e32b72007-02-26 20:28:43 +00002 * Copyright (c) 2001, 2007 IBM Corporation and others.
kchong38cbf172006-03-29 03:38:21 +00003 * 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 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.editor;
kchong38cbf172006-03-29 03:38:21 +000012
kchong6e0ae092006-05-09 02:44:20 +000013import java.util.ArrayList;
kchong38cbf172006-03-29 03:38:21 +000014import java.util.Iterator;
15import java.util.List;
16
17import org.eclipse.gef.editparts.ZoomManager;
18import org.eclipse.gef.ui.actions.ActionRegistry;
kchong6e0ae092006-05-09 02:44:20 +000019import org.eclipse.gef.ui.actions.GEFActionConstants;
kchong38cbf172006-03-29 03:38:21 +000020import org.eclipse.gef.ui.actions.ZoomComboContributionItem;
21import org.eclipse.gef.ui.actions.ZoomInRetargetAction;
22import org.eclipse.gef.ui.actions.ZoomOutRetargetAction;
kchong66e32b72007-02-26 20:28:43 +000023import org.eclipse.jface.action.Action;
kchong38cbf172006-03-29 03:38:21 +000024import org.eclipse.jface.action.IAction;
25import org.eclipse.jface.action.IMenuManager;
26import org.eclipse.jface.action.IToolBarManager;
27import org.eclipse.jface.action.MenuManager;
28import org.eclipse.jface.action.Separator;
29import org.eclipse.ui.IActionBars;
kchong6e0ae092006-05-09 02:44:20 +000030import org.eclipse.ui.IEditorActionBarContributor;
kchong38cbf172006-03-29 03:38:21 +000031import org.eclipse.ui.IEditorPart;
csalter143154d2006-07-06 05:55:09 +000032import org.eclipse.ui.IEditorSite;
kchong38cbf172006-03-29 03:38:21 +000033import org.eclipse.ui.IWorkbenchActionConstants;
kchong6e0ae092006-05-09 02:44:20 +000034import org.eclipse.ui.IWorkbenchPage;
csalter143154d2006-07-06 05:55:09 +000035import org.eclipse.ui.IWorkbenchPartSite;
kchong38cbf172006-03-29 03:38:21 +000036import org.eclipse.ui.actions.ActionFactory;
kchong6e0ae092006-05-09 02:44:20 +000037import org.eclipse.ui.actions.RetargetAction;
kchong38cbf172006-03-29 03:38:21 +000038import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
39import org.eclipse.ui.part.MultiPageEditorPart;
40import org.eclipse.ui.texteditor.ITextEditor;
csalter143154d2006-07-06 05:55:09 +000041import org.eclipse.ui.texteditor.ITextEditorActionConstants;
kchong6e0ae092006-05-09 02:44:20 +000042import org.eclipse.wst.sse.ui.internal.ISourceViewerActionBarContributor;
kchong2be71b32006-04-11 16:32:03 +000043import org.eclipse.wst.xsd.ui.internal.actions.IXSDToolbarAction;
kchong66e32b72007-02-26 20:28:43 +000044import org.eclipse.wst.xsd.ui.internal.adt.actions.CaptureScreenAction;
kchong6e0ae092006-05-09 02:44:20 +000045import org.eclipse.wst.xsd.ui.internal.adt.actions.DeleteAction;
kchong38cbf172006-03-29 03:38:21 +000046
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 */
53public class XSDMultiPageEditorContributor extends MultiPageEditorActionBarContributor
54{
55 private IEditorPart activeEditorPart;
kchong6e0ae092006-05-09 02:44:20 +000056 private InternalXSDMultiPageEditor xsdEditor;
57 protected ITextEditor textEditor;
58 protected IEditorActionBarContributor sourceViewerActionContributor = null;
59 protected List fPartListeners= new ArrayList();
60 ZoomInRetargetAction zoomInRetargetAction;
61 ZoomOutRetargetAction zoomOutRetargetAction;
kchong66e32b72007-02-26 20:28:43 +000062 Action captureScreenAction;
kchong6e0ae092006-05-09 02:44:20 +000063 ZoomComboContributionItem zoomComboContributionItem;
kchong38cbf172006-03-29 03:38:21 +000064 /**
65 * Creates a multi-page contributor.
66 */
67 public XSDMultiPageEditorContributor()
68 {
69 super();
kchong6e0ae092006-05-09 02:44:20 +000070 sourceViewerActionContributor = new SourcePageActionContributor();
71 zoomInRetargetAction = new ZoomInRetargetAction();
72 zoomOutRetargetAction = new ZoomOutRetargetAction();
kchong66e32b72007-02-26 20:28:43 +000073 captureScreenAction = new CaptureScreenAction();
kchong6e0ae092006-05-09 02:44:20 +000074 fPartListeners.add(zoomInRetargetAction);
75 fPartListeners.add(zoomOutRetargetAction);
kchong38cbf172006-03-29 03:38:21 +000076 }
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();
kchong6e0ae092006-05-09 02:44:20 +0000101 boolean isSource = false;
102
103 if (activeEditorPart != null && activeEditorPart instanceof ITextEditor)
kchong38cbf172006-03-29 03:38:21 +0000104 {
kchong6e0ae092006-05-09 02:44:20 +0000105 isSource = true;
106 zoomInRetargetAction.setEnabled(false);
107 zoomOutRetargetAction.setEnabled(false);
kchong62b5f1e2007-03-07 20:32:52 +0000108 captureScreenAction.setEnabled(false);
kchong6e0ae092006-05-09 02:44:20 +0000109 activateSourcePage(activeEditorPart, true);
110 }
111 else
112 {
113 activateSourcePage(xsdEditor, false);
114 if (part instanceof InternalXSDMultiPageEditor)
kchong38cbf172006-03-29 03:38:21 +0000115 {
kchong6e0ae092006-05-09 02:44:20 +0000116 xsdEditor = (InternalXSDMultiPageEditor) part;
117 }
118 if (xsdEditor != null)
csalter143154d2006-07-06 05:55:09 +0000119 {
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
kchong6e0ae092006-05-09 02:44:20 +0000132 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));
kchongc9c08cb2006-07-19 19:11:27 +0000139 actionBars.setGlobalActionHandler(ActionFactory.PRINT.getId(), registry.getAction(ActionFactory.PRINT.getId()));
kchong6e0ae092006-05-09 02:44:20 +0000140 zoomInRetargetAction.setEnabled(true);
141 zoomOutRetargetAction.setEnabled(true);
kchong62b5f1e2007-03-07 20:32:52 +0000142 captureScreenAction.setEnabled(true);
kchong6e0ae092006-05-09 02:44:20 +0000143 }
kchong38cbf172006-03-29 03:38:21 +0000144 }
145 }
146
kchong6e0ae092006-05-09 02:44:20 +0000147 if (actionBars != null) {
148 // update menu bar and tool bar
kchong38cbf172006-03-29 03:38:21 +0000149 actionBars.updateActionBars();
150 }
kchong6e0ae092006-05-09 02:44:20 +0000151
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 }
kchong38cbf172006-03-29 03:38:21 +0000166 }
167
168 public void setActiveEditor(IEditorPart part)
169 {
170 IEditorPart activeNestedEditor = null;
171 if (part instanceof MultiPageEditorPart)
172 {
kchong3fab7d52006-04-19 19:16:06 +0000173 activeNestedEditor = part;
kchong38cbf172006-03-29 03:38:21 +0000174 }
175 setActivePage(activeNestedEditor);
kchong6e0ae092006-05-09 02:44:20 +0000176
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
kchong38cbf172006-03-29 03:38:21 +0000189 List list = XSDEditorPlugin.getPlugin().getXSDEditorConfiguration().getToolbarActions();
190 for (Iterator i = list.iterator(); i.hasNext(); )
191 {
192 ((IXSDToolbarAction)i.next()).setEditorPart(activeNestedEditor);
193 }
kchong6e0ae092006-05-09 02:44:20 +0000194
195 super.setActiveEditor(part);
kchong38cbf172006-03-29 03:38:21 +0000196 }
197
198 public void contributeToMenu(IMenuManager manager)
199 {
kchong618a1942006-04-13 07:14:23 +0000200 IMenuManager menu = new MenuManager(Messages._UI_MENU_XSD_EDITOR);
kchong38cbf172006-03-29 03:38:21 +0000201 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
kchong6e0ae092006-05-09 02:44:20 +0000210 menu.add(zoomInRetargetAction);
211 menu.add(zoomOutRetargetAction);
kchong62b5f1e2007-03-07 20:32:52 +0000212 menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
kchong66e32b72007-02-26 20:28:43 +0000213 menu.add(captureScreenAction);
kchong38cbf172006-03-29 03:38:21 +0000214
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 };
kchong6e0ae092006-05-09 02:44:20 +0000230 zoomComboContributionItem = new ZoomComboContributionItem(getPage(), zoomStrings);
231 manager.add(zoomComboContributionItem);
kchong66e32b72007-02-26 20:28:43 +0000232 manager.add(captureScreenAction);
kchong38cbf172006-03-29 03:38:21 +0000233 }
kchong6e0ae092006-05-09 02:44:20 +0000234
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
kchong38cbf172006-03-29 03:38:21 +0000263}