KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.text.AttributeSet JavaDoc;
29 import javax.swing.text.StyleConstants JavaDoc;
30
31 /**
32 * Immutable class that stores font and foreground and background colors.
33 * The coloring can be applied to either the drawing context, component
34 * or some other coloring. Applying first checks whether each of the font
35 * and the colors is non-null. If it's null the particular thing is left unchanged.
36 * For example to change the background color only the Coloring must
37 * be created with the font and foreground color being null.
38 * It's also possible to use a more advanced way to apply the coloring
39 * by using the modes. There are two modes - font-mode and color-mode.
40 * The deafult font-mode simply overwrites the whole font. The font-mode
41 * constants allow to change the underlying font by only changing some
42 * of the font-name, style or size. The <tt>FONT_MODE_APPLY_STYLE</tt>
43 * for example will only apply the style of the coloring's font.
44 * The "rest" of the coloring's font (name and size in this case)
45 * is not used so there can be any valid values ("Monospaced" and 12 for example).
46 * The colors can use the alpha value.
47 * It's also possible to set the color of the underline line
48 * or the color of the strikethrough line.
49 *
50 * Generally the editor uses two sets of the colorings to colorize the text.
51 * They are component-set and printing-set. The component-set is used
52 * for the editor component while the printing-set is used solely
53 * for colorizing the printed text.
54 *
55 *
56 * @author Miloslav Metelka
57 * @version 1.00
58 */

59
60 public final class Coloring implements java.io.Serializable JavaDoc {
61
62     /** Apply only the name from the font. This flag can be combined with
63     * other FONT_MODE flags. When using solely this constant
64     * the "rest" of the coloring's font (style and size in this case)
65     * is unused so there can be any valid values used to create the font
66     * (Font.PLAIN and 12 for example).
67     */

68     public static final int FONT_MODE_APPLY_NAME = 1;
69
70     /** Apply only the style from the font. This flag can be combined with
71     * other FONT_MODE flags. When using solely this constant
72     * the "rest" of the coloring's font (name and size in this case)
73     * is unused so there can be any valid values used
74     * ("Monospaced" and 12 for example).
75     */

76     public static final int FONT_MODE_APPLY_STYLE = 2;
77
78     /** Apply only the size from the font. This flag can be combined with
79     * other FONT_MODE flags. When using solely this constant
80     * the "rest" of the coloring's font (name and style in this case)
81     * is unused so there can be any valid values used to create the font
82     * ("Monospaced" and Font.PLAIN for example).
83     */

84     public static final int FONT_MODE_APPLY_SIZE = 4;
85
86     /** Replace the underlying font by the coloring's font.
87     * This value is a binary combination of
88     * FONT_MODE_APPLY_NAME, FONT_MODE_APPLY_STYLE, FONT_MODE_APPLY_SIZE.
89     */

90     public static final int FONT_MODE_DEFAULT
91     = FONT_MODE_APPLY_NAME | FONT_MODE_APPLY_STYLE | FONT_MODE_APPLY_SIZE;
92
93     /** Font */
94     private Font JavaDoc font;
95
96     /** Font mode */
97     private int fontMode;
98
99     /** Foreground color */
100     private Color JavaDoc foreColor;
101
102     /** Background color */
103     private Color JavaDoc backColor;
104
105     /** Underline line color */
106     private Color JavaDoc underlineColor;
107
108     /** Wave underline line color */
109     private Color JavaDoc waveUnderlineColor;
110     
111     /** Strikethrough line color */
112     private Color JavaDoc strikeThroughColor;
113
114     /** Cache holding the [original-font, derived-font] pairs
115     * and also original [fore-color, derived-fore-color] pairs.
116     * This helps to avoid the repetitive computations of the
117     * derived font and foreground-color (the backround-color is
118     * handled through different cache) and it also avoids the repetitive
119     * creations of the derived font and color objects.
120     */

121     private transient HashMap JavaDoc fontAndForeColorCache;
122
123     /** Cache holding the [back-color, derived-back-color] pairs */
124     private transient HashMap JavaDoc backColorCache;
125
126     static final long serialVersionUID =-1382649127124476675L;
127
128     /** Construct empty coloring */
129     public Coloring() {
130         this(null, null, null);
131     }
132
133     /** Construct new coloring */
134     public Coloring(Font JavaDoc font, Color JavaDoc foreColor, Color JavaDoc backColor) {
135         this(font, FONT_MODE_DEFAULT, foreColor, backColor, null, null);
136     }
137
138     /** Construct new coloring */
139     public Coloring(Font JavaDoc font, int fontMode, Color JavaDoc foreColor, Color JavaDoc backColor) {
140         this(font, fontMode, foreColor, backColor, null, null);
141     }
142     /** Construct new coloring */
143     public Coloring(Font JavaDoc font, int fontMode, Color JavaDoc foreColor,
144     Color JavaDoc backColor, Color JavaDoc underlineColor, Color JavaDoc strikeThroughColor) {
145         this(font, fontMode, foreColor, backColor, underlineColor, strikeThroughColor, null);
146     }
147         
148     /** Construct new coloring */
149     public Coloring(Font JavaDoc font, int fontMode, Color JavaDoc foreColor,
150     Color JavaDoc backColor, Color JavaDoc underlineColor, Color JavaDoc strikeThroughColor, Color JavaDoc waveUnderlineColor) {
151         font = (fontMode != 0) ? font : null;
152         fontMode = (font != null) ? fontMode : FONT_MODE_DEFAULT;
153
154         this.font = font;
155         this.fontMode = fontMode;
156
157         this.foreColor = foreColor;
158         this.backColor = backColor;
159
160         this.underlineColor = underlineColor;
161         this.strikeThroughColor = strikeThroughColor;
162         this.waveUnderlineColor = waveUnderlineColor;
163
164         checkCaches();
165     }
166     
167     private void checkCaches() {
168         // Possibly create the caches
169
boolean createFontCache = (font != null && fontMode != 0 && fontMode != FONT_MODE_DEFAULT);
170         boolean createForeColorCache = (foreColor != null && hasAlpha(foreColor));
171         if (createFontCache || createForeColorCache) {
172             fontAndForeColorCache = new HashMap JavaDoc(
173                 (createFontCache && createForeColorCache) ? 47 : 23);
174         }
175
176         if (backColor != null && hasAlpha(backColor)) {
177             backColorCache = new HashMap JavaDoc(23);
178         }
179     }
180
181     /** Whether the color has non-default alpha. */
182     private boolean hasAlpha(Color JavaDoc c) {
183         return ((c.getRGB() & 0xFF000000) != 0xFF000000);
184     }
185
186     /** Getter for font */
187     public Font JavaDoc getFont() {
188         return font;
189     }
190
191     /** Getter for font-mode */
192     public int getFontMode() {
193         return fontMode;
194     }
195
196     /** Getter for foreground color */
197     public Color JavaDoc getForeColor() {
198         return foreColor;
199     }
200
201     /** Getter for background color */
202     public Color JavaDoc getBackColor() {
203         return backColor;
204     }
205
206     /** Getter for underline line color */
207     public Color JavaDoc getUnderlineColor() {
208         return underlineColor;
209     }
210
211     /** Getter for wave underline line color */
212     public Color JavaDoc getWaveUnderlineColor() {
213         return waveUnderlineColor;
214     }
215
216     /** Getter for strikethrough line color */
217     public Color JavaDoc getStrikeThroughColor() {
218         return strikeThroughColor;
219     }
220
221     /** Modify the given font according to the font-mode */
222     private Font JavaDoc modifyFont(Font JavaDoc f) {
223         return new Font JavaDoc(
224                    ((fontMode & FONT_MODE_APPLY_NAME) != 0) ? font.getName() : f.getName(),
225                    ((fontMode & FONT_MODE_APPLY_STYLE) != 0) ? font.getStyle() : f.getStyle(),
226                    ((fontMode & FONT_MODE_APPLY_SIZE) != 0) ? font.getSize() : f.getSize()
227                );
228     }
229
230     private Color JavaDoc modifyForeColor(Color JavaDoc underForeColor) {
231         int alpha = foreColor.getAlpha(); // alpha 0 - 255
232
int fcRGB = foreColor.getRGB();
233         int underRGB = underForeColor.getRGB();
234
235         int rgb = (((foreColor.getRed() * alpha
236                 + underForeColor.getRed() * (255 - alpha)) / 255) & 0x000000FF) << 16;
237
238         rgb += ((((fcRGB & 0x0000FF00) * alpha
239                    + (underRGB & 0x0000FF00) * (255 - alpha)) / 255) & 0x0000FF00) // green
240
+ ((((fcRGB & 0x000000FF) * alpha
241                    + (underRGB & 0x000000FF) * (255 - alpha)) / 255) & 0x000000FF);// blue
242

243         return new Color JavaDoc(rgb, false);
244     }
245
246     private Color JavaDoc modifyBackColor(Color JavaDoc underBackColor) {
247         int alpha = backColor.getAlpha(); // alpha 0 - 255
248
int bcRGB = backColor.getRGB();
249         int underRGB = underBackColor.getRGB();
250         int rgb = (((backColor.getRed() * alpha
251                 + underBackColor.getRed() * (255 - alpha)) / 255) & 0x000000FF) << 16;
252
253
254         rgb += ((((bcRGB & 0x0000FF00) * alpha
255                   + (underRGB & 0x0000FF00) * (255 - alpha)) / 255) & 0x0000FF00) // green
256
+ ((((bcRGB & 0x000000FF) * alpha
257                   + (underRGB & 0x000000FF) * (255 - alpha)) / 255) & 0x000000FF);// blue
258

259         return new Color JavaDoc(rgb, false);
260     }
261
262     /** Apply this coloring to draw context. */
263     public void apply(DrawContext ctx) {
264         // Possibly change font
265
if (font != null) {
266             if (fontMode == FONT_MODE_DEFAULT) {
267                 ctx.setFont(font);
268
269             } else { // non-default font-mode
270
Font JavaDoc origFont = ctx.getFont();
271                 if (origFont != null) {
272                     synchronized (fontAndForeColorCache) {
273                         Font JavaDoc f = (Font JavaDoc)fontAndForeColorCache.get(origFont);
274                         if (f == null) {
275                             f = modifyFont(origFont);
276                             fontAndForeColorCache.put(origFont, f);
277                         }
278                         ctx.setFont(f);
279                     }
280                 }
281             }
282         }
283
284         // Possibly change fore-color
285
if (foreColor != null) {
286             if (!hasAlpha(foreColor)) { // doesn't have an alpha
287
ctx.setForeColor(foreColor);
288
289             } else { // has alpha
290
Color JavaDoc origForeColor = ctx.getForeColor();
291                 if (origForeColor != null) {
292                     synchronized (fontAndForeColorCache) {
293                         Color JavaDoc fc = (Color JavaDoc)fontAndForeColorCache.get(origForeColor);
294                         if (fc == null) {
295                             fc = modifyForeColor(origForeColor);
296                             fontAndForeColorCache.put(origForeColor, fc);
297                         }
298                         ctx.setForeColor(fc);
299                     }
300                 }
301             }
302         }
303
304         // Possibly change back-color
305
if (backColor != null) {
306             if (!hasAlpha(backColor)) {
307                 ctx.setBackColor(backColor);
308
309             } else { // non-default back color-mode
310
Color JavaDoc origBackColor = ctx.getBackColor();
311                 if (origBackColor != null) {
312                     synchronized (backColorCache) {
313                         Color JavaDoc bc = (Color JavaDoc)backColorCache.get(origBackColor);
314                         if (bc == null) {
315                             bc = modifyBackColor(origBackColor);
316                             backColorCache.put(origBackColor, bc);
317                         }
318                         ctx.setBackColor(bc);
319                     }
320                 }
321             }
322         }
323
324         if (underlineColor != null) {
325             ctx.setUnderlineColor(underlineColor);
326         }
327
328         if (waveUnderlineColor != null) {
329             ctx.setWaveUnderlineColor(waveUnderlineColor);
330         }
331
332         if (strikeThroughColor != null) {
333             ctx.setStrikeThroughColor(strikeThroughColor);
334         }
335     }
336
337     /** Apply this coloring to component colors/font.
338     * The underline and strikeThrough line colors have no effect here.
339     */

340     public void apply(JComponent JavaDoc c) {
341         // Possibly change font
342
if (font != null) {
343             if (fontMode == FONT_MODE_DEFAULT) {
344                 c.setFont(font);
345
346             } else { // non-default font-mode
347
Font JavaDoc origFont = c.getFont();
348                 if (origFont != null) {
349                     synchronized (fontAndForeColorCache) {
350                         Font JavaDoc f = (Font JavaDoc)fontAndForeColorCache.get(origFont);
351                         if (f == null) {
352                             f = modifyFont(origFont);
353                             fontAndForeColorCache.put(origFont, f);
354                         }
355                         c.setFont(f);
356                     }
357                 }
358             }
359         }
360
361         // Possibly change fore-color
362
if (foreColor != null) {
363             if (!hasAlpha(foreColor)) {
364                 c.setForeground(foreColor);
365
366             } else { // non-default fore color-mode
367
Color JavaDoc origForeColor = c.getForeground();
368                 if (origForeColor != null) {
369                     synchronized (fontAndForeColorCache) {
370                         Color JavaDoc fc = (Color JavaDoc)fontAndForeColorCache.get(origForeColor);
371                         if (fc == null) {
372                             fc = modifyForeColor(origForeColor);
373                             fontAndForeColorCache.put(origForeColor, fc);
374                         }
375                         c.setForeground(fc);
376                     }
377                 }
378             }
379         }
380
381         // Possibly change back-color
382
if (backColor != null) {
383             if (!hasAlpha(backColor)) {
384                 c.setBackground(backColor);
385
386             } else { // non-default back color-mode
387
Color JavaDoc origBackColor = c.getBackground();
388                 if (origBackColor != null) {
389                     synchronized (backColorCache) {
390                         Color JavaDoc bc = (Color JavaDoc)backColorCache.get(origBackColor);
391                         if (bc == null) {
392                             bc = modifyBackColor(origBackColor);
393                             backColorCache.put(origBackColor, bc);
394                         }
395                         c.setBackground(bc);
396                     }
397                 }
398             }
399         }
400     }
401
402     /** Apply this coloring to some other coloring c and return
403     * the resulting coloring.
404     * @param c coloring to which this coloring will be applied.
405     * If it's null then this coloring will be returned as result.
406     */

407     public Coloring apply(Coloring c) {
408         if (c == null) { // if c is null, return this coloring as result
409
return this;
410         }
411
412         Font JavaDoc newFont = c.font;
413         int newFontMode = FONT_MODE_DEFAULT;
414         Color JavaDoc newForeColor = c.foreColor;
415         Color JavaDoc newBackColor = c.backColor;
416         Color JavaDoc newUnderlineColor = c.underlineColor;
417         Color JavaDoc newWaveUnderlineColor = c.waveUnderlineColor;
418         Color JavaDoc newStrikeThroughColor = c.strikeThroughColor;
419
420         // Possibly change font
421
if (font != null) {
422             if (fontMode == FONT_MODE_DEFAULT) {
423                 newFont = font;
424
425             } else { // non-default font-mode
426
if (newFont != null) {
427                     synchronized (fontAndForeColorCache) {
428                         Font JavaDoc f = (Font JavaDoc)fontAndForeColorCache.get(newFont);
429                         if (f == null) {
430                             f = modifyFont(newFont);
431                             fontAndForeColorCache.put(newFont, f);
432                         }
433                         newFont = f;
434                     }
435                 }
436             }
437
438         } else { // fixing of #17669
439
// inherit font mode too
440
newFontMode = c.fontMode;
441         }
442         
443         // Possibly change fore-color
444
if (foreColor != null) {
445             if (!hasAlpha(foreColor)) {
446                 newForeColor = foreColor;
447
448             } else { // non-default fore color-mode
449
if (newForeColor != null) {
450                     synchronized (fontAndForeColorCache) {
451                         Color JavaDoc fc = (Color JavaDoc)fontAndForeColorCache.get(newForeColor);
452                         if (fc == null) {
453                             fc = modifyForeColor(newForeColor);
454                             fontAndForeColorCache.put(newForeColor, fc);
455                         }
456                         newForeColor = fc;
457                     }
458                 }
459             }
460         }
461
462         // Possibly change back-color
463
if (backColor != null) {
464             if (!hasAlpha(backColor)) {
465                 newBackColor = backColor;
466
467             } else { // non-default back color-mode
468
newBackColor = backColor;
469                 if (newBackColor != null) {
470                     synchronized (backColorCache) {
471                         Color JavaDoc bc = (Color JavaDoc)backColorCache.get(newBackColor);
472                         if (bc == null) {
473                             bc = modifyBackColor(newBackColor);
474                             backColorCache.put(newBackColor, bc);
475                         }
476                         newBackColor = bc;
477                     }
478                 }
479             }
480         }
481
482         if (underlineColor != null) {
483             newUnderlineColor = underlineColor;
484         }
485
486         if (waveUnderlineColor != null) {
487             newWaveUnderlineColor = waveUnderlineColor;
488         }
489
490         if (strikeThroughColor != null) {
491             newStrikeThroughColor = strikeThroughColor;
492         }
493
494         if (c.fontMode != FONT_MODE_DEFAULT
495                 || newFont != c.font // currently only equality
496
|| newForeColor != c.foreColor // currently only equality
497
|| newBackColor != c.backColor // currently only equality
498
|| newUnderlineColor != c.underlineColor // currently only equality
499
|| newWaveUnderlineColor != c.waveUnderlineColor // currently only equality
500
|| newStrikeThroughColor != c.strikeThroughColor // currently only equality
501
) {
502             return new Coloring(newFont, newFontMode,
503                                 newForeColor, newBackColor,
504                                 newUnderlineColor, newStrikeThroughColor, newWaveUnderlineColor
505                                );
506         } else {
507             return c; // return original coloring
508
}
509
510     }
511
512     /** All font, foreColor and backColor are the same. */
513     public boolean equals(Object JavaDoc o) {
514         if (o instanceof Coloring) {
515             Coloring c = (Coloring)o;
516             return ((font == null && c.font == null) || (font != null && font.equals(c.font)))
517                    && (fontMode == c.fontMode)
518                    && ((foreColor == null && c.foreColor == null)
519                        || (foreColor != null && foreColor.equals(c.foreColor)))
520                    && ((backColor == null && c.backColor == null)
521                        || (backColor != null && backColor.equals(c.backColor)))
522                    && ((underlineColor == null && c.underlineColor == null)
523                        || (underlineColor != null && underlineColor.equals(c.underlineColor)))
524                    && ((waveUnderlineColor == null && c.waveUnderlineColor == null)
525                        || (waveUnderlineColor != null && waveUnderlineColor.equals(c.waveUnderlineColor)))
526                    && ((strikeThroughColor == null && c.strikeThroughColor == null)
527                        || (strikeThroughColor != null
528                            && strikeThroughColor.equals(c.strikeThroughColor)));
529         }
530         return false;
531     }
532
533     public int hashCode() {
534         return font.hashCode() ^ foreColor.hashCode() ^ backColor.hashCode();
535     }
536
537     /** Derive a new coloring by changing the font and leaving
538     * the rest of the coloring (including the font-mode) unchanged.
539     */

540     public static Coloring changeFont(Coloring c, Font JavaDoc newFont) {
541         return changeFont(c, newFont, c.getFontMode());
542     }
543
544     /** Derive a new coloring by changing the font and font-mode and leaving
545     * the rest of the coloring unchanged.
546     */

547     public static Coloring changeFont(Coloring c, Font JavaDoc newFont, int newFontMode) {
548         if ((newFont == null && c.font == null)
549                 || (newFont != null && newFont.equals(c.font)
550                     && c.fontMode == newFontMode)
551            ) {
552             return c;
553         }
554
555         return new Coloring(newFont, c.foreColor, c.backColor);
556     }
557
558     /** Derive a new coloring by changing
559     * the foreground-color and its color-mode
560     * and leaving the rest of the coloring unchanged.
561     */

562     public static Coloring changeForeColor(Coloring c, Color JavaDoc newForeColor) {
563         if ((newForeColor == null && c.foreColor == null)
564                 || (newForeColor != null && newForeColor.equals(c.foreColor))
565            ) {
566             return c;
567         }
568
569         return new Coloring(c.font, newForeColor, c.backColor);
570     }
571
572     /** Derive a new coloring by changing
573     * the background-color and its color-mode
574     * and leaving the rest of the coloring unchanged.
575     */

576     public static Coloring changeBackColor(Coloring c, Color JavaDoc newBackColor) {
577         if ((newBackColor == null && c.backColor == null)
578                 || (newBackColor != null && newBackColor.equals(c.backColor))
579            ) {
580             return c;
581         }
582
583         return new Coloring(c.font, c.foreColor, newBackColor);
584     }
585
586     private void readObject(java.io.ObjectInputStream JavaDoc ois)
587     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
588         ois.defaultReadObject();
589
590         if (fontMode == 0) {
591             fontMode = FONT_MODE_DEFAULT;
592         }
593
594         checkCaches();
595     }
596
597     public String JavaDoc toString() {
598         return "font=" + font + ", fontMode=" + fontMode // NOI18N
599
+ ", foreColor=" + foreColor // NOI18N
600
+ ", backColor=" + backColor // NOI18N
601
+ ", underlineColor=" + underlineColor // NOI18N
602
+ ", waveUnderlineColor=" + waveUnderlineColor // NOI18N
603
+ ", strikeThroughColor=" + strikeThroughColor; // NOI18N
604
}
605
606     private static final WeakHashMap JavaDoc<AttributeSet JavaDoc, Coloring> colorings = new WeakHashMap JavaDoc<AttributeSet JavaDoc, Coloring>();
607
608     /**
609      * Converts <code>AttributeSet</code> to a <code>Coloring</code>. This method
610      * will extract any font related information as well as the 'well-known' colors
611      * from the <code>AttributeSet</code> passed in and will use them for creating
612      * a <code>Coloring</code> instance that will be returned.
613      *
614      * <p>The list of supported colors:
615      * <ul>
616      * <li><code>StyleConstants.Foreground</code>
617      * <li><code>StyleConstants.Background</code>
618      * <li><code>StyleConstants.Underline</code>
619      * <li><code>StyleConstants.StrikeThrough</code>
620      * <li><code>EditorStyleConstants.WaveUnderlineColor</code>
621      * </ul>
622      *
623      * @param as The <code>AttributeSet</code> to convert.
624      *
625      * @return <code>Coloring</code> that will contain font and color information
626      * from the supplied <code>AttributeSet</code>.
627      * @since 1.11
628      */

629     public static Coloring fromAttributeSet(AttributeSet JavaDoc as) {
630         synchronized (colorings) {
631             Coloring coloring = colorings.get(as);
632
633             if (coloring == null) {
634                 Object JavaDoc [] fontObj = toFont(as);
635
636                 coloring = new Coloring(
637                     (Font JavaDoc) fontObj[0],
638                     ((Integer JavaDoc) fontObj[1]).intValue(),
639                     (Color JavaDoc) as.getAttribute(StyleConstants.Foreground),
640                     (Color JavaDoc) as.getAttribute(StyleConstants.Background),
641                     (Color JavaDoc) as.getAttribute(StyleConstants.Underline),
642                     (Color JavaDoc) as.getAttribute(StyleConstants.StrikeThrough),
643                     findWaveUnderlineColorHack(as)
644                 );
645
646                 colorings.put(as, coloring);
647             }
648
649             return coloring;
650         }
651     }
652
653     // XXX: This would not be neccessary if we could simply depend on editor/settings.
654
private static Color JavaDoc findWaveUnderlineColorHack(AttributeSet JavaDoc as) {
655         Enumeration JavaDoc<?> names = as.getAttributeNames();
656         
657         while (names.hasMoreElements()) {
658             Object JavaDoc name = names.nextElement();
659             if (name != null && name.toString().equals("wave underline color")) { //NOI18N
660
return (Color JavaDoc) as.getAttribute(name);
661             }
662         }
663         
664         return null;
665     }
666     
667     private static Object JavaDoc [] toFont(AttributeSet JavaDoc as) {
668         int applyMode = 0;
669
670         // Determine font family
671
String JavaDoc fontFamily = null;
672         {
673             Object JavaDoc fontFamilyObj = as.getAttribute(StyleConstants.FontFamily);
674             if (fontFamilyObj instanceof String JavaDoc) {
675                 fontFamily = (String JavaDoc) fontFamilyObj;
676                 applyMode += Coloring.FONT_MODE_APPLY_NAME;
677             }
678         }
679
680         // Determine font size
681
int fontSize = 0;
682         {
683             Object JavaDoc fontSizeObj = as.getAttribute(StyleConstants.FontSize);
684             if (fontSizeObj instanceof Integer JavaDoc) {
685                 fontSize = ((Integer JavaDoc) fontSizeObj).intValue();
686                 applyMode += Coloring.FONT_MODE_APPLY_SIZE;
687             }
688         }
689
690         // Determine font style
691
int style = 0;
692         {
693             Object JavaDoc boldStyleObj = as.getAttribute(StyleConstants.Bold);
694             Object JavaDoc italicStyleObj = as.getAttribute(StyleConstants.Italic);
695
696             if (boldStyleObj != null || italicStyleObj != null) {
697                 if (Boolean.TRUE.equals(boldStyleObj)){
698                     style += Font.BOLD;
699                 }
700
701                 if (Boolean.TRUE.equals(italicStyleObj)){
702                     style += Font.ITALIC;
703                 }
704
705                 applyMode += Coloring.FONT_MODE_APPLY_STYLE;
706             }
707         }
708
709         return new Object JavaDoc [] {
710             new Font JavaDoc(fontFamily, style, fontSize),
711             new Integer JavaDoc(applyMode)
712         };
713     }
714     
715 }
716
Popular Tags