1 19 20 package org.netbeans.editor; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.util.Map ; 25 import java.util.List ; 26 import java.util.Iterator ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.beans.PropertyChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 import javax.swing.text.JTextComponent ; 35 36 42 43 public class SettingsUtil { 44 45 public static final String TOKEN_COLORING_INITIALIZER_NAME_SUFFIX 46 = "token-coloring-initializer"; 48 public static final PrintColoringEvaluator defaultPrintColoringEvaluator 49 = new PrintColoringEvaluator(); 50 51 private static final float defaultPrintFontSize = 10; 52 53 58 public static List getClonedList(List l) { 59 return (l != null) ? new ArrayList (l) : new ArrayList (); 60 } 61 62 public static List getClonedList(Class kitClass, String settingName) { 63 return getClonedList((List )Settings.getValue(kitClass, settingName)); 64 } 65 66 67 public static List getClonedList(Map settingsMap, String settingName) { 68 if (settingsMap != null) { 69 return getClonedList((List )settingsMap.get(settingName)); 70 } else { 71 return null; 72 } 73 } 74 75 76 public static Map getClonedMap(Map m) { 77 return (m != null) ? new HashMap (m) : new HashMap (); 78 } 79 80 public static Map getClonedMap(Class kitClass, String settingName) { 81 return getClonedMap((Map )Settings.getValue(kitClass, settingName)); 82 } 83 84 85 public static Map getClonedMap(Map settingsMap, String settingName) { 86 if (settingsMap != null) { 87 return getClonedMap((Map )settingsMap.get(settingName)); 88 } else { 89 return null; 90 } 91 } 92 93 94 public static Object getValue(Class kitClass, String settingName, 95 Object defaultValue) { 96 Object value = Settings.getValue(kitClass, settingName); 97 return (value != null) ? value : defaultValue; 98 } 99 100 public static int getInteger(Class kitClass, String settingName, 101 int defaultValue) { 102 Object value = Settings.getValue(kitClass, settingName); 103 return (value instanceof Integer ) ? ((Integer )value).intValue() : defaultValue; 104 } 105 106 public static int getInteger(Class kitClass, String settingName, 107 Integer defaultValue) { 108 return getInteger(kitClass, settingName, defaultValue.intValue()); 109 } 110 111 public static int getPositiveInteger(Class kitClass, String settingName, 112 int defaultValue) { 113 int ret = getInteger(kitClass, settingName, defaultValue); 114 if (ret <= 0) { 115 ret = defaultValue; 116 } 117 return ret; 118 } 119 120 public static int getPositiveInteger(Class kitClass, String settingName, 121 Integer defaultValue) { 122 return getPositiveInteger(kitClass, settingName, defaultValue.intValue()); 123 } 124 125 public static int getNonNegativeInteger(Class kitClass, String settingName, 126 int defaultValue) { 127 int ret = getInteger(kitClass, settingName, defaultValue); 128 if (ret < 0) { 129 ret = defaultValue; 130 } 131 return ret; 132 } 133 134 public static int getNonNegativeInteger(Class kitClass, String settingName, 135 Integer defaultValue) { 136 return getNonNegativeInteger(kitClass, settingName, defaultValue.intValue()); 137 } 138 139 public static boolean getBoolean(Class kitClass, String settingName, 140 boolean defaultValue) { 141 Object value = Settings.getValue(kitClass, settingName); 142 return (value instanceof Boolean ) ? ((Boolean )value).booleanValue() : defaultValue; 143 } 144 145 public static boolean getBoolean(Class kitClass, String settingName, 146 Boolean defaultValue) { 147 return getBoolean(kitClass, settingName, defaultValue.booleanValue()); 148 } 149 150 public static String getString(Class kitClass, String settingName, 151 String defaultValue) { 152 Object value = Settings.getValue(kitClass, settingName); 153 return (value instanceof String ) ? (String )value : defaultValue; 154 } 155 156 public static Acceptor getAcceptor(Class kitClass, String settingName, 157 Acceptor defaultValue) { 158 Object value = Settings.getValue(kitClass, settingName); 159 return (value instanceof Acceptor) ? (Acceptor)value : defaultValue; 160 } 161 162 public static List getList(Class kitClass, String settingName, 163 List defaultValue) { 164 Object value = Settings.getValue(kitClass, settingName); 165 return (value instanceof List ) ? (List )value : defaultValue; 166 } 167 168 public static List getCumulativeList(Class kitClass, String settingName, 169 List defaultValue) { 170 Settings.KitAndValue[] kva = Settings.getValueHierarchy(kitClass, settingName); 171 if (kva != null && kva.length > 0) { 172 List l = new ArrayList (); 173 for (int i = 0; i < kva.length; i++) { 174 if (kva[i].value instanceof List ) { 175 l.addAll((List )kva[i].value); 176 } 177 } 178 return l; 179 } else { 180 return defaultValue; 181 } 182 } 183 184 public static Map getMap(Class kitClass, String settingName, 185 Map defaultValue) { 186 Object value = Settings.getValue(kitClass, settingName); 187 return (value instanceof Map ) ? (Map )value : defaultValue; 188 } 189 190 191 public static void updateListSetting(Class kitClass, 192 String settingName, Object [] addToList) { 193 if (addToList != null && addToList.length > 0) { 194 List l = getClonedList(kitClass, settingName); 195 l.addAll(Arrays.asList(addToList)); 196 Settings.setValue(kitClass, settingName, l); 197 } 198 } 199 200 public static void updateListSetting(Map settingsMap, 201 String settingName, Object [] addToList) { 202 if (settingsMap != null && addToList != null && addToList.length > 0) { 203 List l = getClonedList(settingsMap, settingName); 204 l.addAll(Arrays.asList(addToList)); 205 settingsMap.put(settingName, l); 206 } 207 } 208 209 private static String getColoringSettingName(String coloringName, 210 boolean printingSet) { 211 return (coloringName 212 + (printingSet ? SettingsNames.COLORING_NAME_PRINT_SUFFIX : SettingsNames.COLORING_NAME_SUFFIX) 213 ).intern(); 214 } 215 216 public static Coloring getColoring(Class kitClass, String coloringName, 217 boolean printingSet) { 218 return (Coloring)getColoring(kitClass, coloringName, printingSet, true); 219 } 220 221 public static Object getColoring(Class kitClass, String coloringName, 222 boolean printingSet, boolean evaluateEvaluators) { 223 return Settings.getValue(kitClass, 224 getColoringSettingName(coloringName, printingSet), 225 evaluateEvaluators 226 ); 227 } 228 229 public static Coloring getTokenColoring(TokenContextPath tcp, 230 TokenCategory tokenIDOrCategory, boolean printingSet) { 231 return (Coloring)getTokenColoring(tcp, tokenIDOrCategory, printingSet, true); 232 } 233 234 public static Object getTokenColoring(TokenContextPath tcp, 235 TokenCategory tokenIDOrCategory, boolean printingSet, boolean evaluateEvaluators) { 236 return getColoring(BaseKit.class, tcp.getFullTokenName(tokenIDOrCategory), 237 printingSet, evaluateEvaluators); 238 } 239 240 248 public static Object getColoring(Map settingsMap, String coloringName, 249 boolean printingSet) { 250 return settingsMap.get(getColoringSettingName(coloringName, printingSet)); 251 } 252 253 public static void setColoring(Class kitClass, String coloringName, 254 Object newValue, boolean printingSet) { 255 Settings.setValue(kitClass, getColoringSettingName(coloringName, printingSet), newValue); 256 } 257 258 public static void setColoring(Class kitClass, String coloringName, 259 Object componentColoringNewValue) { 260 setColoring(kitClass, coloringName, componentColoringNewValue, false); 261 setColoring(kitClass, coloringName, defaultPrintColoringEvaluator, true); 262 } 263 264 public static void setColoring(Class kitClass, String coloringName, 265 Object componentColoringNewValue, Object printColoringNewValue) { 266 setColoring(kitClass, coloringName, componentColoringNewValue, false); 267 setColoring(kitClass, coloringName, printColoringNewValue, true); 268 } 269 270 273 public static void setColoring(Map settingsMap, String coloringName, 274 Object newValue, boolean printingSet) { 275 settingsMap.put(getColoringSettingName(coloringName, printingSet), newValue); 276 } 277 278 281 public static void setColoring(Map settingsMap, String coloringName, 282 Object componentColoringNewValue) { 283 setColoring(settingsMap, coloringName, componentColoringNewValue, false); 284 setColoring(settingsMap, coloringName, defaultPrintColoringEvaluator, true); 285 } 286 287 public static void setColoring(Map settingsMap, String coloringName, 288 Object componentColoringNewValue, Object printColoringNewValue) { 289 setColoring(settingsMap, coloringName, componentColoringNewValue, false); 290 setColoring(settingsMap, coloringName, printColoringNewValue, true); 291 } 292 293 294 301 public static Map getColoringMap(Class kitClass, boolean printingSet, 302 boolean evaluateEvaluators) { 303 HashMap coloringMap = new HashMap (); 304 List nameList = getCumulativeList(kitClass, SettingsNames.COLORING_NAME_LIST, null); 305 306 if (nameList != null) { 308 for (int i = nameList.size() - 1; i >= 0; i--) { 309 String name = (String )nameList.get(i); 310 Object coloring = getColoring(kitClass, name, printingSet, evaluateEvaluators); 311 if (coloring != null) { 312 coloringMap.put(name, coloring); 313 } 314 } 315 } 316 317 List tokenContextList = getList(kitClass, SettingsNames.TOKEN_CONTEXT_LIST, null); 319 if (tokenContextList != null) { 320 for (int i = tokenContextList.size() - 1; i >= 0; i--) { 321 TokenContext tc = (TokenContext)tokenContextList.get(i); 322 TokenContextPath[] allPaths = tc.getAllContextPaths(); 323 for (int j = 0; j < allPaths.length; j++) { 324 TokenContext firstContext = allPaths[j].getContexts()[0]; 325 326 TokenCategory[] tokenCategories = firstContext.getTokenCategories(); 328 for (int k = 0; k < tokenCategories.length; k++) { 329 Object coloring = getTokenColoring(allPaths[j], 330 tokenCategories[k], printingSet, evaluateEvaluators); 331 if (coloring != null) { 332 String fullName = allPaths[j].getFullTokenName(tokenCategories[k]); 333 coloringMap.put(fullName, coloring); 334 } 335 } 336 337 TokenID[] tokenIDs = firstContext.getTokenIDs(); 339 for (int k = 0; k < tokenIDs.length; k++) { 340 Object coloring = getTokenColoring(allPaths[j], 341 tokenIDs[k], printingSet, evaluateEvaluators); 342 if (coloring != null) { 343 String fullName = allPaths[j].getFullTokenName(tokenIDs[k]); 344 coloringMap.put(fullName, coloring); 345 } 346 } 347 } 348 } 349 } 350 351 return coloringMap; 352 } 353 354 355 364 public static void setColoringMap( Class kitClass, Map coloringMap, boolean printingSet ) { 365 List colNameList = getCumulativeList(kitClass, SettingsNames.COLORING_NAME_LIST, null); 366 if (colNameList != null && coloringMap != null && coloringMap.size() > 0) { 367 HashSet nameSet = new HashSet (colNameList); 368 369 for( Iterator i = coloringMap.keySet().iterator(); i.hasNext(); ) { 370 String name = (String )i.next(); 371 Object coloring = coloringMap.get( name ); 372 if( nameSet.contains( name ) ) { 373 setColoring( kitClass, name, coloring, printingSet ); 374 } else { 375 setColoring( BaseKit.class, name, coloring, printingSet ); 376 } 377 } 378 } 379 } 380 381 393 public static Settings.Initializer getColoringMapInitializer( 394 Class kitClass, Map coloringMap, boolean printingSet, 395 String initializerName) { 396 return new ColoringMapInitializer(kitClass, coloringMap, printingSet, 397 initializerName); 398 } 399 400 403 public static class PrintColoringEvaluator implements Settings.Evaluator { 404 405 412 protected Coloring getPrintColoring(Class kitClass, 413 String coloringName, Coloring componentColoring) { 414 Coloring printColoring = componentColoring; 415 if (printColoring != null) { 416 if (printColoring.getBackColor() != null) { 418 printColoring = Coloring.changeBackColor(printColoring, Color.white); 419 } 420 if (printColoring.getForeColor() != null) { 422 printColoring = Coloring.changeForeColor(printColoring, Color.black); 423 } 424 float pfh = getPrintFontSize(); 426 if (pfh >= 0) { 427 Font f = printColoring.getFont(); 428 if (f != null) { 429 printColoring = Coloring.changeFont(printColoring, f.deriveFont(pfh)); 430 } 431 } 432 } 433 return printColoring; 434 } 435 436 439 protected float getPrintFontSize() { 440 return defaultPrintFontSize; 441 } 442 443 public Object getValue(Class kitClass, String settingName) { 444 if (settingName.endsWith(SettingsNames.COLORING_NAME_PRINT_SUFFIX)) { 445 String coloringName = settingName.substring(0, 446 settingName.length() - SettingsNames.COLORING_NAME_PRINT_SUFFIX.length()); 447 Coloring c = getColoring(kitClass, coloringName, false); 448 return getPrintColoring(kitClass, coloringName, c); 449 } 450 return null; 451 } 452 453 } 454 455 458 public static class FontStylePrintColoringEvaluator extends PrintColoringEvaluator { 459 460 private int fontStyle; 461 462 public FontStylePrintColoringEvaluator(int fontStyle) { 463 this.fontStyle = fontStyle; 464 } 465 466 protected Coloring getPrintColoring(Class kitClass, String coloringName, 467 Coloring componentColoring) { 468 Coloring printColoring = super.getPrintColoring(kitClass, coloringName, componentColoring); 469 Font f = printColoring.getFont(); 470 if (f != null) { 471 printColoring = Coloring.changeFont(printColoring, f.deriveFont(fontStyle)); 472 } 473 return printColoring; 474 } 475 476 } 477 478 481 public static class ForeColorPrintColoringEvaluator extends PrintColoringEvaluator { 482 483 private Color foreColor; 484 485 public ForeColorPrintColoringEvaluator(Color foreColor) { 486 this.foreColor = foreColor; 487 } 488 489 protected Coloring getPrintColoring(Class kitClass, String coloringName, 490 Coloring componentColoring) { 491 return Coloring.changeForeColor( 492 super.getPrintColoring(kitClass, coloringName, componentColoring), 493 foreColor 494 ); 495 } 496 497 } 498 499 500 public static class TokenColoringEvaluator 501 implements Settings.Evaluator, java.io.Serializable { 502 503 private String parentFullTokenIDName; 504 505 private Coloring coloring; 506 507 private boolean printingSet; 508 509 519 public TokenColoringEvaluator(TokenContextPath tokenContextPath, 520 TokenCategory tokenIDOrCategory, Coloring coloring, boolean printingSet) { 521 this(tokenContextPath.getParent().getFullTokenName(tokenIDOrCategory), 522 coloring, printingSet); 523 } 524 525 532 public TokenColoringEvaluator(String parentFullTokenIDName, Coloring coloring, 533 boolean printingSet) { 534 this.parentFullTokenIDName = parentFullTokenIDName; 535 this.coloring = coloring; 536 this.printingSet = printingSet; 537 } 538 539 public Object getValue(Class kitClass, String settingName) { 540 Coloring ret; 541 ret = getColoring(BaseKit.class, parentFullTokenIDName, printingSet); 542 543 if (coloring != null) { if (ret != null) { ret = coloring.apply(ret); } 547 } 548 549 return ret; 550 } 551 552 } 553 554 556 public static abstract class TokenColoringInitializer 557 extends Settings.AbstractInitializer { 558 559 560 private TokenContext tokenContext; 561 562 568 public TokenColoringInitializer(TokenContext tokenContext) { 569 this(tokenContext, tokenContext.getNamePrefix() 570 + TOKEN_COLORING_INITIALIZER_NAME_SUFFIX); 571 } 572 573 579 public TokenColoringInitializer(TokenContext tokenContext, 580 String initializerName) { 581 super(initializerName); 582 this.tokenContext = tokenContext; 583 } 584 585 public void updateSettingsMap(Class kitClass, Map settingsMap) { 586 587 if (kitClass == BaseKit.class) { 589 TokenContextPath[] allPaths = tokenContext.getAllContextPaths(); 590 for (int i = 0; i < allPaths.length; i++) { 591 TokenContextPath tcp = allPaths[i]; 593 boolean printingSet = false; do { 595 596 TokenContext firstContext = tcp.getContexts()[0]; 597 TokenCategory[] tokenCategories = firstContext.getTokenCategories(); 598 for (int j = 0; j < tokenCategories.length; j++) { 599 Object catColoring = getTokenColoring(tcp, tokenCategories[j], 600 printingSet); 601 if (catColoring != null) { 602 String fullName = tcp.getFullTokenName(tokenCategories[j]); 603 SettingsUtil.setColoring(settingsMap, fullName, catColoring, printingSet); 604 } 605 } 606 607 TokenID[] tokenIDs = firstContext.getTokenIDs(); 609 for (int j = 0; j < tokenIDs.length; j++) { 610 Object tokenColoring = getTokenColoring(tcp, tokenIDs[j], 611 printingSet); 612 if (tokenColoring != null) { 613 String fullName = tcp.getFullTokenName(tokenIDs[j]); 614 SettingsUtil.setColoring(settingsMap, fullName, tokenColoring, printingSet); 615 } 616 } 617 618 printingSet = !printingSet; 619 620 } while (printingSet); 621 } 622 } 623 624 } 625 626 636 public abstract Object getTokenColoring(TokenContextPath tokenContextPath, 637 TokenCategory tokenIDOrCategory, boolean printingSet); 638 639 } 640 641 644 static class ColoringMapInitializer extends Settings.AbstractInitializer { 645 646 private Class kitClass; 647 648 private HashMap baseKitMap; 649 650 private HashMap kitClassMap; 651 652 ColoringMapInitializer(Class kitClass, Map coloringMap, boolean printingSet, 653 String initializerName) { 654 655 super(initializerName); 656 this.kitClass = kitClass; 657 baseKitMap = new HashMap (31); 658 kitClassMap = new HashMap (37); 659 660 List colNameList = getCumulativeList(kitClass, SettingsNames.COLORING_NAME_LIST, null); 661 if (colNameList != null && coloringMap != null && coloringMap.size() > 0) { 662 HashSet nameSet = new HashSet (colNameList); 663 664 for( Iterator i = coloringMap.keySet().iterator(); i.hasNext(); ) { 665 String name = (String )i.next(); 666 Object coloring = coloringMap.get( name ); 667 if( nameSet.contains( name ) ) { 668 setColoring( kitClassMap, name, coloring, printingSet ); 669 } else { 670 setColoring( baseKitMap, name, coloring, printingSet ); 671 } 672 } 673 } 674 } 675 676 public void updateSettingsMap(Class kitClass, Map settingsMap) { 677 678 if (kitClass == BaseKit.class) { 679 settingsMap.putAll(baseKitMap); 680 681 } else if (kitClass == this.kitClass) { 682 settingsMap.putAll(kitClassMap); 683 } 684 685 } 686 687 } 688 689 } 690 | Popular Tags |