| 1 |
/** |
| 2 |
* Copyright (c) 2002-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 v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.edit.ui.action; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collection; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.jface.action.Action; |
| 19 |
import org.eclipse.jface.action.IAction; |
| 20 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 21 |
import org.eclipse.jface.viewers.ISelection; |
| 22 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 |
import org.eclipse.swt.widgets.Event; |
| 24 |
import org.eclipse.ui.IActionDelegate2; |
| 25 |
import org.eclipse.ui.IEditorActionDelegate; |
| 26 |
import org.eclipse.ui.IEditorPart; |
| 27 |
import org.eclipse.ui.IViewActionDelegate; |
| 28 |
import org.eclipse.ui.IViewPart; |
| 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 to implement a selection-based {@link IAction} on the menubar, the toolbar, or a popup menu |
| 42 |
* by delegating all required behaviour to a {@link Command}. |
| 43 |
* All aspects of the action are delegated, |
| 44 |
* namely the enablement state, the menu text, the toolbar icon, and the help tip text. |
| 45 |
* A derived class implements {@link #createActionCommand createActionCommand} |
| 46 |
* to return a command based on the {@link EditingDomain} and the collection of selected objects. |
| 47 |
* |
| 48 |
* <p> |
| 49 |
* This class can also be used to implement actions not based on a selection, |
| 50 |
* in that case the method {@link #selectionChanged selectionChanged} should be overridden to do nothing. |
| 51 |
*/ |
| 52 |
public class CommandAction implements IEditorActionDelegate, IViewActionDelegate, IActionDelegate2 |
| 53 |
{ |
| 54 |
/** |
| 55 |
* This records the editor or view with which the action is currently associated. |
| 56 |
* @since 2.1.0 |
| 57 |
*/ |
| 58 |
protected IWorkbenchPart workbenchPart; |
| 59 |
|
| 60 |
/** |
| 61 |
* If this action delegate is associated with an editor, it is also recorded here. |
| 62 |
* This field was retained for backwards compatibility. |
| 63 |
* @deprecated As of EMF 2.1.0, replaced by {@link #workbenchPart}. |
| 64 |
*/ |
| 65 |
@Deprecated |
| 66 |
protected IEditorPart editorPart; |
| 67 |
|
| 68 |
/** |
| 69 |
* This records the proxy action created by the platform. |
| 70 |
*/ |
| 71 |
protected IAction action; |
| 72 |
|
| 73 |
/** |
| 74 |
* This records the editing domain of the current editor. |
| 75 |
* For global popups, we try to determine the editing domain from the selected objects themselves. |
| 76 |
*/ |
| 77 |
protected EditingDomain editingDomain; |
| 78 |
|
| 79 |
/** |
| 80 |
* This records the collection of selected objects so that a new command can be easily constructed |
| 81 |
* after the execution of the command previously constructed from this selection. |
| 82 |
*/ |
| 83 |
protected Collection<Object> collection; |
| 84 |
|
| 85 |
/** |
| 86 |
* This records the command that is created each time the selection changes. |
| 87 |
*/ |
| 88 |
protected Command command; |
| 89 |
|
| 90 |
/** |
| 91 |
* This constructs an instance. |
| 92 |
*/ |
| 93 |
public CommandAction() |
| 94 |
{ |
| 95 |
super(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* This method must be implemented to create the command for this action, |
| 100 |
* given the editing domain and the collection of selected objects. |
| 101 |
*/ |
| 102 |
protected Command createActionCommand(EditingDomain editingDomain, Collection<?> collection) |
| 103 |
{ |
| 104 |
return UnexecutableCommand.INSTANCE; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* This returns the image descriptor if the command does not provide an override. |
| 109 |
*/ |
| 110 |
protected ImageDescriptor getDefaultImageDescriptor() |
| 111 |
{ |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* This is called immediately after this action delegate is created. |
| 117 |
* We use this as an opportunity to record the proxy action for later use. |
| 118 |
* @since 2.1.0 |
| 119 |
*/ |
| 120 |
public void init(IAction action) |
| 121 |
{ |
| 122 |
this.action = action; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* This is called when this action delegate is no longer needed. This implementation does nothing. |
| 127 |
* @since 2.1.0 |
| 128 |
*/ |
| 129 |
public void dispose() |
| 130 |
{ |
| 131 |
// Do nothing |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* For editor actions, the framework calls this when the active editor changes, so that we can connect with it. |
| 136 |
* We call {@link #setActiveWorkbenchPart} to record it and its editing domain, if it can provide one. |
| 137 |
*/ |
| 138 |
public void setActiveEditor(IAction action, IEditorPart editorPart) |
| 139 |
{ |
| 140 |
setActiveWorkbenchPart(editorPart); |
| 141 |
this.editorPart = editorPart; |
| 142 |
this.action = action; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* For view actions, the framework calls this when the view is shown, so that we can connect with it. |
| 147 |
* We call {@link #setActiveWorkbenchPart} to record it and its editing domain, if it can provide one. |
| 148 |
* @since 2.1.0 |
| 149 |
*/ |
| 150 |
public void init(IViewPart view) |
| 151 |
{ |
| 152 |
setActiveWorkbenchPart(view); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* This records the specified workbench part, and if it is an editing domain provider, its editing domain. |
| 157 |
* @since 2.1.0 |
| 158 |
*/ |
| 159 |
public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) |
| 160 |
{ |
| 161 |
// If the workbench part changes... |
| 162 |
// |
| 163 |
if (this.workbenchPart != workbenchPart) |
| 164 |
{ |
| 165 |
// Discard the old editing domain. |
| 166 |
// |
| 167 |
editingDomain = null; |
| 168 |
|
| 169 |
// If there is a new one... |
| 170 |
// |
| 171 |
if (workbenchPart != null) |
| 172 |
{ |
| 173 |
// Does this part provide an editing domain? |
| 174 |
// |
| 175 |
if (workbenchPart instanceof IEditingDomainProvider) |
| 176 |
{ |
| 177 |
editingDomain = ((IEditingDomainProvider)workbenchPart).getEditingDomain(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// Record the part. |
| 182 |
// |
| 183 |
this.workbenchPart = workbenchPart; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Because we implement {@link IActionDelegate2}, this is called instead of the old {@link #run(IAction) run}. |
| 189 |
* This simply calls that method, which must be invoked since a subclass may have overridden it. |
| 190 |
* @since 2.1.0 |
| 191 |
*/ |
| 192 |
public void runWithEvent(IAction action, Event event) |
| 193 |
{ |
| 194 |
run(action); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* The action must have been enabled for this to have been called, |
| 199 |
* so we must have stored the selection already by this point. |
| 200 |
*/ |
| 201 |
public void run(IAction action) |
| 202 |
{ |
| 203 |
// This guard is for extra security, but should not be necessary. |
| 204 |
// |
| 205 |
if (editingDomain != null && command != null) |
| 206 |
{ |
| 207 |
// Use up the command. |
| 208 |
// Note that notification will cause a new command to be created. |
| 209 |
// |
| 210 |
editingDomain.getCommandStack().execute(command); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* This is invoked by the framework so that the action state can be updated. |
| 216 |
*/ |
| 217 |
public void selectionChanged(IAction action, ISelection selection) |
| 218 |
{ |
| 219 |
// We will only deal with structured selections. |
| 220 |
// |
| 221 |
if (selection instanceof IStructuredSelection) |
| 222 |
{ |
| 223 |
// Convert the selection to a collection of the selected objects. |
| 224 |
// |
| 225 |
List<?> list = ((IStructuredSelection)selection).toList(); |
| 226 |
collection = new ArrayList<Object>(list); |
| 227 |
|
| 228 |
// If we aren't getting the domain from the workbench part... |
| 229 |
// This happens when this action is used for a global popup action. |
| 230 |
// We try to get the editing domain from one of the objects in the selection. |
| 231 |
// |
| 232 |
if (workbenchPart == null && editorPart == null) //DMS editingDomain == null) ? |
| 233 |
{ |
| 234 |
for (Object object : collection) |
| 235 |
{ |
| 236 |
editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(object); |
| 237 |
if (editingDomain != null) |
| 238 |
{ |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// If we have a good editing domain... |
| 245 |
// |
| 246 |
if (editingDomain != null) |
| 247 |
{ |
| 248 |
// Delegate the action for this object to the editing domain. |
| 249 |
// |
| 250 |
command = createActionCommand(editingDomain, collection); |
| 251 |
|
| 252 |
// We can enable the action as indicated by the command, |
| 253 |
// and we can set all the other values from the command. |
| 254 |
// |
| 255 |
((Action)action).setEnabled(command.canExecute()); |
| 256 |
|
| 257 |
if (command instanceof CommandActionDelegate) |
| 258 |
{ |
| 259 |
CommandActionDelegate commandActionDelegate = (CommandActionDelegate)command; |
| 260 |
Object object = commandActionDelegate.getImage(); |
| 261 |
ImageDescriptor imageDescriptor = objectToImageDescriptor(object); |
| 262 |
if (imageDescriptor != null) |
| 263 |
{ |
| 264 |
((Action)action).setImageDescriptor(imageDescriptor); |
| 265 |
} |
| 266 |
else if (getDefaultImageDescriptor() != null) |
| 267 |
{ |
| 268 |
((Action)action).setImageDescriptor(getDefaultImageDescriptor()); |
| 269 |
} |
| 270 |
|
| 271 |
if (commandActionDelegate.getText() != null) |
| 272 |
{ |
| 273 |
((Action)action).setText(commandActionDelegate.getText()); |
| 274 |
} |
| 275 |
|
| 276 |
if (commandActionDelegate.getDescription() != null) |
| 277 |
{ |
| 278 |
((Action)action).setDescription(commandActionDelegate.getDescription()); |
| 279 |
} |
| 280 |
|
| 281 |
if (commandActionDelegate.getToolTipText() != null) |
| 282 |
{ |
| 283 |
((Action)action).setToolTipText(commandActionDelegate.getToolTipText()); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Nothing more to do and we don't want to do the default stuff below. |
| 288 |
// |
| 289 |
return; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
// We just can't do it. |
| 294 |
// |
| 295 |
((Action)action).setEnabled(false); |
| 296 |
|
| 297 |
// No point in keeping garbage. |
| 298 |
// |
| 299 |
command = null; |
| 300 |
collection = null; |
| 301 |
|
| 302 |
// Show the colourless image. |
| 303 |
// |
| 304 |
if (getDefaultImageDescriptor() != null) |
| 305 |
{ |
| 306 |
((Action)action).setImageDescriptor(getDefaultImageDescriptor()); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
protected ImageDescriptor objectToImageDescriptor(Object object) |
| 311 |
{ |
| 312 |
return ExtendedImageRegistry.getInstance().getImageDescriptor(object); |
| 313 |
} |
| 314 |
} |