KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > SlaveCommandService


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.internal.commands;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.commands.Category;
21 import org.eclipse.core.commands.Command;
22 import org.eclipse.core.commands.IExecutionListener;
23 import org.eclipse.core.commands.IHandler;
24 import org.eclipse.core.commands.ParameterType;
25 import org.eclipse.core.commands.ParameterizedCommand;
26 import org.eclipse.core.commands.SerializationException;
27 import org.eclipse.core.commands.common.NotDefinedException;
28 import org.eclipse.ui.commands.IElementReference;
29 import org.eclipse.ui.commands.ICommandService;
30 import org.eclipse.ui.menus.UIElement;
31
32 /**
33  * A command service which delegates almost all responsibility to the parent
34  * service.
35  * <p>
36  * This class is not intended for use outside of the
37  * <code>org.eclipse.ui.workbench</code> plug-in.
38  * </p>
39  *
40  * @since 3.2
41  */

42 public class SlaveCommandService implements ICommandService {
43
44     private Collection JavaDoc fExecutionListeners = new ArrayList JavaDoc();
45
46     /**
47      * The collection of ICallbackReferences added through this service.
48      *
49      * @since 3.3
50      */

51     private Set JavaDoc fCallbackCache = new HashSet JavaDoc();
52
53     private ICommandService fParentService;
54
55     /**
56      * The scoping constant added to callback registrations submitted through
57      * this service.
58      *
59      * @since 3.3
60      */

61     private String JavaDoc fScopingName;
62
63     /**
64      * The object to scope. In theory, the service locator that would find this
65      * service.
66      *
67      * @since 3.3
68      */

69     private Object JavaDoc fScopingValue;
70
71     /**
72      * Build the slave service.
73      *
74      * @param parent
75      * the parent service. This must not be <code>null</code>.
76      */

77     public SlaveCommandService(ICommandService parent, String JavaDoc scopeName,
78             Object JavaDoc scopeValue) {
79         if (parent == null) {
80             throw new NullPointerException JavaDoc(
81                     "The parent command service must not be null"); //$NON-NLS-1$
82
}
83         fParentService = parent;
84         fScopingName = scopeName;
85         fScopingValue = scopeValue;
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.eclipse.ui.commands.ICommandService#addExecutionListener(org.eclipse.core.commands.IExecutionListener)
92      */

93     public void addExecutionListener(IExecutionListener listener) {
94         if (!fExecutionListeners.contains(listener)) {
95             fExecutionListeners.add(listener);
96         }
97         fParentService.addExecutionListener(listener);
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.eclipse.ui.commands.ICommandService#defineUncategorizedCategory(java.lang.String,
104      * java.lang.String)
105      */

106     public void defineUncategorizedCategory(String JavaDoc name, String JavaDoc description) {
107         fParentService.defineUncategorizedCategory(name, description);
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.eclipse.ui.commands.ICommandService#deserialize(java.lang.String)
114      */

115     public ParameterizedCommand deserialize(
116             String JavaDoc serializedParameterizedCommand) throws NotDefinedException,
117             SerializationException {
118         return fParentService.deserialize(serializedParameterizedCommand);
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.eclipse.ui.services.IDisposable#dispose()
125      */

126     public void dispose() {
127         if (!fExecutionListeners.isEmpty()) {
128             Object JavaDoc[] array = fExecutionListeners.toArray();
129             for (int i = 0; i < array.length; i++) {
130                 removeExecutionListener((IExecutionListener) array[i]);
131             }
132             fExecutionListeners.clear();
133         }
134         if (!fCallbackCache.isEmpty()) {
135             Object JavaDoc[] array = fCallbackCache.toArray();
136             for (int i = 0; i < array.length; i++) {
137                 unregisterElement((IElementReference) array[i]);
138             }
139         }
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.eclipse.ui.commands.ICommandService#getCategory(java.lang.String)
146      */

147     public Category getCategory(String JavaDoc categoryId) {
148         return fParentService.getCategory(categoryId);
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.eclipse.ui.commands.ICommandService#getCommand(java.lang.String)
155      */

156     public Command getCommand(String JavaDoc commandId) {
157         return fParentService.getCommand(commandId);
158     }
159
160     /*
161      * (non-Javadoc)
162      *
163      * @see org.eclipse.ui.commands.ICommandService#getDefinedCategories()
164      */

165     public Category[] getDefinedCategories() {
166         return fParentService.getDefinedCategories();
167     }
168
169     /*
170      * (non-Javadoc)
171      *
172      * @see org.eclipse.ui.commands.ICommandService#getDefinedCategoryIds()
173      */

174     public Collection JavaDoc getDefinedCategoryIds() {
175         return fParentService.getDefinedCategoryIds();
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.ui.commands.ICommandService#getDefinedCommandIds()
182      */

183     public Collection JavaDoc getDefinedCommandIds() {
184         return fParentService.getDefinedCommandIds();
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see org.eclipse.ui.commands.ICommandService#getDefinedCommands()
191      */

192     public Command[] getDefinedCommands() {
193         return fParentService.getDefinedCommands();
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.eclipse.ui.commands.ICommandService#getDefinedParameterTypeIds()
200      */

201     public Collection JavaDoc getDefinedParameterTypeIds() {
202         return fParentService.getDefinedParameterTypeIds();
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.ui.commands.ICommandService#getDefinedParameterTypes()
209      */

210     public ParameterType[] getDefinedParameterTypes() {
211         return fParentService.getDefinedParameterTypes();
212     }
213
214     public final String JavaDoc getHelpContextId(final Command command)
215             throws NotDefinedException {
216         return fParentService.getHelpContextId(command);
217     }
218
219     public final String JavaDoc getHelpContextId(final String JavaDoc commandId)
220             throws NotDefinedException {
221         return fParentService.getHelpContextId(commandId);
222     }
223
224     /*
225      * (non-Javadoc)
226      *
227      * @see org.eclipse.ui.commands.ICommandService#getParameterType(java.lang.String)
228      */

229     public ParameterType getParameterType(String JavaDoc parameterTypeId) {
230         return fParentService.getParameterType(parameterTypeId);
231     }
232
233     /*
234      * (non-Javadoc)
235      *
236      * @see org.eclipse.ui.commands.ICommandService#readRegistry()
237      */

238     public void readRegistry() {
239         fParentService.readRegistry();
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see org.eclipse.ui.commands.ICommandService#removeExecutionListener(org.eclipse.core.commands.IExecutionListener)
246      */

247     public void removeExecutionListener(IExecutionListener listener) {
248         fExecutionListeners.remove(listener);
249         fParentService.removeExecutionListener(listener);
250     }
251
252     public final void setHelpContextId(final IHandler handler,
253             final String JavaDoc helpContextId) {
254         fParentService.setHelpContextId(handler, helpContextId);
255     }
256
257     /*
258      * (non-Javadoc)
259      *
260      * @see org.eclipse.ui.commands.ICommandService#refreshElements(java.lang.String,
261      * java.util.Map)
262      */

263     public void refreshElements(String JavaDoc commandId, Map JavaDoc filter) {
264         fParentService.refreshElements(commandId, filter);
265     }
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see org.eclipse.ui.commands.ICommandService#registerElementForCommand(org.eclipse.core.commands.ParameterizedCommand,
271      * org.eclipse.ui.menus.UIElement)
272      */

273     public IElementReference registerElementForCommand(
274             ParameterizedCommand command, UIElement element)
275             throws NotDefinedException {
276         if (!command.getCommand().isDefined()) {
277             throw new NotDefinedException(
278                     "Cannot define a callback for undefined command " //$NON-NLS-1$
279
+ command.getCommand().getId());
280         }
281         if (element == null) {
282             throw new NotDefinedException("No callback defined for command " //$NON-NLS-1$
283
+ command.getCommand().getId());
284         }
285
286         ElementReference ref = new ElementReference(command.getId(), element,
287                 command.getParameterMap());
288         registerElement(ref);
289         return ref;
290     }
291
292     /*
293      * (non-Javadoc)
294      *
295      * @see org.eclipse.ui.commands.ICommandService#registerElement(org.eclipse.ui.commands.IElementReference)
296      */

297     public void registerElement(IElementReference elementReference) {
298         fCallbackCache.add(elementReference);
299         elementReference.getParameters().put(fScopingName, fScopingValue);
300         fParentService.registerElement(elementReference);
301     }
302
303     /*
304      * (non-Javadoc)
305      *
306      * @see org.eclipse.ui.commands.ICommandService#unregisterElement(org.eclipse.ui.commands.IElementReference)
307      */

308     public void unregisterElement(IElementReference elementReference) {
309         fCallbackCache.remove(elementReference);
310         fParentService.unregisterElement(elementReference);
311     }
312 }
313
Popular Tags