KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > ExtensionContextRegistry


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.contexts;
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.internal.util.ConfigurationElementMemento;
26
27 final class ExtensionContextRegistry extends AbstractContextRegistry {
28     private List JavaDoc contextContextBindingDefinitions;
29     private List JavaDoc contextDefinitions;
30     private IExtensionRegistry extensionRegistry;
31
32     ExtensionContextRegistry(IExtensionRegistry extensionRegistry) {
33         if (extensionRegistry == null)
34             throw new NullPointerException JavaDoc();
35
36         this.extensionRegistry = extensionRegistry;
37
38         this
39             .extensionRegistry
40             .addRegistryChangeListener(new IRegistryChangeListener() {
41             public void registryChanged(IRegistryChangeEvent registryChangeEvent) {
42                 IExtensionDelta[] extensionDeltas =
43                     registryChangeEvent.getExtensionDeltas(
44                         Persistence.PACKAGE_PREFIX,
45                         Persistence.PACKAGE_BASE);
46
47                 if (extensionDeltas.length != 0)
48                     try {
49                         load();
50                     } catch (IOException JavaDoc eIO) {
51                     }
52             }
53         });
54
55         try {
56             load();
57         } catch (IOException JavaDoc eIO) {
58         }
59     }
60
61     private String JavaDoc getNamespace(IConfigurationElement configurationElement) {
62         String JavaDoc namespace = null;
63
64         if (configurationElement != null) {
65             IExtension extension = configurationElement.getDeclaringExtension();
66
67             if (extension != null)
68                 namespace = extension.getNamespace();
69         }
70
71         return namespace;
72     }
73
74     private void load() throws IOException JavaDoc {
75         if (contextContextBindingDefinitions == null)
76             contextContextBindingDefinitions = new ArrayList JavaDoc();
77         else
78             contextContextBindingDefinitions.clear();
79
80         if (contextDefinitions == null)
81             contextDefinitions = new ArrayList JavaDoc();
82         else
83             contextDefinitions.clear();
84
85         // TODO deprecated start
86
IConfigurationElement[] deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.acceleratorScopes"); //$NON-NLS-1$
87

88         for (int i = 0; i < deprecatedConfigurationElements.length; i++) {
89             IConfigurationElement deprecatedConfigurationElement =
90                 deprecatedConfigurationElements[i];
91             String JavaDoc name = deprecatedConfigurationElement.getName();
92
93             if ("acceleratorScope".equals(name)) //$NON-NLS-1$
94
readContextDefinition(deprecatedConfigurationElement);
95         }
96         
97         deprecatedConfigurationElements = extensionRegistry.getConfigurationElementsFor("org.eclipse.ui.commands"); //$NON-NLS-1$
98

99         for (int i = 0; i < deprecatedConfigurationElements.length; i++) {
100             IConfigurationElement deprecatedConfigurationElement =
101                 deprecatedConfigurationElements[i];
102             String JavaDoc name = deprecatedConfigurationElement.getName();
103
104             if ("scope".equals(name)) //$NON-NLS-1$
105
readContextDefinition(deprecatedConfigurationElement);
106         }
107         // TODO deprecated end
108

109         IConfigurationElement[] configurationElements =
110             extensionRegistry.getConfigurationElementsFor(
111                 Persistence.PACKAGE_FULL);
112
113         for (int i = 0; i < configurationElements.length; i++) {
114             IConfigurationElement configurationElement =
115                 configurationElements[i];
116             String JavaDoc name = configurationElement.getName();
117
118             if (Persistence.TAG_CONTEXT.equals(name))
119                 readContextDefinition(configurationElement);
120         }
121
122         boolean contextRegistryChanged = false;
123
124         if (!contextContextBindingDefinitions
125             .equals(super.contextContextBindingDefinitions)) {
126             super.contextContextBindingDefinitions =
127                 Collections.unmodifiableList(contextContextBindingDefinitions);
128             contextRegistryChanged = true;
129         }
130
131         if (!contextDefinitions.equals(super.contextDefinitions)) {
132             super.contextDefinitions =
133                 Collections.unmodifiableList(contextDefinitions);
134             contextRegistryChanged = true;
135         }
136
137         if (contextRegistryChanged)
138             fireContextRegistryChanged();
139     }
140
141     private void readContextDefinition(IConfigurationElement configurationElement) {
142         ContextDefinition contextDefinition =
143             Persistence.readContextDefinition(
144                 new ConfigurationElementMemento(configurationElement),
145                 getNamespace(configurationElement));
146
147         if (contextDefinition != null)
148             contextDefinitions.add(contextDefinition);
149     }
150 }
151
Popular Tags