001 /*
002 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.plaf.synth;
026
027 /**
028 * A typesafe enumeration of colors that can be fetched from a style.
029 * <p>
030 * Each <code>SynthStyle</code> has a set of <code>ColorType</code>s that
031 * are accessed by way of the
032 * {@link SynthStyle#getColor(SynthContext, ColorType)} method.
033 * <code>SynthStyle</code>'s <code>installDefaults</code> will install
034 * the <code>FOREGROUND</code> color
035 * as the foreground of
036 * the Component, and the <code>BACKGROUND</code> color to the background of
037 * the component (assuming that you have not explicitly specified a
038 * foreground and background color). Some components
039 * support more color based properties, for
040 * example <code>JList</code> has the property
041 * <code>selectionForeground</code> which will be mapped to
042 * <code>FOREGROUND</code> with a component state of
043 * <code>SynthConstants.SELECTED</code>.
044 * <p>
045 * The following example shows a custom <code>SynthStyle</code> that returns
046 * a red Color for the <code>DISABLED</code> state, otherwise a black color.
047 * <pre>
048 * class MyStyle extends SynthStyle {
049 * private Color disabledColor = new ColorUIResource(Color.RED);
050 * private Color color = new ColorUIResource(Color.BLACK);
051 * protected Color getColorForState(SynthContext context, ColorType type){
052 * if (context.getComponentState() == SynthConstants.DISABLED) {
053 * return disabledColor;
054 * }
055 * return color;
056 * }
057 * }
058 * </pre>
059 *
060 * @version 1.19, 05/05/07
061 * @since 1.5
062 * @author Scott Violet
063 */
064 public class ColorType {
065 /**
066 * ColorType for the foreground of a region.
067 */
068 public static final ColorType FOREGROUND = new ColorType(
069 "Foreground");
070
071 /**
072 * ColorType for the background of a region.
073 */
074 public static final ColorType BACKGROUND = new ColorType(
075 "Background");
076
077 /**
078 * ColorType for the foreground of a region.
079 */
080 public static final ColorType TEXT_FOREGROUND = new ColorType(
081 "TextForeground");
082
083 /**
084 * ColorType for the background of a region.
085 */
086 public static final ColorType TEXT_BACKGROUND = new ColorType(
087 "TextBackground");
088
089 /**
090 * ColorType for the focus.
091 */
092 public static final ColorType FOCUS = new ColorType("Focus");
093
094 /**
095 * Maximum number of <code>ColorType</code>s.
096 */
097 public static final int MAX_COUNT;
098
099 private static int nextID;
100
101 private String description;
102 private int index;
103
104 static {
105 MAX_COUNT = Math.max(FOREGROUND.getID(), Math.max(BACKGROUND
106 .getID(), FOCUS.getID())) + 1;
107 }
108
109 /**
110 * Creates a new ColorType with the specified description.
111 *
112 * @param description String description of the ColorType.
113 */
114 protected ColorType(String description) {
115 if (description == null) {
116 throw new NullPointerException(
117 "ColorType must have a valid description");
118 }
119 this .description = description;
120 synchronized (ColorType.class) {
121 this .index = nextID++;
122 }
123 }
124
125 /**
126 * Returns a unique id, as an integer, for this ColorType.
127 *
128 * @return a unique id, as an integer, for this ColorType.
129 */
130 public final int getID() {
131 return index;
132 }
133
134 /**
135 * Returns the textual description of this <code>ColorType</code>.
136 * This is the same value that the <code>ColorType</code> was created
137 * with.
138 *
139 * @return the description of the string
140 */
141 public String toString() {
142 return description;
143 }
144 }
|