1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.ui.IActionFilter; 23 import org.eclipse.ui.internal.util.BundleUtility; 24 import org.eclipse.ui.internal.util.Util; 25 import org.osgi.framework.Bundle; 26 27 31 public class ActionExpression { 32 33 private static abstract class AbstractExpression { 34 35 40 protected transient int expressionHashCode = HASH_CODE_NOT_COMPUTED; 41 42 53 public String [] extractObjectClasses() { 54 return null; 55 } 56 57 64 public abstract boolean isEnabledFor(Object object); 65 66 78 public boolean isEnabledForExpression(Object object, 79 String expressionType) { 80 return false; 81 } 82 83 94 public Collection valuesForExpression(String expressionType) { 95 return null; 96 } 97 } 98 99 private static class AndExpression extends CompositeExpression { 100 101 111 public AndExpression(IConfigurationElement element) 112 throws IllegalStateException { 113 super(element); 114 } 115 116 public final boolean equals(final Object object) { 117 if (object instanceof AndExpression) { 118 final AndExpression that = (AndExpression) object; 119 return Util.equals(this.list, that.list); 120 } 121 122 return false; 123 } 124 125 128 public boolean isEnabledFor(Object object) { 129 Iterator iter = list.iterator(); 130 while (iter.hasNext()) { 131 AbstractExpression expr = (AbstractExpression) iter.next(); 132 if (!expr.isEnabledFor(object)) { 133 return false; 134 } 135 } 136 return true; 137 } 138 } 139 140 private static abstract class CompositeExpression extends 141 AbstractExpression { 142 145 protected ArrayList list; 146 147 156 public CompositeExpression(IConfigurationElement element) 157 throws IllegalStateException { 158 super(); 159 160 IConfigurationElement[] children = element.getChildren(); 161 if (children.length == 0) { 162 throw new IllegalStateException ( 163 "Composite expression cannot be empty"); } 165 166 list = new ArrayList (children.length); 167 for (int i = 0; i < children.length; i++) { 168 String tag = children[i].getName(); 169 AbstractExpression expr = createExpression(children[i]); 170 if (EXP_TYPE_OBJECT_CLASS.equals(tag)) { 171 list.add(0, expr); 172 } else { 173 list.add(expr); 174 } 175 } 176 } 177 178 183 public String [] extractObjectClasses() { 184 Iterator iterator = list.iterator(); 185 List classNames = null; 186 while (iterator.hasNext()) { 187 AbstractExpression next = (AbstractExpression) iterator.next(); 188 String [] objectClasses = next.extractObjectClasses(); 189 if (objectClasses != null) { 190 if (classNames == null) { 191 classNames = new ArrayList (); 192 } 193 for (int i = 0; i < objectClasses.length; i++) { 194 classNames.add(objectClasses[i]); 195 } 196 } 197 } 198 if (classNames == null) { 199 return null; 200 } 201 202 String [] returnValue = new String [classNames.size()]; 203 classNames.toArray(returnValue); 204 return returnValue; 205 } 206 207 212 public final int hashCode() { 213 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 214 expressionHashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(list); 215 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 216 expressionHashCode++; 217 } 218 } 219 return expressionHashCode; 220 } 221 222 225 public boolean isEnabledForExpression(Object object, 226 String expressionType) { 227 Iterator iterator = list.iterator(); 228 while (iterator.hasNext()) { 229 AbstractExpression next = (AbstractExpression) iterator.next(); 230 if (next.isEnabledForExpression(object, expressionType)) { 231 return true; 232 } 233 } 234 return false; 235 } 236 237 242 public Collection valuesForExpression(String expressionType) { 243 Iterator iterator = list.iterator(); 244 Collection allValues = null; 245 while (iterator.hasNext()) { 246 AbstractExpression next = (AbstractExpression) iterator.next(); 247 Collection values = next.valuesForExpression(expressionType); 248 if (values != null) { 249 if (allValues == null) { 250 allValues = values; 251 } else { 252 allValues.addAll(values); 253 } 254 } 255 256 } 257 return allValues; 258 } 259 } 260 261 private static class NotExpression extends SingleExpression { 262 263 273 public NotExpression(IConfigurationElement element) 274 throws IllegalStateException { 275 super(element); 276 } 277 278 281 public boolean isEnabledFor(Object object) { 282 return !super.isEnabledFor(object); 283 } 284 } 285 286 private static class ObjectClassExpression extends AbstractExpression { 287 private String className; 288 289 private boolean extracted; 290 291 301 public ObjectClassExpression(IConfigurationElement element) 302 throws IllegalStateException { 303 super(); 304 305 className = element.getAttribute(ATT_NAME); 306 if (className == null) { 307 throw new IllegalStateException ( 308 "Object class expression missing name attribute"); } 310 } 311 312 318 public ObjectClassExpression(String className) { 319 super(); 320 321 if (className != null) { 322 this.className = className; 323 } else { 324 throw new IllegalStateException ( 325 "Object class expression must have class name"); } 327 } 328 329 338 private boolean checkInterfaceHierarchy(Class interfaceToCheck) { 339 if (interfaceToCheck.getName().equals(className)) { 340 return true; 341 } 342 Class [] superInterfaces = interfaceToCheck.getInterfaces(); 343 for (int i = 0; i < superInterfaces.length; i++) { 344 if (checkInterfaceHierarchy(superInterfaces[i])) { 345 return true; 346 } 347 } 348 return false; 349 } 350 351 public final boolean equals(final Object object) { 352 if (object instanceof ObjectClassExpression) { 353 final ObjectClassExpression that = (ObjectClassExpression) object; 354 return Util.equals(this.className, that.className) 355 && Util.equals(this.extracted, that.extracted); 356 } 357 358 return false; 359 } 360 361 366 public String [] extractObjectClasses() { 367 extracted = true; 368 return new String [] { className }; 369 } 370 371 376 public final int hashCode() { 377 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 378 expressionHashCode = HASH_INITIAL * HASH_FACTOR 379 + Util.hashCode(className); 380 expressionHashCode = expressionHashCode * HASH_FACTOR + Util.hashCode(extracted); 381 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 382 expressionHashCode++; 383 } 384 } 385 return expressionHashCode; 386 } 387 388 391 public boolean isEnabledFor(Object object) { 392 if (object == null) { 393 return false; 394 } 395 if (extracted) { 396 return true; 397 } 398 399 Class clazz = object.getClass(); 400 while (clazz != null) { 401 if (clazz.getName().equals(className)) { 403 return true; 404 } 405 406 Class [] interfaces = clazz.getInterfaces(); 408 for (int i = 0; i < interfaces.length; i++) { 409 if (checkInterfaceHierarchy(interfaces[i])) { 410 return true; 411 } 412 } 413 414 clazz = clazz.getSuperclass(); 416 } 417 418 return false; 419 } 420 421 424 public boolean isEnabledForExpression(Object object, 425 String expressionType) { 426 if (expressionType.equals(EXP_TYPE_OBJECT_CLASS)) { 427 return isEnabledFor(object); 428 } 429 return false; 430 } 431 } 432 433 private static class ObjectStateExpression extends AbstractExpression { 434 private String name; 435 436 private String value; 437 438 448 public ObjectStateExpression(IConfigurationElement element) 449 throws IllegalStateException { 450 super(); 451 452 name = element.getAttribute(ATT_NAME); 453 value = element.getAttribute(ATT_VALUE); 454 if (name == null || value == null) { 455 throw new IllegalStateException ( 456 "Object state expression missing attribute"); } 458 } 459 460 public final boolean equals(final Object object) { 461 if (object instanceof ObjectStateExpression) { 462 final ObjectStateExpression that = (ObjectStateExpression) object; 463 return Util.equals(this.name, that.name) 464 && Util.equals(this.value, that.value); 465 } 466 467 return false; 468 } 469 470 private IActionFilter getActionFilter(Object object) { 471 return (IActionFilter)Util.getAdapter(object, IActionFilter.class); 472 } 473 474 479 public final int hashCode() { 480 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 481 expressionHashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(name); 482 expressionHashCode = expressionHashCode * HASH_FACTOR + Util.hashCode(value); 483 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 484 expressionHashCode++; 485 } 486 } 487 return expressionHashCode; 488 } 489 490 493 public boolean isEnabledFor(Object object) { 494 if (object == null) { 495 return false; 496 } 497 498 if (preciselyMatches(object)) { 500 return true; 501 } 502 503 Class resourceClass = LegacyResourceSupport.getResourceClass(); 505 if (resourceClass == null) { 506 return false; 507 } 508 509 if (resourceClass.isInstance(object)) { 510 return false; 511 } 512 513 Object res = Util.getAdapter(object, resourceClass); 514 if (res == null) { 515 return false; 516 } 517 518 return preciselyMatches(res); 519 520 } 521 522 private boolean preciselyMatches(Object object) { 523 IActionFilter filter = getActionFilter(object); 525 if (filter == null) { 526 return false; 527 } 528 529 return filter.testAttribute(object, name, value); 531 } 532 533 538 public Collection valuesForExpression(String expressionType) { 539 if (expressionType.equals(name)) { 540 Collection returnValue = new HashSet (); 541 returnValue.add(value); 542 return returnValue; 543 } 544 return null; 545 } 546 547 } 548 549 private static class OrExpression extends CompositeExpression { 550 551 561 public OrExpression(IConfigurationElement element) 562 throws IllegalStateException { 563 super(element); 564 } 565 566 public final boolean equals(final Object object) { 567 if (object instanceof OrExpression) { 568 final OrExpression that = (OrExpression) object; 569 return Util.equals(this.list, that.list); 570 } 571 572 return false; 573 } 574 575 578 public boolean isEnabledFor(Object object) { 579 Iterator iter = list.iterator(); 580 while (iter.hasNext()) { 581 AbstractExpression expr = (AbstractExpression) iter.next(); 582 if (expr.isEnabledFor(object)) { 583 return true; 584 } 585 } 586 return false; 587 } 588 } 589 590 private static class PluginStateExpression extends AbstractExpression { 591 private String id; 592 593 private String value; 594 595 605 public PluginStateExpression(IConfigurationElement element) 606 throws IllegalStateException { 607 super(); 608 609 id = element.getAttribute(ATT_ID); 610 value = element.getAttribute(ATT_VALUE); 611 if (id == null || value == null) { 612 throw new IllegalStateException ( 613 "Plugin state expression missing attribute"); } 615 } 616 617 public final boolean equals(final Object object) { 618 if (object instanceof PluginStateExpression) { 619 final PluginStateExpression that = (PluginStateExpression) object; 620 return Util.equals(this.id, that.id) 621 && Util.equals(this.value, that.value); 622 } 623 624 return false; 625 } 626 627 632 public final int hashCode() { 633 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 634 expressionHashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(id); 635 expressionHashCode = expressionHashCode * HASH_FACTOR + Util.hashCode(value); 636 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 637 expressionHashCode++; 638 } 639 } 640 return expressionHashCode; 641 } 642 643 646 public boolean isEnabledFor(Object object) { 647 Bundle bundle = Platform.getBundle(id); 648 if (!BundleUtility.isReady(bundle)) { 649 return false; 650 } 651 if (value.equals(PLUGIN_INSTALLED)) { 652 return true; 653 } 654 if (value.equals(PLUGIN_ACTIVATED)) { 655 return BundleUtility.isActivated(bundle); 656 } 657 return false; 658 } 659 } 660 661 private static class SingleExpression extends AbstractExpression { 662 private AbstractExpression child; 663 664 673 public SingleExpression(AbstractExpression expression) 674 throws IllegalStateException { 675 super(); 676 677 if (expression != null) { 678 child = expression; 679 } else { 680 throw new IllegalStateException ( 681 "Single expression must contain 1 expression"); } 683 } 684 685 694 public SingleExpression(IConfigurationElement element) 695 throws IllegalStateException { 696 super(); 697 698 IConfigurationElement[] children = element.getChildren(); 699 if (children.length != 1) { 700 throw new IllegalStateException ( 701 "Single expression does not contain only 1 expression"); } 703 child = createExpression(children[0]); 704 } 705 706 public final boolean equals(final Object object) { 707 if (object instanceof SingleExpression) { 708 final SingleExpression that = (SingleExpression) object; 709 return Util.equals(this.child, that.child); 710 } 711 712 return false; 713 } 714 715 720 public String [] extractObjectClasses() { 721 return child.extractObjectClasses(); 722 } 723 724 729 public final int hashCode() { 730 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 731 expressionHashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(child); 732 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 733 expressionHashCode++; 734 } 735 } 736 return expressionHashCode; 737 } 738 739 742 public boolean isEnabledFor(Object object) { 743 return child.isEnabledFor(object); 744 } 745 746 749 public boolean isEnabledForExpression(Object object, 750 String expressionType) { 751 return child.isEnabledForExpression(object, expressionType); 752 } 753 754 759 public Collection valuesForExpression(String expressionType) { 760 return child.valuesForExpression(expressionType); 761 } 762 763 } 764 765 private static class SystemPropertyExpression extends AbstractExpression { 766 private String name; 767 768 private String value; 769 770 780 public SystemPropertyExpression(IConfigurationElement element) 781 throws IllegalStateException { 782 super(); 783 784 name = element.getAttribute(ATT_NAME); 785 value = element.getAttribute(ATT_VALUE); 786 if (name == null || value == null) { 787 throw new IllegalStateException ( 788 "System property expression missing attribute"); } 790 } 791 792 795 public boolean isEnabledFor(Object object) { 796 String str = System.getProperty(name); 797 if (str == null) { 798 return false; 799 } 800 return value.equals(str); 801 } 802 803 public final boolean equals(final Object object) { 804 if (object instanceof SystemPropertyExpression) { 805 final SystemPropertyExpression that = (SystemPropertyExpression) object; 806 return Util.equals(this.name, that.name) 807 && Util.equals(this.value, that.value); 808 } 809 810 return false; 811 } 812 813 818 public final int hashCode() { 819 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 820 expressionHashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(name); 821 expressionHashCode = expressionHashCode * HASH_FACTOR + Util.hashCode(value); 822 if (expressionHashCode == HASH_CODE_NOT_COMPUTED) { 823 expressionHashCode++; 824 } 825 } 826 return expressionHashCode; 827 } 828 } 829 830 private static final String ATT_ID = "id"; 832 private static final String ATT_NAME = "name"; 834 private static final String ATT_VALUE = "value"; 836 840 public static final String EXP_TYPE_AND = "and"; 842 846 public static final String EXP_TYPE_NOT = "not"; 848 852 public static final String EXP_TYPE_OBJECT_CLASS = "objectClass"; 854 858 public static final String EXP_TYPE_OBJECT_STATE = "objectState"; 860 864 public static final String EXP_TYPE_OR = "or"; 866 870 public static final String EXP_TYPE_PLUG_IN_STATE = "pluginState"; 872 876 public static final String EXP_TYPE_SYSTEM_PROPERTY = "systemProperty"; 878 882 private static final int HASH_CODE_NOT_COMPUTED = -1; 883 884 887 private static final int HASH_FACTOR = 89; 888 889 892 private static final int HASH_INITIAL = ActionExpression.class.getName() 893 .hashCode(); 894 895 private static final String PLUGIN_ACTIVATED = "activated"; 897 private static final String PLUGIN_INSTALLED = "installed"; 899 910 private static AbstractExpression createExpression( 911 IConfigurationElement element) throws IllegalStateException { 912 String tag = element.getName(); 913 if (tag.equals(EXP_TYPE_OR)) { 914 return new OrExpression(element); 915 } 916 if (tag.equals(EXP_TYPE_AND)) { 917 return new AndExpression(element); 918 } 919 if (tag.equals(EXP_TYPE_NOT)) { 920 return new NotExpression(element); 921 } 922 if (tag.equals(EXP_TYPE_OBJECT_STATE)) { 923 return new ObjectStateExpression(element); 924 } 925 if (tag.equals(EXP_TYPE_OBJECT_CLASS)) { 926 return new ObjectClassExpression(element); 927 } 928 if (tag.equals(EXP_TYPE_PLUG_IN_STATE)) { 929 return new PluginStateExpression(element); 930 } 931 if (tag.equals(EXP_TYPE_SYSTEM_PROPERTY)) { 932 return new SystemPropertyExpression(element); 933 } 934 935 throw new IllegalStateException ( 936 "Action expression unrecognized element: " + tag); } 938 939 943 private transient int hashCode = HASH_CODE_NOT_COMPUTED; 944 945 private SingleExpression root; 946 947 953 public ActionExpression(IConfigurationElement element) { 954 try { 955 root = new SingleExpression(element); 956 } catch (IllegalStateException e) { 957 e.printStackTrace(); 958 root = null; 959 } 960 } 961 962 972 public ActionExpression(String expressionType, String expressionValue) { 973 if (expressionType.equals(EXP_TYPE_OBJECT_CLASS)) { 974 root = new SingleExpression(new ObjectClassExpression( 975 expressionValue)); 976 } 977 } 978 979 public final boolean equals(final Object object) { 980 if (object instanceof ActionExpression) { 981 final ActionExpression that = (ActionExpression) object; 982 return Util.equals(this.root, that.root); 983 } 984 985 return false; 986 } 987 988 997 public String [] extractObjectClasses() { 998 return root.extractObjectClasses(); 999 } 1000 1001 1006 public final int hashCode() { 1007 if (hashCode == HASH_CODE_NOT_COMPUTED) { 1008 hashCode = HASH_INITIAL * HASH_FACTOR + Util.hashCode(root); 1009 if (hashCode == HASH_CODE_NOT_COMPUTED) { 1010 hashCode++; 1011 } 1012 } 1013 return hashCode; 1014 } 1015 1016 1024 public boolean isEnabledFor(IStructuredSelection selection) { 1025 if (root == null) { 1026 return false; 1027 } 1028 1029 if (selection == null || selection.isEmpty()) { 1030 return root.isEnabledFor(null); 1031 } 1032 1033 Iterator elements = selection.iterator(); 1034 while (elements.hasNext()) { 1035 if (!isEnabledFor(elements.next())) { 1036 return false; 1037 } 1038 } 1039 return true; 1040 } 1041 1042 1049 public boolean isEnabledFor(Object object) { 1050 if (root == null) { 1051 return false; 1052 } 1053 return root.isEnabledFor(object); 1054 } 1055 1056 1068 public boolean isEnabledForExpression(Object object, String expressionType) { 1069 if (root == null) { 1070 return false; 1071 } 1072 return root.isEnabledForExpression(object, expressionType); 1073 } 1074 1075 1086 public Collection valuesForExpression(String expressionType) { 1087 return root.valuesForExpression(expressionType); 1088 } 1089} 1090 | Popular Tags |