KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
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.IRegistryChangeListener;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.commands.IHandler;
27 import org.eclipse.ui.internal.util.ConfigurationElementMemento;
28
29 public final class ExtensionCommandRegistry extends AbstractCommandRegistry {
30
31     private List JavaDoc activeKeyConfigurationDefinitions;
32     private List JavaDoc categoryDefinitions;
33     private List JavaDoc commandDefinitions;
34     private IExtensionRegistry extensionRegistry;
35     /**
36      * The valid handlers read from XML. This list is <code>null</code> until
37      * the first call to <code>load</code>. After this, it will contain a list
38      * of all the handlers read during the most recent call to
39      * <code>load</code>.
40      */

41     private List JavaDoc handlers;
42     private List JavaDoc keyConfigurationDefinitions;
43     private List JavaDoc keySequenceBindingDefinitions;
44
45     public ExtensionCommandRegistry(IExtensionRegistry extensionRegistry) {
46         if (extensionRegistry == null)
47             throw new NullPointerException JavaDoc();
48
49         this.extensionRegistry = extensionRegistry;
50
51         this
52             .extensionRegistry
53             .addRegistryChangeListener(new IRegistryChangeListener() {
54             public void registryChanged(IRegistryChangeEvent registryChangeEvent) {
55                 IExtensionDelta[] extensionDeltas =
56                     registryChangeEvent.getExtensionDeltas(
57                         Persistence.PACKAGE_PREFIX,
58                         Persistence.PACKAGE_BASE);
59
60                 if (extensionDeltas.length != 0)
61                     try {
62                         load();
63                     } catch (IOException JavaDoc eIO) {
64                         // Do nothing
65
}
66             }
67         });
68
69         try {
70             load();
71         } catch (IOException JavaDoc eIO) {
72             // Do nothing
73
}
74     }
75
76     private String JavaDoc getNamespace(IConfigurationElement configurationElement) {
77         String JavaDoc namespace = null;
78
79         if (configurationElement != null) {
80             IExtension extension = configurationElement.getDeclaringExtension();
81
82             if (extension != null)
83                 namespace = extension.getNamespace();
84         }
85
86         return namespace;
87     }
88
89     private void load() throws IOException JavaDoc {
90         if (activeKeyConfigurationDefinitions == null)
91             activeKeyConfigurationDefinitions = new ArrayList JavaDoc();
92         else
93             activeKeyConfigurationDefinitions.clear();
94
95         if (categoryDefinitions == null)
96             categoryDefinitions = new ArrayList JavaDoc();
97         else
98             categoryDefinitions.clear();
99
100         if (commandDefinitions == null)
101             commandDefinitions = new ArrayList JavaDoc();
102         else
103             commandDefinitions.clear();
104         
105         if (handlers == null) {
106             handlers = new ArrayList JavaDoc();
107         } else {
108             handlers.clear();
109         }
110
111         if (imageBindingDefinitions == null)
112             imageBindingDefinitions = new ArrayList JavaDoc();
113         else
114             imageBindingDefinitions.clear();
115
116         if (keyConfigurationDefinitions == null)
117             keyConfigurationDefinitions = new ArrayList JavaDoc();
118         else
119             keyConfigurationDefinitions.clear();
120
121         if (keySequenceBindingDefinitions == null)
122             keySequenceBindingDefinitions = new ArrayList JavaDoc();
123         else
124             keySequenceBindingDefinitions.clear();
125
126         // TODO deprecated start
127
IConfigurationElement[] deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.acceleratorConfigurations"); //$NON-NLS-1$
128

129         for (int i = 0; i < deprecatedConfigurationElements.length; i++) {
130             IConfigurationElement deprecatedConfigurationElement =
131                 deprecatedConfigurationElements[i];
132             String JavaDoc name = deprecatedConfigurationElement.getName();
133
134             if ("acceleratorConfiguration".equals(name)) //$NON-NLS-1$
135
readKeyConfigurationDefinition(deprecatedConfigurationElement);
136         }
137
138         deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.acceleratorSets"); //$NON-NLS-1$
139

140         for (int i = 0; i < deprecatedConfigurationElements.length; i++) {
141             IConfigurationElement deprecatedConfigurationElement =
142                 deprecatedConfigurationElements[i];
143             String JavaDoc name = deprecatedConfigurationElement.getName();
144
145             if ("acceleratorSet".equals(name)) { //$NON-NLS-1$
146
IMemento memento =
147                     new ConfigurationElementMemento(deprecatedConfigurationElement);
148                 String JavaDoc keyConfigurationId = memento.getString("configurationId"); //$NON-NLS-1$
149
String JavaDoc scopeId = memento.getString("scopeId"); //$NON-NLS-1$
150
IConfigurationElement[] deprecatedConfigurationElements2 = deprecatedConfigurationElement.getChildren("accelerator"); //$NON-NLS-1$
151

152                 for (int j = 0;
153                     j < deprecatedConfigurationElements2.length;
154                     j++) {
155                     IConfigurationElement deprecatedConfigurationElement2 =
156                         deprecatedConfigurationElements2[j];
157                     KeySequenceBindingDefinition keySequenceBindingDefinition =
158                         Persistence.readKeySequenceBindingDefinition(
159                             new ConfigurationElementMemento(deprecatedConfigurationElement2),
160                             getNamespace(deprecatedConfigurationElement2));
161
162                     if (keySequenceBindingDefinition != null) {
163                         keySequenceBindingDefinition =
164                             new KeySequenceBindingDefinition(
165                                 scopeId,
166                                 keySequenceBindingDefinition.getCommandId(),
167                                 keyConfigurationId,
168                                 keySequenceBindingDefinition.getKeySequence(),
169                                 keySequenceBindingDefinition.getLocale(),
170                                 keySequenceBindingDefinition.getPlatform(),
171                                 keySequenceBindingDefinition.getSourceId());
172
173                         keySequenceBindingDefinitions.add(
174                             keySequenceBindingDefinition);
175                     }
176                 }
177             }
178         }
179
180         deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.actionDefinitions"); //$NON-NLS-1$
181

182         for (int i = 0; i < deprecatedConfigurationElements.length; i++) {
183             IConfigurationElement deprecatedConfigurationElement =
184                 deprecatedConfigurationElements[i];
185             String JavaDoc name = deprecatedConfigurationElement.getName();
186
187             if ("actionDefinition".equals(name)) //$NON-NLS-1$
188
readCommandDefinition(deprecatedConfigurationElement);
189         }
190         // TODO deprecated end
191

192         IConfigurationElement[] configurationElements =
193             extensionRegistry.getConfigurationElementsFor(
194                 Persistence.PACKAGE_FULL);
195
196         for (int i = 0; i < configurationElements.length; i++) {
197             IConfigurationElement configurationElement = configurationElements[i];
198             String JavaDoc name = configurationElement.getName();
199
200             if (Persistence.TAG_ACTIVE_KEY_CONFIGURATION.equals(name))
201                 readActiveKeyConfigurationDefinition(configurationElement);
202             else if (Persistence.TAG_CATEGORY.equals(name))
203                 readCategoryDefinition(configurationElement);
204             else if (Persistence.TAG_COMMAND.equals(name))
205                 readCommandDefinition(configurationElement);
206             else if (Persistence.TAG_HANDLER.equals(name)) {
207                 readHandlerSubmissionDefinition(configurationElement);
208             } else if (Persistence.TAG_KEY_CONFIGURATION.equals(name))
209                 readKeyConfigurationDefinition(configurationElement);
210             else if (Persistence.TAG_KEY_SEQUENCE_BINDING.equals(name))
211                     readKeySequenceBindingDefinition(configurationElement);
212         }
213
214         boolean commandRegistryChanged = false;
215
216         if (!activeKeyConfigurationDefinitions
217             .equals(super.activeKeyConfigurationDefinitions)) {
218             super.activeKeyConfigurationDefinitions =
219                 Collections.unmodifiableList(activeKeyConfigurationDefinitions);
220             commandRegistryChanged = true;
221         }
222
223         if (!categoryDefinitions.equals(super.categoryDefinitions)) {
224             super.categoryDefinitions =
225                 Collections.unmodifiableList(categoryDefinitions);
226             commandRegistryChanged = true;
227         }
228
229         if (!commandDefinitions.equals(super.commandDefinitions)) {
230             super.commandDefinitions =
231                 Collections.unmodifiableList(commandDefinitions);
232             commandRegistryChanged = true;
233         }
234
235         if (!handlers.equals(super.handlers)) {
236             super.handlers = Collections.unmodifiableList(handlers);
237             commandRegistryChanged = true;
238         }
239
240         if (!imageBindingDefinitions.equals(super.imageBindingDefinitions)) {
241             super.imageBindingDefinitions =
242                 Collections.unmodifiableList(imageBindingDefinitions);
243             commandRegistryChanged = true;
244         }
245
246         if (!keyConfigurationDefinitions
247             .equals(super.keyConfigurationDefinitions)) {
248             super.keyConfigurationDefinitions =
249                 Collections.unmodifiableList(keyConfigurationDefinitions);
250             commandRegistryChanged = true;
251         }
252
253         if (!keySequenceBindingDefinitions
254             .equals(super.keySequenceBindingDefinitions)) {
255             super.keySequenceBindingDefinitions =
256                 Collections.unmodifiableList(keySequenceBindingDefinitions);
257             commandRegistryChanged = true;
258         }
259
260         if (commandRegistryChanged)
261             fireCommandRegistryChanged();
262     }
263
264     private void readActiveKeyConfigurationDefinition(IConfigurationElement configurationElement) {
265         ActiveKeyConfigurationDefinition activeKeyConfigurationDefinition =
266             Persistence.readActiveKeyConfigurationDefinition(
267                 new ConfigurationElementMemento(configurationElement),
268                 getNamespace(configurationElement));
269
270         if (activeKeyConfigurationDefinition != null)
271             activeKeyConfigurationDefinitions.add(
272                 activeKeyConfigurationDefinition);
273     }
274
275     private void readCategoryDefinition(IConfigurationElement configurationElement) {
276         CategoryDefinition categoryDefinition =
277             Persistence.readCategoryDefinition(
278                 new ConfigurationElementMemento(configurationElement),
279                 getNamespace(configurationElement));
280
281         if (categoryDefinition != null)
282             categoryDefinitions.add(categoryDefinition);
283     }
284
285     private void readCommandDefinition(IConfigurationElement configurationElement) {
286         CommandDefinition commandDefinition =
287             Persistence.readCommandDefinition(
288                 new ConfigurationElementMemento(configurationElement),
289                 getNamespace(configurationElement));
290
291         if (commandDefinition != null)
292             commandDefinitions.add(commandDefinition);
293     }
294
295     /**
296      * Reads the handler definition from XML -- creating a proxy to submit to
297      * the workbench command support. If the handler definition is valid, then
298      * it will be added to <code>handlers</code> to be picked up later.
299      *
300      * @param configurationElement The configuration element from which to read;
301      * must not be <code>null</code>.
302      */

303     private final void readHandlerSubmissionDefinition(final IConfigurationElement configurationElement) {
304         final IHandler handler =
305             Persistence.readHandlerSubmissionDefinition(configurationElement);
306
307         if (handler != null)
308             handlers.add(handler);
309     }
310
311     private void readKeyConfigurationDefinition(IConfigurationElement configurationElement) {
312         KeyConfigurationDefinition keyConfigurationDefinition =
313             Persistence.readKeyConfigurationDefinition(
314                 new ConfigurationElementMemento(configurationElement),
315                 getNamespace(configurationElement));
316
317         if (keyConfigurationDefinition != null)
318             keyConfigurationDefinitions.add(keyConfigurationDefinition);
319     }
320
321     private void readKeySequenceBindingDefinition(IConfigurationElement configurationElement) {
322         KeySequenceBindingDefinition keySequenceBindingDefinition =
323             Persistence.readKeySequenceBindingDefinition(
324                 new ConfigurationElementMemento(configurationElement),
325                 getNamespace(configurationElement));
326
327         if (keySequenceBindingDefinition != null)
328             keySequenceBindingDefinitions.add(keySequenceBindingDefinition);
329     }
330 }
331
Popular Tags