1 7 package javax.swing.plaf.synth; 8 9 import javax.swing.*; 10 import java.util.*; 11 12 24 public class SynthContext { 25 private static final Map contextMap; 26 27 private JComponent component; 28 private Region region; 29 private SynthStyle style; 30 private int state; 31 32 33 static { 34 contextMap = new HashMap(); 35 } 36 37 38 static SynthContext getContext(Class type, JComponent component, 39 Region region, SynthStyle style, 40 int state) { 41 SynthContext context = null; 42 43 synchronized(contextMap) { 44 java.util.List instances = (java.util.List )contextMap.get(type); 45 46 if (instances != null) { 47 int size = instances.size(); 48 49 if (size > 0) { 50 context = (SynthContext )instances.remove(size - 1); 51 } 52 } 53 } 54 if (context == null) { 55 try { 56 context = (SynthContext )type.newInstance(); 57 } catch (IllegalAccessException iae) { 58 } catch (InstantiationException ie) { 59 } 60 } 61 context.reset(component, region, style, state); 62 return context; 63 } 64 65 static void releaseContext(SynthContext context) { 66 synchronized(contextMap) { 67 java.util.List instances = (java.util.List )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 93 public SynthContext(JComponent component, Region region, SynthStyle style, 94 int state) { 95 if (component == null || region == null || style == null) { 96 throw new NullPointerException ( 97 "You must supply a non-null component, region and style"); 98 } 99 reset(component, region, style, state); 100 } 101 102 103 108 public JComponent getComponent() { 109 return component; 110 } 111 112 117 public Region getRegion() { 118 return region; 119 } 120 121 124 boolean isSubregion() { 125 return getRegion().isSubregion(); 126 } 127 128 void setStyle(SynthStyle style) { 129 this.style = style; 130 } 131 132 137 public SynthStyle getStyle() { 138 return style; 139 } 140 141 void setComponentState(int state) { 142 this.state = state; 143 } 144 145 155 public int getComponentState() { 156 return state; 157 } 158 159 162 void reset(JComponent component, Region region, SynthStyle 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 180 SynthPainter getPainter() { 181 SynthPainter painter = getStyle().getPainter(this); 182 183 if (painter != null) { 184 return painter; 185 } 186 return SynthPainter.NULL_PAINTER; 187 } 188 } 189 | Popular Tags |