KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > ColorType


1 /*
2  * @(#)ColorType.java 1.11 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.synth;
8
9 /**
10  * A typesafe enumeration of colors that can be fetched from a style.
11  * <p>
12  * Each <code>SynthStyle</code> has a set of <code>ColorType</code>s that
13  * are accessed by way of the
14  * {@link SynthStyle#getColor(SynthContext, ColorType)} method.
15  * <code>SynthStyle</code>'s <code>installDefaults</code> will install
16  * the <code>FOREGROUND</code> color
17  * as the foreground of
18  * the Component, and the <code>BACKGROUND</code> color to the background of
19  * the component (assuming that you have not explicitly specified a
20  * foreground and background color). Some components
21  * support more color based properties, for
22  * example <code>JList</code> has the property
23  * <code>selectionForeground</code> which will be mapped to
24  * <code>FOREGROUND</code> with a component state of
25  * <code>SynthConstants.SELECTED</code>.
26  * <p>
27  * The following example shows a custom <code>SynthStyle</code> that returns
28  * a red Color for the <code>DISABLED</code> state, otherwise a black color.
29  * <pre>
30  * class MyStyle extends SynthStyle {
31  * private Color disabledColor = new ColorUIResource(Color.RED);
32  * private Color color = new ColorUIResource(Color.BLACK);
33  * protected Color getColorForState(SynthContext context, ColorType type){
34  * if (context.getComponentState() == SynthConstants.DISABLED) {
35  * return disabledColor;
36  * }
37  * return color;
38  * }
39  * }
40  * </pre>
41  *
42  * @version 1.11, 12/19/03
43  * @since 1.5
44  * @author Scott Violet
45  */

46 public class ColorType {
47     /**
48      * ColorType for the foreground of a region.
49      */

50     public static final ColorType JavaDoc FOREGROUND = new ColorType JavaDoc("Foreground");
51
52     /**
53      * ColorType for the background of a region.
54      */

55     public static final ColorType JavaDoc BACKGROUND = new ColorType JavaDoc("Background");
56
57     /**
58      * ColorType for the foreground of a region.
59      */

60     public static final ColorType JavaDoc TEXT_FOREGROUND = new ColorType JavaDoc(
61                                        "TextForeground");
62
63     /**
64      * ColorType for the background of a region.
65      */

66     public static final ColorType JavaDoc TEXT_BACKGROUND =new ColorType JavaDoc(
67                                        "TextBackground");
68
69     /**
70      * ColorType for the focus.
71      */

72     public static final ColorType JavaDoc FOCUS = new ColorType JavaDoc("Focus");
73
74     /**
75      * Maximum number of <code>ColorType</code>s.
76      */

77     public static final int MAX_COUNT;
78
79     private static int nextID;
80
81     private String JavaDoc description;
82     private int index;
83
84     static {
85         MAX_COUNT = Math.max(FOREGROUND.getID(), Math.max(
86                                  BACKGROUND.getID(), FOCUS.getID())) + 1;
87     }
88
89     /**
90      * Creates a new ColorType with the specified description.
91      *
92      * @param description String description of the ColorType.
93      */

94     protected ColorType(String JavaDoc description) {
95         if (description == null) {
96             throw new NullPointerException JavaDoc(
97                           "ColorType must have a valid description");
98         }
99         this.description = description;
100         synchronized(ColorType JavaDoc.class) {
101             this.index = nextID++;
102         }
103     }
104
105     /**
106      * Returns a unique id, as an integer, for this ColorType.
107      *
108      * @return a unique id, as an integer, for this ColorType.
109      */

110     public final int getID() {
111         return index;
112     }
113
114     /**
115      * Returns the textual description of this <code>ColorType</code>.
116      *
117      * @return description of the string.
118      */

119     public String JavaDoc toString() {
120         return description;
121     }
122 }
123
Popular Tags