1 11 12 package org.eclipse.jdt.internal.ui.javaeditor; 13 14 import org.eclipse.swt.graphics.RGB; 15 16 import org.eclipse.jface.preference.IPreferenceStore; 17 import org.eclipse.jface.preference.PreferenceConverter; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 20 import org.eclipse.jdt.core.dom.ASTNode; 21 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; 22 import org.eclipse.jdt.core.dom.ArrayAccess; 23 import org.eclipse.jdt.core.dom.CastExpression; 24 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 25 import org.eclipse.jdt.core.dom.ConditionalExpression; 26 import org.eclipse.jdt.core.dom.Expression; 27 import org.eclipse.jdt.core.dom.FieldDeclaration; 28 import org.eclipse.jdt.core.dom.IBinding; 29 import org.eclipse.jdt.core.dom.IMethodBinding; 30 import org.eclipse.jdt.core.dom.ITypeBinding; 31 import org.eclipse.jdt.core.dom.IVariableBinding; 32 import org.eclipse.jdt.core.dom.InfixExpression; 33 import org.eclipse.jdt.core.dom.MemberValuePair; 34 import org.eclipse.jdt.core.dom.MethodDeclaration; 35 import org.eclipse.jdt.core.dom.Modifier; 36 import org.eclipse.jdt.core.dom.ParameterizedType; 37 import org.eclipse.jdt.core.dom.PrefixExpression; 38 import org.eclipse.jdt.core.dom.QualifiedName; 39 import org.eclipse.jdt.core.dom.QualifiedType; 40 import org.eclipse.jdt.core.dom.SimpleName; 41 import org.eclipse.jdt.core.dom.SimpleType; 42 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 43 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 44 import org.eclipse.jdt.core.dom.Type; 45 import org.eclipse.jdt.core.dom.VariableDeclaration; 46 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 47 48 import org.eclipse.jdt.internal.corext.dom.Bindings; 49 50 import org.eclipse.jdt.ui.PreferenceConstants; 51 52 53 58 public class SemanticHighlightings { 59 60 63 public static final String STATIC_FINAL_FIELD="staticFinalField"; 65 68 public static final String STATIC_FIELD="staticField"; 70 73 public static final String FIELD="field"; 75 78 public static final String METHOD_DECLARATION="methodDeclarationName"; 80 83 public static final String STATIC_METHOD_INVOCATION="staticMethodInvocation"; 85 88 public static final String INHERITED_METHOD_INVOCATION="inheritedMethodInvocation"; 90 94 public static final String ANNOTATION_ELEMENT_REFERENCE="annotationElementReference"; 96 99 public static final String ABSTRACT_METHOD_INVOCATION="abstractMethodInvocation"; 101 104 public static final String LOCAL_VARIABLE_DECLARATION="localVariableDeclaration"; 106 109 public static final String LOCAL_VARIABLE="localVariable"; 111 114 public static final String PARAMETER_VARIABLE="parameterVariable"; 116 119 public static final String DEPRECATED_MEMBER="deprecatedMember"; 121 125 public static final String TYPE_VARIABLE="typeParameter"; 127 133 public static final String METHOD="method"; 135 141 public static final String AUTOBOXING="autoboxing"; 143 148 public static final String CLASS="class"; 150 155 public static final String ENUM="enum"; 157 162 public static final String INTERFACE="interface"; 164 169 public static final String ANNOTATION="annotation"; 171 176 public static final String TYPE_ARGUMENT="typeArgument"; 178 181 private static SemanticHighlighting[] fgSemanticHighlightings; 182 183 186 private static final class StaticFinalFieldHighlighting extends SemanticHighlighting { 187 188 191 public String getPreferenceKey() { 192 return STATIC_FINAL_FIELD; 193 } 194 195 198 public RGB getDefaultDefaultTextColor() { 199 return new RGB(0, 0, 0); 200 } 201 202 205 public boolean isBoldByDefault() { 206 return false; 207 } 208 209 212 public boolean isItalicByDefault() { 213 return false; 214 } 215 216 219 public boolean isEnabledByDefault() { 220 return false; 221 } 222 223 226 public String getDisplayName() { 227 return JavaEditorMessages.SemanticHighlighting_staticFinalField; 228 } 229 230 233 public boolean consumes(SemanticToken token) { 234 IBinding binding= token.getBinding(); 235 return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField() && (binding.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) == (Modifier.FINAL | Modifier.STATIC); 236 } 237 } 238 239 242 private static final class StaticFieldHighlighting extends SemanticHighlighting { 243 244 247 public String getPreferenceKey() { 248 return STATIC_FIELD; 249 } 250 251 254 public RGB getDefaultDefaultTextColor() { 255 return new RGB(0, 0, 192); 256 } 257 258 261 public boolean isBoldByDefault() { 262 return false; 263 } 264 265 268 public boolean isItalicByDefault() { 269 return true; 270 } 271 272 275 public boolean isEnabledByDefault() { 276 return true; 277 } 278 279 282 public String getDisplayName() { 283 return JavaEditorMessages.SemanticHighlighting_staticField; 284 } 285 286 289 public boolean consumes(SemanticToken token) { 290 IBinding binding= token.getBinding(); 291 return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField() && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC; 292 } 293 } 294 295 298 private static final class FieldHighlighting extends SemanticHighlighting { 299 300 303 public String getPreferenceKey() { 304 return FIELD; 305 } 306 307 310 public RGB getDefaultDefaultTextColor() { 311 return new RGB(0, 0, 192); 312 } 313 314 317 public boolean isBoldByDefault() { 318 return false; 319 } 320 321 324 public boolean isItalicByDefault() { 325 return false; 326 } 327 328 331 public boolean isEnabledByDefault() { 332 return true; 333 } 334 335 338 public String getDisplayName() { 339 return JavaEditorMessages.SemanticHighlighting_field; 340 } 341 342 345 public boolean consumes(SemanticToken token) { 346 IBinding binding= token.getBinding(); 347 return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding)binding).isField(); 348 } 349 } 350 351 354 private static final class AutoboxHighlighting extends SemanticHighlighting { 355 356 359 public String getPreferenceKey() { 360 return AUTOBOXING; 361 } 362 363 366 public RGB getDefaultDefaultTextColor() { 367 return new RGB(171, 48, 0); 368 } 369 370 373 public boolean isBoldByDefault() { 374 return false; 375 } 376 377 380 public boolean isItalicByDefault() { 381 return false; 382 } 383 384 387 public boolean isEnabledByDefault() { 388 return false; 389 } 390 391 394 public String getDisplayName() { 395 return JavaEditorMessages.SemanticHighlighting_autoboxing; 396 } 397 398 401 public boolean consumesLiteral(SemanticToken token) { 402 return isAutoUnBoxing(token.getLiteral()); 403 } 404 405 408 public boolean consumes(SemanticToken token) { 409 return isAutoUnBoxing(token.getNode()); 410 } 411 412 private boolean isAutoUnBoxing(Expression node) { 413 if (isAutoUnBoxingExpression(node)) 414 return true; 415 StructuralPropertyDescriptor desc= node.getLocationInParent(); 420 if (desc == ArrayAccess.ARRAY_PROPERTY 421 || desc == InfixExpression.LEFT_OPERAND_PROPERTY 422 || desc == InfixExpression.RIGHT_OPERAND_PROPERTY 423 || desc == ConditionalExpression.THEN_EXPRESSION_PROPERTY 424 || desc == PrefixExpression.OPERAND_PROPERTY 425 || desc == CastExpression.EXPRESSION_PROPERTY 426 || desc == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) { 427 ASTNode parent= node.getParent(); 428 if (parent instanceof Expression) 429 return isAutoUnBoxingExpression((Expression) parent); 430 } 431 if (desc == SimpleType.NAME_PROPERTY || desc == QualifiedType.NAME_PROPERTY) { 433 ASTNode parent= node.getParent(); 434 if (parent != null && parent.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { 435 parent= parent.getParent(); 436 if (parent instanceof Expression) 437 return isAutoUnBoxingExpression((Expression) parent); 438 } 439 } 440 return false; 441 } 442 443 private boolean isAutoUnBoxingExpression(Expression expression) { 444 return expression.resolveBoxing() || expression.resolveUnboxing(); 445 } 446 } 447 448 451 private static final class MethodDeclarationHighlighting extends SemanticHighlighting { 452 453 456 public String getPreferenceKey() { 457 return METHOD_DECLARATION; 458 } 459 460 463 public RGB getDefaultDefaultTextColor() { 464 return new RGB(0, 0, 0); 465 } 466 467 470 public boolean isBoldByDefault() { 471 return true; 472 } 473 474 477 public boolean isItalicByDefault() { 478 return false; 479 } 480 481 484 public boolean isEnabledByDefault() { 485 return false; 486 } 487 488 491 public String getDisplayName() { 492 return JavaEditorMessages.SemanticHighlighting_methodDeclaration; 493 } 494 495 498 public boolean consumes(SemanticToken token) { 499 StructuralPropertyDescriptor location= token.getNode().getLocationInParent(); 500 return location == MethodDeclaration.NAME_PROPERTY || location == AnnotationTypeMemberDeclaration.NAME_PROPERTY; 501 } 502 } 503 504 507 private static final class StaticMethodInvocationHighlighting extends SemanticHighlighting { 508 509 512 public String getPreferenceKey() { 513 return STATIC_METHOD_INVOCATION; 514 } 515 516 519 public RGB getDefaultDefaultTextColor() { 520 return new RGB(0, 0, 0); 521 } 522 523 526 public boolean isBoldByDefault() { 527 return false; 528 } 529 530 533 public boolean isItalicByDefault() { 534 return true; 535 } 536 537 540 public boolean isEnabledByDefault() { 541 return true; 542 } 543 544 547 public String getDisplayName() { 548 return JavaEditorMessages.SemanticHighlighting_staticMethodInvocation; 549 } 550 551 554 public boolean consumes(SemanticToken token) { 555 SimpleName node= token.getNode(); 556 if (node.isDeclaration()) 557 return false; 558 559 IBinding binding= token.getBinding(); 560 return binding != null && binding.getKind() == IBinding.METHOD && (binding.getModifiers() & Modifier.STATIC) == Modifier.STATIC; 561 } 562 } 563 564 568 private static final class AnnotationElementReferenceHighlighting extends SemanticHighlighting { 569 570 573 public String getPreferenceKey() { 574 return ANNOTATION_ELEMENT_REFERENCE; 575 } 576 577 580 public RGB getDefaultDefaultTextColor() { 581 return new RGB(0, 0, 0); 582 } 583 584 587 public boolean isBoldByDefault() { 588 return false; 589 } 590 591 594 public boolean isItalicByDefault() { 595 return false; 596 } 597 598 601 public boolean isEnabledByDefault() { 602 return false; 603 } 604 605 608 public String getDisplayName() { 609 return JavaEditorMessages.SemanticHighlighting_annotationElementReference; 610 } 611 612 615 public boolean consumes(SemanticToken token) { 616 SimpleName node= token.getNode(); 617 if (node.getParent() instanceof MemberValuePair) { 618 IBinding binding= token.getBinding(); 619 boolean isAnnotationElement= binding != null && binding.getKind() == IBinding.METHOD; 620 621 return isAnnotationElement; 622 } 623 624 return false; 625 } 626 } 627 628 631 private static final class AbstractMethodInvocationHighlighting extends SemanticHighlighting { 632 633 636 public String getPreferenceKey() { 637 return ABSTRACT_METHOD_INVOCATION; 638 } 639 640 643 public RGB getDefaultDefaultTextColor() { 644 return new RGB(0, 0, 0); 645 } 646 647 650 public boolean isBoldByDefault() { 651 return false; 652 } 653 654 657 public boolean isItalicByDefault() { 658 return false; 659 } 660 661 664 public boolean isEnabledByDefault() { 665 return false; 666 } 667 668 671 public String getDisplayName() { 672 return JavaEditorMessages.SemanticHighlighting_abstractMethodInvocation; 673 } 674 675 678 public boolean consumes(SemanticToken token) { 679 SimpleName node= token.getNode(); 680 if (node.isDeclaration()) 681 return false; 682 683 IBinding binding= token.getBinding(); 684 boolean isAbstractMethod= binding != null && binding.getKind() == IBinding.METHOD && (binding.getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT; 685 if (!isAbstractMethod) 686 return false; 687 688 if (binding != null) { 690 ITypeBinding declaringType= ((IMethodBinding)binding).getDeclaringClass(); 691 if (declaringType.isAnnotation()) 692 return false; 693 } 694 695 return true; 696 } 697 } 698 699 702 private static final class InheritedMethodInvocationHighlighting extends SemanticHighlighting { 703 704 707 public String getPreferenceKey() { 708 return INHERITED_METHOD_INVOCATION; 709 } 710 711 714 public RGB getDefaultDefaultTextColor() { 715 return new RGB(0, 0, 0); 716 } 717 718 721 public boolean isBoldByDefault() { 722 return false; 723 } 724 725 728 public boolean isItalicByDefault() { 729 return false; 730 } 731 732 735 public boolean isEnabledByDefault() { 736 return false; 737 } 738 739 742 public String getDisplayName() { 743 return JavaEditorMessages.SemanticHighlighting_inheritedMethodInvocation; 744 } 745 746 749 public boolean consumes(SemanticToken token) { 750 SimpleName node= token.getNode(); 751 if (node.isDeclaration()) 752 return false; 753 754 IBinding binding= token.getBinding(); 755 if (binding == null || binding.getKind() != IBinding.METHOD) 756 return false; 757 758 ITypeBinding currentType= Bindings.getBindingOfParentType(node); 759 ITypeBinding declaringType= ((IMethodBinding) binding).getDeclaringClass(); 760 if (currentType == declaringType || currentType == null) 761 return false; 762 763 return Bindings.isSuperType(declaringType, currentType); 764 } 765 } 766 767 770 private static final class MethodHighlighting extends SemanticHighlighting { 771 772 775 public String getPreferenceKey() { 776 return METHOD; 777 } 778 779 782 public RGB getDefaultDefaultTextColor() { 783 return new RGB(0, 0, 0); 784 } 785 786 789 public boolean isBoldByDefault() { 790 return false; 791 } 792 793 796 public boolean isItalicByDefault() { 797 return false; 798 } 799 800 803 public boolean isEnabledByDefault() { 804 return false; 805 } 806 807 810 public String getDisplayName() { 811 return JavaEditorMessages.SemanticHighlighting_method; 812 } 813 814 817 public boolean consumes(SemanticToken token) { 818 IBinding binding= getMethodBinding(token); 819 return binding != null && binding.getKind() == IBinding.METHOD; 820 } 821 822 832 private IBinding getMethodBinding(SemanticToken token) { 833 IBinding binding= null; 834 ASTNode node= token.getNode(); 836 ASTNode parent= node.getParent(); 837 while (isTypePath(node, parent)) { 838 node= parent; 839 parent= parent.getParent(); 840 } 841 842 if (parent != null && node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) 843 binding= ((ClassInstanceCreation) parent).resolveConstructorBinding(); 844 else 845 binding= token.getBinding(); 846 return binding; 847 } 848 849 856 private boolean isTypePath(ASTNode child, ASTNode parent) { 857 if (parent instanceof Type) { 858 StructuralPropertyDescriptor location= child.getLocationInParent(); 859 return location == ParameterizedType.TYPE_PROPERTY || location == SimpleType.NAME_PROPERTY; 860 } else if (parent instanceof QualifiedName) { 861 StructuralPropertyDescriptor location= child.getLocationInParent(); 862 return location == QualifiedName.NAME_PROPERTY; 863 } 864 return false; 865 } 866 } 867 868 871 private static final class LocalVariableDeclarationHighlighting extends SemanticHighlighting { 872 873 876 public String getPreferenceKey() { 877 return LOCAL_VARIABLE_DECLARATION; 878 } 879 880 883 public RGB getDefaultDefaultTextColor() { 884 return new RGB(0, 0, 0); 885 } 886 887 890 public boolean isBoldByDefault() { 891 return false; 892 } 893 894 897 public boolean isItalicByDefault() { 898 return false; 899 } 900 901 904 public boolean isEnabledByDefault() { 905 return false; 906 } 907 908 911 public String getDisplayName() { 912 return JavaEditorMessages.SemanticHighlighting_localVariableDeclaration; 913 } 914 915 918 public boolean consumes(SemanticToken token) { 919 SimpleName node= token.getNode(); 920 StructuralPropertyDescriptor location= node.getLocationInParent(); 921 if (location == VariableDeclarationFragment.NAME_PROPERTY || location == SingleVariableDeclaration.NAME_PROPERTY) { 922 ASTNode parent= node.getParent(); 923 if (parent instanceof VariableDeclaration) { 924 parent= parent.getParent(); 925 return parent == null || !(parent instanceof FieldDeclaration); 926 } 927 } 928 return false; 929 } 930 } 931 932 935 private static final class LocalVariableHighlighting extends SemanticHighlighting { 936 937 940 public String getPreferenceKey() { 941 return LOCAL_VARIABLE; 942 } 943 944 947 public RGB getDefaultDefaultTextColor() { 948 return new RGB(0, 0, 0); 949 } 950 951 954 public boolean isBoldByDefault() { 955 return false; 956 } 957 958 961 public boolean isItalicByDefault() { 962 return false; 963 } 964 965 968 public boolean isEnabledByDefault() { 969 return false; 970 } 971 972 975 public String getDisplayName() { 976 return JavaEditorMessages.SemanticHighlighting_localVariable; 977 } 978 979 982 public boolean consumes(SemanticToken token) { 983 IBinding binding= token.getBinding(); 984 if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) { 985 ASTNode decl= token.getRoot().findDeclaringNode(binding); 986 return decl instanceof VariableDeclaration; 987 } 988 return false; 989 } 990 } 991 992 995 private static final class ParameterVariableHighlighting extends SemanticHighlighting { 996 997 1000 public String getPreferenceKey() { 1001 return PARAMETER_VARIABLE; 1002 } 1003 1004 1007 public RGB getDefaultDefaultTextColor() { 1008 return new RGB(0, 0, 0); 1009 } 1010 1011 1014 public boolean isBoldByDefault() { 1015 return false; 1016 } 1017 1018 1021 public boolean isItalicByDefault() { 1022 return false; 1023 } 1024 1025 1028 public boolean isEnabledByDefault() { 1029 return false; 1030 } 1031 1032 1035 public String getDisplayName() { 1036 return JavaEditorMessages.SemanticHighlighting_parameterVariable; 1037 } 1038 1039 1042 public boolean consumes(SemanticToken token) { 1043 IBinding binding= token.getBinding(); 1044 if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) { 1045 ASTNode decl= token.getRoot().findDeclaringNode(binding); 1046 return decl != null && decl.getLocationInParent() == MethodDeclaration.PARAMETERS_PROPERTY; 1047 } 1048 return false; 1049 } 1050 } 1051 1052 1055 private static final class DeprecatedMemberHighlighting extends SemanticHighlighting { 1056 1057 1060 public String getPreferenceKey() { 1061 return DEPRECATED_MEMBER; 1062 } 1063 1064 1067 public RGB getDefaultDefaultTextColor() { 1068 return new RGB(0, 0, 0); 1069 } 1070 1071 1074 public boolean isBoldByDefault() { 1075 return false; 1076 } 1077 1078 1081 public boolean isItalicByDefault() { 1082 return false; 1083 } 1084 1085 1089 public boolean isStrikethroughByDefault() { 1090 return true; 1091 } 1092 1093 1096 public boolean isEnabledByDefault() { 1097 return true; 1098 } 1099 1100 1103 public String getDisplayName() { 1104 return JavaEditorMessages.SemanticHighlighting_deprecatedMember; 1105 } 1106 1107 1110 public boolean consumes(SemanticToken token) { 1111 IBinding binding= getMethodBinding(token); 1112 return binding != null ? binding.isDeprecated() : false; 1113 } 1114 1115 1125 private IBinding getMethodBinding(SemanticToken token) { 1126 IBinding binding= null; 1127 ASTNode node= token.getNode(); 1129 ASTNode parent= node.getParent(); 1130 while (isTypePath(node, parent)) { 1131 node= parent; 1132 parent= parent.getParent(); 1133 } 1134 1135 if (parent != null && node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) 1136 binding= ((ClassInstanceCreation) parent).resolveConstructorBinding(); 1137 else 1138 binding= token.getBinding(); 1139 return binding; 1140 } 1141 1142 1149 private boolean isTypePath(ASTNode child, ASTNode parent) { 1150 if (parent instanceof Type) { 1151 StructuralPropertyDescriptor location= child.getLocationInParent(); 1152 return location == ParameterizedType.TYPE_PROPERTY || location == SimpleType.NAME_PROPERTY; 1153 } else if (parent instanceof QualifiedName) { 1154 StructuralPropertyDescriptor location= child.getLocationInParent(); 1155 return location == QualifiedName.NAME_PROPERTY; 1156 } 1157 return false; 1158 } 1159 } 1160 1161 1165 private static final class TypeVariableHighlighting extends SemanticHighlighting { 1166 1167 1170 public String getPreferenceKey() { 1171 return TYPE_VARIABLE; 1172 } 1173 1174 1177 public RGB getDefaultDefaultTextColor() { 1178 return new RGB(100, 70, 50); 1179 } 1180 1181 1184 public boolean isBoldByDefault() { 1185 return true; 1186 } 1187 1188 1191 public boolean isItalicByDefault() { 1192 return false; 1193 } 1194 1195 1198 public boolean isEnabledByDefault() { 1199 return false; 1200 } 1201 1202 1205 public String getDisplayName() { 1206 return JavaEditorMessages.SemanticHighlighting_typeVariables; 1207 } 1208 1209 1212 public boolean consumes(SemanticToken token) { 1213 1214 SimpleName name= token.getNode(); 1216 ASTNode node= name.getParent(); 1217 if (node.getNodeType() != ASTNode.SIMPLE_TYPE && node.getNodeType() != ASTNode.TYPE_PARAMETER) 1218 return false; 1219 1220 IBinding binding= token.getBinding(); 1222 return binding instanceof ITypeBinding && ((ITypeBinding) binding).isTypeVariable(); 1223 } 1224 } 1225 1226 1230 private static final class ClassHighlighting extends SemanticHighlighting { 1231 1232 1235 public String getPreferenceKey() { 1236 return CLASS; 1237 } 1238 1239 1242 public RGB getDefaultDefaultTextColor() { 1243 return new RGB(0, 80, 50); 1244 } 1245 1246 1249 public boolean isBoldByDefault() { 1250 return false; 1251 } 1252 1253 1256 public boolean isItalicByDefault() { 1257 return false; 1258 } 1259 1260 1263 public boolean isEnabledByDefault() { 1264 return false; 1265 } 1266 1267 1270 public String getDisplayName() { 1271 return JavaEditorMessages.SemanticHighlighting_classes; 1272 } 1273 1274 1277 public boolean consumes(SemanticToken token) { 1278 1279 SimpleName name= token.getNode(); 1281 ASTNode node= name.getParent(); 1282 int nodeType= node.getNodeType(); 1283 if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.THIS_EXPRESSION && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION) 1284 return false; 1285 while (nodeType == ASTNode.QUALIFIED_NAME) { 1286 node= node.getParent(); 1287 nodeType= node.getNodeType(); 1288 if (nodeType == ASTNode.IMPORT_DECLARATION) 1289 return false; 1290 } 1291 1292 IBinding binding= token.getBinding(); 1294 return binding instanceof ITypeBinding && ((ITypeBinding) binding).isClass(); 1295 } 1296 } 1297 1298 1302 private static final class EnumHighlighting extends SemanticHighlighting { 1303 1304 1307 public String getPreferenceKey() { 1308 return ENUM; 1309 } 1310 1311 1314 public RGB getDefaultDefaultTextColor() { 1315 return new RGB(100, 70, 50); 1316 } 1317 1318 1321 public boolean isBoldByDefault() { 1322 return false; 1323 } 1324 1325 1328 public boolean isItalicByDefault() { 1329 return false; 1330 } 1331 1332 1335 public boolean isEnabledByDefault() { 1336 return false; 1337 } 1338 1339 1342 public String getDisplayName() { 1343 return JavaEditorMessages.SemanticHighlighting_enums; 1344 } 1345 1346 1349 public boolean consumes(SemanticToken token) { 1350 1351 SimpleName name= token.getNode(); 1353 ASTNode node= name.getParent(); 1354 int nodeType= node.getNodeType(); 1355 if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.ENUM_DECLARATION) 1356 return false; 1357 while (nodeType == ASTNode.QUALIFIED_NAME) { 1358 node= node.getParent(); 1359 nodeType= node.getNodeType(); 1360 if (nodeType == ASTNode.IMPORT_DECLARATION) 1361 return false; 1362 } 1363 1364 IBinding binding= token.getBinding(); 1366 return binding instanceof ITypeBinding && ((ITypeBinding) binding).isEnum(); 1367 } 1368 } 1369 1370 1374 private static final class InterfaceHighlighting extends SemanticHighlighting { 1375 1376 1379 public String getPreferenceKey() { 1380 return INTERFACE; 1381 } 1382 1383 1386 public RGB getDefaultDefaultTextColor() { 1387 return new RGB(50, 63, 112); 1388 } 1389 1390 1393 public boolean isBoldByDefault() { 1394 return false; 1395 } 1396 1397 1400 public boolean isItalicByDefault() { 1401 return false; 1402 } 1403 1404 1407 public boolean isEnabledByDefault() { 1408 return false; 1409 } 1410 1411 1414 public String getDisplayName() { 1415 return JavaEditorMessages.SemanticHighlighting_interfaces; 1416 } 1417 1418 1421 public boolean consumes(SemanticToken token) { 1422 1423 SimpleName name= token.getNode(); 1425 ASTNode node= name.getParent(); 1426 int nodeType= node.getNodeType(); 1427 if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION) 1428 return false; 1429 while (nodeType == ASTNode.QUALIFIED_NAME) { 1430 node= node.getParent(); 1431 nodeType= node.getNodeType(); 1432 if (nodeType == ASTNode.IMPORT_DECLARATION) 1433 return false; 1434 } 1435 1436 IBinding binding= token.getBinding(); 1438 return binding instanceof ITypeBinding && ((ITypeBinding) binding).isInterface(); 1439 } 1440 } 1441 1442 1446 private static final class AnnotationHighlighting extends SemanticHighlighting { 1447 1448 1451 public String getPreferenceKey() { 1452 return ANNOTATION; 1453 } 1454 1455 1458 public RGB getDefaultDefaultTextColor() { 1459 return new RGB(100, 100, 100); 1460 } 1461 1462 1465 public boolean isBoldByDefault() { 1466 return false; 1467 } 1468 1469 1472 public boolean isItalicByDefault() { 1473 return false; 1474 } 1475 1476 1479 public boolean isEnabledByDefault() { 1480 return true; } 1482 1483 1486 public String getDisplayName() { 1487 return JavaEditorMessages.SemanticHighlighting_annotations; 1488 } 1489 1490 1493 public boolean consumes(SemanticToken token) { 1494 1495 SimpleName name= token.getNode(); 1497 ASTNode node= name.getParent(); 1498 int nodeType= node.getNodeType(); 1499 if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.ANNOTATION_TYPE_DECLARATION 1500 && nodeType != ASTNode.MARKER_ANNOTATION && nodeType != ASTNode.NORMAL_ANNOTATION && nodeType != ASTNode.SINGLE_MEMBER_ANNOTATION) 1501 return false; 1502 while (nodeType == ASTNode.QUALIFIED_NAME) { 1503 node= node.getParent(); 1504 nodeType= node.getNodeType(); 1505 if (nodeType == ASTNode.IMPORT_DECLARATION) 1506 return false; 1507 } 1508 1509 IBinding binding= token.getBinding(); 1511 return binding instanceof ITypeBinding && ((ITypeBinding) binding).isAnnotation(); 1512 } 1513 } 1514 1515 1519 private static final class TypeArgumentHighlighting extends SemanticHighlighting { 1520 1521 1524 public String getPreferenceKey() { 1525 return TYPE_ARGUMENT; 1526 } 1527 1528 1531 public RGB getDefaultDefaultTextColor() { 1532 return new RGB(13, 100, 0); 1533 } 1534 1535 1538 public boolean isBoldByDefault() { 1539 return false; 1540 } 1541 1542 1545 public boolean isItalicByDefault() { 1546 return false; 1547 } 1548 1549 1552 public boolean isEnabledByDefault() { 1553 return false; 1554 } 1555 1556 1559 public String getDisplayName() { 1560 return JavaEditorMessages.SemanticHighlighting_typeArguments; 1561 } 1562 1563 1566 public boolean consumes(SemanticToken token) { 1567 1568 SimpleName name= token.getNode(); 1570 ASTNode node= name.getParent(); 1571 int nodeType= node.getNodeType(); 1572 if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE) 1573 return false; 1574 1575 StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); 1577 if (locationInParent == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) 1578 return true; 1579 1580 return false; 1581 } 1582 } 1583 1584 1590 public static String getColorPreferenceKey(SemanticHighlighting semanticHighlighting) { 1591 return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + semanticHighlighting.getPreferenceKey() + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_COLOR_SUFFIX; 1592 } 1593 1594 1600 public static String getBoldPreferenceKey(SemanticHighlighting semanticHighlighting) { 1601 return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + semanticHighlighting.getPreferenceKey() + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_BOLD_SUFFIX; 1602 } 1603 1604 1610 public static String getItalicPreferenceKey(SemanticHighlighting semanticHighlighting) { 1611 return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + semanticHighlighting.getPreferenceKey() + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ITALIC_SUFFIX; 1612 } 1613 1614 1621 public static String getStrikethroughPreferenceKey(SemanticHighlighting semanticHighlighting) { 1622 return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + semanticHighlighting.getPreferenceKey() + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_STRIKETHROUGH_SUFFIX; 1623 } 1624 1625 1632 public static String getUnderlinePreferenceKey(SemanticHighlighting semanticHighlighting) { 1633 return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + semanticHighlighting.getPreferenceKey() + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_UNDERLINE_SUFFIX; 1634 } 1635 1636 1642 public static String getEnabledPreferenceKey(SemanticHighlighting semanticHighlighting) { 1643 return PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + semanticHighlighting.getPreferenceKey() + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED_SUFFIX; 1644 } 1645 1646 1649 public static SemanticHighlighting[] getSemanticHighlightings() { 1650 if (fgSemanticHighlightings == null) 1651 fgSemanticHighlightings= new SemanticHighlighting[] { 1652 new DeprecatedMemberHighlighting(), 1653 new AutoboxHighlighting(), 1654 new StaticFinalFieldHighlighting(), 1655 new StaticFieldHighlighting(), 1656 new FieldHighlighting(), 1657 new MethodDeclarationHighlighting(), 1658 new StaticMethodInvocationHighlighting(), 1659 new AbstractMethodInvocationHighlighting(), 1660 new AnnotationElementReferenceHighlighting(), 1661 new InheritedMethodInvocationHighlighting(), 1662 new ParameterVariableHighlighting(), 1663 new LocalVariableDeclarationHighlighting(), 1664 new LocalVariableHighlighting(), 1665 new TypeVariableHighlighting(), new MethodHighlighting(), new TypeArgumentHighlighting(), new ClassHighlighting(), 1669 new EnumHighlighting(), 1670 new AnnotationHighlighting(), new InterfaceHighlighting(), 1672 }; 1673 return fgSemanticHighlightings; 1674 } 1675 1676 1680 public static void initDefaults(IPreferenceStore store) { 1681 SemanticHighlighting[] semanticHighlightings= getSemanticHighlightings(); 1682 for (int i= 0, n= semanticHighlightings.length; i < n; i++) { 1683 SemanticHighlighting semanticHighlighting= semanticHighlightings[i]; 1684 setDefaultAndFireEvent(store, SemanticHighlightings.getColorPreferenceKey(semanticHighlighting), semanticHighlighting.getDefaultTextColor()); 1685 store.setDefault(SemanticHighlightings.getBoldPreferenceKey(semanticHighlighting), semanticHighlighting.isBoldByDefault()); 1686 store.setDefault(SemanticHighlightings.getItalicPreferenceKey(semanticHighlighting), semanticHighlighting.isItalicByDefault()); 1687 store.setDefault(SemanticHighlightings.getStrikethroughPreferenceKey(semanticHighlighting), semanticHighlighting.isStrikethroughByDefault()); 1688 store.setDefault(SemanticHighlightings.getUnderlinePreferenceKey(semanticHighlighting), semanticHighlighting.isUnderlineByDefault()); 1689 store.setDefault(SemanticHighlightings.getEnabledPreferenceKey(semanticHighlighting), semanticHighlighting.isEnabledByDefault()); 1690 } 1691 1692 convertMethodHighlightingPreferences(store); 1693 convertAnnotationHighlightingPreferences(store); 1694 } 1695 1696 1706 public static boolean affectsEnablement(IPreferenceStore store, PropertyChangeEvent event) { 1707 String relevantKey= null; 1708 SemanticHighlighting[] highlightings= getSemanticHighlightings(); 1709 for (int i= 0; i < highlightings.length; i++) { 1710 if (event.getProperty().equals(getEnabledPreferenceKey(highlightings[i]))) { 1711 relevantKey= event.getProperty(); 1712 break; 1713 } 1714 } 1715 if (relevantKey == null) 1716 return false; 1717 1718 for (int i= 0; i < highlightings.length; i++) { 1719 String key= getEnabledPreferenceKey(highlightings[i]); 1720 if (key.equals(relevantKey)) 1721 continue; 1722 if (store.getBoolean(key)) 1723 return false; } 1725 1726 return true; 1728 } 1729 1730 1738 public static boolean isEnabled(IPreferenceStore store) { 1739 SemanticHighlighting[] highlightings= getSemanticHighlightings(); 1740 boolean enable= false; 1741 for (int i= 0; i < highlightings.length; i++) { 1742 String enabledKey= getEnabledPreferenceKey(highlightings[i]); 1743 if (store.getBoolean(enabledKey)) { 1744 enable= true; 1745 break; 1746 } 1747 } 1748 1749 return enable; 1750 } 1751 1752 1767 private static void convertMethodHighlightingPreferences(IPreferenceStore store) { 1768 String colorkey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + METHOD + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_COLOR_SUFFIX; 1769 String boldkey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + METHOD + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_BOLD_SUFFIX; 1770 String italickey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + METHOD + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ITALIC_SUFFIX; 1771 String enabledkey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + METHOD + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED_SUFFIX; 1772 1773 String oldColorkey= PreferenceConstants.EDITOR_JAVA_METHOD_NAME_COLOR; 1774 String oldBoldkey= PreferenceConstants.EDITOR_JAVA_METHOD_NAME_BOLD; 1775 String oldItalickey= PreferenceConstants.EDITOR_JAVA_METHOD_NAME_ITALIC; 1776 1777 if (conditionalReset(store, oldColorkey, colorkey) 1778 || conditionalReset(store, oldBoldkey, boldkey) 1779 || conditionalReset(store, oldItalickey, italickey)) { 1780 store.setValue(enabledkey, true); 1781 } 1782 1783 } 1784 1785 1799 private static void convertAnnotationHighlightingPreferences(IPreferenceStore store) { 1800 String colorkey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + ANNOTATION + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_COLOR_SUFFIX; 1801 String boldkey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + ANNOTATION + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_BOLD_SUFFIX; 1802 String italickey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + ANNOTATION + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ITALIC_SUFFIX; 1803 String strikethroughKey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + ANNOTATION + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_STRIKETHROUGH_SUFFIX; 1804 String underlineKey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + ANNOTATION + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_UNDERLINE_SUFFIX; 1805 String enabledkey= PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX + ANNOTATION + PreferenceConstants.EDITOR_SEMANTIC_HIGHLIGHTING_ENABLED_SUFFIX; 1806 1807 String oldColorkey= PreferenceConstants.EDITOR_JAVA_ANNOTATION_COLOR; 1808 String oldBoldkey= PreferenceConstants.EDITOR_JAVA_ANNOTATION_BOLD; 1809 String oldItalickey= PreferenceConstants.EDITOR_JAVA_ANNOTATION_ITALIC; 1810 String oldStrikethroughKey= PreferenceConstants.EDITOR_JAVA_ANNOTATION_STRIKETHROUGH; 1811 String oldUnderlineKey= PreferenceConstants.EDITOR_JAVA_ANNOTATION_UNDERLINE; 1812 1813 if (conditionalReset(store, oldColorkey, colorkey) 1814 || conditionalReset(store, oldBoldkey, boldkey) 1815 || conditionalReset(store, oldItalickey, italickey) 1816 || conditionalReset(store, oldStrikethroughKey, strikethroughKey) 1817 || conditionalReset(store, oldUnderlineKey, underlineKey)) { 1818 store.setValue(enabledkey, true); 1819 } 1820 1821 } 1822 1823 1838 private static boolean conditionalReset(IPreferenceStore store, String oldKey, String newKey) { 1839 if (!store.isDefault(oldKey)) { 1840 if (store.isDefault(newKey)) 1841 store.setValue(newKey, store.getString(oldKey)); 1842 store.setToDefault(oldKey); 1843 return true; 1844 } 1845 return false; 1846 } 1847 1848 1857 private static void setDefaultAndFireEvent(IPreferenceStore store, String key, RGB newValue) { 1858 RGB oldValue= null; 1859 if (store.isDefault(key)) 1860 oldValue= PreferenceConverter.getDefaultColor(store, key); 1861 1862 PreferenceConverter.setDefault(store, key, newValue); 1863 1864 if (oldValue != null && !oldValue.equals(newValue)) 1865 store.firePropertyChangeEvent(key, oldValue, newValue); 1866 } 1867 1868 1871 private SemanticHighlightings() { 1872 } 1873} 1874 | Popular Tags |