1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.jface.action.Action; 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.viewers.ISelection; 20 import org.eclipse.jface.viewers.ISelectionChangedListener; 21 import org.eclipse.jface.viewers.SelectionChangedEvent; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.swt.widgets.Event; 24 import org.eclipse.ui.IActionDelegate; 25 import org.eclipse.ui.IActionDelegate2; 26 import org.eclipse.ui.IActionDelegateWithEvent; 27 import org.eclipse.ui.INullSelectionListener; 28 import org.eclipse.ui.IPluginContribution; 29 import org.eclipse.ui.ISelectionListener; 30 import org.eclipse.ui.IWorkbenchPart; 31 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 32 import org.eclipse.ui.SelectionEnabler; 33 import org.eclipse.ui.WorkbenchException; 34 import org.eclipse.ui.internal.misc.StatusUtil; 35 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 36 import org.eclipse.ui.internal.util.BundleUtility; 37 import org.eclipse.ui.internal.util.Util; 38 39 53 54 public abstract class PluginAction extends Action implements 55 ISelectionListener, ISelectionChangedListener, INullSelectionListener, 56 IPluginContribution { 57 private IActionDelegate delegate; 58 59 private SelectionEnabler enabler; 60 61 private ISelection selection; 62 63 private IConfigurationElement configElement; 64 65 private String pluginId; 66 67 private String runAttribute = IWorkbenchRegistryConstants.ATT_CLASS; 68 69 private static int actionCount = 0; 70 71 78 public PluginAction(IConfigurationElement actionElement, String id, 79 int style) { 80 super(null, style); 81 82 this.configElement = actionElement; 83 84 if (id != null) { 85 setId(id); 86 } else { 87 setId("PluginAction." + Integer.toString(actionCount)); ++actionCount; 90 } 91 92 String defId = actionElement 93 .getAttribute(IWorkbenchRegistryConstants.ATT_DEFINITION_ID); 94 setActionDefinitionId(defId); 95 96 pluginId = configElement.getNamespace(); 97 98 if (configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ENABLES_FOR) != null) { 100 enabler = new SelectionEnabler(configElement); 101 } else { 102 IConfigurationElement[] kids = configElement 103 .getChildren(IWorkbenchRegistryConstants.TAG_ENABLEMENT); 104 IConfigurationElement[] kids2 = configElement 105 .getChildren(IWorkbenchRegistryConstants.TAG_SELECTION); 106 if (kids.length > 0 || kids2.length>0) { 107 enabler = new SelectionEnabler(configElement); 108 } 109 } 110 111 selectionChanged(new StructuredSelection()); 113 } 114 115 118 protected final void createDelegate() { 119 if (delegate == null && runAttribute != null) { 121 try { 122 Object obj = WorkbenchPlugin.createExtension(configElement, 123 runAttribute); 124 delegate = validateDelegate(obj); 125 initDelegate(); 126 refreshEnablement(); 127 } catch (Throwable e) { 128 runAttribute = null; 129 IStatus status = null; 130 if (e instanceof CoreException) { 131 status = ((CoreException) e).getStatus(); 132 } else { 133 status = StatusUtil 134 .newStatus( 135 IStatus.ERROR, 136 "Internal plug-in action delegate error on creation.", e); } 138 String id = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 139 WorkbenchPlugin 140 .log( 141 "Could not create action delegate for id: " + id, status); return; 143 } 144 } 145 } 146 147 158 protected IActionDelegate validateDelegate(Object obj) 159 throws WorkbenchException { 160 if (obj instanceof IActionDelegate) { 161 return (IActionDelegate) obj; 162 } 163 164 throw new WorkbenchException( 165 "Action must implement IActionDelegate"); } 167 168 172 protected void initDelegate() { 173 if (delegate instanceof IActionDelegate2) { 174 ((IActionDelegate2) delegate).init(this); 175 } 176 } 177 178 183 protected IActionDelegate getDelegate() { 184 return delegate; 185 } 186 187 192 protected boolean isOkToCreateDelegate() { 193 if (getStyle() == IAction.AS_DROP_DOWN_MENU 194 && !WWinPluginPulldown.class.isInstance(this)) { 195 return true; 196 } 197 198 String bundleId = configElement.getNamespace(); 200 return BundleUtility.isActive(bundleId); 201 } 202 203 206 protected void refreshEnablement() { 207 if (enabler != null) { 208 setEnabled(enabler.isEnabledForSelection(selection)); 209 } 210 if (delegate != null) { 211 delegate.selectionChanged(this, selection); 212 } 213 } 214 215 218 public void run() { 219 runWithEvent(null); 220 } 221 222 225 public void runWithEvent(Event event) { 226 if (delegate == null) { 228 createDelegate(); 229 if (delegate == null) { 230 MessageDialog 231 .openInformation( 232 Util.getShellToParentOn(), 233 WorkbenchMessages.Information, 234 WorkbenchMessages.PluginAction_operationNotAvailableMessage); 235 return; 236 } 237 if (!isEnabled()) { 238 MessageDialog.openInformation(Util.getShellToParentOn(), WorkbenchMessages.Information, 239 WorkbenchMessages.PluginAction_disabledMessage); 240 return; 241 } 242 } 243 244 if (event != null) { 245 if (delegate instanceof IActionDelegate2) { 246 ((IActionDelegate2) delegate).runWithEvent(this, event); 247 return; 248 } 249 if (delegate instanceof IActionDelegateWithEvent) { 251 ((IActionDelegateWithEvent) delegate).runWithEvent(this, event); 252 return; 253 } 254 } 255 256 delegate.run(this); 257 } 258 259 266 public void selectionChanged(ISelection newSelection) { 267 selection = newSelection; 269 if (selection == null) { 270 selection = StructuredSelection.EMPTY; 271 } 272 273 277 if (delegate == null && isOkToCreateDelegate()) { 280 createDelegate(); 281 } else { 282 refreshEnablement(); 283 } 284 } 285 286 292 public void selectionChanged(SelectionChangedEvent event) { 293 ISelection sel = event.getSelection(); 294 selectionChanged(sel); 295 } 296 297 303 public void selectionChanged(IWorkbenchPart part, ISelection sel) { 304 selectionChanged(sel); 305 } 306 307 313 public ISelection getSelection() { 314 return selection; 315 } 316 317 323 public String getOverrideActionId() { 324 return null; 325 } 326 327 332 protected IConfigurationElement getConfigElement() { 333 return configElement; 334 } 335 336 339 public String getLocalId() { 340 return getId(); 341 } 342 343 346 public String getPluginId() { 347 return pluginId; 348 } 349 350 355 public void disposeDelegate() { 356 if (getDelegate() instanceof IActionDelegate2) { 359 ((IActionDelegate2) getDelegate()).dispose(); 360 } 361 else if (getDelegate() instanceof IWorkbenchWindowActionDelegate) { 362 ((IWorkbenchWindowActionDelegate) getDelegate()).dispose(); 363 } 364 delegate = null; 365 } 366 367 372 public void dispose() { 373 disposeDelegate(); 374 } 375 } 376 | Popular Tags |