KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > State


1 /*
2  * @(#)State.java 1.4 08/01/28
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11 import javax.swing.JComponent JavaDoc;
12 import javax.swing.plaf.synth.SynthConstants JavaDoc;
13
14 /**
15  * <p>Represents a built in, or custom, state in Nimbus.</p>
16  *
17  * <p>Synth provides several built in states, which are:
18  * <ul>
19  * <li>Enabled</li>
20  * <li>Mouse Over</li>
21  * <li>Pressed</li>
22  * <li>Disabled</li>
23  * <li>Focused</li>
24  * <li>Selected</li>
25  * <li>Default</li>
26  * </ul>
27  *
28  * <p>However, there are many more states that could be described in a LookAndFeel, and it
29  * would be nice to style components differently based on these different states.
30  * For example, a progress bar could be "indeterminate". It would be very convenient
31  * to allow this to be defined as a "state".</p>
32  *
33  * <p>This class, State, is intended to be used for such situations.
34  * Simply implement the abstract #isInState method. It returns true if the given
35  * JComponent is "in this state", false otherwise. This method will be called
36  * <em>many</em> times in <em>performance sensitive loops</em>. It must execute
37  * very quickly.</p>
38  *
39  * <p>For example, the following might be an implementation of a custom
40  * "Indeterminate" state for JProgressBars:</p>
41  *
42  * <pre><code>
43  * public final class IndeterminateState extends State&lt;JProgressBar&gt; {
44  * public IndeterminateState() {
45  * super("Indeterminate");
46  * }
47  *
48  * @Override
49  * protected boolean isInState(JProgressBar c) {
50  * return c.isIndeterminate();
51  * }
52  * }
53  * </code></pre>
54  */

55 public abstract class State<T extends JComponent JavaDoc>{
56     static final Map JavaDoc<String JavaDoc, StandardState> standardStates = new HashMap JavaDoc<String JavaDoc, StandardState>(7);
57     static final State Enabled = new StandardState(SynthConstants.ENABLED);
58     static final State MouseOver = new StandardState(SynthConstants.MOUSE_OVER);
59     static final State Pressed = new StandardState(SynthConstants.PRESSED);
60     static final State Disabled = new StandardState(SynthConstants.DISABLED);
61     static final State Focused = new StandardState(SynthConstants.FOCUSED);
62     static final State Selected = new StandardState(SynthConstants.SELECTED);
63     static final State Default = new StandardState(SynthConstants.DEFAULT);
64     
65     private String JavaDoc name;
66
67     /**
68      * <p>Create a new custom State. Specify the name for the state. The name should
69      * be unique within the states set for any one particular component.
70      * The name of the state should coincide with the name used in UIDefaults.</p>
71      *
72      * <p>For example, the following would be correct:</p>
73      * <pre><code>
74      * defaults.put("Button.States", "Enabled, Foo, Disabled");
75      * defaults.put("Button.Foo", new FooState("Foo"));
76      * </code></pre>
77      *
78      * @param name a simple user friendly name for the state, such as "Indeterminate"
79      * or "EmbeddedPanel" or "Blurred". It is customary to use camel case,
80      * with the first letter capitalized.
81      */

82     protected State(String JavaDoc name) {
83         this.name = name;
84     }
85     
86     @Override JavaDoc public String JavaDoc toString() { return name; }
87     
88     /**
89      * <p>This is the main entry point, called by NimbusStyle.</p>
90      *
91      * <p>There are both custom states and standard states. Standard states
92      * correlate to the states defined in SynthConstants. When a UI delegate
93      * constructs a SynthContext, it specifies the state that the component is
94      * in according to the states defined in SynthConstants. Our NimbusStyle
95      * will then take this state, and query each State instance in the style
96      * asking whether isInState(c, s).</p>
97      *
98      * <p>Now, only the standard states care about the "s" param. So we have
99      * this odd arrangement:</p>
100      * <ul>
101      * <li>NimbusStyle calls State.isInState(c, s)</li>
102      * <li>State.isInState(c, s) simply delegates to State.isInState(c)</li>
103      * <li><em>EXCEPT</em>, StandardState overrides State.isInState(c, s) and
104      * returns directly from that method after checking its state, and
105      * does not call isInState(c) (since it is not needed for standard states).</li>
106      * </ul>
107      */

108     boolean isInState(T c, int s) {
109         return isInState(c);
110     }
111     
112     /**
113      * <p>Gets whether the specified JComponent is in the custom state represented
114      * by this class. <em>This is an extremely performance sensitive loop.</em>
115      * Please take proper precautions to ensure that it executes quickly.</p>
116      *
117      * <p>Nimbus uses this method to help determine what state a JComponent is
118      * in. For example, a custom State could exist for JProgressBar such that
119      * it would return <code>true</code> when the progress bar is indeterminate.
120      * Such an implementation of this method would simply be:</p>
121      *
122      * <pre><code> return c.isIndeterminate();</code></pre>
123      *
124      * @param c the JComponent to test. This will never be null.
125      * @return true if <code>c</code> is in the custom state represented by
126      * this <code>State</code> instance
127      */

128     protected abstract boolean isInState(T c);
129     
130     String JavaDoc getName() { return name; }
131     
132     static boolean isStandardStateName(String JavaDoc name) {
133         return standardStates.containsKey(name);
134     }
135     
136     static StandardState getStandardState(String JavaDoc name) {
137         return standardStates.get(name);
138     }
139     
140     static final class StandardState extends State<JComponent JavaDoc> {
141         private int state;
142         
143         private StandardState(int state) {
144             super(toString(state));
145             this.state = state;
146             standardStates.put(getName(), this);
147         }
148
149         public int getState() {
150             return state;
151         }
152
153         @Override JavaDoc
154         boolean isInState(JComponent JavaDoc c, int s) {
155             return (s & state) == state;
156         }
157         
158         @Override JavaDoc
159         protected boolean isInState(JComponent JavaDoc c) {
160             throw new AssertionError JavaDoc("This method should never be called");
161         }
162         
163         private static String JavaDoc toString(int state) {
164             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
165             if ((state & SynthConstants.DEFAULT) == SynthConstants.DEFAULT) {
166                 buffer.append("Default");
167             }
168             if ((state & SynthConstants.DISABLED) == SynthConstants.DISABLED) {
169                 if (buffer.length() > 0) buffer.append("+");
170                 buffer.append("Disabled");
171             }
172             if ((state & SynthConstants.ENABLED) == SynthConstants.ENABLED) {
173                 if (buffer.length() > 0) buffer.append("+");
174                 buffer.append("Enabled");
175             }
176             if ((state & SynthConstants.FOCUSED) == SynthConstants.FOCUSED) {
177                 if (buffer.length() > 0) buffer.append("+");
178                 buffer.append("Focused");
179             }
180             if ((state & SynthConstants.MOUSE_OVER) == SynthConstants.MOUSE_OVER) {
181                 if (buffer.length() > 0) buffer.append("+");
182                 buffer.append("MouseOver");
183             }
184             if ((state & SynthConstants.PRESSED) == SynthConstants.PRESSED) {
185                 if (buffer.length() > 0) buffer.append("+");
186                 buffer.append("Pressed");
187             }
188             if ((state & SynthConstants.SELECTED) == SynthConstants.SELECTED) {
189                 if (buffer.length() > 0) buffer.append("+");
190                 buffer.append("Selected");
191             }
192             return buffer.toString();
193         }
194     }
195 }
196
Popular Tags