1 17 package org.eclipse.emf.edit.ui.action; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 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 58 public class CommandAction implements IEditorActionDelegate, IViewActionDelegate, IActionDelegate2 59 { 60 64 protected IWorkbenchPart workbenchPart; 65 66 71 protected IEditorPart editorPart; 72 73 76 protected IAction action; 77 78 82 protected EditingDomain editingDomain; 83 84 88 protected Collection collection; 89 90 93 protected Command command; 94 95 98 public CommandAction() 99 { 100 } 101 102 106 protected Command createActionCommand(EditingDomain editingDomain, Collection collection) 107 { 108 return UnexecutableCommand.INSTANCE; 109 } 110 111 114 protected ImageDescriptor getDefaultImageDescriptor() 115 { 116 return null; 117 } 118 119 124 public void init(IAction action) 125 { 126 this.action = action; 127 } 128 129 133 public void dispose() 134 { 135 } 136 137 141 public void setActiveEditor(IAction action, IEditorPart editorPart) 142 { 143 setActiveWorkbenchPart(editorPart); 144 this.editorPart = editorPart; 145 this.action = action; 146 } 147 148 153 public void init(IViewPart view) 154 { 155 setActiveWorkbenchPart(view); 156 } 157 158 162 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) 163 { 164 if (this.workbenchPart != workbenchPart) 167 { 168 editingDomain = null; 171 172 if (workbenchPart != null) 175 { 176 if (workbenchPart instanceof IEditingDomainProvider) 179 { 180 editingDomain = ((IEditingDomainProvider)workbenchPart).getEditingDomain(); 181 } 182 } 183 184 this.workbenchPart = workbenchPart; 187 } 188 } 189 190 195 public void runWithEvent(IAction action, Event event) 196 { 197 run(action); 198 } 199 200 204 public void run(IAction action) 205 { 206 if (editingDomain != null && command != null) 209 { 210 editingDomain.getCommandStack().execute(command); 214 } 215 } 216 217 220 public void selectionChanged(IAction action, ISelection selection) 221 { 222 if (selection instanceof IStructuredSelection) 225 { 226 collection = new ArrayList (); 229 for (Iterator elements = ((IStructuredSelection)selection).iterator(); elements.hasNext(); ) 230 { 231 collection.add(elements.next()); 232 } 233 234 if (workbenchPart == null && editorPart == null) { 240 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 241 { 242 Object object = objects.next(); 243 editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(object); 244 if (editingDomain != null) 245 { 246 break; 247 } 248 } 249 } 250 251 if (editingDomain != null) 254 { 255 command = createActionCommand(editingDomain, collection); 258 259 ((Action)action).setEnabled(command.canExecute()); 263 264 if (command instanceof CommandActionDelegate) 265 { 266 CommandActionDelegate commandActionDelegate = (CommandActionDelegate)command; 267 Object 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 return; 297 } 298 } 299 300 ((Action)action).setEnabled(false); 303 304 command = null; 307 collection = null; 308 309 if (getDefaultImageDescriptor() != null) 312 { 313 ((Action)action).setImageDescriptor(getDefaultImageDescriptor()); 314 } 315 } 316 317 protected ImageDescriptor objectToImageDescriptor(Object object) 318 { 319 return ExtendedImageRegistry.getInstance().getImageDescriptor(object); 320 } 321 } 322 | Popular Tags |