blob: 1f896bc144362d8c234fe798a6be4f09a07b0a7c [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
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 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.editor;
kchong38cbf172006-03-29 03:38:21 +000012
13import java.util.Iterator;
14import java.util.List;
15
16import org.eclipse.gef.editparts.ZoomManager;
17import org.eclipse.gef.ui.actions.ActionRegistry;
18import org.eclipse.gef.ui.actions.ZoomComboContributionItem;
19import org.eclipse.gef.ui.actions.ZoomInRetargetAction;
20import org.eclipse.gef.ui.actions.ZoomOutRetargetAction;
21import org.eclipse.jface.action.IAction;
22import org.eclipse.jface.action.IMenuManager;
23import org.eclipse.jface.action.IToolBarManager;
24import org.eclipse.jface.action.MenuManager;
25import org.eclipse.jface.action.Separator;
26import org.eclipse.ui.IActionBars;
27import org.eclipse.ui.IEditorPart;
28import org.eclipse.ui.IWorkbenchActionConstants;
29import org.eclipse.ui.actions.ActionFactory;
30import org.eclipse.ui.ide.IDEActionFactory;
31import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
32import org.eclipse.ui.part.MultiPageEditorPart;
33import org.eclipse.ui.texteditor.ITextEditor;
34import org.eclipse.ui.texteditor.ITextEditorActionConstants;
kchong2be71b32006-04-11 16:32:03 +000035import org.eclipse.wst.xsd.ui.internal.actions.IXSDToolbarAction;
kchong38cbf172006-03-29 03:38:21 +000036
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 */
43public 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 {
kchong3fab7d52006-04-19 19:16:06 +0000115 activeNestedEditor = part;
kchong38cbf172006-03-29 03:38:21 +0000116 }
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 {
kchong618a1942006-04-13 07:14:23 +0000127 IMenuManager menu = new MenuManager(Messages._UI_MENU_XSD_EDITOR);
kchong38cbf172006-03-29 03:38:21 +0000128 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}