1. /*
  2. * @(#)SynthStyle.java 1.19 03/09/04
  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 java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.border.Border;
  11. import javax.swing.plaf.UIResource;
  12. /**
  13. * Bag of style properties.
  14. *
  15. * @version 1.19, 09/04/03
  16. * @author Scott Violet
  17. */
  18. abstract class SynthStyle {
  19. /**
  20. * Shared SynthGraphics.
  21. */
  22. private static final SynthGraphics SYNTH_GRAPHICS = new SynthGraphics();
  23. /**
  24. * Returns a SynthGraphics.
  25. *
  26. * @param context SynthContext indentifying requestor
  27. * @return SynthGraphics
  28. */
  29. public SynthGraphics getSynthGraphics(SynthContext context) {
  30. return SYNTH_GRAPHICS;
  31. }
  32. /**
  33. * Returns a Color for the specified state.
  34. *
  35. * @param state SynthContext indentifying requestor
  36. * @param type Type of color being requested.
  37. * @return Color
  38. */
  39. public Color getColor(SynthContext state, ColorType type) {
  40. return getColor(state.getComponent(), state.getRegion(),
  41. state.getComponentState(), type);
  42. }
  43. /**
  44. * Returns a Font for the specified state.
  45. *
  46. * @param state SynthContext indentifying requestor
  47. * @return Font
  48. */
  49. public Font getFont(SynthContext state) {
  50. return getFont(state.getComponent(), state.getRegion(),
  51. state.getComponentState());
  52. }
  53. public Color getColor(JComponent c, Region id, int state,
  54. ColorType type) {
  55. if (!id.isSubregion() &&
  56. (state & SynthConstants.ENABLED) == SynthConstants.ENABLED) {
  57. if (type == ColorType.BACKGROUND) {
  58. return c.getBackground();
  59. }
  60. else if (type == ColorType.FOREGROUND ||
  61. type == ColorType.TEXT_FOREGROUND) {
  62. return c.getForeground();
  63. }
  64. }
  65. return _getColor(c, id, state, type);
  66. }
  67. protected abstract Color _getColor(JComponent c, Region id, int state,
  68. ColorType type);
  69. public Font getFont(JComponent c, Region id, int state) {
  70. if (state == SynthConstants.ENABLED) {
  71. return c.getFont();
  72. }
  73. Font cFont = c.getFont();
  74. if (cFont != null && !(cFont instanceof UIResource)) {
  75. return cFont;
  76. }
  77. return _getFont(c, id, state);
  78. }
  79. protected abstract Font _getFont(JComponent c, Region id, int state);
  80. /**
  81. * Returns the Insets that are used to calculate sizing information.
  82. *
  83. * @param state SynthContext indentifying requestor
  84. * @param insets Insets to place return value in.
  85. * @return Sizing Insets.
  86. */
  87. public Insets getInsets(SynthContext state, Insets insets) {
  88. if (insets == null) {
  89. insets = new Insets(0, 0, 0, 0);
  90. }
  91. insets.top = insets.bottom = insets.left = insets.right = 0;
  92. return insets;
  93. }
  94. /**
  95. * Returns the Border for the passed in Component. This may return null.
  96. * <p>
  97. * The returned border should also render the focus indicator, as
  98. * appropriate.
  99. * <p>
  100. * Some components may not support borders for different states.
  101. *
  102. * @param state SynthContext indentifying requestor
  103. * @return SynthPainter for the Border.
  104. */
  105. public SynthPainter getBorderPainter(SynthContext state) {
  106. return null;
  107. }
  108. /**
  109. * Returns the Painter used to paint the background.
  110. *
  111. * @param state SynthContext indentifying requestor
  112. * @return SynthPainter for the background.
  113. */
  114. public SynthPainter getBackgroundPainter(SynthContext state) {
  115. return null;
  116. }
  117. /**
  118. * Returns true if the region is opaque.
  119. *
  120. * @param state SynthContext indentifying requestor
  121. * @return true if region is opaque.
  122. */
  123. public boolean isOpaque(SynthContext state) {
  124. return true;
  125. }
  126. /**
  127. * Getter for a region specific style property.
  128. *
  129. * @param state SynthContext indentifying requestor
  130. * @param key Property being requested.
  131. */
  132. public abstract Object get(SynthContext state, Object key);
  133. void installDefaults(SynthContext context, SynthUI ui) {
  134. // Special case the Border as this will likely change when the LAF
  135. // can have more control over this.
  136. if (!context.isSubregion()) {
  137. JComponent c = context.getComponent();
  138. Border border = c.getBorder();
  139. if (border == null || border instanceof UIResource) {
  140. c.setBorder(new SynthBorder(ui, getInsets(context, null)));
  141. }
  142. }
  143. installDefaults(context);
  144. }
  145. /**
  146. * Installs the necessary state from this Style onto <code>c</code>.
  147. *
  148. * @param context SynthContext identifying component to install properties
  149. * to.
  150. */
  151. public void installDefaults(SynthContext context) {
  152. if (!context.isSubregion()) {
  153. JComponent c = context.getComponent();
  154. Region region = context.getRegion();
  155. Font font = c.getFont();
  156. if (font == null || (font instanceof UIResource)) {
  157. c.setFont(_getFont(c, region, SynthUI.ENABLED));
  158. }
  159. Color background = c.getBackground();
  160. if (background == null || (background instanceof UIResource)) {
  161. c.setBackground(_getColor(c, region, SynthUI.ENABLED,
  162. ColorType.BACKGROUND));
  163. }
  164. Color foreground = c.getForeground();
  165. if (foreground == null || (foreground instanceof UIResource)) {
  166. c.setForeground(_getColor(c, region, SynthUI.ENABLED,
  167. ColorType.FOREGROUND));
  168. }
  169. // PENDING: there needs to be a better way to express this.
  170. if (region != Region.LABEL ||
  171. !(c instanceof javax.swing.table.TableCellRenderer)) {
  172. c.setOpaque(isOpaque(context));
  173. }
  174. }
  175. }
  176. /**
  177. * Uninstalls any state that this style installed on <code>c</code>.
  178. * <p>
  179. * Styles should NOT depend upon this being called, in certain cases
  180. * it may never be called.
  181. *
  182. * @param state SynthContext identifying component to install properties
  183. * to.
  184. */
  185. public void uninstallDefaults(SynthContext state) {
  186. if (!state.isSubregion()) {
  187. // NOTE: because getForeground, getBackground and getFont will look
  188. // at the parent Container, if we set them to null it may
  189. // mean we they return a non-null and non-UIResource value
  190. // preventing install from correctly settings its colors/font. For
  191. // this reason we do not uninstall the fg/bg/font.
  192. JComponent c = state.getComponent();
  193. Border border = c.getBorder();
  194. if (border instanceof UIResource) {
  195. c.setBorder(null);
  196. }
  197. }
  198. }
  199. /**
  200. * Convenience method to get an integer value from the Style.
  201. */
  202. public int getInt(SynthContext context, Object key, int defaultValue) {
  203. Object value = get(context, key);
  204. if (value instanceof Number) {
  205. return ((Number)value).intValue();
  206. }
  207. return defaultValue;
  208. }
  209. /**
  210. * Convenience method to get a Boolean value from the Style.
  211. */
  212. public boolean getBoolean(SynthContext state, Object key,
  213. boolean defaultValue) {
  214. Object value = get(state, key);
  215. if (value instanceof Boolean) {
  216. return ((Boolean)value).booleanValue();
  217. }
  218. return defaultValue;
  219. }
  220. /**
  221. * Convenience method to get a Boolean value from the Style.
  222. */
  223. public Icon getIcon(SynthContext state, Object key) {
  224. Object value = get(state, key);
  225. if (value instanceof Icon) {
  226. return (Icon)value;
  227. }
  228. return null;
  229. }
  230. /**
  231. * Convenience method to get a String value from the Style.
  232. */
  233. public String getString(SynthContext state, Object key,
  234. String defaultValue) {
  235. Object value = get(state, key);
  236. if (value instanceof String) {
  237. return (String)value;
  238. }
  239. return defaultValue;
  240. }
  241. }