KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > action > EditingDomainActionBarContributor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: EditingDomainActionBarContributor.java,v 1.8 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.action;
18
19
20 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
21 import org.eclipse.jface.action.ActionContributionItem;
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.IStatusLineManager;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IPropertyListener;
34 import org.eclipse.ui.ISharedImages;
35 import org.eclipse.ui.IViewPart;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.actions.ActionFactory;
38 import org.eclipse.ui.part.IPage;
39 import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
40 import org.eclipse.ui.views.properties.IPropertySheetPage;
41 import org.eclipse.ui.views.properties.PropertySheet;
42
43
44 /**
45  * This is a contributor for an editor, multipage or otherwise,
46  * that implements {@link IEditingDomainProvider}.
47  * It automatically hooks up the Undo, Redo, Cut, Copy, Paste, and Delete actions on the Edit menu
48  * to the corresponding commands supported by the {@link org.eclipse.emf.edit.domain.EditingDomain}.
49  * The editor site'selection provider is used to keep the Cut, Copy, Paste, and Delete actions up-to-date.
50  * The actions are also refreshed everytime the editor fires to its {@link IPropertyListener}s.
51  * <p>
52  * Another very useful feature of this contributor is that it can be used as follows:
53  * <pre>
54  * ((IMenuListener)((IEditorSite)getSite()).getActionBarContributor()).menuAboutToShow(menuManager);
55  * </pre>
56  * to contribute the Edit menu actions to a popup menu.
57  */

58 public class EditingDomainActionBarContributor
59   extends
60     MultiPageEditorActionBarContributor
61   implements
62     IMenuListener,
63     IPropertyListener
64 {
65   /**
66    * This keeps track of the current editor part.
67    */

68   protected IEditorPart activeEditor;
69
70   /**
71    * This is the action used to implement delete.
72    */

73   protected DeleteAction deleteAction;
74
75   /**
76    * This is the action used to implement cut.
77    */

78   protected CutAction cutAction;
79
80   /**
81    * This is the action used to implement copy.
82    */

83   protected CopyAction copyAction;
84
85   /**
86    * This is the action used to implement paste.
87    */

88   protected PasteAction pasteAction;
89
90   /**
91    * This is the action used to implement undo.
92    */

93   protected UndoAction undoAction;
94
95   /**
96    * This is the action used to implement redo.
97    */

98   protected RedoAction redoAction;
99
100   /**
101    * This is the action used to load a resource.
102    */

103   protected LoadResourceAction loadResourceAction;
104
105   /**
106    * This is the action used to perform validation.
107    */

108   protected ValidateAction validateAction;
109
110   /**
111    * This creates an instance the contributor.
112    */

113   public EditingDomainActionBarContributor()
114   {
115     super();
116   }
117
118   public void init(IActionBars actionBars)
119   {
120     super.init(actionBars);
121     ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
122
123     deleteAction = new DeleteAction();
124     deleteAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
125     actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);
126
127     cutAction = new CutAction();
128     cutAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT));
129     actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction);
130
131     copyAction = new CopyAction();
132     copyAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
133     actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
134
135     pasteAction = new PasteAction();
136     pasteAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
137     actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction);
138
139     undoAction = new UndoAction();
140     undoAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
141     actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
142
143     redoAction = new RedoAction();
144     redoAction.setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_REDO));
145     actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
146   }
147
148   public void contributeToMenu(IMenuManager menuManager)
149   {
150     super.contributeToMenu(menuManager);
151   }
152
153   public void contributeToStatusLine(IStatusLineManager statusLineManager)
154   {
155     super.contributeToStatusLine(statusLineManager);
156   }
157
158   public void contributeToToolBar(IToolBarManager toolBarManager)
159   {
160     super.contributeToToolBar(toolBarManager);
161   }
162
163   public void shareGlobalActions(IPage page, IActionBars actionBars)
164   {
165     if (!(page instanceof IPropertySheetPage))
166     {
167       actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);
168       actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction);
169       actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
170       actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction);
171     }
172     actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
173     actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
174   }
175
176   /**
177    * @deprecated
178    */

179   public void setActiveView(IViewPart part)
180   {
181     IActionBars actionBars = part.getViewSite().getActionBars();
182     if (!(part instanceof PropertySheet))
183     {
184       actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);
185       actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutAction);
186       actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
187       actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteAction);
188     }
189     actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
190     actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
191
192     actionBars.updateActionBars();
193   }
194
195   public IEditorPart getActiveEditor()
196   {
197     return activeEditor;
198   }
199
200   public void setActiveEditor(IEditorPart part)
201   {
202     super.setActiveEditor(part);
203
204     if (part != activeEditor)
205     {
206       if (activeEditor != null)
207       {
208         deactivate();
209       }
210
211       if (part instanceof IEditingDomainProvider)
212       {
213         activeEditor = part;
214         activate();
215
216       }
217     }
218   }
219
220   public void setActivePage(IEditorPart part)
221   {
222   }
223
224   public void deactivate()
225   {
226     activeEditor.removePropertyListener(this);
227
228     deleteAction.setActiveWorkbenchPart(null);
229     cutAction.setActiveWorkbenchPart(null);
230     copyAction.setActiveWorkbenchPart(null);
231     pasteAction.setActiveWorkbenchPart(null);
232     undoAction.setActiveWorkbenchPart(null);
233     redoAction.setActiveWorkbenchPart(null);
234
235     if (loadResourceAction != null)
236     {
237       loadResourceAction.setActiveWorkbenchPart(null);
238     }
239
240     ISelectionProvider selectionProvider =
241       activeEditor instanceof ISelectionProvider ?
242         (ISelectionProvider)activeEditor :
243         activeEditor.getEditorSite().getSelectionProvider();
244     selectionProvider.removeSelectionChangedListener(deleteAction);
245     selectionProvider.removeSelectionChangedListener(cutAction);
246     selectionProvider.removeSelectionChangedListener(copyAction);
247     selectionProvider.removeSelectionChangedListener(pasteAction);
248
249     if (validateAction != null)
250     {
251       validateAction.setActiveWorkbenchPart(null);
252       selectionProvider.removeSelectionChangedListener(validateAction);
253     }
254   }
255
256   public void activate()
257   {
258     activeEditor.addPropertyListener(this);
259
260     deleteAction.setActiveWorkbenchPart(activeEditor);
261     cutAction.setActiveWorkbenchPart(activeEditor);
262     copyAction.setActiveWorkbenchPart(activeEditor);
263     pasteAction.setActiveWorkbenchPart(activeEditor);
264     undoAction.setActiveWorkbenchPart(activeEditor);
265     redoAction.setActiveWorkbenchPart(activeEditor);
266
267     if (loadResourceAction != null)
268     {
269       loadResourceAction.setActiveWorkbenchPart(activeEditor);
270     }
271
272     ISelectionProvider selectionProvider =
273       activeEditor instanceof ISelectionProvider ?
274         (ISelectionProvider)activeEditor :
275         activeEditor.getEditorSite().getSelectionProvider();
276     selectionProvider.addSelectionChangedListener(deleteAction);
277     selectionProvider.addSelectionChangedListener(cutAction);
278     selectionProvider.addSelectionChangedListener(copyAction);
279     selectionProvider.addSelectionChangedListener(pasteAction);
280
281     if (validateAction != null)
282     {
283       validateAction.setActiveWorkbenchPart(activeEditor);
284       selectionProvider.addSelectionChangedListener(validateAction);
285     }
286
287     update();
288   }
289
290   public void update()
291   {
292     ISelectionProvider selectionProvider =
293       activeEditor instanceof ISelectionProvider ?
294         (ISelectionProvider)activeEditor :
295         activeEditor.getEditorSite().getSelectionProvider();
296     ISelection selection = selectionProvider.getSelection();
297     IStructuredSelection structuredSelection =
298       selection instanceof IStructuredSelection ? (IStructuredSelection)selection : StructuredSelection.EMPTY;
299
300     deleteAction.updateSelection(structuredSelection);
301     cutAction.updateSelection(structuredSelection);
302     copyAction.updateSelection(structuredSelection);
303     pasteAction.updateSelection(structuredSelection);
304     undoAction.update();
305     redoAction.update();
306
307     if (loadResourceAction != null)
308     {
309       loadResourceAction.update();
310     }
311
312     if (validateAction != null)
313     {
314       validateAction.updateSelection(structuredSelection);
315     }
316   }
317
318   /**
319    * This implements {@link org.eclipse.jface.action.IMenuListener} to help fill the context menus with contributions from the Edit menu.
320    */

321   public void menuAboutToShow(IMenuManager menuManager)
322   {
323     // Add our standard marker.
324
//
325
menuManager.add(new Separator("additions"));
326     menuManager.add(new Separator("edit"));
327
328     // Add the edit menu actions.
329
//
330
menuManager.add(new ActionContributionItem(undoAction));
331     menuManager.add(new ActionContributionItem(redoAction));
332     menuManager.add(new Separator());
333     menuManager.add(new ActionContributionItem(cutAction));
334     menuManager.add(new ActionContributionItem(copyAction));
335     menuManager.add(new ActionContributionItem(pasteAction));
336     menuManager.add(new Separator());
337     menuManager.add(new ActionContributionItem(deleteAction));
338     menuManager.add(new Separator());
339
340     // Add our other standard marker.
341
//
342
menuManager.add(new Separator("additions-end"));
343
344     addGlobalActions(menuManager);
345   }
346
347   /**
348    * This inserts global actions before the "additions-end" separator.
349    */

350   protected void addGlobalActions(IMenuManager menuManager)
351   {
352     if (validateAction != null)
353     {
354       menuManager.insertBefore("additions-end", new ActionContributionItem(validateAction));
355       menuManager.insertBefore("additions-end", new Separator());
356     }
357
358     if (loadResourceAction != null)
359     {
360       menuManager.insertBefore("additions-end", new ActionContributionItem(loadResourceAction));
361       menuManager.insertBefore("additions-end", new Separator());
362     }
363   }
364
365   public void propertyChanged(Object JavaDoc source, int id)
366   {
367     update();
368   }
369 }
370
Popular Tags