KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > SettingsUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35
36 /**
37 * Utility methods for managing settings
38 *
39 * @author Miloslav Metelka
40 * @version 1.00
41 */

42
43 public class SettingsUtil {
44
45     public static final String JavaDoc TOKEN_COLORING_INITIALIZER_NAME_SUFFIX
46         = "token-coloring-initializer"; // NOI18N
47

48     public static final PrintColoringEvaluator defaultPrintColoringEvaluator
49     = new PrintColoringEvaluator();
50
51     private static final float defaultPrintFontSize = 10;
52
53     /** Get either the cloned list or new list if the old
54     * one was null.
55     * @param l list to check
56     * @return the cloned list if it was non-null or the new list
57     */

58     public static List JavaDoc getClonedList(List JavaDoc l) {
59         return (l != null) ? new ArrayList JavaDoc(l) : new ArrayList JavaDoc();
60     }
61
62     public static List JavaDoc getClonedList(Class JavaDoc kitClass, String JavaDoc settingName) {
63         return getClonedList((List JavaDoc)Settings.getValue(kitClass, settingName));
64     }
65
66     /** Useful for initializers */
67     public static List JavaDoc getClonedList(Map JavaDoc settingsMap, String JavaDoc settingName) {
68         if (settingsMap != null) {
69             return getClonedList((List JavaDoc)settingsMap.get(settingName));
70         } else {
71             return null;
72         }
73     }
74
75
76     public static Map JavaDoc getClonedMap(Map JavaDoc m) {
77         return (m != null) ? new HashMap JavaDoc(m) : new HashMap JavaDoc();
78     }
79
80     public static Map JavaDoc getClonedMap(Class JavaDoc kitClass, String JavaDoc settingName) {
81         return getClonedMap((Map JavaDoc)Settings.getValue(kitClass, settingName));
82     }
83
84     /** Useful for initializers */
85     public static Map JavaDoc getClonedMap(Map JavaDoc settingsMap, String JavaDoc settingName) {
86         if (settingsMap != null) {
87             return getClonedMap((Map JavaDoc)settingsMap.get(settingName));
88         } else {
89             return null;
90         }
91     }
92
93
94     public static Object JavaDoc getValue(Class JavaDoc kitClass, String JavaDoc settingName,
95                                   Object JavaDoc defaultValue) {
96         Object JavaDoc value = Settings.getValue(kitClass, settingName);
97         return (value != null) ? value : defaultValue;
98     }
99
100     public static int getInteger(Class JavaDoc kitClass, String JavaDoc settingName,
101                                  int defaultValue) {
102         Object JavaDoc value = Settings.getValue(kitClass, settingName);
103         return (value instanceof Integer JavaDoc) ? ((Integer JavaDoc)value).intValue() : defaultValue;
104     }
105
106     public static int getInteger(Class JavaDoc kitClass, String JavaDoc settingName,
107                                  Integer JavaDoc defaultValue) {
108         return getInteger(kitClass, settingName, defaultValue.intValue());
109     }
110
111     public static int getPositiveInteger(Class JavaDoc kitClass, String JavaDoc 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 JavaDoc kitClass, String JavaDoc settingName,
121                                          Integer JavaDoc defaultValue) {
122         return getPositiveInteger(kitClass, settingName, defaultValue.intValue());
123     }
124
125     public static int getNonNegativeInteger(Class JavaDoc kitClass, String JavaDoc 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 JavaDoc kitClass, String JavaDoc settingName,
135                                             Integer JavaDoc defaultValue) {
136         return getNonNegativeInteger(kitClass, settingName, defaultValue.intValue());
137     }
138
139     public static boolean getBoolean(Class JavaDoc kitClass, String JavaDoc settingName,
140                                      boolean defaultValue) {
141         Object JavaDoc value = Settings.getValue(kitClass, settingName);
142         return (value instanceof Boolean JavaDoc) ? ((Boolean JavaDoc)value).booleanValue() : defaultValue;
143     }
144
145     public static boolean getBoolean(Class JavaDoc kitClass, String JavaDoc settingName,
146                                      Boolean JavaDoc defaultValue) {
147         return getBoolean(kitClass, settingName, defaultValue.booleanValue());
148     }
149
150     public static String JavaDoc getString(Class JavaDoc kitClass, String JavaDoc settingName,
151                                    String JavaDoc defaultValue) {
152         Object JavaDoc value = Settings.getValue(kitClass, settingName);
153         return (value instanceof String JavaDoc) ? (String JavaDoc)value : defaultValue;
154     }
155
156     public static Acceptor getAcceptor(Class JavaDoc kitClass, String JavaDoc settingName,
157                                        Acceptor defaultValue) {
158         Object JavaDoc value = Settings.getValue(kitClass, settingName);
159         return (value instanceof Acceptor) ? (Acceptor)value : defaultValue;
160     }
161
162     public static List JavaDoc getList(Class JavaDoc kitClass, String JavaDoc settingName,
163                                List JavaDoc defaultValue) {
164         Object JavaDoc value = Settings.getValue(kitClass, settingName);
165         return (value instanceof List JavaDoc) ? (List JavaDoc)value : defaultValue;
166     }
167
168     public static List JavaDoc getCumulativeList(Class JavaDoc kitClass, String JavaDoc settingName,
169                                          List JavaDoc defaultValue) {
170         Settings.KitAndValue[] kva = Settings.getValueHierarchy(kitClass, settingName);
171         if (kva != null && kva.length > 0) {
172             List JavaDoc l = new ArrayList JavaDoc();
173             for (int i = 0; i < kva.length; i++) {
174                 if (kva[i].value instanceof List JavaDoc) {
175                     l.addAll((List JavaDoc)kva[i].value);
176                 }
177             }
178             return l;
179         } else {
180             return defaultValue;
181         }
182     }
183
184     public static Map JavaDoc getMap(Class JavaDoc kitClass, String JavaDoc settingName,
185                              Map JavaDoc defaultValue) {
186         Object JavaDoc value = Settings.getValue(kitClass, settingName);
187         return (value instanceof Map JavaDoc) ? (Map JavaDoc)value : defaultValue;
188     }
189
190
191     public static void updateListSetting(Class JavaDoc kitClass,
192                                          String JavaDoc settingName, Object JavaDoc[] addToList) {
193         if (addToList != null && addToList.length > 0) {
194             List JavaDoc 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 JavaDoc settingsMap,
201                                          String JavaDoc settingName, Object JavaDoc[] addToList) {
202         if (settingsMap != null && addToList != null && addToList.length > 0) {
203             List JavaDoc l = getClonedList(settingsMap, settingName);
204             l.addAll(Arrays.asList(addToList));
205             settingsMap.put(settingName, l);
206         }
207     }
208
209     private static String JavaDoc getColoringSettingName(String JavaDoc 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 JavaDoc kitClass, String JavaDoc coloringName,
217     boolean printingSet) {
218         return (Coloring)getColoring(kitClass, coloringName, printingSet, true);
219     }
220
221     public static Object JavaDoc getColoring(Class JavaDoc kitClass, String JavaDoc 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 JavaDoc getTokenColoring(TokenContextPath tcp,
235     TokenCategory tokenIDOrCategory, boolean printingSet, boolean evaluateEvaluators) {
236         return getColoring(BaseKit.class, tcp.getFullTokenName(tokenIDOrCategory),
237                 printingSet, evaluateEvaluators);
238     }
239
240     /** Get the coloring setting from the map that holds the settings values
241     * for the particular kit.
242     * @param settingsMap map that holds the [setting-name, setting-value] pairs
243     * for some kit-class.
244     * @param coloringName name of the coloring to retrieve
245     * @param printingSet retrieve the value of printing coloring
246     * instead of component coloring.
247     */

248     public static Object JavaDoc getColoring(Map JavaDoc settingsMap, String JavaDoc coloringName,
249     boolean printingSet) {
250         return settingsMap.get(getColoringSettingName(coloringName, printingSet));
251     }
252
253     public static void setColoring(Class JavaDoc kitClass, String JavaDoc coloringName,
254     Object JavaDoc newValue, boolean printingSet) {
255         Settings.setValue(kitClass, getColoringSettingName(coloringName, printingSet), newValue);
256     }
257
258     public static void setColoring(Class JavaDoc kitClass, String JavaDoc coloringName,
259     Object JavaDoc componentColoringNewValue) {
260         setColoring(kitClass, coloringName, componentColoringNewValue, false);
261         setColoring(kitClass, coloringName, defaultPrintColoringEvaluator, true);
262     }
263
264     public static void setColoring(Class JavaDoc kitClass, String JavaDoc coloringName,
265     Object JavaDoc componentColoringNewValue, Object JavaDoc printColoringNewValue) {
266         setColoring(kitClass, coloringName, componentColoringNewValue, false);
267         setColoring(kitClass, coloringName, printColoringNewValue, true);
268     }
269
270     /** Put the coloring into a map holding the settings for the particular kit.
271     *
272     */

273     public static void setColoring(Map JavaDoc settingsMap, String JavaDoc coloringName,
274     Object JavaDoc newValue, boolean printingSet) {
275         settingsMap.put(getColoringSettingName(coloringName, printingSet), newValue);
276     }
277
278     /** Put the coloring into a map holding the settings for the particular kit and assign
279     * a default print coloring Evaluator to the print coloring setting.
280     */

281     public static void setColoring(Map JavaDoc settingsMap, String JavaDoc coloringName,
282     Object JavaDoc componentColoringNewValue) {
283         setColoring(settingsMap, coloringName, componentColoringNewValue, false);
284         setColoring(settingsMap, coloringName, defaultPrintColoringEvaluator, true);
285     }
286
287     public static void setColoring(Map JavaDoc settingsMap, String JavaDoc coloringName,
288     Object JavaDoc componentColoringNewValue, Object JavaDoc printColoringNewValue) {
289         setColoring(settingsMap, coloringName, componentColoringNewValue, false);
290         setColoring(settingsMap, coloringName, printColoringNewValue, true);
291     }
292
293
294     /** Get the map holding [coloring-name, coloring-value] pairs for all the colorings
295     * defined for the given kit. The <tt>SettingsNames.COLORING_NAME_LIST</tt> setting
296     * is used to the coloring names that will apear in the map.
297     * @param kitClass kit class for which the colorings are retrieved from the settings.
298     * @param printingSet retrieve the printing colorings instead of component colorings.
299     * @param evaluateEvaluators evaluate all the Settings.Evaluator instances or not.
300     */

301     public static Map JavaDoc getColoringMap(Class JavaDoc kitClass, boolean printingSet,
302                                      boolean evaluateEvaluators) {
303         HashMap JavaDoc coloringMap = new HashMap JavaDoc();
304         List JavaDoc nameList = getCumulativeList(kitClass, SettingsNames.COLORING_NAME_LIST, null);
305
306         // First take the name of the colorings
307
if (nameList != null) {
308             for (int i = nameList.size() - 1; i >= 0; i--) {
309                 String JavaDoc name = (String JavaDoc)nameList.get(i);
310                 Object JavaDoc coloring = getColoring(kitClass, name, printingSet, evaluateEvaluators);
311                 if (coloring != null) {
312                     coloringMap.put(name, coloring);
313                 }
314             }
315         }
316
317         // Add the colorings from syntaxes
318
List JavaDoc 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                     // Add token-categories colorings
327
TokenCategory[] tokenCategories = firstContext.getTokenCategories();
328                     for (int k = 0; k < tokenCategories.length; k++) {
329                         Object JavaDoc coloring = getTokenColoring(allPaths[j],
330                                 tokenCategories[k], printingSet, evaluateEvaluators);
331                         if (coloring != null) {
332                             String JavaDoc fullName = allPaths[j].getFullTokenName(tokenCategories[k]);
333                             coloringMap.put(fullName, coloring);
334                         }
335                     }
336
337                     // Add token-ids colorings
338
TokenID[] tokenIDs = firstContext.getTokenIDs();
339                     for (int k = 0; k < tokenIDs.length; k++) {
340                         Object JavaDoc coloring = getTokenColoring(allPaths[j],
341                                 tokenIDs[k], printingSet, evaluateEvaluators);
342                         if (coloring != null) {
343                             String JavaDoc fullName = allPaths[j].getFullTokenName(tokenIDs[k]);
344                             coloringMap.put(fullName, coloring);
345                         }
346                     }
347                 }
348             }
349         }
350
351         return coloringMap;
352     }
353
354
355     /** Update the settings according to the changes provided in the coloringMap.
356      * @param kitClass class of the kit for which the colorings are being updated.
357      * Only the colorings with the names contained in <code>COLORING_NAME_LIST</code>
358      * will be updated for the kitClass settings. The rest is considered
359      * to be the token colorings so they are updated in BaseKit settings.
360      * @param coloringMap map containing [coloring-name, coloring-value] pairs.
361      * @param printingSet whether printing colorings should be updated instead
362      * of component colorings.
363      */

364     public static void setColoringMap( Class JavaDoc kitClass, Map JavaDoc coloringMap, boolean printingSet ) {
365         List JavaDoc colNameList = getCumulativeList(kitClass, SettingsNames.COLORING_NAME_LIST, null);
366         if (colNameList != null && coloringMap != null && coloringMap.size() > 0) {
367             HashSet JavaDoc nameSet = new HashSet JavaDoc(colNameList);
368
369             for( Iterator JavaDoc i = coloringMap.keySet().iterator(); i.hasNext(); ) {
370                 String JavaDoc name = (String JavaDoc)i.next();
371                 Object JavaDoc 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     /** Create initializer that reflects the colorings given by the coloring map.
382      * @param kitClass class of the kit for which the colorings will be updated
383      * by the initializer.
384      * Only the colorings with the names contained in <code>COLORING_NAME_LIST</code>
385      * will be updated for the kitClass settings. The rest is considered
386      * to be the token colorings so they are updated in BaseKit settings
387      * by the initializer.
388      * @param coloringMap map containing [coloring-name, coloring-value] pairs.
389      * @param printingSet whether printing colorings should be updated instead
390      * of component colorings.
391      * @param initializerName name that will be assigned to the initializer.
392      */

393     public static Settings.Initializer getColoringMapInitializer(
394     Class JavaDoc kitClass, Map JavaDoc coloringMap, boolean printingSet,
395     String JavaDoc initializerName) {
396         return new ColoringMapInitializer(kitClass, coloringMap, printingSet,
397             initializerName);
398     }
399     
400     /** Evaluator that translates the regular coloring to the print coloring
401     * by the default black-and-white rules.
402     */

403     public static class PrintColoringEvaluator implements Settings.Evaluator {
404
405         /** Translates the regular coloring to the print coloring
406         * @param kitClass kit class for which the coloring is being retrieved
407         * @param coloringName name of the coloring without the suffix
408         * @param componentColoring component coloring retrieved from the settings. It's provided
409         * for convenience because the majority of Evaluator will derive
410         * the particular print coloring from the given component coloring.
411         */

412         protected Coloring getPrintColoring(Class JavaDoc kitClass,
413                                             String JavaDoc coloringName, Coloring componentColoring) {
414             Coloring printColoring = componentColoring;
415             if (printColoring != null) {
416                 // Make the background color white
417
if (printColoring.getBackColor() != null) {
418                     printColoring = Coloring.changeBackColor(printColoring, Color.white);
419                 }
420                 // Make the foreground color black
421
if (printColoring.getForeColor() != null) {
422                     printColoring = Coloring.changeForeColor(printColoring, Color.black);
423                 }
424                 // Update the font height
425
float pfh = getPrintFontSize();
426                 if (pfh >= 0) {
427                     Font JavaDoc f = printColoring.getFont();
428                     if (f != null) {
429                         printColoring = Coloring.changeFont(printColoring, f.deriveFont(pfh));
430                     }
431                 }
432             }
433             return printColoring;
434         }
435
436         /** Return the font size to which the coloring font should be updated.
437         * Negative value means not to update the coloring font.
438         */

439         protected float getPrintFontSize() {
440             return defaultPrintFontSize;
441         }
442
443         public Object JavaDoc getValue(Class JavaDoc kitClass, String JavaDoc settingName) {
444             if (settingName.endsWith(SettingsNames.COLORING_NAME_PRINT_SUFFIX)) {
445                 String JavaDoc 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     /** Print coloring Evaluator that changes the font style. It's available here
456     * because it's often used.
457     */

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 JavaDoc kitClass, String JavaDoc coloringName,
467                                             Coloring componentColoring) {
468             Coloring printColoring = super.getPrintColoring(kitClass, coloringName, componentColoring);
469             Font JavaDoc f = printColoring.getFont();
470             if (f != null) {
471                 printColoring = Coloring.changeFont(printColoring, f.deriveFont(fontStyle));
472             }
473             return printColoring;
474         }
475
476     }
477
478     /** Print coloring Evaluator that changes the foreground color to the color given
479     * in the constructor. It's available here because it's often used.
480     */

481     public static class ForeColorPrintColoringEvaluator extends PrintColoringEvaluator {
482
483         private Color JavaDoc foreColor;
484
485         public ForeColorPrintColoringEvaluator(Color JavaDoc foreColor) {
486             this.foreColor = foreColor;
487         }
488
489         protected Coloring getPrintColoring(Class JavaDoc kitClass, String JavaDoc coloringName,
490                                             Coloring componentColoring) {
491             return Coloring.changeForeColor(
492                        super.getPrintColoring(kitClass, coloringName, componentColoring),
493                        foreColor
494                    );
495         }
496
497     }
498
499     /** Evaluator for the token coloring. */
500     public static class TokenColoringEvaluator
501     implements Settings.Evaluator, java.io.Serializable JavaDoc {
502
503         private String JavaDoc parentFullTokenIDName;
504
505         private Coloring coloring;
506
507         private boolean printingSet;
508
509         /** Create new token-coloring evaluator.
510         * @param tokenContextPath token-context path for which the evaluator
511         * is being created. The parent path is retrieved and full token name
512         * will be got on it for the token-id or category provided.
513         * @param tokenIDOrCategory token-id or category for which the evaluator
514         * is being created.
515         * @param coloring additional coloring that, if non-null will be applied
516         * to the coloring of the parentTokenID to form the resulting coloring.
517         * @param printingSet whether this evaluator is for component or printing set coloring
518         */

519         public TokenColoringEvaluator(TokenContextPath tokenContextPath,
520         TokenCategory tokenIDOrCategory, Coloring coloring, boolean printingSet) {
521             this(tokenContextPath.getParent().getFullTokenName(tokenIDOrCategory),
522                     coloring, printingSet);
523         }
524
525         /** Create new token-coloring evaluator.
526         * @param parentFullTokenIDName name of the token from which is this token (for which
527         * is this evaluator created) derived.
528         * @param coloring additional coloring that, if non-null will be applied
529         * to the coloring of the parentTokenID to form the resulting coloring.
530         * @param printingSet whether this evaluator is for component or printing set coloring
531         */

532         public TokenColoringEvaluator(String JavaDoc parentFullTokenIDName, Coloring coloring,
533         boolean printingSet) {
534             this.parentFullTokenIDName = parentFullTokenIDName;
535             this.coloring = coloring;
536             this.printingSet = printingSet;
537         }
538
539         public Object JavaDoc getValue(Class JavaDoc kitClass, String JavaDoc settingName) {
540             Coloring ret;
541             ret = getColoring(BaseKit.class, parentFullTokenIDName, printingSet);
542
543             if (coloring != null) { // make change only if coloring is non-null
544
if (ret != null) { // parent is non-null
545
ret = coloring.apply(ret); // apply to parent
546
}
547             }
548
549             return ret;
550         }
551
552     }
553
554     /** Initializer for the token-coloring settings.
555     */

556     public static abstract class TokenColoringInitializer
557     extends Settings.AbstractInitializer {
558
559         /** Token context for which the colorings are being initialized. */
560         private TokenContext tokenContext;
561
562         /** Create settings-initializer for the colorings of the tokens
563         * in the given context and all its context-paths. The name for the new
564         * initializer is composed from the prefix of the tokenContext.
565         * @param tokenContext context for which the colorings are being
566         * initialized.
567         */

568         public TokenColoringInitializer(TokenContext tokenContext) {
569             this(tokenContext, tokenContext.getNamePrefix()
570                     + TOKEN_COLORING_INITIALIZER_NAME_SUFFIX);
571         }
572
573         /** Create settings-initializer for the colorings of the tokens
574         * in the given context and all its context-paths.
575         * @param tokenContext context for which the colorings are being
576         * initialized.
577         * @param initializerName name that will be given to the initializer.
578         */

579         public TokenColoringInitializer(TokenContext tokenContext,
580         String JavaDoc initializerName) {
581             super(initializerName);
582             this.tokenContext = tokenContext;
583         }
584
585         public void updateSettingsMap(Class JavaDoc kitClass, Map JavaDoc settingsMap) {
586
587             if (kitClass == BaseKit.class) { // all token colorings on base-kit level
588

589                 TokenContextPath[] allPaths = tokenContext.getAllContextPaths();
590                 for (int i = 0; i < allPaths.length; i++) {
591                     TokenContextPath tcp = allPaths[i]; // current path
592

593                     boolean printingSet = false; // process both sets
594
do {
595
596                         TokenContext firstContext = tcp.getContexts()[0];
597                         TokenCategory[] tokenCategories = firstContext.getTokenCategories();
598                         for (int j = 0; j < tokenCategories.length; j++) {
599                             Object JavaDoc catColoring = getTokenColoring(tcp, tokenCategories[j],
600                                     printingSet);
601                             if (catColoring != null) {
602                                 String JavaDoc fullName = tcp.getFullTokenName(tokenCategories[j]);
603                                 SettingsUtil.setColoring(settingsMap, fullName, catColoring, printingSet);
604                             }
605                         }
606
607                         // Add token-ids
608
TokenID[] tokenIDs = firstContext.getTokenIDs();
609                         for (int j = 0; j < tokenIDs.length; j++) {
610                             Object JavaDoc tokenColoring = getTokenColoring(tcp, tokenIDs[j],
611                                     printingSet);
612                             if (tokenColoring != null) {
613                                 String JavaDoc 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         /** Get either coloring or settings evaluator for the given target token-id.
627         * @param tokenContextPath token-context-path for which the coloring
628         * is being created. All the context paths for the given token-context
629         * are processed.
630         * @param tokenIDOrCategory token-id or token-category for which
631         * the coloring or evaluator is being created.
632         * @param printingSet whether the set of the colorings used for printing
633         * is created instead of the colorings used for the component coloring.
634         * @return either Coloring instance or an evaluator that returns a coloring.
635         */

636         public abstract Object JavaDoc getTokenColoring(TokenContextPath tokenContextPath,
637         TokenCategory tokenIDOrCategory, boolean printingSet);
638
639     }
640     
641     /** Coloring map initializer is used when coloring map is given
642      * and it's necessary to provide the same settings through initializer.
643      */

644     static class ColoringMapInitializer extends Settings.AbstractInitializer {
645         
646         private Class JavaDoc kitClass;
647         
648         private HashMap JavaDoc baseKitMap;
649         
650         private HashMap JavaDoc kitClassMap;
651         
652         ColoringMapInitializer(Class JavaDoc kitClass, Map JavaDoc coloringMap, boolean printingSet,
653         String JavaDoc initializerName) {
654
655             super(initializerName);
656             this.kitClass = kitClass;
657             baseKitMap = new HashMap JavaDoc(31);
658             kitClassMap = new HashMap JavaDoc(37);
659
660             List JavaDoc colNameList = getCumulativeList(kitClass, SettingsNames.COLORING_NAME_LIST, null);
661             if (colNameList != null && coloringMap != null && coloringMap.size() > 0) {
662                 HashSet JavaDoc nameSet = new HashSet JavaDoc(colNameList);
663
664                 for( Iterator JavaDoc i = coloringMap.keySet().iterator(); i.hasNext(); ) {
665                     String JavaDoc name = (String JavaDoc)i.next();
666                     Object JavaDoc 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 JavaDoc kitClass, Map JavaDoc 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