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. import javax.swing.*;
  9. import java.util.*;
  10. /**
  11. * An immutable transient object containing contextual information about
  12. * a <code>Region</code>. A <code>SynthContext</code> should only be
  13. * considered valid for the duration
  14. * of the method it is passed to. In other words you should not cache
  15. * a <code>SynthContext</code> that is passed to you and expect it to
  16. * remain valid.
  17. *
  18. * @version 1.9, 12/19/03
  19. * @since 1.5
  20. * @author Scott Violet
  21. */
  22. public class SynthContext {
  23. private static final Map contextMap;
  24. private JComponent component;
  25. private Region region;
  26. private SynthStyle style;
  27. private int state;
  28. static {
  29. contextMap = new HashMap();
  30. }
  31. static SynthContext getContext(Class type, JComponent component,
  32. Region region, SynthStyle style,
  33. int state) {
  34. SynthContext context = null;
  35. synchronized(contextMap) {
  36. java.util.List instances = (java.util.List)contextMap.get(type);
  37. if (instances != null) {
  38. int size = instances.size();
  39. if (size > 0) {
  40. context = (SynthContext)instances.remove(size - 1);
  41. }
  42. }
  43. }
  44. if (context == null) {
  45. try {
  46. context = (SynthContext)type.newInstance();
  47. } catch (IllegalAccessException iae) {
  48. } catch (InstantiationException ie) {
  49. }
  50. }
  51. context.reset(component, region, style, state);
  52. return context;
  53. }
  54. static void releaseContext(SynthContext context) {
  55. synchronized(contextMap) {
  56. java.util.List instances = (java.util.List)contextMap.get(
  57. context.getClass());
  58. if (instances == null) {
  59. instances = new ArrayList(5);
  60. contextMap.put(context.getClass(), instances);
  61. }
  62. instances.add(context);
  63. }
  64. }
  65. SynthContext() {
  66. }
  67. /**
  68. * Creates a SynthContext with the specified values. This is meant
  69. * for subclasses and custom UI implementors. You very rarely need to
  70. * construct a SynthContext, though some methods will take one.
  71. *
  72. * @param component JComponent
  73. * @param region Identifies the portion of the JComponent
  74. * @param style Style associated with the component
  75. * @param state State of the component as defined in SynthConstants.
  76. * @throws NullPointerException if component, region of style is null.
  77. */
  78. public SynthContext(JComponent component, Region region, SynthStyle style,
  79. int state) {
  80. if (component == null || region == null || style == null) {
  81. throw new NullPointerException(
  82. "You must supply a non-null component, region and style");
  83. }
  84. reset(component, region, style, state);
  85. }
  86. /**
  87. * Returns the hosting component containing the region.
  88. *
  89. * @return Hosting Component
  90. */
  91. public JComponent getComponent() {
  92. return component;
  93. }
  94. /**
  95. * Returns the Region identifying this state.
  96. *
  97. * @return Region of the hosting component
  98. */
  99. public Region getRegion() {
  100. return region;
  101. }
  102. /**
  103. * A convenience method for <code>getRegion().isSubregion()</code>.
  104. */
  105. boolean isSubregion() {
  106. return getRegion().isSubregion();
  107. }
  108. void setStyle(SynthStyle style) {
  109. this.style = style;
  110. }
  111. /**
  112. * Returns the style associated with this Region.
  113. *
  114. * @return SynthStyle associated with the region.
  115. */
  116. public SynthStyle getStyle() {
  117. return style;
  118. }
  119. void setComponentState(int state) {
  120. this.state = state;
  121. }
  122. /**
  123. * Returns the state of the widget, which is a bitmask of the
  124. * values defined in <code>SynthConstants</code>. A region will at least
  125. * be in one of
  126. * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
  127. * or <code>DISABLED</code>.
  128. *
  129. * @see SynthConstants
  130. * @return State of Component
  131. */
  132. public int getComponentState() {
  133. return state;
  134. }
  135. /**
  136. * Resets the state of the Context.
  137. */
  138. void reset(JComponent component, Region region, SynthStyle style,
  139. int state) {
  140. this.component = component;
  141. this.region = region;
  142. this.style = style;
  143. this.state = state;
  144. }
  145. void dispose() {
  146. this.component = null;
  147. this.style = null;
  148. releaseContext(this);
  149. }
  150. /**
  151. * Convenience method to get the Painter from the current SynthStyle.
  152. * This will NEVER return null.
  153. */
  154. SynthPainter getPainter() {
  155. SynthPainter painter = getStyle().getPainter(this);
  156. if (painter != null) {
  157. return painter;
  158. }
  159. return SynthPainter.NULL_PAINTER;
  160. }
  161. }