KickJava   Java API By Example, From Geeks To Geeks.

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


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: CommandAction.java,v 1.3 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.action;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.ui.IActionDelegate2;
31 import org.eclipse.ui.IEditorActionDelegate;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IViewActionDelegate;
34 import org.eclipse.ui.IViewPart;
35 import org.eclipse.ui.IWorkbenchPart;
36
37 import org.eclipse.emf.common.command.Command;
38 import org.eclipse.emf.common.command.UnexecutableCommand;
39 import org.eclipse.emf.edit.command.CommandActionDelegate;
40 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
41 import org.eclipse.emf.edit.domain.EditingDomain;
42 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
43 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
44
45
46 /**
47  * This class is used to implement a selection-based {@link IAction} on the menubar, the toolbar, or a popup menu
48  * by delegating all required behaviour to a {@link Command}.
49  * All aspects of the action are delegated,
50  * namely the enablement state, the menu text, the toolbar icon, and the help tip text.
51  * A derived class implements {@link #createActionCommand createActionCommand}
52  * to return a command based on the {@link EditingDomain} and the collection of selected objects.
53  *
54  * <p>
55  * This class can also be used to implement actions not based on a selection,
56  * in that case the method {@link #selectionChanged selectionChanged} should be overriden to do nothing.
57  */

58 public class CommandAction implements IEditorActionDelegate, IViewActionDelegate, IActionDelegate2
59 {
60   /**
61    * This records the editor or view with which the action is currently associated.
62    * @since 2.1.0
63    */

64   protected IWorkbenchPart workbenchPart;
65
66   /**
67    * If this action delegate is associated with an editor, it is also recorded here.
68    * This field was retained for backwards compatibility.
69    * @deprecated As of EMF 2.1.0, replaced by {@link #workbenchPart}.
70    */

71   protected IEditorPart editorPart;
72
73   /**
74    * This records the proxy action created by the platform.
75    */

76   protected IAction action;
77
78   /**
79    * This records the editing domain of the current editor.
80    * For global popups, we try to determine the editing domain from the selected objects themselves.
81    */

82   protected EditingDomain editingDomain;
83
84   /**
85    * This records the collection of selected objects so that a new command can be easily constructed
86    * after the execution of the command previously constructed from this selection.
87    */

88   protected Collection JavaDoc collection;
89
90   /**
91    * This records the command that is created each time the selection changes.
92    */

93   protected Command command;
94
95   /**
96    * This constructs an instance.
97    */

98   public CommandAction()
99   {
100   }
101
102   /**
103    * This method must be implemented to create the command for this action,
104    * given the editing domain and the collection of selected objects.
105    */

106   protected Command createActionCommand(EditingDomain editingDomain, Collection JavaDoc collection)
107   {
108     return UnexecutableCommand.INSTANCE;
109   }
110
111   /**
112    * This returns the image descriptor if the command does not provide an override.
113    */

114   protected ImageDescriptor getDefaultImageDescriptor()
115   {
116     return null;
117   }
118
119   /**
120    * This is called immediately after this action delegate is created.
121    * We use this as an opportunity to record the proxy action for later use.
122    * @since 2.1.0
123    */

124   public void init(IAction action)
125   {
126     this.action = action;
127   }
128
129   /**
130    * This is called when this action delegate is no longer needed. This implementation does nothing.
131    * @since 2.1.0
132    */

133   public void dispose()
134   {
135   }
136
137   /**
138    * For editor actions, the framework calls this when the active editor changes, so that we can connect with it.
139    * We call {@link #setActiveWorkbenchPart} to record it and its editing domain, if it can provide one.
140    */

141   public void setActiveEditor(IAction action, IEditorPart editorPart)
142   {
143     setActiveWorkbenchPart(editorPart);
144     this.editorPart = editorPart;
145     this.action = action;
146   }
147
148   /**
149    * For view actions, the framework calls this when the view is shown, so that we can connect with it.
150    * We call {@link #setActiveWorkbenchPart} to record it and its editing domain, if it can provide one.
151    * @since 2.1.0
152    */

153   public void init(IViewPart view)
154   {
155     setActiveWorkbenchPart(view);
156   }
157
158   /**
159    * This records the specified workbench part, and if it is an editing domain provider, its editing domain.
160    * @since 2.1.0
161    */

162   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
163   {
164     // If the workbench part changes...
165
//
166
if (this.workbenchPart != workbenchPart)
167     {
168       // Discard the old editing domain.
169
//
170
editingDomain = null;
171
172       // If there is a new one...
173
//
174
if (workbenchPart != null)
175       {
176         // Does this part provide an editing domain?
177
//
178
if (workbenchPart instanceof IEditingDomainProvider)
179         {
180           editingDomain = ((IEditingDomainProvider)workbenchPart).getEditingDomain();
181         }
182       }
183
184       // Record the part.
185
//
186
this.workbenchPart = workbenchPart;
187     }
188   }
189
190   /**
191    * Because we implement {@link IActionDelegate2}, this is called instead of the old {@link #run(IAction) run}.
192    * This simply calls that method, which must be invokved since a subclass may have overridden it.
193    * @since 2.1.0
194    */

195   public void runWithEvent(IAction action, Event event)
196   {
197     run(action);
198   }
199
200   /**
201    * The action must have been enabled for this to have been called,
202    * so we must have stored the selection already by this point.
203    */

204   public void run(IAction action)
205   {
206     // This guard is for extra security, but should not be necessary.
207
//
208
if (editingDomain != null && command != null)
209     {
210       // Use up the command.
211
// Note that notification will cause a new command to be created.
212
//
213
editingDomain.getCommandStack().execute(command);
214     }
215   }
216
217   /**
218    * This is invoked by the framework so that the action state can be updated.
219    */

220   public void selectionChanged(IAction action, ISelection selection)
221   {
222     // We will only deal with structured selections.
223
//
224
if (selection instanceof IStructuredSelection)
225     {
226       // Convert the selection to a collection of the selected objects.
227
//
228
collection = new ArrayList JavaDoc();
229       for (Iterator JavaDoc elements = ((IStructuredSelection)selection).iterator(); elements.hasNext(); )
230       {
231         collection.add(elements.next());
232       }
233
234       // If we aren't getting the domain from the workbench part...
235
// This happens when this action is used for a global popup action.
236
// We try to get the editing domain from one of the objects in the selection.
237
//
238
if (workbenchPart == null && editorPart == null) //DMS editingDomain == null) ?
239
{
240         for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
241         {
242           Object JavaDoc object = objects.next();
243           editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(object);
244           if (editingDomain != null)
245           {
246             break;
247           }
248         }
249       }
250
251       // If we have a good editing domain...
252
//
253
if (editingDomain != null)
254       {
255         // Delegate the action for this object to the editing domain.
256
//
257
command = createActionCommand(editingDomain, collection);
258
259         // We can enable the action as indicated by the command,
260
// and we can set all the other values from the command.
261
//
262
((Action)action).setEnabled(command.canExecute());
263
264         if (command instanceof CommandActionDelegate)
265         {
266           CommandActionDelegate commandActionDelegate = (CommandActionDelegate)command;
267           Object JavaDoc object = commandActionDelegate.getImage();
268           ImageDescriptor imageDescriptor = objectToImageDescriptor(object);
269           if (imageDescriptor != null)
270           {
271             ((Action)action).setImageDescriptor(imageDescriptor);
272           }
273           else if (getDefaultImageDescriptor() != null)
274           {
275             ((Action)action).setImageDescriptor(getDefaultImageDescriptor());
276           }
277
278           if (commandActionDelegate.getText() != null)
279           {
280             ((Action)action).setText(commandActionDelegate.getText());
281           }
282
283           if (commandActionDelegate.getDescription() != null)
284           {
285             ((Action)action).setDescription(commandActionDelegate.getDescription());
286           }
287
288           if (commandActionDelegate.getToolTipText() != null)
289           {
290             ((Action)action).setToolTipText(commandActionDelegate.getToolTipText());
291           }
292         }
293
294         // Nothing more to do and we don't want to do the default stuff below.
295
//
296
return;
297       }
298     }
299
300     // We just can't do it.
301
//
302
((Action)action).setEnabled(false);
303
304     // No point in keeping garbage.
305
//
306
command = null;
307     collection = null;
308
309     // Show the colourless image.
310
//
311
if (getDefaultImageDescriptor() != null)
312     {
313       ((Action)action).setImageDescriptor(getDefaultImageDescriptor());
314     }
315   }
316
317   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
318   {
319     return ExtendedImageRegistry.getInstance().getImageDescriptor(object);
320   }
321 }
322
Popular Tags