KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > ContributedAction


1 /*******************************************************************************
2  * Copyright (c) 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 v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.actions;
13
14 import java.util.Collections JavaDoc;
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 /**
43  * For a declarative editor action, see if we can link it to a command.
44  * <p>
45  * This is a legacy bridge class, and should not be used outside of the Eclipse
46  * SDK. Please use menu contributions to display a command in a menu or toolbar.
47  * </p>
48  * <p>
49  * <b>Note:</b> Clients may instantiate.
50  * </p>
51  *
52  * @since 3.3
53  */

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     /**
64      * Create an action that can call a command.
65      *
66      * @param locator
67      * The appropriate service locator to use. If you use a part site
68      * as your locator, this action will be tied to your part.
69      * @param element
70      * the contributed action element
71      */

72     public ContributedAction(IServiceLocator locator,
73             IConfigurationElement element) throws CommandNotMappedException {
74
75         String JavaDoc actionId = element
76                 .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
77         String JavaDoc commandId = element
78                 .getAttribute(IWorkbenchRegistryConstants.ATT_DEFINITION_ID);
79
80         // TODO throw some more exceptions here :-)
81

82         String JavaDoc contributionId = null;
83         if (commandId == null) {
84
85             Object JavaDoc 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 " //$NON-NLS-1$
91
+ actionId + " configuration element invalid"); //$NON-NLS-1$
92
}
93             }
94             // legacy bridge part
95
IActionCommandMappingService mapping = (IActionCommandMappingService) locator
96                     .getService(IActionCommandMappingService.class);
97             if (mapping == null) {
98                 throw new CommandNotMappedException(
99                         "No action mapping service available"); //$NON-NLS-1$
100
}
101
102             commandId = mapping.getCommandId(mapping.getGeneratedCommandId(
103                     contributionId, actionId));
104         }
105         // what, still no command?
106
if (commandId == null) {
107             throw new CommandNotMappedException("Action " + actionId //$NON-NLS-1$
108
+ " in contribution " + contributionId //$NON-NLS-1$
109
+ " not mapped to a command"); //$NON-NLS-1$
110
}
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 JavaDoc commandId, String JavaDoc 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         // set up the appContext as we would want it.
133
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             // if we can't find the handler, then at least we can
152
// call the action delegate run method
153
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     /*
167      * (non-Javadoc)
168      *
169      * @see org.eclipse.ui.internal.actions.CommandAction#runWithEvent(org.eclipse.swt.widgets.Event)
170      */

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                 // TODO some logging, perhaps?
180
} catch (NotDefinedException e) {
181                 // TODO some logging, perhaps?
182
} catch (NotEnabledException e) {
183                 // TODO some logging, perhaps?
184
} catch (NotHandledException e) {
185                 // TODO some logging, perhaps?
186
} finally {
187                 getParameterizedCommand().getCommand().setHandler(oldHandler);
188             }
189         } else {
190             super.runWithEvent(event);
191         }
192     }
193
194     /*
195      * (non-Javadoc)
196      *
197      * @see org.eclipse.jface.action.Action#isEnabled()
198      */

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     // TODO make public in 3.4
246
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