1 /*
2  * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package javax.swing.plaf.synth;
26 
27 /**
28  * A typesafe enumeration of colors that can be fetched from a style.
29  * <p>
30  * Each <code>SynthStyle</code> has a set of <code>ColorType</code>s that
31  * are accessed by way of the
32  * {@link SynthStyle#getColor(SynthContext, ColorType)} method.
33  * <code>SynthStyle</code>'s <code>installDefaults</code> will install
34  * the <code>FOREGROUND</code> color
35  * as the foreground of
36  * the Component, and the <code>BACKGROUND</code> color to the background of
37  * the component (assuming that you have not explicitly specified a
38  * foreground and background color). Some components
39  * support more color based properties, for
40  * example <code>JList</code> has the property
41  * <code>selectionForeground</code> which will be mapped to
42  * <code>FOREGROUND</code> with a component state of
43  * <code>SynthConstants.SELECTED</code>.
44  * <p>
45  * The following example shows a custom <code>SynthStyle</code> that returns
46  * a red Color for the <code>DISABLED</code> state, otherwise a black color.
47  * <pre>
48  * class MyStyle extends SynthStyle {
49  *     private Color disabledColor = new ColorUIResource(Color.RED);
50  *     private Color color = new ColorUIResource(Color.BLACK);
51  *     protected Color getColorForState(SynthContext context, ColorType type){
52  *         if (context.getComponentState() == SynthConstants.DISABLED) {
53  *             return disabledColor;
54  *         }
55  *         return color;
56  *     }
57  * }
58  * </pre>
59  *
60  * @since 1.5
61  * @author Scott Violet
62  */
63 public class ColorType {
64     /**
65      * ColorType for the foreground of a region.
66      */
67     public static final ColorType FOREGROUND = new ColorType("Foreground");
68 
69     /**
70      * ColorType for the background of a region.
71      */
72     public static final ColorType BACKGROUND = new ColorType("Background");
73 
74     /**
75      * ColorType for the foreground of a region.
76      */
77     public static final ColorType TEXT_FOREGROUND = new ColorType(
78                                        "TextForeground");
79 
80     /**
81      * ColorType for the background of a region.
82      */
83     public static final ColorType TEXT_BACKGROUND =new ColorType(
84                                        "TextBackground");
85 
86     /**
87      * ColorType for the focus.
88      */
89     public static final ColorType FOCUS = new ColorType("Focus");
90 
91     /**
92      * Maximum number of <code>ColorType</code>s.
93      */
94     public static final int MAX_COUNT;
95 
96     private static int nextID;
97 
98     private String description;
99     private int index;
100 
101     static {
102         MAX_COUNT = Math.max(FOREGROUND.getID(), Math.max(
103                                  BACKGROUND.getID(), FOCUS.getID())) + 1;
104     }
105 
106     /**
107      * Creates a new ColorType with the specified description.
108      *
109      * @param description String description of the ColorType.
110      */
ColorType(String description)111     protected ColorType(String description) {
112         if (description == null) {
113             throw new NullPointerException(
114                           "ColorType must have a valid description");
115         }
116         this.description = description;
117         synchronized(ColorType.class) {
118             this.index = nextID++;
119         }
120     }
121 
122     /**
123      * Returns a unique id, as an integer, for this ColorType.
124      *
125      * @return a unique id, as an integer, for this ColorType.
126      */
getID()127     public final int getID() {
128         return index;
129     }
130 
131     /**
132      * Returns the textual description of this <code>ColorType</code>.
133      * This is the same value that the <code>ColorType</code> was created
134      * with.
135      *
136      * @return the description of the string
137      */
toString()138     public String toString() {
139         return description;
140     }
141 }
142