1 11 12 package org.eclipse.ui.internal.services; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.List ; 17 18 import org.eclipse.core.commands.Command; 19 import org.eclipse.core.commands.IParameter; 20 import org.eclipse.core.commands.Parameterization; 21 import org.eclipse.core.commands.ParameterizedCommand; 22 import org.eclipse.core.commands.common.NotDefinedException; 23 import org.eclipse.core.expressions.ElementHandler; 24 import org.eclipse.core.expressions.EvaluationResult; 25 import org.eclipse.core.expressions.Expression; 26 import org.eclipse.core.expressions.ExpressionConverter; 27 import org.eclipse.core.expressions.IEvaluationContext; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IConfigurationElement; 30 import org.eclipse.core.runtime.IExtensionRegistry; 31 import org.eclipse.core.runtime.IRegistryChangeEvent; 32 import org.eclipse.core.runtime.IRegistryChangeListener; 33 import org.eclipse.core.runtime.IStatus; 34 import org.eclipse.core.runtime.MultiStatus; 35 import org.eclipse.core.runtime.Platform; 36 import org.eclipse.core.runtime.Status; 37 import org.eclipse.swt.widgets.Display; 38 import org.eclipse.ui.commands.ICommandService; 39 import org.eclipse.ui.internal.WorkbenchPlugin; 40 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 41 import org.eclipse.ui.internal.util.Util; 42 import org.eclipse.ui.services.IDisposable; 43 44 57 public abstract class RegistryPersistence implements IDisposable, 58 IWorkbenchRegistryConstants { 59 60 63 protected static final Expression ERROR_EXPRESSION = new Expression() { 64 public final EvaluationResult evaluate(final IEvaluationContext context) { 65 return null; 66 } 67 }; 68 69 85 protected static final void addElementToIndexedArray( 86 final IConfigurationElement elementToAdd, 87 final IConfigurationElement[][] indexedArray, final int index, 88 final int currentCount) { 89 final IConfigurationElement[] elements; 90 if (currentCount == 0) { 91 elements = new IConfigurationElement[1]; 92 indexedArray[index] = elements; 93 } else { 94 if (currentCount >= indexedArray[index].length) { 95 final IConfigurationElement[] copy = new IConfigurationElement[indexedArray[index].length * 2]; 96 System.arraycopy(indexedArray[index], 0, copy, 0, currentCount); 97 elements = copy; 98 indexedArray[index] = elements; 99 } else { 100 elements = indexedArray[index]; 101 } 102 } 103 elements[currentCount] = elementToAdd; 104 } 105 106 118 protected static final void addWarning(final List warningsToLog, 119 final String message, final IConfigurationElement element) { 120 addWarning(warningsToLog, message, element, null, null, null); 121 } 122 123 139 protected static final void addWarning(final List warningsToLog, 140 final String message, final IConfigurationElement element, 141 final String id) { 142 addWarning(warningsToLog, message, element, id, null, null); 143 } 144 145 167 protected static final void addWarning(final List warningsToLog, 168 final String message, final IConfigurationElement element, 169 final String id, final String extraAttributeName, 170 final String extraAttributeValue) { 171 String statusMessage = message; 172 if (element != null) { 173 statusMessage = statusMessage 174 + ": plug-in='" + element.getNamespace() + '\''; } 176 if (id != null) { 177 if (element != null) { 178 statusMessage = statusMessage + ','; 179 } else { 180 statusMessage = statusMessage + ':'; 181 } 182 statusMessage = statusMessage + " id='" + id + '\''; } 184 if (extraAttributeName != null) { 185 if ((element != null) || (id != null)) { 186 statusMessage = statusMessage + ','; 187 } else { 188 statusMessage = statusMessage + ':'; 189 } 190 statusMessage = statusMessage + ' ' + extraAttributeName + "='" + extraAttributeValue + '\''; 192 } 193 194 final IStatus status = new Status(IStatus.WARNING, 195 WorkbenchPlugin.PI_WORKBENCH, 0, statusMessage, null); 196 warningsToLog.add(status); 197 } 198 199 217 protected static final boolean checkClass( 218 final IConfigurationElement configurationElement, 219 final List warningsToLog, final String message, final String id) { 220 if ((configurationElement.getAttribute(ATT_CLASS) == null) 222 && (configurationElement.getChildren(TAG_CLASS).length == 0)) { 223 addWarning(warningsToLog, message, configurationElement, id); 224 return false; 225 } 226 227 return true; 228 } 229 230 240 protected static final boolean isPulldown( 241 final IConfigurationElement element) { 242 final String style = readOptional(element, ATT_STYLE); 243 final boolean pulldown = readBoolean(element, ATT_PULLDOWN, false); 244 return (pulldown || STYLE_PULLDOWN.equals(style)); 245 } 246 247 256 protected static final void logWarnings(final List warningsToLog, 257 final String message) { 258 if ((warningsToLog != null) && (!warningsToLog.isEmpty())) { 260 final IStatus status = new MultiStatus( 261 WorkbenchPlugin.PI_WORKBENCH, 0, (IStatus[]) warningsToLog 262 .toArray(new IStatus[warningsToLog.size()]), 263 message, null); 264 WorkbenchPlugin.log(status); 265 } 266 } 267 268 280 protected static final boolean readBoolean( 281 final IConfigurationElement configurationElement, 282 final String attribute, final boolean defaultValue) { 283 final String value = configurationElement.getAttribute(attribute); 284 if (value == null) { 285 return defaultValue; 286 } 287 288 if (defaultValue) { 289 return !value.equalsIgnoreCase("false"); } 291 292 return value.equalsIgnoreCase("true"); } 294 295 306 protected static final String readOptional( 307 final IConfigurationElement configurationElement, 308 final String attribute) { 309 String value = configurationElement.getAttribute(attribute); 310 if ((value != null) && (value.length() == 0)) { 311 value = null; 312 } 313 314 return value; 315 } 316 317 341 protected static final ParameterizedCommand readParameterizedCommand( 342 final IConfigurationElement configurationElement, 343 final ICommandService commandService, final List warningsToLog, 344 final String message, final String id) { 345 final String commandId = readRequired(configurationElement, 346 ATT_COMMAND_ID, warningsToLog, message, id); 347 if (commandId == null) { 348 return null; 349 } 350 351 final Command command = commandService.getCommand(commandId); 352 final ParameterizedCommand parameterizedCommand = readParameters( 353 configurationElement, warningsToLog, command); 354 355 return parameterizedCommand; 356 } 357 358 377 protected static final ParameterizedCommand readParameters( 378 final IConfigurationElement configurationElement, 379 final List warningsToLog, final Command command) { 380 final IConfigurationElement[] parameterElements = configurationElement 381 .getChildren(TAG_PARAMETER); 382 if ((parameterElements == null) || (parameterElements.length == 0)) { 383 return new ParameterizedCommand(command, null); 384 } 385 386 final Collection parameters = new ArrayList (); 387 for (int i = 0; i < parameterElements.length; i++) { 388 final IConfigurationElement parameterElement = parameterElements[i]; 389 390 final String id = parameterElement.getAttribute(ATT_ID); 392 if ((id == null) || (id.length() == 0)) { 393 addWarning(warningsToLog, "Parameters need a name", configurationElement); 396 continue; 397 } 398 399 IParameter parameter = null; 401 try { 402 final IParameter[] commandParameters = command.getParameters(); 403 if (parameters != null) { 404 for (int j = 0; j < commandParameters.length; j++) { 405 final IParameter currentParameter = commandParameters[j]; 406 if (Util.equals(currentParameter.getId(), id)) { 407 parameter = currentParameter; 408 break; 409 } 410 } 411 412 } 413 } catch (final NotDefinedException e) { 414 } 416 if (parameter == null) { 417 addWarning(warningsToLog, 419 "Could not find a matching parameter", configurationElement, id); 421 continue; 422 } 423 424 final String value = parameterElement.getAttribute(ATT_VALUE); 426 if ((value == null) || (value.length() == 0)) { 427 addWarning(warningsToLog, "Parameters need a value", configurationElement, id); 430 continue; 431 } 432 433 parameters.add(new Parameterization(parameter, value)); 434 } 435 436 if (parameters.isEmpty()) { 437 return new ParameterizedCommand(command, null); 438 } 439 440 return new ParameterizedCommand(command, 441 (Parameterization[]) parameters 442 .toArray(new Parameterization[parameters.size()])); 443 } 444 445 460 protected static final String readRequired( 461 final IConfigurationElement configurationElement, 462 final String attribute, final List warningsToLog, 463 final String message) { 464 return readRequired(configurationElement, attribute, warningsToLog, 465 message, null); 466 } 467 468 487 protected static final String readRequired( 488 final IConfigurationElement configurationElement, 489 final String attribute, final List warningsToLog, 490 final String message, final String id) { 491 final String value = configurationElement.getAttribute(attribute); 492 if ((value == null) || (value.length() == 0)) { 493 addWarning(warningsToLog, message, configurationElement, id); 494 return null; 495 } 496 497 return value; 498 } 499 500 519 protected static final Expression readWhenElement( 520 final IConfigurationElement parentElement, 521 final String whenElementName, final String id, 522 final List warningsToLog) { 523 final IConfigurationElement[] whenElements = parentElement 525 .getChildren(whenElementName); 526 Expression whenExpression = null; 527 if (whenElements.length > 0) { 528 if (whenElements.length > 1) { 530 addWarning(warningsToLog, 532 "There should only be one when element", parentElement, id, "whenElementName", whenElementName); 535 return ERROR_EXPRESSION; 536 } 537 538 final IConfigurationElement whenElement = whenElements[0]; 539 final IConfigurationElement[] expressionElements = whenElement 540 .getChildren(); 541 if (expressionElements.length > 0) { 542 if (expressionElements.length > 1) { 544 addWarning( 546 warningsToLog, 547 "There should only be one expression element", parentElement, id, "whenElementName", whenElementName); 550 return ERROR_EXPRESSION; 551 } 552 553 final ElementHandler elementHandler = ElementHandler 555 .getDefault(); 556 final ExpressionConverter converter = ExpressionConverter 557 .getDefault(); 558 final IConfigurationElement expressionElement = expressionElements[0]; 559 try { 560 whenExpression = elementHandler.create(converter, 561 expressionElement); 562 } catch (final CoreException e) { 563 addWarning( 565 warningsToLog, 566 "Problem creating when element", parentElement, id, 568 "whenElementName", whenElementName); return ERROR_EXPRESSION; 570 } 571 } 572 } 573 574 return whenExpression; 575 } 576 577 580 private final IRegistryChangeListener registryChangeListener; 581 582 586 protected boolean registryListenerAttached = false; 587 588 592 protected RegistryPersistence() { 593 registryChangeListener = new IRegistryChangeListener() { 594 public final void registryChanged(final IRegistryChangeEvent event) { 595 if (isChangeImportant(event)) { 596 Display.getDefault().asyncExec(new Runnable () { 597 public final void run() { 598 read(); 599 } 600 }); 601 } 602 } 603 }; 604 } 605 606 609 public void dispose() { 610 final IExtensionRegistry registry = Platform.getExtensionRegistry(); 611 registry.removeRegistryChangeListener(registryChangeListener); 612 registryListenerAttached = false; 613 } 614 615 624 protected abstract boolean isChangeImportant( 625 final IRegistryChangeEvent event); 626 627 631 protected void read() { 632 if (!registryListenerAttached) { 633 final IExtensionRegistry registry = Platform.getExtensionRegistry(); 634 registry.addRegistryChangeListener(registryChangeListener); 635 registryListenerAttached = true; 636 } 637 } 638 } 639 | Popular Tags |