1 11 package org.eclipse.core.commands; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.WeakHashMap ; 21 22 import org.eclipse.core.commands.common.HandleObjectManager; 23 import org.eclipse.core.commands.common.NotDefinedException; 24 import org.eclipse.core.runtime.ListenerList; 25 26 37 public final class CommandManager extends HandleObjectManager implements 38 ICategoryListener, ICommandListener, IParameterTypeListener { 39 40 47 private final class ExecutionListener implements 48 IExecutionListenerWithChecks { 49 50 public void notDefined(String commandId, NotDefinedException exception) { 51 if (executionListeners != null) { 52 final Object [] listeners = executionListeners.getListeners(); 53 for (int i = 0; i < listeners.length; i++) { 54 final Object object = listeners[i]; 55 if (object instanceof IExecutionListenerWithChecks) { 56 final IExecutionListenerWithChecks listener = (IExecutionListenerWithChecks) object; 57 listener.notDefined(commandId, exception); 58 } 59 } 60 } 61 } 62 63 public void notEnabled(String commandId, NotEnabledException exception) { 64 if (executionListeners != null) { 65 final Object [] listeners = executionListeners.getListeners(); 66 for (int i = 0; i < listeners.length; i++) { 67 final Object object = listeners[i]; 68 if (object instanceof IExecutionListenerWithChecks) { 69 final IExecutionListenerWithChecks listener = (IExecutionListenerWithChecks) object; 70 listener.notEnabled(commandId, exception); 71 } 72 } 73 } 74 } 75 76 public final void notHandled(final String commandId, 77 final NotHandledException exception) { 78 if (executionListeners != null) { 79 final Object [] listeners = executionListeners.getListeners(); 80 for (int i = 0; i < listeners.length; i++) { 81 final Object object = listeners[i]; 82 if (object instanceof IExecutionListener) { 83 final IExecutionListener listener = (IExecutionListener) object; 84 listener.notHandled(commandId, exception); 85 } 86 } 87 } 88 } 89 90 public final void postExecuteFailure(final String commandId, 91 final ExecutionException exception) { 92 if (executionListeners != null) { 93 final Object [] listeners = executionListeners.getListeners(); 94 for (int i = 0; i < listeners.length; i++) { 95 final Object object = listeners[i]; 96 if (object instanceof IExecutionListener) { 97 final IExecutionListener listener = (IExecutionListener) object; 98 listener.postExecuteFailure(commandId, exception); 99 } 100 } 101 } 102 } 103 104 public final void postExecuteSuccess(final String commandId, 105 final Object returnValue) { 106 if (executionListeners != null) { 107 final Object [] listeners = executionListeners.getListeners(); 108 for (int i = 0; i < listeners.length; i++) { 109 final Object object = listeners[i]; 110 if (object instanceof IExecutionListener) { 111 final IExecutionListener listener = (IExecutionListener) object; 112 listener.postExecuteSuccess(commandId, returnValue); 113 } 114 } 115 } 116 } 117 118 public final void preExecute(final String commandId, 119 final ExecutionEvent event) { 120 if (executionListeners != null) { 121 final Object [] listeners = executionListeners.getListeners(); 122 for (int i = 0; i < listeners.length; i++) { 123 final Object object = listeners[i]; 124 if (object instanceof IExecutionListener) { 125 final IExecutionListener listener = (IExecutionListener) object; 126 listener.preExecute(commandId, event); 127 } 128 } 129 } 130 } 131 } 132 133 139 public static final String AUTOGENERATED_CATEGORY_ID = "org.eclipse.core.commands.categories.autogenerated"; 141 145 static final char ESCAPE_CHAR = '%'; 146 147 150 static final char ID_VALUE_CHAR = '='; 151 152 155 static final char PARAMETER_END_CHAR = ')'; 156 157 160 static final char PARAMETER_SEPARATOR_CHAR = ','; 161 162 165 static final char PARAMETER_START_CHAR = '('; 166 167 186 private static final String unescape(final String escapedText) 187 throws SerializationException { 188 189 StringBuffer buffer = null; 191 192 for (int i = 0; i < escapedText.length(); i++) { 193 194 char c = escapedText.charAt(i); 195 if (c != ESCAPE_CHAR) { 196 if (buffer != null) { 198 buffer.append(c); 199 } 200 } else { 201 if (buffer == null) { 202 buffer = new StringBuffer (escapedText.substring(0, i)); 203 } 204 205 if (++i < escapedText.length()) { 206 c = escapedText.charAt(i); 207 switch (c) { 208 case PARAMETER_START_CHAR: 209 case PARAMETER_END_CHAR: 210 case ID_VALUE_CHAR: 211 case PARAMETER_SEPARATOR_CHAR: 212 case ESCAPE_CHAR: 213 buffer.append(c); 214 break; 215 default: 216 throw new SerializationException( 217 "Invalid character '" + c + "' in escape sequence"); } 219 } else { 220 throw new SerializationException( 221 "Unexpected termination of escape sequence"); } 223 } 224 225 } 226 227 if (buffer == null) { 228 return escapedText; 229 } 230 231 return buffer.toString(); 232 } 233 234 239 private final Map categoriesById = new HashMap (); 240 241 245 private final Set definedCategoryIds = new HashSet (); 246 247 253 private final Set definedParameterTypeIds = new HashSet (); 254 255 260 private IExecutionListener executionListener = null; 261 262 266 private ListenerList executionListeners = null; 267 268 275 private final Map helpContextIdsByHandler = new WeakHashMap (); 276 277 284 private final Map parameterTypesById = new HashMap (); 285 286 294 public final void addCommandManagerListener( 295 final ICommandManagerListener listener) { 296 addListenerObject(listener); 297 } 298 299 307 public final void addExecutionListener(final IExecutionListener listener) { 308 if (listener == null) { 309 throw new NullPointerException ( 310 "Cannot add a null execution listener"); } 312 313 if (executionListeners == null) { 314 executionListeners = new ListenerList(ListenerList.IDENTITY); 315 316 executionListener = new ExecutionListener(); 318 final Iterator commandItr = handleObjectsById.values().iterator(); 319 while (commandItr.hasNext()) { 320 final Command command = (Command) commandItr.next(); 321 command.addExecutionListener(executionListener); 322 } 323 324 } 325 326 executionListeners.add(listener); 327 } 328 329 334 public final void categoryChanged(CategoryEvent categoryEvent) { 335 if (categoryEvent.isDefinedChanged()) { 336 final Category category = categoryEvent.getCategory(); 337 final String categoryId = category.getId(); 338 final boolean categoryIdAdded = category.isDefined(); 339 if (categoryIdAdded) { 340 definedCategoryIds.add(categoryId); 341 } else { 342 definedCategoryIds.remove(categoryId); 343 } 344 if (isListenerAttached()) { 345 fireCommandManagerChanged(new CommandManagerEvent(this, null, 346 false, false, categoryId, categoryIdAdded, true)); 347 } 348 } 349 } 350 351 356 public final void commandChanged(final CommandEvent commandEvent) { 357 if (commandEvent.isDefinedChanged()) { 358 final Command command = commandEvent.getCommand(); 359 final String commandId = command.getId(); 360 final boolean commandIdAdded = command.isDefined(); 361 if (commandIdAdded) { 362 definedHandleObjects.add(command); 363 } else { 364 definedHandleObjects.remove(command); 365 } 366 if (isListenerAttached()) { 367 fireCommandManagerChanged(new CommandManagerEvent(this, 368 commandId, commandIdAdded, true, null, false, false)); 369 } 370 } 371 } 372 373 386 public final void defineUncategorizedCategory(final String name, 387 final String description) { 388 final Category category = getCategory(AUTOGENERATED_CATEGORY_ID); 389 category.define(name, description); 390 } 391 392 432 public final ParameterizedCommand deserialize( 433 final String serializedParameterizedCommand) 434 throws NotDefinedException, SerializationException { 435 436 final int lparenPosition = unescapedIndexOf( 437 serializedParameterizedCommand, PARAMETER_START_CHAR); 438 439 final String commandIdEscaped; 440 final String serializedParameters; 441 if (lparenPosition == -1) { 442 commandIdEscaped = serializedParameterizedCommand; 443 serializedParameters = null; 444 } else { 445 commandIdEscaped = serializedParameterizedCommand.substring(0, 446 lparenPosition); 447 448 if (serializedParameterizedCommand 449 .charAt(serializedParameterizedCommand.length() - 1) != PARAMETER_END_CHAR) { 450 throw new SerializationException( 451 "Parentheses must be balanced in serialized ParameterizedCommand"); } 453 454 serializedParameters = serializedParameterizedCommand.substring( 455 lparenPosition + 1, serializedParameterizedCommand.length() - 1); } 459 460 final String commandId = unescape(commandIdEscaped); 461 final Command command = getCommand(commandId); 462 final IParameter[] parameters = command.getParameters(); 463 final Parameterization[] parameterizations = getParameterizations( 464 serializedParameters, parameters); 465 466 return new ParameterizedCommand(command, parameterizations); 467 } 468 469 477 private final void fireCommandManagerChanged(final CommandManagerEvent event) { 478 if (event == null) { 479 throw new NullPointerException (); 480 } 481 482 final Object [] listeners = getListeners(); 483 for (int i = 0; i < listeners.length; i++) { 484 final ICommandManagerListener listener = (ICommandManagerListener) listeners[i]; 485 listener.commandManagerChanged(event); 486 } 487 } 488 489 496 public final Command[] getAllCommands() { 497 return (Command[]) handleObjectsById.values().toArray( 498 new Command[handleObjectsById.size()]); 499 } 500 501 513 public final Category getCategory(final String categoryId) { 514 if (categoryId == null) { 515 return getCategory(AUTOGENERATED_CATEGORY_ID); 516 } 517 518 checkId(categoryId); 519 520 Category category = (Category) categoriesById.get(categoryId); 521 if (category == null) { 522 category = new Category(categoryId); 523 categoriesById.put(categoryId, category); 524 category.addCategoryListener(this); 525 } 526 527 return category; 528 } 529 530 541 public final Command getCommand(final String commandId) { 542 checkId(commandId); 543 544 Command command = (Command) handleObjectsById.get(commandId); 545 if (command == null) { 546 command = new Command(commandId); 547 handleObjectsById.put(commandId, command); 548 command.addCommandListener(this); 549 550 if (executionListener != null) { 551 command.addExecutionListener(executionListener); 552 } 553 } 554 555 return command; 556 } 557 558 565 public final Category[] getDefinedCategories() { 566 final Category[] categories = new Category[definedCategoryIds.size()]; 567 final Iterator categoryIdItr = definedCategoryIds.iterator(); 568 int i = 0; 569 while (categoryIdItr.hasNext()) { 570 String categoryId = (String ) categoryIdItr.next(); 571 categories[i++] = getCategory(categoryId); 572 } 573 return categories; 574 } 575 576 582 public final Set getDefinedCategoryIds() { 583 return Collections.unmodifiableSet(definedCategoryIds); 584 } 585 586 592 public final Set getDefinedCommandIds() { 593 return getDefinedHandleObjectIds(); 594 } 595 596 603 public final Command[] getDefinedCommands() { 604 return (Command[]) definedHandleObjects 605 .toArray(new Command[definedHandleObjects.size()]); 606 } 607 608 616 public final Set getDefinedParameterTypeIds() { 617 return Collections.unmodifiableSet(definedParameterTypeIds); 618 } 619 620 627 public final ParameterType[] getDefinedParameterTypes() { 628 final ParameterType[] parameterTypes = new ParameterType[definedParameterTypeIds 629 .size()]; 630 final Iterator iterator = definedParameterTypeIds.iterator(); 631 int i = 0; 632 while (iterator.hasNext()) { 633 final String parameterTypeId = (String ) iterator.next(); 634 parameterTypes[i++] = getParameterType(parameterTypeId); 635 } 636 return parameterTypes; 637 } 638 639 655 public final String getHelpContextId(final Command command) 656 throws NotDefinedException { 657 if (!command.isDefined()) { 659 throw new NotDefinedException("The command is not defined. " + command.getId()); 661 } 662 663 final IHandler handler = command.getHandler(); 665 if (handler != null) { 666 final String helpContextId = (String ) helpContextIdsByHandler 667 .get(handler); 668 if (helpContextId != null) { 669 return helpContextId; 670 } 671 } 672 673 return command.getHelpContextId(); 675 } 676 677 695 private final Parameterization[] getParameterizations( 696 String serializedParameters, final IParameter[] parameters) 697 throws SerializationException { 698 699 if (serializedParameters == null 700 || (serializedParameters.length() == 0)) { 701 return null; 702 } 703 704 if ((parameters == null) || (parameters.length == 0)) { 705 return null; 706 } 707 708 final ArrayList paramList = new ArrayList (); 709 710 int commaPosition; do { 712 commaPosition = unescapedIndexOf(serializedParameters, ','); 713 714 final String idEqualsValue; 715 if (commaPosition == -1) { 716 idEqualsValue = serializedParameters; 718 } else { 719 idEqualsValue = serializedParameters 721 .substring(0, commaPosition); 722 723 serializedParameters = serializedParameters 725 .substring(commaPosition + 1); 726 } 727 728 final int equalsPosition = unescapedIndexOf(idEqualsValue, '='); 729 730 final String parameterId; 731 final String parameterValue; 732 if (equalsPosition == -1) { 733 parameterId = unescape(idEqualsValue); 735 parameterValue = null; 736 } else { 737 parameterId = unescape(idEqualsValue.substring(0, 738 equalsPosition)); 739 parameterValue = unescape(idEqualsValue 740 .substring(equalsPosition + 1)); 741 } 742 743 for (int i = 0; i < parameters.length; i++) { 744 final IParameter parameter = parameters[i]; 745 if (parameter.getId().equals(parameterId)) { 746 paramList.add(new Parameterization(parameter, 747 parameterValue)); 748 break; 749 } 750 } 751 752 } while (commaPosition != -1); 753 754 return (Parameterization[]) paramList 755 .toArray(new Parameterization[paramList.size()]); 756 } 757 758 770 public final ParameterType getParameterType(final String parameterTypeId) { 771 checkId(parameterTypeId); 772 773 ParameterType parameterType = (ParameterType) parameterTypesById 774 .get(parameterTypeId); 775 if (parameterType == null) { 776 parameterType = new ParameterType(parameterTypeId); 777 parameterTypesById.put(parameterTypeId, parameterType); 778 parameterType.addListener(this); 779 } 780 781 return parameterType; 782 } 783 784 789 public final void parameterTypeChanged( 790 final ParameterTypeEvent parameterTypeEvent) { 791 if (parameterTypeEvent.isDefinedChanged()) { 792 final ParameterType parameterType = parameterTypeEvent 793 .getParameterType(); 794 final String parameterTypeId = parameterType.getId(); 795 final boolean parameterTypeIdAdded = parameterType.isDefined(); 796 if (parameterTypeIdAdded) { 797 definedParameterTypeIds.add(parameterTypeId); 798 } else { 799 definedParameterTypeIds.remove(parameterTypeId); 800 } 801 802 fireCommandManagerChanged(new CommandManagerEvent(this, 803 parameterTypeId, parameterTypeIdAdded, true)); 804 } 805 } 806 807 813 public final void removeCommandManagerListener( 814 final ICommandManagerListener listener) { 815 removeListenerObject(listener); 816 } 817 818 824 public final void removeExecutionListener(final IExecutionListener listener) { 825 if (listener == null) { 826 throw new NullPointerException ("Cannot remove a null listener"); } 828 829 if (executionListeners == null) { 830 return; 831 } 832 833 executionListeners.remove(listener); 834 835 if (executionListeners.isEmpty()) { 836 executionListeners = null; 837 838 final Iterator commandItr = handleObjectsById.values().iterator(); 840 while (commandItr.hasNext()) { 841 final Command command = (Command) commandItr.next(); 842 command.removeExecutionListener(executionListener); 843 } 844 executionListener = null; 845 846 } 847 } 848 849 862 public final void setHandlersByCommandId(final Map handlersByCommandId) { 863 final Iterator commandIdItr = handlersByCommandId.keySet().iterator(); 865 while (commandIdItr.hasNext()) { 866 getCommand((String ) commandIdItr.next()); 867 } 868 869 final Iterator commandItr = handleObjectsById.values().iterator(); 871 while (commandItr.hasNext()) { 872 final Command command = (Command) commandItr.next(); 873 final String commandId = command.getId(); 874 final Object value = handlersByCommandId.get(commandId); 875 if (value instanceof IHandler) { 876 command.setHandler((IHandler) value); 877 } else { 878 command.setHandler(null); 879 } 880 } 881 } 882 883 895 public final void setHelpContextId(final IHandler handler, 896 final String helpContextId) { 897 if (handler == null) { 898 throw new NullPointerException ("The handler cannot be null"); } 900 if (helpContextId == null) { 901 helpContextIdsByHandler.remove(handler); 902 } else { 903 helpContextIdsByHandler.put(handler, helpContextId); 904 } 905 } 906 907 923 private final int unescapedIndexOf(final String escapedText, final char ch) { 924 925 int pos = escapedText.indexOf(ch); 926 927 if (pos == 0) { 929 return pos; 930 } 931 932 while (pos != -1) { 933 if (escapedText.charAt(pos - 1) != ESCAPE_CHAR) { 935 return pos; 936 } 937 938 pos = escapedText.indexOf(ch, pos + 1); 940 } 941 942 return pos; 943 944 } 945 946 } 947 | Popular Tags |