| 1 |
/** |
| 2 |
* Copyright (c) 2002-2007 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.resource.ImageDescriptor; |
| 20 |
import org.eclipse.jface.viewers.ISelection; |
| 21 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 22 |
import org.eclipse.ui.IEditorPart; |
| 23 |
import org.eclipse.ui.IWorkbenchPart; |
| 24 |
|
| 25 |
import org.eclipse.emf.common.command.Command; |
| 26 |
import org.eclipse.emf.common.command.UnexecutableCommand; |
| 27 |
import org.eclipse.emf.edit.command.CommandActionDelegate; |
| 28 |
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
| 29 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 30 |
import org.eclipse.emf.edit.domain.IEditingDomainProvider; |
| 31 |
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry; |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* This class is used as a basis for implementing an {@link org.eclipse.jface.action.IAction} |
| 36 |
* on the menubar, the toolbar, or a pop-up menu by delegating all required |
| 37 |
* behaviour to a {@link Command}, only when it is guaranteed that the |
| 38 |
* selection will not change during the life of the action. In other words, |
| 39 |
* the action itself would be created based on the selection, and destroyed |
| 40 |
* when the selection changed. All possible aspects of the action are |
| 41 |
* delegated to the command, namely the enablement state and, if it |
| 42 |
* implements {@link CommandActionDelegate}, the text, the toolbar icon, and |
| 43 |
* the tool tip text; however, this need only be done once, at the time the |
| 44 |
* action is created. |
| 45 |
* |
| 46 |
* <p>Subclasses must provide an implementation for {@link #createActionCommand} |
| 47 |
* that creates the command to perform this action. |
| 48 |
* They may also override {@link #getDefaultImageDescriptor} to provide a |
| 49 |
* default icon and {@link #disable} to set the action's state when a command |
| 50 |
* cannot be created. |
| 51 |
*/ |
| 52 |
public abstract class StaticSelectionCommandAction extends Action |
| 53 |
{ |
| 54 |
/** |
| 55 |
* This records the editing domain of the current editor or viewer. For global |
| 56 |
* popups, we try to determine the editing domain from the selected |
| 57 |
* objects themselves. |
| 58 |
*/ |
| 59 |
protected EditingDomain editingDomain; |
| 60 |
|
| 61 |
/** |
| 62 |
* This records the command. |
| 63 |
*/ |
| 64 |
protected Command command; |
| 65 |
|
| 66 |
/** |
| 67 |
* This constructs an instance for a command to be executed via |
| 68 |
* workbenchPart's editing domain. |
| 69 |
* @since 2.1.0 |
| 70 |
*/ |
| 71 |
public StaticSelectionCommandAction(IWorkbenchPart workbenchPart) |
| 72 |
{ |
| 73 |
super(); |
| 74 |
|
| 75 |
// try to get editing domain from workbench part |
| 76 |
if (workbenchPart instanceof IEditingDomainProvider) |
| 77 |
{ |
| 78 |
editingDomain = ((IEditingDomainProvider)workbenchPart).getEditingDomain(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* This constructor is simply retained for binary compatibility. It just |
| 84 |
* calls the {@link #StaticSelectionCommandAction(IWorkbenchPart) new form}. |
| 85 |
*/ |
| 86 |
public StaticSelectionCommandAction(IEditorPart editorPart) |
| 87 |
{ |
| 88 |
this((IWorkbenchPart)editorPart); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* This constructs an instance for a command to be executed via the given editing domain. |
| 93 |
* @since 2.4.0 |
| 94 |
*/ |
| 95 |
public StaticSelectionCommandAction(EditingDomain editingDomain) |
| 96 |
{ |
| 97 |
this.editingDomain = editingDomain; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* This constructs an instance without a specified workbenchPart. |
| 102 |
*/ |
| 103 |
public StaticSelectionCommandAction() |
| 104 |
{ |
| 105 |
super(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* This should be implemented to create a command that performs the action. |
| 110 |
*/ |
| 111 |
protected abstract Command createActionCommand(EditingDomain editingDomain, Collection<?> collection); |
| 112 |
|
| 113 |
/** |
| 114 |
* This extracts the objects from selection, invokes createActionCommand |
| 115 |
* to create the command, and then configures the action based on the |
| 116 |
* result. |
| 117 |
*/ |
| 118 |
public void configureAction(ISelection selection) |
| 119 |
{ |
| 120 |
// only handle structured selections |
| 121 |
if (!(selection instanceof IStructuredSelection)) |
| 122 |
{ |
| 123 |
disable(); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
// convert the selection to a collection of the selected objects |
| 128 |
IStructuredSelection sselection = (IStructuredSelection) selection; |
| 129 |
List<?> list = sselection.toList(); |
| 130 |
Collection<Object> collection = new ArrayList<Object>(list); |
| 131 |
|
| 132 |
// if the editing domain wasn't given by the workbench part, try to get |
| 133 |
// it from the selection |
| 134 |
if (editingDomain == null) |
| 135 |
{ |
| 136 |
for (Object o : collection) |
| 137 |
{ |
| 138 |
editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(o); |
| 139 |
if (editingDomain != null) |
| 140 |
{ |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// if we found an editing domain, create command |
| 147 |
if (editingDomain != null) |
| 148 |
{ |
| 149 |
command = createActionCommand(editingDomain, collection); |
| 150 |
setEnabled(command.canExecute()); |
| 151 |
} |
| 152 |
|
| 153 |
// give up if we couldn't create the command; otherwise, use a |
| 154 |
// CommandActionDelegate to set the action's text, tool-tip, icon, |
| 155 |
// etc. or just use the default icon |
| 156 |
if (command == null || command == UnexecutableCommand.INSTANCE) |
| 157 |
{ |
| 158 |
disable(); |
| 159 |
} |
| 160 |
else if (!(command instanceof CommandActionDelegate)) |
| 161 |
{ |
| 162 |
if (getDefaultImageDescriptor() != null) |
| 163 |
{ |
| 164 |
setImageDescriptor(getDefaultImageDescriptor()); |
| 165 |
} |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
CommandActionDelegate commandActionDelegate = |
| 170 |
(CommandActionDelegate) command; |
| 171 |
|
| 172 |
ImageDescriptor imageDescriptor = |
| 173 |
objectToImageDescriptor(commandActionDelegate.getImage()); |
| 174 |
if (imageDescriptor != null) |
| 175 |
{ |
| 176 |
setImageDescriptor(imageDescriptor); |
| 177 |
} |
| 178 |
else if (getDefaultImageDescriptor() != null) |
| 179 |
{ |
| 180 |
setImageDescriptor(getDefaultImageDescriptor()); |
| 181 |
} |
| 182 |
|
| 183 |
if (commandActionDelegate.getText() != null) |
| 184 |
{ |
| 185 |
setText(commandActionDelegate.getText()); |
| 186 |
} |
| 187 |
|
| 188 |
if (commandActionDelegate.getDescription() != null) |
| 189 |
{ |
| 190 |
setDescription(commandActionDelegate.getDescription()); |
| 191 |
} |
| 192 |
|
| 193 |
if (commandActionDelegate.getToolTipText() != null) |
| 194 |
{ |
| 195 |
setToolTipText(commandActionDelegate.getToolTipText()); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* This can be overridden to provide the image descriptor used when the |
| 203 |
* command does not provide one. This implementation simply returns null. |
| 204 |
*/ |
| 205 |
protected ImageDescriptor getDefaultImageDescriptor() |
| 206 |
{ |
| 207 |
return null; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* This gets invoked when the selection is inappropriate or the command |
| 212 |
* cannot be created. It puts the action in the correct state for such |
| 213 |
* error conditions. This implementation disables the action and sets its |
| 214 |
* icon to the default. |
| 215 |
*/ |
| 216 |
protected void disable() |
| 217 |
{ |
| 218 |
setEnabled(false); |
| 219 |
if (getDefaultImageDescriptor() != null) |
| 220 |
{ |
| 221 |
setImageDescriptor(getDefaultImageDescriptor()); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* This executes the command. |
| 227 |
*/ |
| 228 |
@Override |
| 229 |
public void run() |
| 230 |
{ |
| 231 |
// this guard is for extra security, but should not be necessary |
| 232 |
if (editingDomain != null && command != null) |
| 233 |
{ |
| 234 |
// use up the command |
| 235 |
editingDomain.getCommandStack().execute(command); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* If necessary, this converts any image representation into an image |
| 241 |
* descriptor. |
| 242 |
*/ |
| 243 |
protected ImageDescriptor objectToImageDescriptor(Object object) |
| 244 |
{ |
| 245 |
return ExtendedImageRegistry.getInstance().getImageDescriptor(object); |
| 246 |
} |
| 247 |
} |