1 11 12 package org.eclipse.ui.actions; 13 14 import java.util.Collections ; 15 16 import org.eclipse.core.commands.ExecutionException; 17 import org.eclipse.core.commands.IHandler; 18 import org.eclipse.core.commands.NotEnabledException; 19 import org.eclipse.core.commands.NotHandledException; 20 import org.eclipse.core.commands.common.NotDefinedException; 21 import org.eclipse.core.expressions.EvaluationContext; 22 import org.eclipse.core.expressions.IEvaluationContext; 23 import org.eclipse.core.runtime.IConfigurationElement; 24 import org.eclipse.jface.viewers.StructuredSelection; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.ui.IEditorSite; 27 import org.eclipse.ui.IPartListener; 28 import org.eclipse.ui.ISources; 29 import org.eclipse.ui.IWorkbench; 30 import org.eclipse.ui.IWorkbenchPart; 31 import org.eclipse.ui.IWorkbenchPartSite; 32 import org.eclipse.ui.IWorkbenchWindow; 33 import org.eclipse.ui.handlers.IHandlerService; 34 import org.eclipse.ui.internal.actions.CommandAction; 35 import org.eclipse.ui.internal.handlers.ActionDelegateHandlerProxy; 36 import org.eclipse.ui.internal.handlers.HandlerService; 37 import org.eclipse.ui.internal.handlers.IActionCommandMappingService; 38 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 39 import org.eclipse.ui.part.MultiPageEditorSite; 40 import org.eclipse.ui.services.IServiceLocator; 41 42 54 public final class ContributedAction extends CommandAction { 55 private IEvaluationContext appContext; 56 57 private IHandler partHandler; 58 59 private boolean localHandler = false; 60 61 private IPartListener partListener; 62 63 72 public ContributedAction(IServiceLocator locator, 73 IConfigurationElement element) throws CommandNotMappedException { 74 75 String actionId = element 76 .getAttribute(IWorkbenchRegistryConstants.ATT_ID); 77 String commandId = element 78 .getAttribute(IWorkbenchRegistryConstants.ATT_DEFINITION_ID); 79 80 82 String contributionId = null; 83 if (commandId == null) { 84 85 Object obj = element.getParent(); 86 if (obj instanceof IConfigurationElement) { 87 contributionId = ((IConfigurationElement) obj) 88 .getAttribute(IWorkbenchRegistryConstants.ATT_ID); 89 if (contributionId == null) { 90 throw new CommandNotMappedException("Action " + actionId + " configuration element invalid"); } 93 } 94 IActionCommandMappingService mapping = (IActionCommandMappingService) locator 96 .getService(IActionCommandMappingService.class); 97 if (mapping == null) { 98 throw new CommandNotMappedException( 99 "No action mapping service available"); } 101 102 commandId = mapping.getCommandId(mapping.getGeneratedCommandId( 103 contributionId, actionId)); 104 } 105 if (commandId == null) { 107 throw new CommandNotMappedException("Action " + actionId + " in contribution " + contributionId + " not mapped to a command"); } 111 112 init(locator, commandId, null); 113 114 if (locator instanceof IWorkbenchPartSite) { 115 updateSiteAssociations((IWorkbenchPartSite) locator, commandId, 116 actionId, element); 117 } 118 119 setId(actionId); 120 } 121 122 private void updateSiteAssociations(IWorkbenchPartSite site, 123 String commandId, String actionId, IConfigurationElement element) { 124 IWorkbench workbench = (IWorkbench) site.getService(IWorkbench.class); 125 IWorkbenchWindow window = (IWorkbenchWindow) site 126 .getService(IWorkbenchWindow.class); 127 IHandlerService serv = (IHandlerService) workbench 128 .getService(IHandlerService.class); 129 appContext = new EvaluationContext(serv.getCurrentState(), 130 Collections.EMPTY_LIST); 131 132 appContext.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, 134 StructuredSelection.EMPTY); 135 appContext.addVariable(ISources.ACTIVE_PART_NAME, site.getPart()); 136 appContext.addVariable(ISources.ACTIVE_PART_ID_NAME, site.getId()); 137 appContext.addVariable(ISources.ACTIVE_SITE_NAME, site); 138 if (site instanceof IEditorSite) { 139 appContext.addVariable(ISources.ACTIVE_EDITOR_NAME, site.getPart()); 140 appContext 141 .addVariable(ISources.ACTIVE_EDITOR_ID_NAME, site.getId()); 142 } 143 appContext.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, window); 144 appContext.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME, 145 window.getShell()); 146 147 HandlerService realService = (HandlerService) serv; 148 partHandler = realService.findHandler(commandId, appContext); 149 if (partHandler == null) { 150 localHandler = true; 151 partHandler = new ActionDelegateHandlerProxy(element, 154 IWorkbenchRegistryConstants.ATT_CLASS, actionId, 155 getParameterizedCommand(), site.getWorkbenchWindow(), null, 156 null, null); 157 } 158 159 if (getParameterizedCommand() != null) { 160 getParameterizedCommand().getCommand().removeCommandListener( 161 getCommandListener()); 162 } 163 site.getPage().addPartListener(getPartListener()); 164 } 165 166 171 public void runWithEvent(Event event) { 172 if (partHandler != null) { 173 IHandler oldHandler = getParameterizedCommand().getCommand() 174 .getHandler(); 175 try { 176 getParameterizedCommand().getCommand().setHandler(partHandler); 177 getParameterizedCommand().executeWithChecks(event, appContext); 178 } catch (ExecutionException e) { 179 } catch (NotDefinedException e) { 181 } catch (NotEnabledException e) { 183 } catch (NotHandledException e) { 185 } finally { 187 getParameterizedCommand().getCommand().setHandler(oldHandler); 188 } 189 } else { 190 super.runWithEvent(event); 191 } 192 } 193 194 199 public boolean isEnabled() { 200 if (partHandler != null) { 201 if (partHandler instanceof ActionDelegateHandlerProxy) { 202 return ((ActionDelegateHandlerProxy) partHandler) 203 .isEnabled(appContext); 204 } 205 return partHandler.isEnabled(); 206 } 207 return false; 208 } 209 210 private IPartListener getPartListener() { 211 if (partListener == null) { 212 final IWorkbenchPartSite site = (IWorkbenchPartSite) appContext 213 .getVariable(ISources.ACTIVE_SITE_NAME); 214 215 final IWorkbenchPart currentPart; 216 if (site instanceof MultiPageEditorSite) { 217 currentPart = ((MultiPageEditorSite) site).getMultiPageEditor(); 218 } else { 219 currentPart = site.getPart(); 220 } 221 222 partListener = new IPartListener() { 223 public void partActivated(IWorkbenchPart part) { 224 } 225 226 public void partBroughtToTop(IWorkbenchPart part) { 227 } 228 229 public void partClosed(IWorkbenchPart part) { 230 if (part == currentPart) { 231 ContributedAction.this.disposeAction(); 232 } 233 } 234 235 public void partDeactivated(IWorkbenchPart part) { 236 } 237 238 public void partOpened(IWorkbenchPart part) { 239 } 240 }; 241 } 242 return partListener; 243 } 244 245 private void disposeAction() { 247 if (appContext != null) { 248 if (localHandler) { 249 partHandler.dispose(); 250 } 251 if (partListener != null) { 252 IWorkbenchPartSite site = (IWorkbenchPartSite) appContext 253 .getVariable(ISources.ACTIVE_SITE_NAME); 254 site.getPage().removePartListener(partListener); 255 partListener = null; 256 } 257 appContext = null; 258 partHandler = null; 259 } 260 dispose(); 261 } 262 } 263 | Popular Tags |