KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > HandlerPersistence


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.handlers;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.commands.IHandler;
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtensionDelta;
22 import org.eclipse.core.runtime.IExtensionRegistry;
23 import org.eclipse.core.runtime.IRegistryChangeEvent;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.handlers.IHandlerService;
27 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
28 import org.eclipse.ui.internal.services.IEvaluationService;
29 import org.eclipse.ui.internal.services.RegistryPersistence;
30
31 /**
32  * <p>
33  * A static class for accessing the registry.
34  * </p>
35  *
36  * @since 3.1
37  */

38 final class HandlerPersistence extends RegistryPersistence {
39
40     /**
41      * The index of the command elements in the indexed array.
42      *
43      * @see HandlerPersistence#read()
44      */

45     private static final int INDEX_COMMAND_DEFINITIONS = 0;
46
47     /**
48      * The index of the command elements in the indexed array.
49      *
50      * @see HandlerPersistence#read()
51      */

52     private static final int INDEX_HANDLER_DEFINITIONS = 1;
53
54     /**
55      * The index of the handler submissions in the indexed array.
56      *
57      * @see HandlerPersistence#read()
58      */

59     private static final int INDEX_HANDLER_SUBMISSIONS = 2;
60
61     /**
62      * The handler activations that have come from the registry. This is used to
63      * flush the activations when the registry is re-read. This value is never
64      * <code>null</code>
65      */

66     private final Collection JavaDoc handlerActivations = new ArrayList JavaDoc();
67
68     /**
69      * The handler service with which this persistence class is associated. This
70      * value must not be <code>null</code>.
71      */

72     private final IHandlerService handlerService;
73
74     private IEvaluationService evaluationService;
75
76     /**
77      * Constructs a new instance of <code>HandlerPersistence</code>.
78      *
79      * @param handlerService
80      * The handler service with which the handlers should be
81      * registered; must not be <code>null</code>.
82      * @param evaluationService
83      * The evaluation service used by handler proxies with enabled
84      * when expressions
85      */

86     HandlerPersistence(final IHandlerService handlerService,
87             IEvaluationService evaluationService) {
88         this.handlerService = handlerService;
89         this.evaluationService = evaluationService;
90     }
91
92     /**
93      * Deactivates all of the activations made by this class, and then clears
94      * the collection. This should be called before every read.
95      *
96      * @param handlerService
97      * The service handling the activations; must not be
98      * <code>null</code>.
99      */

100     private final void clearActivations(final IHandlerService handlerService) {
101         handlerService.deactivateHandlers(handlerActivations);
102         handlerActivations.clear();
103     }
104
105     public final void dispose() {
106         super.dispose();
107         clearActivations(handlerService);
108     }
109
110     protected final boolean isChangeImportant(final IRegistryChangeEvent event) {
111         /*
112          * Handlers will need to be re-read (i.e., re-verified) if any of the
113          * handler extensions change (i.e., handlers, commands), or if any of
114          * the command extensions change (i.e., action definitions).
115          */

116         final IExtensionDelta[] handlerDeltas = event.getExtensionDeltas(
117                 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_HANDLERS);
118         if (handlerDeltas.length == 0) {
119             final IExtensionDelta[] commandDeltas = event.getExtensionDeltas(
120                     PlatformUI.PLUGIN_ID,
121                     IWorkbenchRegistryConstants.PL_COMMANDS);
122             if (commandDeltas.length == 0) {
123                 final IExtensionDelta[] actionDefinitionDeltas = event
124                         .getExtensionDeltas(
125                                 PlatformUI.PLUGIN_ID,
126                                 IWorkbenchRegistryConstants.PL_ACTION_DEFINITIONS);
127                 if (actionDefinitionDeltas.length == 0) {
128                     return false;
129                 }
130             }
131         }
132
133         return true;
134     }
135
136     /**
137      * Reads all of the handlers from the registry
138      *
139      * @param handlerService
140      * The handler service which should be populated with the values
141      * from the registry; must not be <code>null</code>.
142      */

143     protected final void read() {
144         super.read();
145
146         // Create the extension registry mementos.
147
final IExtensionRegistry registry = Platform.getExtensionRegistry();
148         int commandDefinitionCount = 0;
149         int handlerDefinitionCount = 0;
150         int handlerSubmissionCount = 0;
151         final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[3][];
152
153         // Sort the commands extension point based on element name.
154
final IConfigurationElement[] commandsExtensionPoint = registry
155                 .getConfigurationElementsFor(EXTENSION_COMMANDS);
156         for (int i = 0; i < commandsExtensionPoint.length; i++) {
157             final IConfigurationElement configurationElement = commandsExtensionPoint[i];
158             final String JavaDoc name = configurationElement.getName();
159
160             // Check if it is a handler submission or a command definition.
161
if (TAG_HANDLER_SUBMISSION.equals(name)) {
162                 addElementToIndexedArray(configurationElement,
163                         indexedConfigurationElements,
164                         INDEX_HANDLER_SUBMISSIONS, handlerSubmissionCount++);
165             } else if (TAG_COMMAND.equals(name)) {
166                 addElementToIndexedArray(configurationElement,
167                         indexedConfigurationElements,
168                         INDEX_COMMAND_DEFINITIONS, commandDefinitionCount++);
169             }
170         }
171
172         // Sort the handler extension point based on element name.
173
final IConfigurationElement[] handlersExtensionPoint = registry
174                 .getConfigurationElementsFor(EXTENSION_HANDLERS);
175         for (int i = 0; i < handlersExtensionPoint.length; i++) {
176             final IConfigurationElement configurationElement = handlersExtensionPoint[i];
177             final String JavaDoc name = configurationElement.getName();
178
179             // Check if it is a handler submission or a command definition.
180
if (TAG_HANDLER.equals(name)) {
181                 addElementToIndexedArray(configurationElement,
182                         indexedConfigurationElements,
183                         INDEX_HANDLER_DEFINITIONS, handlerDefinitionCount++);
184             }
185         }
186
187         clearActivations(handlerService);
188         readDefaultHandlersFromRegistry(
189                 indexedConfigurationElements[INDEX_COMMAND_DEFINITIONS],
190                 commandDefinitionCount);
191         readHandlerSubmissionsFromRegistry(
192                 indexedConfigurationElements[INDEX_HANDLER_SUBMISSIONS],
193                 handlerSubmissionCount);
194         readHandlersFromRegistry(
195                 indexedConfigurationElements[INDEX_HANDLER_DEFINITIONS],
196                 handlerDefinitionCount);
197     }
198
199     /**
200      * Reads the default handlers from an array of command elements from the
201      * commands extension point.
202      *
203      * @param configurationElements
204      * The configuration elements in the commands extension point;
205      * must not be <code>null</code>, but may be empty.
206      * @param configurationElementCount
207      * The number of configuration elements that are really in the
208      * array.
209      */

210     private final void readDefaultHandlersFromRegistry(
211             final IConfigurationElement[] configurationElements,
212             final int configurationElementCount) {
213         for (int i = 0; i < configurationElementCount; i++) {
214             final IConfigurationElement configurationElement = configurationElements[i];
215
216             /*
217              * Read out the command identifier. This was already checked by
218              * <code>CommandPersistence</code>, so we'll just ignore any
219              * problems here.
220              */

221             final String JavaDoc commandId = readOptional(configurationElement, ATT_ID);
222             if (commandId == null) {
223                 continue;
224             }
225
226             // Check to see if we have a default handler of any kind.
227
if ((configurationElement.getAttribute(ATT_DEFAULT_HANDLER) == null)
228                     && (configurationElement.getChildren(TAG_DEFAULT_HANDLER).length == 0)) {
229                 continue;
230             }
231
232             handlerActivations.add(handlerService
233                     .activateHandler(commandId, new HandlerProxy(
234                             configurationElement, ATT_DEFAULT_HANDLER)));
235         }
236     }
237
238     /**
239      * Reads all of the handlers from the handlers extension point.
240      *
241      * @param configurationElements
242      * The configuration elements in the commands extension point;
243      * must not be <code>null</code>, but may be empty.
244      * @param configurationElementCount
245      * The number of configuration elements that are really in the
246      * array.
247      * @param handlerService
248      * The handler service to which the handlers should be added;
249      * must not be <code>null</code>.
250      */

251     private final void readHandlersFromRegistry(
252             final IConfigurationElement[] configurationElements,
253             final int configurationElementCount) {
254         final List JavaDoc warningsToLog = new ArrayList JavaDoc(1);
255
256         for (int i = 0; i < configurationElementCount; i++) {
257             final IConfigurationElement configurationElement = configurationElements[i];
258
259             // Read out the command identifier.
260
final String JavaDoc commandId = readRequired(configurationElement,
261                     ATT_COMMAND_ID, warningsToLog, "Handlers need a command id"); //$NON-NLS-1$
262
if (commandId == null) {
263                 continue;
264             }
265
266             // Check to see if we have a handler class.
267
if (!checkClass(configurationElement, warningsToLog,
268                     "Handlers need a class", commandId)) { //$NON-NLS-1$
269
continue;
270             }
271
272             // Get the activeWhen and enabledWhen expressions.
273
final Expression activeWhenExpression = readWhenElement(
274                     configurationElement, TAG_ACTIVE_WHEN, commandId,
275                     warningsToLog);
276             if (activeWhenExpression == ERROR_EXPRESSION) {
277                 continue;
278             }
279             final Expression enabledWhenExpression = readWhenElement(
280                     configurationElement, TAG_ENABLED_WHEN, commandId,
281                     warningsToLog);
282             if (enabledWhenExpression == ERROR_EXPRESSION) {
283                 continue;
284             }
285
286             final IHandler proxy = new HandlerProxy(configurationElement,
287                     ATT_CLASS, enabledWhenExpression, evaluationService);
288             handlerActivations.add(handlerService.activateHandler(commandId,
289                     proxy, activeWhenExpression));
290
291             // Read out the help context identifier.
292
final String JavaDoc helpContextId = readOptional(configurationElement,
293                     ATT_HELP_CONTEXT_ID);
294             handlerService.setHelpContextId(proxy, helpContextId);
295         }
296
297         logWarnings(
298                 warningsToLog,
299                 "Warnings while parsing the handlers from the 'org.eclipse.ui.handlers' extension point."); //$NON-NLS-1$
300
}
301
302     /**
303      * Reads all of the handler submissions from the commands extension point.
304      *
305      * @param configurationElements
306      * The configuration elements in the commands extension point;
307      * must not be <code>null</code>, but may be empty.
308      * @param configurationElementCount
309      * The number of configuration elements that are really in the
310      * array.
311      * @param handlerService
312      * The handler service to which the handlers should be added;
313      * must not be <code>null</code>.
314      */

315     private final void readHandlerSubmissionsFromRegistry(
316             final IConfigurationElement[] configurationElements,
317             final int configurationElementCount) {
318         final List JavaDoc warningsToLog = new ArrayList JavaDoc(1);
319
320         for (int i = 0; i < configurationElementCount; i++) {
321             final IConfigurationElement configurationElement = configurationElements[i];
322
323             // Read out the command identifier.
324
final String JavaDoc commandId = readRequired(configurationElement,
325                     ATT_COMMAND_ID, warningsToLog,
326                     "Handler submissions need a command id"); //$NON-NLS-1$
327
if (commandId == null) {
328                 continue;
329             }
330
331             handlerActivations.add(handlerService.activateHandler(commandId,
332                     new LegacyHandlerWrapper(new LegacyHandlerProxy(
333                             configurationElement))));
334         }
335
336         logWarnings(
337                 warningsToLog,
338                 "Warnings while parsing the handler submissions from the 'org.eclipse.ui.commands' extension point."); //$NON-NLS-1$
339
}
340 }
341
Popular Tags