1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package sk.uniba.euromath.editor.actions;
17
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.gef.ui.actions.ActionRegistry;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.IStatusLineManager;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.action.SubMenuManager;
29 import org.eclipse.jface.action.SubStatusLineManager;
30 import org.eclipse.jface.action.SubToolBarManager;
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IWorkbenchActionConstants;
34 import org.eclipse.ui.actions.ActionFactory;
35 import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
36
37 import sk.uniba.euromath.editor.EditorSite;
38 import sk.uniba.euromath.editor.IEditor;
39 import sk.uniba.euromath.editor.IFocusListener;
40 import sk.uniba.euromath.editor.xmlEditor.actions.ShowDocumentPropertiesAction;
41
42 /***
43 * MultiViewActionContributor is contributor for special IEditorPart -
44 * MutliViewXMLEditor. It is contributor for all opened documents and views
45 * (from that name multi view). Contributes to global bars by own contributions
46 * and by contributions of active IEditor. When focus is changed in EditorSite
47 * (active IEditor has changed) or in workbench page (another EditorSite has
48 * been activated, it means another view) contributions are updated.<br>
49 *
50 * <h2>Terminology</h2>
51 * In Euromath MutliViewXMLEditor and MultiViewActionContributor correspond to
52 * one opened document. On this opened document can be opened many views(view
53 * corresponds to {@link EditorSite}) and one view is composed of many
54 * {@link IEditor}s with {@link IActionContributor}s corresponding to rendered
55 * namespaces in view.<br>
56 *
57 * <h2>Contributing by {@link IEditor}'s {@link IActionContributor}</h2>
58 * There are two possibilities to contribute to global bar:
59 * <ul>
60 * <li>contribute to bar with not shareable contribution item - in such case
61 * when {@link IEditor} is activated, its associated {@link IActionContributor}
62 * is asked to contribute and when is deactivated its contributions are removed.</li>
63 * <li>contribute to bar with shareable action - read more. NOT WORKING</li>
64 * </ul>
65 *
66 * <h2>NOT WORKING Action sharing in one opened document</h2>
67 * Actions of in one type {@link IEditor} can be shared by it instances in all
68 * opened views. This is action sharing is implemented in views over opened
69 * document. Such shareable action contributed by {@link IActionContributor}
70 * must implement {@link IMultiViewAction} and must have unique id. Its context
71 * is changed by
72 * {@link IMultiViewAction#setWorkbenchPart(org.eclipse.ui.IWorkbenchPart)}.
73 *
74 * @author Tomáš Studva 19.9.2005
75 */
76 public class MultiViewActionContributor extends
77 MultiPageEditorActionBarContributor implements IFocusListener {
78
79 /***
80 * Holds EditorSite with focus.
81 */
82 private EditorSite activeEditorSite;
83
84 /***
85 * Global action registry for registering IEditor's contibutions by
86 * their contributors.
87 */
88 private final ActionRegistry actionRegistry;
89
90 /***
91 * Local action registry.
92 */
93
94 /***
95 * SubManager to store contributions of active IEditor.
96 */
97 private SubMenuManager subMenuManager;
98
99 /***
100 * SubManager to store contributions of active IEditor.
101 */
102 private SubStatusLineManager subStatusLineManager;
103
104 /***
105 * SubManager to store contributions of active IEditor.
106 */
107 private SubToolBarManager subToolBarManager;
108
109 /***
110 * Holds action handlers of active editor.
111 */
112 private Set<String> editorGlobalHandlerIds = new HashSet<String>();
113
114 /***
115 * Constructor.
116 */
117 public MultiViewActionContributor() {
118 super();
119 this.actionRegistry = new ActionRegistry();
120
121 }
122
123 @Override
124 public void init(IActionBars bars) {
125 super.init(bars);
126 this.subMenuManager = new SubMenuManager(getActionBars()
127 .getMenuManager());
128 this.subMenuManager.setVisible(true);
129 this.subToolBarManager = new SubToolBarManager(getActionBars()
130 .getToolBarManager());
131 this.subToolBarManager.setVisible(true);
132 this.subStatusLineManager = new SubStatusLineManager(
133 getActionBars().getStatusLineManager());
134 this.subStatusLineManager.setVisible(true);
135 }
136
137
138
139
140
141
142 @Override
143 public void setActivePage(IEditorPart activeEditor) {
144 if (!(activeEditor instanceof EditorSite))
145 throw new IllegalStateException();
146
147 if (this.activeEditorSite != null)
148 this.activeEditorSite.removeFocusListener(this);
149 removeContributionByEditors();
150
151 this.activeEditorSite = ((EditorSite) activeEditor);
152 this.activeEditorSite.setActionContributor(this);
153 this.activeEditorSite.addFocusListener(this);
154 contributeByActiveEditor();
155 }
156
157 /***
158 * Adds contributions of active editor to bars. Contributions are added
159 * to subcontribution managers, to be easily removed when needed.
160 */
161 private void contributeByActiveEditor() {
162 if ((this.activeEditorSite == null)
163 || (this.activeEditorSite.getActiveEditor() == null))
164 return;
165
166
167 IActionContributor contributor = this.activeEditorSite
168 .getActiveEditor().getActionContributor();
169 contributor.contributeToMenu(this.subMenuManager);
170 contributor.contributeToStatusLine(this.subStatusLineManager);
171 contributor.contributeToToolBar(this.subToolBarManager);
172
173 Map<String, IAction> handlers = contributor
174 .getGlobalActionHandlers();
175 this.editorGlobalHandlerIds = handlers.keySet();
176 for (String id : this.editorGlobalHandlerIds) {
177 getActionBars().setGlobalActionHandler(id,
178 handlers.get(id));
179 }
180 getActionBars().updateActionBars();
181 }
182
183 /***
184 * Removes contributions of active editor from bars.
185 */
186 private void removeContributionByEditors() {
187 this.subMenuManager.removeAll();
188 this.subStatusLineManager.removeAll();
189 this.subToolBarManager.removeAll();
190
191 for (String id : this.editorGlobalHandlerIds) {
192 getActionBars().setGlobalActionHandler(id, null);
193 }
194
195 getActionBars().updateActionBars();
196 }
197
198 /***
199 * This contributor's action registry.
200 *
201 * @return action registry used to registry actions used to contribute
202 * by this contributor
203 */
204 public ActionRegistry getActionRegistry() {
205 return this.actionRegistry;
206 }
207
208
209
210
211
212
213 @Override
214 public void contributeToMenu(IMenuManager menuManager) {
215 super.contributeToMenu(menuManager);
216
217 final IMenuManager file = (IMenuManager) menuManager
218 .find(IWorkbenchActionConstants.M_FILE);
219 file.insertAfter(ActionFactory.EXPORT.getId(),
220 new ShowDocumentPropertiesAction(this));
221 file.insertAfter(ActionFactory.EXPORT.getId(), new Separator());
222 }
223
224 @Override
225 public void contributeToStatusLine(IStatusLineManager statusLineManager) {
226 super.contributeToStatusLine(statusLineManager);
227 }
228
229 @Override
230 public void contributeToToolBar(IToolBarManager toolBarManager) {
231 super.contributeToToolBar(toolBarManager);
232 }
233
234
235
236
237
238
239 public void focusGained(IEditor editor) {
240 contributeByActiveEditor();
241 }
242
243
244
245
246
247
248 public void focusLost(IEditor editor) {
249 removeContributionByEditors();
250 }
251 }