KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SynthContext.java 1.9 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 import javax.swing.*;
10 import java.util.*;
11
12 /**
13  * An immutable transient object containing contextual information about
14  * a <code>Region</code>. A <code>SynthContext</code> should only be
15  * considered valid for the duration
16  * of the method it is passed to. In other words you should not cache
17  * a <code>SynthContext</code> that is passed to you and expect it to
18  * remain valid.
19  *
20  * @version 1.9, 12/19/03
21  * @since 1.5
22  * @author Scott Violet
23  */

24 public class SynthContext {
25     private static final Map contextMap;
26
27     private JComponent component;
28     private Region JavaDoc region;
29     private SynthStyle JavaDoc style;
30     private int state;
31
32
33     static {
34         contextMap = new HashMap();
35     }
36
37
38     static SynthContext JavaDoc getContext(Class JavaDoc type, JComponent component,
39                                    Region JavaDoc region, SynthStyle JavaDoc style,
40                                    int state) {
41         SynthContext JavaDoc context = null;
42
43         synchronized(contextMap) {
44             java.util.List JavaDoc instances = (java.util.List JavaDoc)contextMap.get(type);
45
46             if (instances != null) {
47                 int size = instances.size();
48
49                 if (size > 0) {
50                     context = (SynthContext JavaDoc)instances.remove(size - 1);
51                 }
52             }
53         }
54         if (context == null) {
55             try {
56                 context = (SynthContext JavaDoc)type.newInstance();
57             } catch (IllegalAccessException JavaDoc iae) {
58             } catch (InstantiationException JavaDoc ie) {
59             }
60         }
61         context.reset(component, region, style, state);
62         return context;
63     }
64
65     static void releaseContext(SynthContext JavaDoc context) {
66         synchronized(contextMap) {
67             java.util.List JavaDoc instances = (java.util.List JavaDoc)contextMap.get(
68                                        context.getClass());
69
70             if (instances == null) {
71                 instances = new ArrayList(5);
72                 contextMap.put(context.getClass(), instances);
73             }
74             instances.add(context);
75         }
76     }
77
78
79     SynthContext() {
80     }
81
82     /**
83      * Creates a SynthContext with the specified values. This is meant
84      * for subclasses and custom UI implementors. You very rarely need to
85      * construct a SynthContext, though some methods will take one.
86      *
87      * @param component JComponent
88      * @param region Identifies the portion of the JComponent
89      * @param style Style associated with the component
90      * @param state State of the component as defined in SynthConstants.
91      * @throws NullPointerException if component, region of style is null.
92      */

93     public SynthContext(JComponent component, Region JavaDoc region, SynthStyle JavaDoc style,
94                         int state) {
95         if (component == null || region == null || style == null) {
96             throw new NullPointerException JavaDoc(
97                 "You must supply a non-null component, region and style");
98         }
99         reset(component, region, style, state);
100     }
101
102
103     /**
104      * Returns the hosting component containing the region.
105      *
106      * @return Hosting Component
107      */

108     public JComponent getComponent() {
109         return component;
110     }
111
112     /**
113      * Returns the Region identifying this state.
114      *
115      * @return Region of the hosting component
116      */

117     public Region JavaDoc getRegion() {
118         return region;
119     }
120
121     /**
122      * A convenience method for <code>getRegion().isSubregion()</code>.
123      */

124     boolean isSubregion() {
125         return getRegion().isSubregion();
126     }
127
128     void setStyle(SynthStyle JavaDoc style) {
129         this.style = style;
130     }
131
132     /**
133      * Returns the style associated with this Region.
134      *
135      * @return SynthStyle associated with the region.
136      */

137     public SynthStyle JavaDoc getStyle() {
138         return style;
139     }
140
141     void setComponentState(int state) {
142         this.state = state;
143     }
144
145     /**
146      * Returns the state of the widget, which is a bitmask of the
147      * values defined in <code>SynthConstants</code>. A region will at least
148      * be in one of
149      * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
150      * or <code>DISABLED</code>.
151      *
152      * @see SynthConstants
153      * @return State of Component
154      */

155     public int getComponentState() {
156         return state;
157     }
158
159     /**
160      * Resets the state of the Context.
161      */

162     void reset(JComponent component, Region JavaDoc region, SynthStyle JavaDoc style,
163                int state) {
164         this.component = component;
165         this.region = region;
166         this.style = style;
167         this.state = state;
168     }
169
170     void dispose() {
171         this.component = null;
172         this.style = null;
173         releaseContext(this);
174     }
175
176     /**
177      * Convenience method to get the Painter from the current SynthStyle.
178      * This will NEVER return null.
179      */

180     SynthPainter JavaDoc getPainter() {
181         SynthPainter JavaDoc painter = getStyle().getPainter(this);
182
183         if (painter != null) {
184             return painter;
185         }
186         return SynthPainter.NULL_PAINTER;
187     }
188 }
189
Popular Tags