001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.contexts;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.commands.common.HandleObject;
016: import org.eclipse.core.commands.contexts.Context;
017: import org.eclipse.core.commands.contexts.ContextManager;
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IExtensionDelta;
020: import org.eclipse.core.runtime.IExtensionRegistry;
021: import org.eclipse.core.runtime.IRegistryChangeEvent;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
025: import org.eclipse.ui.internal.services.RegistryPersistence;
026:
027: /**
028: * <p>
029: * A static class for accessing the registry.
030: * </p>
031: *
032: * @since 3.1
033: */
034: final class ContextPersistence extends RegistryPersistence {
035:
036: /**
037: * The index of the context elements in the indexed array.
038: *
039: * @see ContextPersistence#read()
040: */
041: private static final int INDEX_CONTEXT_DEFINITIONS = 0;
042:
043: /**
044: * Reads all of the command definitions from the commands extension point.
045: *
046: * @param configurationElements
047: * The configuration elements in the commands extension point;
048: * must not be <code>null</code>, but may be empty.
049: * @param configurationElementCount
050: * The number of configuration elements that are really in the
051: * array.
052: * @param contextManager
053: * The context manager to which the commands should be added;
054: * must not be <code>null</code>.
055: */
056: private static final void readContextsFromRegistry(
057: final IConfigurationElement[] configurationElements,
058: final int configurationElementCount,
059: final ContextManager contextManager) {
060: // Undefine all the previous handle objects.
061: final HandleObject[] handleObjects = contextManager
062: .getDefinedContexts();
063: if (handleObjects != null) {
064: for (int i = 0; i < handleObjects.length; i++) {
065: handleObjects[i].undefine();
066: }
067: }
068:
069: final List warningsToLog = new ArrayList(1);
070:
071: for (int i = 0; i < configurationElementCount; i++) {
072: final IConfigurationElement configurationElement = configurationElements[i];
073:
074: // Read out the command identifier.
075: final String contextId = readRequired(configurationElement,
076: ATT_ID, warningsToLog, "Contexts need an id"); //$NON-NLS-1$
077: if (contextId == null) {
078: continue;
079: }
080:
081: // Read out the name.
082: final String name = readRequired(configurationElement,
083: ATT_NAME, warningsToLog, "Contexts need a name", //$NON-NLS-1$
084: contextId);
085: if (name == null) {
086: continue;
087: }
088:
089: // Read out the description.
090: final String description = readOptional(
091: configurationElement, ATT_DESCRIPTION);
092:
093: // Read out the parent id.
094: String parentId = configurationElement
095: .getAttribute(ATT_PARENT_ID);
096: if ((parentId == null) || (parentId.length() == 0)) {
097: parentId = configurationElement
098: .getAttribute(ATT_PARENT);
099: 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
109: .getContext(contextId);
110: context.define(name, description, parentId);
111: }
112:
113: logWarnings(
114: warningsToLog,
115: "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$
116: }
117:
118: /**
119: * The context manager for this instance; must not be <code>null</code>.
120: */
121: private final ContextManager contextManager;
122:
123: /**
124: * Constructs a new instance of <code>ContextPersistence</code>.
125: */
126: ContextPersistence(final ContextManager contextManager) {
127: if (contextManager == null) {
128: throw new NullPointerException(
129: "The context manager must not be null"); //$NON-NLS-1$
130: }
131: this .contextManager = contextManager;
132: }
133:
134: protected final boolean isChangeImportant(
135: final IRegistryChangeEvent event) {
136: final IExtensionDelta[] acceleratorScopeDeltas = event
137: .getExtensionDeltas(
138: PlatformUI.PLUGIN_ID,
139: IWorkbenchRegistryConstants.PL_ACCELERATOR_SCOPES);
140: if (acceleratorScopeDeltas.length == 0) {
141: final IExtensionDelta[] contextDeltas = event
142: .getExtensionDeltas(PlatformUI.PLUGIN_ID,
143: IWorkbenchRegistryConstants.PL_CONTEXTS);
144: if (contextDeltas.length == 0) {
145: final IExtensionDelta[] commandDeltas = event
146: .getExtensionDeltas(PlatformUI.PLUGIN_ID,
147: IWorkbenchRegistryConstants.PL_COMMANDS);
148: if (commandDeltas.length == 0) {
149: return false;
150: }
151: }
152: }
153:
154: return true;
155: }
156:
157: /**
158: * Reads all of the contexts from the registry,
159: *
160: * @param contextManager
161: * The context manager which should be populated with the values
162: * from the registry; must not be <code>null</code>.
163: */
164: protected final void read() {
165: super .read();
166:
167: // Create the extension registry mementos.
168: final IExtensionRegistry registry = Platform
169: .getExtensionRegistry();
170: int contextDefinitionCount = 0;
171: final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[1][];
172:
173: /*
174: * Retrieve the accelerator scopes from the accelerator scopes extension
175: * point.
176: */
177: final IConfigurationElement[] acceleratorScopesExtensionPoint = registry
178: .getConfigurationElementsFor(EXTENSION_ACCELERATOR_SCOPES);
179: for (int i = 0; i < acceleratorScopesExtensionPoint.length; i++) {
180: final IConfigurationElement configurationElement = acceleratorScopesExtensionPoint[i];
181: final String name = configurationElement.getName();
182:
183: // Check if it is a binding definition.
184: if (TAG_ACCELERATOR_SCOPE.equals(name)) {
185: addElementToIndexedArray(configurationElement,
186: indexedConfigurationElements,
187: INDEX_CONTEXT_DEFINITIONS,
188: contextDefinitionCount++);
189: }
190: }
191:
192: /*
193: * Retrieve the deprecated scopes and contexts from the commands
194: * extension point.
195: */
196: final IConfigurationElement[] commandsExtensionPoint = registry
197: .getConfigurationElementsFor(EXTENSION_COMMANDS);
198: for (int i = 0; i < commandsExtensionPoint.length; i++) {
199: final IConfigurationElement configurationElement = commandsExtensionPoint[i];
200: final String name = configurationElement.getName();
201:
202: // Check if it is a binding definition.
203: if (TAG_SCOPE.equals(name)) {
204: addElementToIndexedArray(configurationElement,
205: indexedConfigurationElements,
206: INDEX_CONTEXT_DEFINITIONS,
207: contextDefinitionCount++);
208: } else if (TAG_CONTEXT.equals(name)) {
209: addElementToIndexedArray(configurationElement,
210: indexedConfigurationElements,
211: INDEX_CONTEXT_DEFINITIONS,
212: contextDefinitionCount++);
213:
214: }
215: }
216:
217: /*
218: * Retrieve the contexts from the contexts extension point.
219: */
220: final IConfigurationElement[] contextsExtensionPoint = registry
221: .getConfigurationElementsFor(EXTENSION_CONTEXTS);
222: for (int i = 0; i < contextsExtensionPoint.length; i++) {
223: final IConfigurationElement configurationElement = contextsExtensionPoint[i];
224: final String name = configurationElement.getName();
225:
226: // Check if it is a binding definition.
227: if (TAG_CONTEXT.equals(name)) {
228: addElementToIndexedArray(configurationElement,
229: indexedConfigurationElements,
230: INDEX_CONTEXT_DEFINITIONS,
231: contextDefinitionCount++);
232: }
233: }
234:
235: readContextsFromRegistry(
236: indexedConfigurationElements[INDEX_CONTEXT_DEFINITIONS],
237: contextDefinitionCount, contextManager);
238: }
239:
240: }
|