KickJava   Java API By Example, From Geeks To Geeks.

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


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: StaticSelectionCommandAction.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.resource.ImageDescriptor;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.IWorkbenchPart;
30
31 import org.eclipse.emf.common.command.Command;
32 import org.eclipse.emf.common.command.UnexecutableCommand;
33 import org.eclipse.emf.edit.command.CommandActionDelegate;
34 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
35 import org.eclipse.emf.edit.domain.EditingDomain;
36 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
37 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
38
39
40 /**
41  * This class is used as a basis for implementing an {@link org.eclipse.jface.action.IAction}
42  * on the menubar, the toolbar, or a popup menu by delegating all required
43  * behaviour to a {@link Command}, only when it is guaranteed that the
44  * selection will not change during the life of the action. In other words,
45  * the action itself would be created based on the selection, and destroyed
46  * when the selection changed. All possible aspects of the action are
47  * delegated to the command, namely the enablement state and, if it
48  * implements {@link CommandActionDelegate}, the text, the toolbar icon, and
49  * the tool tip text; however, this need only be done once, at the time the
50  * action is created.
51  *
52  * <p>Subclasses must provide an implementation for {@link #createActionCommand}
53  * that creates the command to perform this action.
54  * They may also override {@link #getDefaultImageDescriptor} to provide a
55  * default icon and {@link #disable} to set the action's state when a command
56  * cannot be created.
57  */

58 public abstract class StaticSelectionCommandAction extends Action
59 {
60   /**
61    * This records the editing domain of the current editor or viewer. For global
62    * popups, we try to determine the editing domain from the selected
63    * objects themselves.
64    */

65   protected EditingDomain editingDomain;
66
67   /**
68    * This records the command.
69    */

70   protected Command command;
71
72   /**
73    * This constructs an instance for a command to be executed via
74    * workbenchPart's editing domain.
75    * @since 2.1.0
76    */

77   public StaticSelectionCommandAction(IWorkbenchPart workbenchPart)
78   {
79     super();
80
81     // try to get editing domain from workbench part
82
if (workbenchPart instanceof IEditingDomainProvider)
83     {
84       editingDomain = ((IEditingDomainProvider)workbenchPart).getEditingDomain();
85     }
86   }
87
88   /**
89    * This constructor is simply retained for binary compatibility. It just
90    * calls the {@link #StaticSelectionCommandAction(IWorkbenchPart) new form}.
91    */

92   public StaticSelectionCommandAction(IEditorPart editorPart)
93   {
94     this((IWorkbenchPart)editorPart);
95   }
96
97   /**
98    * This constructs an instance without a specified workbenchPart.
99    */

100   public StaticSelectionCommandAction()
101   {
102     super();
103   }
104
105   /**
106    * This should be implemented to create a command that performs the action.
107    */

108   protected abstract Command createActionCommand(EditingDomain editingDomain,
109                                                  Collection JavaDoc collection);
110
111   /**
112    * This extracts the objects from selection, invokes createActionCommand
113    * to create the command, and then configures the action based on the
114    * result.
115    */

116   public void configureAction(ISelection selection)
117   {
118     // only handle structured selections
119
if (!(selection instanceof IStructuredSelection))
120     {
121       disable();
122     }
123     else
124     {
125       // convert the selection to a collection of the selected objects
126
IStructuredSelection sselection = (IStructuredSelection) selection;
127       Collection JavaDoc collection = new ArrayList JavaDoc(sselection.size());
128       for (Iterator JavaDoc i = sselection.iterator(); i.hasNext(); )
129       {
130         collection.add(i.next());
131       }
132       
133       // if the editing domain wasn't given by the workbench part, try to get
134
// it from the selection
135
for (Iterator JavaDoc i = collection.iterator();
136            editingDomain == null && i.hasNext(); )
137       {
138         Object JavaDoc o = i.next();
139         editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(o);
140       }
141
142       // if we found an editing domain, create command
143
if (editingDomain != null)
144       {
145         command = createActionCommand(editingDomain, collection);
146         setEnabled(command.canExecute());
147       }
148
149       // give up if we couldn't create the command; otherwise, use a
150
// CommandActionDelegate to set the action's text, tooltip, icon,
151
// etc. or just use the default icon
152
if (command == null || command == UnexecutableCommand.INSTANCE)
153       {
154         disable();
155       }
156       else if (!(command instanceof CommandActionDelegate))
157       {
158         if (getDefaultImageDescriptor() != null)
159         {
160           setImageDescriptor(getDefaultImageDescriptor());
161         }
162       }
163       else
164       {
165         CommandActionDelegate commandActionDelegate =
166           (CommandActionDelegate) command;
167
168         ImageDescriptor imageDescriptor =
169           objectToImageDescriptor(commandActionDelegate.getImage());
170         if (imageDescriptor != null)
171         {
172           setImageDescriptor(imageDescriptor);
173         }
174         else if (getDefaultImageDescriptor() != null)
175         {
176           setImageDescriptor(getDefaultImageDescriptor());
177         }
178
179         if (commandActionDelegate.getText() != null)
180         {
181           setText(commandActionDelegate.getText());
182         }
183         
184         if (commandActionDelegate.getDescription() != null)
185         {
186           setDescription(commandActionDelegate.getDescription());
187         }
188
189         if (commandActionDelegate.getToolTipText() != null)
190         {
191           setToolTipText(commandActionDelegate.getToolTipText());
192         }
193       }
194     }
195   }
196
197   /**
198    * This can be overridden to provide the image descriptor used when the
199    * command does not provide one. This implementation simply returns null.
200    */

201   protected ImageDescriptor getDefaultImageDescriptor()
202   {
203     return null;
204   }
205
206   /**
207    * This gets invoked when the selection is inappropriate or the command
208    * cannot be created. It puts the action in the correct state for such
209    * error conditions. This implementation disables the action and sets its
210    * icon to the default.
211    */

212   protected void disable()
213   {
214     setEnabled(false);
215     if (getDefaultImageDescriptor() != null)
216     {
217       setImageDescriptor(getDefaultImageDescriptor());
218     }
219   }
220
221   /**
222    * This executes the command.
223    */

224   public void run()
225   {
226     // this guard is for extra security, but should not be necessary
227
if (editingDomain != null && command != null)
228     {
229       // use up the command
230
editingDomain.getCommandStack().execute(command);
231     }
232   }
233
234   /**
235    * If necessary, this converts any image representation into an image
236    * descriptor.
237    */

238   protected ImageDescriptor objectToImageDescriptor(Object JavaDoc object)
239   {
240     return ExtendedImageRegistry.getInstance().getImageDescriptor(object);
241   }
242 }
243
Popular Tags