1 11 12 package org.eclipse.ui.internal.handlers; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 18 import org.eclipse.core.commands.IHandler; 19 import org.eclipse.core.expressions.Expression; 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.handlers.IHandlerService; 27 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 28 import org.eclipse.ui.internal.services.IEvaluationService; 29 import org.eclipse.ui.internal.services.RegistryPersistence; 30 31 38 final class HandlerPersistence extends RegistryPersistence { 39 40 45 private static final int INDEX_COMMAND_DEFINITIONS = 0; 46 47 52 private static final int INDEX_HANDLER_DEFINITIONS = 1; 53 54 59 private static final int INDEX_HANDLER_SUBMISSIONS = 2; 60 61 66 private final Collection handlerActivations = new ArrayList (); 67 68 72 private final IHandlerService handlerService; 73 74 private IEvaluationService evaluationService; 75 76 86 HandlerPersistence(final IHandlerService handlerService, 87 IEvaluationService evaluationService) { 88 this.handlerService = handlerService; 89 this.evaluationService = evaluationService; 90 } 91 92 100 private final void clearActivations(final IHandlerService handlerService) { 101 handlerService.deactivateHandlers(handlerActivations); 102 handlerActivations.clear(); 103 } 104 105 public final void dispose() { 106 super.dispose(); 107 clearActivations(handlerService); 108 } 109 110 protected final boolean isChangeImportant(final IRegistryChangeEvent event) { 111 116 final IExtensionDelta[] handlerDeltas = event.getExtensionDeltas( 117 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_HANDLERS); 118 if (handlerDeltas.length == 0) { 119 final IExtensionDelta[] commandDeltas = event.getExtensionDeltas( 120 PlatformUI.PLUGIN_ID, 121 IWorkbenchRegistryConstants.PL_COMMANDS); 122 if (commandDeltas.length == 0) { 123 final IExtensionDelta[] actionDefinitionDeltas = event 124 .getExtensionDeltas( 125 PlatformUI.PLUGIN_ID, 126 IWorkbenchRegistryConstants.PL_ACTION_DEFINITIONS); 127 if (actionDefinitionDeltas.length == 0) { 128 return false; 129 } 130 } 131 } 132 133 return true; 134 } 135 136 143 protected final void read() { 144 super.read(); 145 146 final IExtensionRegistry registry = Platform.getExtensionRegistry(); 148 int commandDefinitionCount = 0; 149 int handlerDefinitionCount = 0; 150 int handlerSubmissionCount = 0; 151 final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[3][]; 152 153 final IConfigurationElement[] commandsExtensionPoint = registry 155 .getConfigurationElementsFor(EXTENSION_COMMANDS); 156 for (int i = 0; i < commandsExtensionPoint.length; i++) { 157 final IConfigurationElement configurationElement = commandsExtensionPoint[i]; 158 final String name = configurationElement.getName(); 159 160 if (TAG_HANDLER_SUBMISSION.equals(name)) { 162 addElementToIndexedArray(configurationElement, 163 indexedConfigurationElements, 164 INDEX_HANDLER_SUBMISSIONS, handlerSubmissionCount++); 165 } else if (TAG_COMMAND.equals(name)) { 166 addElementToIndexedArray(configurationElement, 167 indexedConfigurationElements, 168 INDEX_COMMAND_DEFINITIONS, commandDefinitionCount++); 169 } 170 } 171 172 final IConfigurationElement[] handlersExtensionPoint = registry 174 .getConfigurationElementsFor(EXTENSION_HANDLERS); 175 for (int i = 0; i < handlersExtensionPoint.length; i++) { 176 final IConfigurationElement configurationElement = handlersExtensionPoint[i]; 177 final String name = configurationElement.getName(); 178 179 if (TAG_HANDLER.equals(name)) { 181 addElementToIndexedArray(configurationElement, 182 indexedConfigurationElements, 183 INDEX_HANDLER_DEFINITIONS, handlerDefinitionCount++); 184 } 185 } 186 187 clearActivations(handlerService); 188 readDefaultHandlersFromRegistry( 189 indexedConfigurationElements[INDEX_COMMAND_DEFINITIONS], 190 commandDefinitionCount); 191 readHandlerSubmissionsFromRegistry( 192 indexedConfigurationElements[INDEX_HANDLER_SUBMISSIONS], 193 handlerSubmissionCount); 194 readHandlersFromRegistry( 195 indexedConfigurationElements[INDEX_HANDLER_DEFINITIONS], 196 handlerDefinitionCount); 197 } 198 199 210 private final void readDefaultHandlersFromRegistry( 211 final IConfigurationElement[] configurationElements, 212 final int configurationElementCount) { 213 for (int i = 0; i < configurationElementCount; i++) { 214 final IConfigurationElement configurationElement = configurationElements[i]; 215 216 221 final String commandId = readOptional(configurationElement, ATT_ID); 222 if (commandId == null) { 223 continue; 224 } 225 226 if ((configurationElement.getAttribute(ATT_DEFAULT_HANDLER) == null) 228 && (configurationElement.getChildren(TAG_DEFAULT_HANDLER).length == 0)) { 229 continue; 230 } 231 232 handlerActivations.add(handlerService 233 .activateHandler(commandId, new HandlerProxy( 234 configurationElement, ATT_DEFAULT_HANDLER))); 235 } 236 } 237 238 251 private final void readHandlersFromRegistry( 252 final IConfigurationElement[] configurationElements, 253 final int configurationElementCount) { 254 final List warningsToLog = new ArrayList (1); 255 256 for (int i = 0; i < configurationElementCount; i++) { 257 final IConfigurationElement configurationElement = configurationElements[i]; 258 259 final String commandId = readRequired(configurationElement, 261 ATT_COMMAND_ID, warningsToLog, "Handlers need a command id"); if (commandId == null) { 263 continue; 264 } 265 266 if (!checkClass(configurationElement, warningsToLog, 268 "Handlers need a class", commandId)) { continue; 270 } 271 272 final Expression activeWhenExpression = readWhenElement( 274 configurationElement, TAG_ACTIVE_WHEN, commandId, 275 warningsToLog); 276 if (activeWhenExpression == ERROR_EXPRESSION) { 277 continue; 278 } 279 final Expression enabledWhenExpression = readWhenElement( 280 configurationElement, TAG_ENABLED_WHEN, commandId, 281 warningsToLog); 282 if (enabledWhenExpression == ERROR_EXPRESSION) { 283 continue; 284 } 285 286 final IHandler proxy = new HandlerProxy(configurationElement, 287 ATT_CLASS, enabledWhenExpression, evaluationService); 288 handlerActivations.add(handlerService.activateHandler(commandId, 289 proxy, activeWhenExpression)); 290 291 final String helpContextId = readOptional(configurationElement, 293 ATT_HELP_CONTEXT_ID); 294 handlerService.setHelpContextId(proxy, helpContextId); 295 } 296 297 logWarnings( 298 warningsToLog, 299 "Warnings while parsing the handlers from the 'org.eclipse.ui.handlers' extension point."); } 301 302 315 private final void readHandlerSubmissionsFromRegistry( 316 final IConfigurationElement[] configurationElements, 317 final int configurationElementCount) { 318 final List warningsToLog = new ArrayList (1); 319 320 for (int i = 0; i < configurationElementCount; i++) { 321 final IConfigurationElement configurationElement = configurationElements[i]; 322 323 final String commandId = readRequired(configurationElement, 325 ATT_COMMAND_ID, warningsToLog, 326 "Handler submissions need a command id"); if (commandId == null) { 328 continue; 329 } 330 331 handlerActivations.add(handlerService.activateHandler(commandId, 332 new LegacyHandlerWrapper(new LegacyHandlerProxy( 333 configurationElement)))); 334 } 335 336 logWarnings( 337 warningsToLog, 338 "Warnings while parsing the handler submissions from the 'org.eclipse.ui.commands' extension point."); } 340 } 341 | Popular Tags |