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.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 58 public abstract class StaticSelectionCommandAction extends Action 59 { 60 65 protected EditingDomain editingDomain; 66 67 70 protected Command command; 71 72 77 public StaticSelectionCommandAction(IWorkbenchPart workbenchPart) 78 { 79 super(); 80 81 if (workbenchPart instanceof IEditingDomainProvider) 83 { 84 editingDomain = ((IEditingDomainProvider)workbenchPart).getEditingDomain(); 85 } 86 } 87 88 92 public StaticSelectionCommandAction(IEditorPart editorPart) 93 { 94 this((IWorkbenchPart)editorPart); 95 } 96 97 100 public StaticSelectionCommandAction() 101 { 102 super(); 103 } 104 105 108 protected abstract Command createActionCommand(EditingDomain editingDomain, 109 Collection collection); 110 111 116 public void configureAction(ISelection selection) 117 { 118 if (!(selection instanceof IStructuredSelection)) 120 { 121 disable(); 122 } 123 else 124 { 125 IStructuredSelection sselection = (IStructuredSelection) selection; 127 Collection collection = new ArrayList (sselection.size()); 128 for (Iterator i = sselection.iterator(); i.hasNext(); ) 129 { 130 collection.add(i.next()); 131 } 132 133 for (Iterator i = collection.iterator(); 136 editingDomain == null && i.hasNext(); ) 137 { 138 Object o = i.next(); 139 editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(o); 140 } 141 142 if (editingDomain != null) 144 { 145 command = createActionCommand(editingDomain, collection); 146 setEnabled(command.canExecute()); 147 } 148 149 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 201 protected ImageDescriptor getDefaultImageDescriptor() 202 { 203 return null; 204 } 205 206 212 protected void disable() 213 { 214 setEnabled(false); 215 if (getDefaultImageDescriptor() != null) 216 { 217 setImageDescriptor(getDefaultImageDescriptor()); 218 } 219 } 220 221 224 public void run() 225 { 226 if (editingDomain != null && command != null) 228 { 229 editingDomain.getCommandStack().execute(command); 231 } 232 } 233 234 238 protected ImageDescriptor objectToImageDescriptor(Object object) 239 { 240 return ExtendedImageRegistry.getInstance().getImageDescriptor(object); 241 } 242 } 243 | Popular Tags |