J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package javax.swing.plaf.synth; |
| 26 | |
| 27 | import javax.swing.*; |
| 28 | import java.util.*; |
| 29 | |
| 30 | /** |
| 31 | * An immutable transient object containing contextual information about |
| 32 | * a <code>Region</code>. A <code>SynthContext</code> should only be |
| 33 | * considered valid for the duration |
| 34 | * of the method it is passed to. In other words you should not cache |
| 35 | * a <code>SynthContext</code> that is passed to you and expect it to |
| 36 | * remain valid. |
| 37 | * |
| 38 | * @since 1.5 |
| 39 | * @author Scott Violet |
| 40 | */ |
| 41 | public class SynthContext { |
| 42 | private static final Map contextMap; |
| 43 | |
| 44 | private JComponent component; |
| 45 | private Region region; |
| 46 | private SynthStyle style; |
| 47 | private int state; |
| 48 | |
| 49 | |
| 50 | static { |
| 51 | contextMap = new HashMap(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static SynthContext getContext(Class type, JComponent component, |
| 56 | Region region, SynthStyle style, |
| 57 | int state) { |
| 58 | SynthContext context = null; |
| 59 | |
| 60 | synchronized(contextMap) { |
| 61 | java.util.List instances = (java.util.List)contextMap.get(type); |
| 62 | |
| 63 | if (instances != null) { |
| 64 | int size = instances.size(); |
| 65 | |
| 66 | if (size > 0) { |
| 67 | context = (SynthContext)instances.remove(size - 1); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | if (context == null) { |
| 72 | try { |
| 73 | context = (SynthContext)type.newInstance(); |
| 74 | } catch (IllegalAccessException iae) { |
| 75 | } catch (InstantiationException ie) { |
| 76 | } |
| 77 | } |
| 78 | context.reset(component, region, style, state); |
| 79 | return context; |
| 80 | } |
| 81 | |
| 82 | static void releaseContext(SynthContext context) { |
| 83 | synchronized(contextMap) { |
| 84 | java.util.List instances = (java.util.List)contextMap.get( |
| 85 | context.getClass()); |
| 86 | |
| 87 | if (instances == null) { |
| 88 | instances = new ArrayList(5); |
| 89 | contextMap.put(context.getClass(), instances); |
| 90 | } |
| 91 | instances.add(context); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | SynthContext() { |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates a SynthContext with the specified values. This is meant |
| 101 | * for subclasses and custom UI implementors. You very rarely need to |
| 102 | * construct a SynthContext, though some methods will take one. |
| 103 | * |
| 104 | * @param component JComponent |
| 105 | * @param region Identifies the portion of the JComponent |
| 106 | * @param style Style associated with the component |
| 107 | * @param state State of the component as defined in SynthConstants. |
| 108 | * @throws NullPointerException if component, region of style is null. |
| 109 | */ |
| 110 | public SynthContext(JComponent component, Region region, SynthStyle style, |
| 111 | int state) { |
| 112 | if (component == null || region == null || style == null) { |
| 113 | throw new NullPointerException( |
| 114 | "You must supply a non-null component, region and style"); |
| 115 | } |
| 116 | reset(component, region, style, state); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Returns the hosting component containing the region. |
| 122 | * |
| 123 | * @return Hosting Component |
| 124 | */ |
| 125 | public JComponent getComponent() { |
| 126 | return component; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the Region identifying this state. |
| 131 | * |
| 132 | * @return Region of the hosting component |
| 133 | */ |
| 134 | public Region getRegion() { |
| 135 | return region; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * A convenience method for <code>getRegion().isSubregion()</code>. |
| 140 | */ |
| 141 | boolean isSubregion() { |
| 142 | return getRegion().isSubregion(); |
| 143 | } |
| 144 | |
| 145 | void setStyle(SynthStyle style) { |
| 146 | this.style = style; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the style associated with this Region. |
| 151 | * |
| 152 | * @return SynthStyle associated with the region. |
| 153 | */ |
| 154 | public SynthStyle getStyle() { |
| 155 | return style; |
| 156 | } |
| 157 | |
| 158 | void setComponentState(int state) { |
| 159 | this.state = state; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns the state of the widget, which is a bitmask of the |
| 164 | * values defined in <code>SynthConstants</code>. A region will at least |
| 165 | * be in one of |
| 166 | * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code> |
| 167 | * or <code>DISABLED</code>. |
| 168 | * |
| 169 | * @see SynthConstants |
| 170 | * @return State of Component |
| 171 | */ |
| 172 | public int getComponentState() { |
| 173 | return state; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Resets the state of the Context. |
| 178 | */ |
| 179 | void reset(JComponent component, Region region, SynthStyle style, |
| 180 | int state) { |
| 181 | this.component = component; |
| 182 | this.region = region; |
| 183 | this.style = style; |
| 184 | this.state = state; |
| 185 | } |
| 186 | |
| 187 | void dispose() { |
| 188 | this.component = null; |
| 189 | this.style = null; |
| 190 | releaseContext(this); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Convenience method to get the Painter from the current SynthStyle. |
| 195 | * This will NEVER return null. |
| 196 | */ |
| 197 | SynthPainter getPainter() { |
| 198 | SynthPainter painter = getStyle().getPainter(this); |
| 199 | |
| 200 | if (painter != null) { |
| 201 | return painter; |
| 202 | } |
| 203 | return SynthPainter.NULL_PAINTER; |
| 204 | } |
| 205 | } |