1 11 12 package org.eclipse.ui.internal.contexts; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 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 36 final class ContextPersistence extends RegistryPersistence { 37 38 43 private static final int INDEX_CONTEXT_DEFINITIONS = 0; 44 45 58 private static final void readContextsFromRegistry( 59 final IConfigurationElement[] configurationElements, 60 final int configurationElementCount, 61 final ContextManager contextManager) { 62 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 warningsToLog = new ArrayList (1); 72 73 for (int i = 0; i < configurationElementCount; i++) { 74 final IConfigurationElement configurationElement = configurationElements[i]; 75 76 final String contextId = readRequired(configurationElement, ATT_ID, 78 warningsToLog, "Contexts need an id"); if (contextId == null) { 80 continue; 81 } 82 83 final String name = readRequired(configurationElement, ATT_NAME, 85 warningsToLog, "Contexts need a name", contextId); 87 if (name == null) { 88 continue; 89 } 90 91 final String description = readOptional(configurationElement, 93 ATT_DESCRIPTION); 94 95 String 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."); } 116 117 120 private final ContextManager contextManager; 121 122 125 ContextPersistence(final ContextManager contextManager) { 126 if (contextManager == null) { 127 throw new NullPointerException ( 128 "The context manager must not be null"); } 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 161 protected final void read() { 162 super.read(); 163 164 final IExtensionRegistry registry = Platform.getExtensionRegistry(); 166 int contextDefinitionCount = 0; 167 final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[1][]; 168 169 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 name = configurationElement.getName(); 178 179 if (TAG_ACCELERATOR_SCOPE.equals(name)) { 181 addElementToIndexedArray(configurationElement, 182 indexedConfigurationElements, 183 INDEX_CONTEXT_DEFINITIONS, contextDefinitionCount++); 184 } 185 } 186 187 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 name = configurationElement.getName(); 196 197 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 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 name = configurationElement.getName(); 218 219 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 |