1. /*
  2. * @(#)SynthContext.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.gtk;
  8. import javax.swing.*;
  9. import java.util.*;
  10. /**
  11. * An immutable transient object containing contextual information about
  12. * a Region. If you are passed a SynthContext you should not keep a reference
  13. * to it as it may be reset and change.
  14. *
  15. * @version 1.7, 01/23/03
  16. * @author Scott Violet
  17. */
  18. class SynthContext {
  19. private static final Map contextMap;
  20. private JComponent component;
  21. private Region region;
  22. private SynthStyle style;
  23. private int state;
  24. static {
  25. contextMap = new HashMap();
  26. }
  27. static SynthContext getContext(Class type, JComponent component,
  28. Region region, SynthStyle style,
  29. int state) {
  30. SynthContext context = null;
  31. synchronized(contextMap) {
  32. java.util.List instances = (java.util.List)contextMap.get(type);
  33. if (instances != null) {
  34. int size = instances.size();
  35. if (size > 0) {
  36. context = (SynthContext)instances.remove(size - 1);
  37. }
  38. }
  39. }
  40. if (context == null) {
  41. // PENDING: this needs to be investigated when running in
  42. // a sandbox. It is possible this will throw as the
  43. // constructor is package private.
  44. try {
  45. context = (SynthContext)type.newInstance();
  46. } catch (IllegalAccessException iae) {
  47. System.out.println("could not create: " + iae);
  48. } catch (InstantiationException ie) {
  49. System.out.println("ie: " + ie);
  50. }
  51. }
  52. context.reset(component, region, style, state);
  53. return context;
  54. }
  55. static void releaseContext(SynthContext context) {
  56. synchronized(contextMap) {
  57. java.util.List instances = (java.util.List)contextMap.get(
  58. context.getClass());
  59. if (instances == null) {
  60. instances = new ArrayList(5);
  61. contextMap.put(context.getClass(), instances);
  62. }
  63. instances.add(context);
  64. }
  65. }
  66. SynthContext() {
  67. }
  68. SynthContext(JComponent component, Region region, SynthStyle style,
  69. int state) {
  70. reset(component, region, style, state);
  71. }
  72. /**
  73. * Returns the hosting component containg the region.
  74. *
  75. * @return Hosting Component
  76. */
  77. public JComponent getComponent() {
  78. return component;
  79. }
  80. /**
  81. * Returns the Region identifying this state.
  82. *
  83. * @return Region of the hosting component
  84. */
  85. public Region getRegion() {
  86. return region;
  87. }
  88. /**
  89. * A convenience method for <code>getRegion().isSubregion()</code>.
  90. */
  91. boolean isSubregion() {
  92. return getRegion().isSubregion();
  93. }
  94. void setStyle(SynthStyle style) {
  95. this.style = style;
  96. }
  97. /**
  98. * Returns the style associated with this Region.
  99. *
  100. * @return SynthStyle associated with the region.
  101. */
  102. public SynthStyle getStyle() {
  103. return style;
  104. }
  105. void setComponentState(int state) {
  106. this.state = state;
  107. }
  108. /**
  109. * Returns the state of the widget, which is a bitmask of the
  110. * values defined in SynthUI. A region will at least be in one of
  111. * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
  112. * or <code>DISABLED</code>.
  113. *
  114. * @return State of Component
  115. */
  116. public int getComponentState() {
  117. return state;
  118. }
  119. /**
  120. * Resets the state of the Context.
  121. */
  122. void reset(JComponent component, Region region, SynthStyle style,
  123. int state) {
  124. this.component = component;
  125. this.region = region;
  126. this.style = style;
  127. this.state = state;
  128. }
  129. void dispose() {
  130. this.component = null;
  131. this.style = null;
  132. releaseContext(this);
  133. }
  134. }