001 /*
002 * Copyright 2002-2003 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 import javax.swing.*;
028 import java.util.*;
029
030 /**
031 * An immutable transient object containing contextual information about
032 * a <code>Region</code>. A <code>SynthContext</code> should only be
033 * considered valid for the duration
034 * of the method it is passed to. In other words you should not cache
035 * a <code>SynthContext</code> that is passed to you and expect it to
036 * remain valid.
037 *
038 * @version 1.16, 05/05/07
039 * @since 1.5
040 * @author Scott Violet
041 */
042 public class SynthContext {
043 private static final Map contextMap;
044
045 private JComponent component;
046 private Region region;
047 private SynthStyle style;
048 private int state;
049
050 static {
051 contextMap = new HashMap();
052 }
053
054 static SynthContext getContext(Class type, JComponent component,
055 Region region, SynthStyle style, int state) {
056 SynthContext context = null;
057
058 synchronized (contextMap) {
059 java.util.List instances = (java.util.List) contextMap
060 .get(type);
061
062 if (instances != null) {
063 int size = instances.size();
064
065 if (size > 0) {
066 context = (SynthContext) instances.remove(size - 1);
067 }
068 }
069 }
070 if (context == null) {
071 try {
072 context = (SynthContext) type.newInstance();
073 } catch (IllegalAccessException iae) {
074 } catch (InstantiationException ie) {
075 }
076 }
077 context.reset(component, region, style, state);
078 return context;
079 }
080
081 static void releaseContext(SynthContext context) {
082 synchronized (contextMap) {
083 java.util.List instances = (java.util.List) contextMap
084 .get(context.getClass());
085
086 if (instances == null) {
087 instances = new ArrayList(5);
088 contextMap.put(context.getClass(), instances);
089 }
090 instances.add(context);
091 }
092 }
093
094 SynthContext() {
095 }
096
097 /**
098 * Creates a SynthContext with the specified values. This is meant
099 * for subclasses and custom UI implementors. You very rarely need to
100 * construct a SynthContext, though some methods will take one.
101 *
102 * @param component JComponent
103 * @param region Identifies the portion of the JComponent
104 * @param style Style associated with the component
105 * @param state State of the component as defined in SynthConstants.
106 * @throws NullPointerException if component, region of style is null.
107 */
108 public SynthContext(JComponent component, Region region,
109 SynthStyle style, int state) {
110 if (component == null || region == null || style == null) {
111 throw new NullPointerException(
112 "You must supply a non-null component, region and style");
113 }
114 reset(component, region, style, state);
115 }
116
117 /**
118 * Returns the hosting component containing the region.
119 *
120 * @return Hosting Component
121 */
122 public JComponent getComponent() {
123 return component;
124 }
125
126 /**
127 * Returns the Region identifying this state.
128 *
129 * @return Region of the hosting component
130 */
131 public Region getRegion() {
132 return region;
133 }
134
135 /**
136 * A convenience method for <code>getRegion().isSubregion()</code>.
137 */
138 boolean isSubregion() {
139 return getRegion().isSubregion();
140 }
141
142 void setStyle(SynthStyle style) {
143 this .style = style;
144 }
145
146 /**
147 * Returns the style associated with this Region.
148 *
149 * @return SynthStyle associated with the region.
150 */
151 public SynthStyle getStyle() {
152 return style;
153 }
154
155 void setComponentState(int state) {
156 this .state = state;
157 }
158
159 /**
160 * Returns the state of the widget, which is a bitmask of the
161 * values defined in <code>SynthConstants</code>. A region will at least
162 * be in one of
163 * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
164 * or <code>DISABLED</code>.
165 *
166 * @see SynthConstants
167 * @return State of Component
168 */
169 public int getComponentState() {
170 return state;
171 }
172
173 /**
174 * Resets the state of the Context.
175 */
176 void reset(JComponent component, Region region, SynthStyle style,
177 int state) {
178 this .component = component;
179 this .region = region;
180 this .style = style;
181 this .state = state;
182 }
183
184 void dispose() {
185 this .component = null;
186 this .style = null;
187 releaseContext(this );
188 }
189
190 /**
191 * Convenience method to get the Painter from the current SynthStyle.
192 * This will NEVER return null.
193 */
194 SynthPainter getPainter() {
195 SynthPainter painter = getStyle().getPainter(this);
196
197 if (painter != null) {
198 return painter;
199 }
200 return SynthPainter.NULL_PAINTER;
201 }
202 }
|