001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.commands;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashSet;
015: import java.util.Map;
016: import java.util.Set;
017:
018: import org.eclipse.core.commands.Category;
019: import org.eclipse.core.commands.Command;
020: import org.eclipse.core.commands.IExecutionListener;
021: import org.eclipse.core.commands.IHandler;
022: import org.eclipse.core.commands.ParameterType;
023: import org.eclipse.core.commands.ParameterizedCommand;
024: import org.eclipse.core.commands.SerializationException;
025: import org.eclipse.core.commands.common.NotDefinedException;
026: import org.eclipse.ui.commands.IElementReference;
027: import org.eclipse.ui.commands.ICommandService;
028: import org.eclipse.ui.menus.UIElement;
029:
030: /**
031: * A command service which delegates almost all responsibility to the parent
032: * service.
033: * <p>
034: * This class is not intended for use outside of the
035: * <code>org.eclipse.ui.workbench</code> plug-in.
036: * </p>
037: *
038: * @since 3.2
039: */
040: public class SlaveCommandService implements ICommandService {
041:
042: private Collection fExecutionListeners = new ArrayList();
043:
044: /**
045: * The collection of ICallbackReferences added through this service.
046: *
047: * @since 3.3
048: */
049: private Set fCallbackCache = new HashSet();
050:
051: private ICommandService fParentService;
052:
053: /**
054: * The scoping constant added to callback registrations submitted through
055: * this service.
056: *
057: * @since 3.3
058: */
059: private String fScopingName;
060:
061: /**
062: * The object to scope. In theory, the service locator that would find this
063: * service.
064: *
065: * @since 3.3
066: */
067: private Object fScopingValue;
068:
069: /**
070: * Build the slave service.
071: *
072: * @param parent
073: * the parent service. This must not be <code>null</code>.
074: */
075: public SlaveCommandService(ICommandService parent,
076: String scopeName, Object scopeValue) {
077: if (parent == null) {
078: throw new NullPointerException(
079: "The parent command service must not be null"); //$NON-NLS-1$
080: }
081: fParentService = parent;
082: fScopingName = scopeName;
083: fScopingValue = scopeValue;
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see org.eclipse.ui.commands.ICommandService#addExecutionListener(org.eclipse.core.commands.IExecutionListener)
090: */
091: public void addExecutionListener(IExecutionListener listener) {
092: if (!fExecutionListeners.contains(listener)) {
093: fExecutionListeners.add(listener);
094: }
095: fParentService.addExecutionListener(listener);
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.eclipse.ui.commands.ICommandService#defineUncategorizedCategory(java.lang.String,
102: * java.lang.String)
103: */
104: public void defineUncategorizedCategory(String name,
105: String description) {
106: fParentService.defineUncategorizedCategory(name, description);
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see org.eclipse.ui.commands.ICommandService#deserialize(java.lang.String)
113: */
114: public ParameterizedCommand deserialize(
115: String serializedParameterizedCommand)
116: throws NotDefinedException, SerializationException {
117: return fParentService
118: .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[] 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[] 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 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 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 getDefinedCategoryIds() {
175: return fParentService.getDefinedCategoryIds();
176: }
177:
178: /*
179: * (non-Javadoc)
180: *
181: * @see org.eclipse.ui.commands.ICommandService#getDefinedCommandIds()
182: */
183: public Collection 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 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 getHelpContextId(final Command command)
215: throws NotDefinedException {
216: return fParentService.getHelpContextId(command);
217: }
218:
219: public final String getHelpContextId(final String 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 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 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 commandId, Map 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(
283: "No callback defined for command " //$NON-NLS-1$
284: + command.getCommand().getId());
285: }
286:
287: ElementReference ref = new ElementReference(command.getId(),
288: element, command.getParameterMap());
289: registerElement(ref);
290: return ref;
291: }
292:
293: /*
294: * (non-Javadoc)
295: *
296: * @see org.eclipse.ui.commands.ICommandService#registerElement(org.eclipse.ui.commands.IElementReference)
297: */
298: public void registerElement(IElementReference elementReference) {
299: fCallbackCache.add(elementReference);
300: elementReference.getParameters().put(fScopingName,
301: fScopingValue);
302: fParentService.registerElement(elementReference);
303: }
304:
305: /*
306: * (non-Javadoc)
307: *
308: * @see org.eclipse.ui.commands.ICommandService#unregisterElement(org.eclipse.ui.commands.IElementReference)
309: */
310: public void unregisterElement(IElementReference elementReference) {
311: fCallbackCache.remove(elementReference);
312: fParentService.unregisterElement(elementReference);
313: }
314: }
|