KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.contexts;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.common.HandleObject;
18 import org.eclipse.core.commands.contexts.Context;
19 import org.eclipse.core.commands.contexts.ContextManager;
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.internal.registry.IWorkbenchRegistryConstants;
27 import org.eclipse.ui.internal.services.RegistryPersistence;
28
29 /**
30  * <p>
31  * A static class for accessing the registry.
32  * </p>
33  *
34  * @since 3.1
35  */

36 final class ContextPersistence extends RegistryPersistence {
37
38     /**
39      * The index of the context elements in the indexed array.
40      *
41      * @see ContextPersistence#read()
42      */

43     private static final int INDEX_CONTEXT_DEFINITIONS = 0;
44
45     /**
46      * Reads all of the command definitions from the commands extension point.
47      *
48      * @param configurationElements
49      * The configuration elements in the commands extension point;
50      * must not be <code>null</code>, but may be empty.
51      * @param configurationElementCount
52      * The number of configuration elements that are really in the
53      * array.
54      * @param contextManager
55      * The context manager to which the commands should be added;
56      * must not be <code>null</code>.
57      */

58     private static final void readContextsFromRegistry(
59             final IConfigurationElement[] configurationElements,
60             final int configurationElementCount,
61             final ContextManager contextManager) {
62         // Undefine all the previous handle objects.
63
final HandleObject[] handleObjects = contextManager
64                 .getDefinedContexts();
65         if (handleObjects != null) {
66             for (int i = 0; i < handleObjects.length; i++) {
67                 handleObjects[i].undefine();
68             }
69         }
70
71         final List JavaDoc warningsToLog = new ArrayList JavaDoc(1);
72
73         for (int i = 0; i < configurationElementCount; i++) {
74             final IConfigurationElement configurationElement = configurationElements[i];
75
76             // Read out the command identifier.
77
final String JavaDoc contextId = readRequired(configurationElement, ATT_ID,
78                     warningsToLog, "Contexts need an id"); //$NON-NLS-1$
79
if (contextId == null) {
80                 continue;
81             }
82
83             // Read out the name.
84
final String JavaDoc name = readRequired(configurationElement, ATT_NAME,
85                     warningsToLog, "Contexts need a name", //$NON-NLS-1$
86
contextId);
87             if (name == null) {
88                 continue;
89             }
90
91             // Read out the description.
92
final String JavaDoc description = readOptional(configurationElement,
93                     ATT_DESCRIPTION);
94
95             // Read out the parent id.
96
String JavaDoc parentId = configurationElement.getAttribute(ATT_PARENT_ID);
97             if ((parentId == null) || (parentId.length() == 0)) {
98                 parentId = configurationElement.getAttribute(ATT_PARENT);
99                 if ((parentId == null) || (parentId.length() == 0)) {
100                     parentId = configurationElement
101                             .getAttribute(ATT_PARENT_SCOPE);
102                 }
103             }
104             if ((parentId != null) && (parentId.length() == 0)) {
105                 parentId = null;
106             }
107
108             final Context context = contextManager.getContext(contextId);
109             context.define(name, description, parentId);
110         }
111
112         logWarnings(
113                 warningsToLog,
114                 "Warnings while parsing the contexts from the 'org.eclipse.ui.contexts', 'org.eclipse.ui.commands' and 'org.eclipse.ui.acceleratorScopes' extension points."); //$NON-NLS-1$
115
}
116
117     /**
118      * The context manager for this instance; must not be <code>null</code>.
119      */

120     private final ContextManager contextManager;
121
122     /**
123      * Constructs a new instance of <code>ContextPersistence</code>.
124      */

125     ContextPersistence(final ContextManager contextManager) {
126         if (contextManager == null) {
127             throw new NullPointerException JavaDoc(
128                     "The context manager must not be null"); //$NON-NLS-1$
129
}
130         this.contextManager = contextManager;
131     }
132
133     protected final boolean isChangeImportant(final IRegistryChangeEvent event) {
134         final IExtensionDelta[] acceleratorScopeDeltas = event
135                 .getExtensionDeltas(PlatformUI.PLUGIN_ID,
136                         IWorkbenchRegistryConstants.PL_ACCELERATOR_SCOPES);
137         if (acceleratorScopeDeltas.length == 0) {
138             final IExtensionDelta[] contextDeltas = event.getExtensionDeltas(
139                     PlatformUI.PLUGIN_ID,
140                     IWorkbenchRegistryConstants.PL_CONTEXTS);
141             if (contextDeltas.length == 0) {
142                 final IExtensionDelta[] commandDeltas = event
143                         .getExtensionDeltas(PlatformUI.PLUGIN_ID,
144                                 IWorkbenchRegistryConstants.PL_COMMANDS);
145                 if (commandDeltas.length == 0) {
146                     return false;
147                 }
148             }
149         }
150
151         return true;
152     }
153
154     /**
155      * Reads all of the contexts from the registry,
156      *
157      * @param contextManager
158      * The context manager which should be populated with the values
159      * from the registry; must not be <code>null</code>.
160      */

161     protected final void read() {
162         super.read();
163
164         // Create the extension registry mementos.
165
final IExtensionRegistry registry = Platform.getExtensionRegistry();
166         int contextDefinitionCount = 0;
167         final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[1][];
168
169         /*
170          * Retrieve the accelerator scopes from the accelerator scopes extension
171          * point.
172          */

173         final IConfigurationElement[] acceleratorScopesExtensionPoint = registry
174                 .getConfigurationElementsFor(EXTENSION_ACCELERATOR_SCOPES);
175         for (int i = 0; i < acceleratorScopesExtensionPoint.length; i++) {
176             final IConfigurationElement configurationElement = acceleratorScopesExtensionPoint[i];
177             final String JavaDoc name = configurationElement.getName();
178
179             // Check if it is a binding definition.
180
if (TAG_ACCELERATOR_SCOPE.equals(name)) {
181                 addElementToIndexedArray(configurationElement,
182                         indexedConfigurationElements,
183                         INDEX_CONTEXT_DEFINITIONS, contextDefinitionCount++);
184             }
185         }
186
187         /*
188          * Retrieve the deprecated scopes and contexts from the commands
189          * extension point.
190          */

191         final IConfigurationElement[] commandsExtensionPoint = registry
192                 .getConfigurationElementsFor(EXTENSION_COMMANDS);
193         for (int i = 0; i < commandsExtensionPoint.length; i++) {
194             final IConfigurationElement configurationElement = commandsExtensionPoint[i];
195             final String JavaDoc name = configurationElement.getName();
196
197             // Check if it is a binding definition.
198
if (TAG_SCOPE.equals(name)) {
199                 addElementToIndexedArray(configurationElement,
200                         indexedConfigurationElements,
201                         INDEX_CONTEXT_DEFINITIONS, contextDefinitionCount++);
202             } else if (TAG_CONTEXT.equals(name)) {
203                 addElementToIndexedArray(configurationElement,
204                         indexedConfigurationElements,
205                         INDEX_CONTEXT_DEFINITIONS, contextDefinitionCount++);
206
207             }
208         }
209
210         /*
211          * Retrieve the contexts from the contexts extension point.
212          */

213         final IConfigurationElement[] contextsExtensionPoint = registry
214                 .getConfigurationElementsFor(EXTENSION_CONTEXTS);
215         for (int i = 0; i < contextsExtensionPoint.length; i++) {
216             final IConfigurationElement configurationElement = contextsExtensionPoint[i];
217             final String JavaDoc name = configurationElement.getName();
218
219             // Check if it is a binding definition.
220
if (TAG_CONTEXT.equals(name)) {
221                 addElementToIndexedArray(configurationElement,
222                         indexedConfigurationElements,
223                         INDEX_CONTEXT_DEFINITIONS, contextDefinitionCount++);
224             }
225         }
226
227         readContextsFromRegistry(
228                 indexedConfigurationElements[INDEX_CONTEXT_DEFINITIONS],
229                 contextDefinitionCount, contextManager);
230     }
231
232 }
233
Popular Tags