1. /*
  2. * @(#)ColorType.java 1.11 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. /**
  9. * A typesafe enumeration of colors that can be fetched from a style.
  10. * <p>
  11. * Each <code>SynthStyle</code> has a set of <code>ColorType</code>s that
  12. * are accessed by way of the
  13. * {@link SynthStyle#getColor(SynthContext, ColorType)} method.
  14. * <code>SynthStyle</code>'s <code>installDefaults</code> will install
  15. * the <code>FOREGROUND</code> color
  16. * as the foreground of
  17. * the Component, and the <code>BACKGROUND</code> color to the background of
  18. * the component (assuming that you have not explicitly specified a
  19. * foreground and background color). Some components
  20. * support more color based properties, for
  21. * example <code>JList</code> has the property
  22. * <code>selectionForeground</code> which will be mapped to
  23. * <code>FOREGROUND</code> with a component state of
  24. * <code>SynthConstants.SELECTED</code>.
  25. * <p>
  26. * The following example shows a custom <code>SynthStyle</code> that returns
  27. * a red Color for the <code>DISABLED</code> state, otherwise a black color.
  28. * <pre>
  29. * class MyStyle extends SynthStyle {
  30. * private Color disabledColor = new ColorUIResource(Color.RED);
  31. * private Color color = new ColorUIResource(Color.BLACK);
  32. * protected Color getColorForState(SynthContext context, ColorType type){
  33. * if (context.getComponentState() == SynthConstants.DISABLED) {
  34. * return disabledColor;
  35. * }
  36. * return color;
  37. * }
  38. * }
  39. * </pre>
  40. *
  41. * @version 1.11, 12/19/03
  42. * @since 1.5
  43. * @author Scott Violet
  44. */
  45. public class ColorType {
  46. /**
  47. * ColorType for the foreground of a region.
  48. */
  49. public static final ColorType FOREGROUND = new ColorType("Foreground");
  50. /**
  51. * ColorType for the background of a region.
  52. */
  53. public static final ColorType BACKGROUND = new ColorType("Background");
  54. /**
  55. * ColorType for the foreground of a region.
  56. */
  57. public static final ColorType TEXT_FOREGROUND = new ColorType(
  58. "TextForeground");
  59. /**
  60. * ColorType for the background of a region.
  61. */
  62. public static final ColorType TEXT_BACKGROUND =new ColorType(
  63. "TextBackground");
  64. /**
  65. * ColorType for the focus.
  66. */
  67. public static final ColorType FOCUS = new ColorType("Focus");
  68. /**
  69. * Maximum number of <code>ColorType</code>s.
  70. */
  71. public static final int MAX_COUNT;
  72. private static int nextID;
  73. private String description;
  74. private int index;
  75. static {
  76. MAX_COUNT = Math.max(FOREGROUND.getID(), Math.max(
  77. BACKGROUND.getID(), FOCUS.getID())) + 1;
  78. }
  79. /**
  80. * Creates a new ColorType with the specified description.
  81. *
  82. * @param description String description of the ColorType.
  83. */
  84. protected ColorType(String description) {
  85. if (description == null) {
  86. throw new NullPointerException(
  87. "ColorType must have a valid description");
  88. }
  89. this.description = description;
  90. synchronized(ColorType.class) {
  91. this.index = nextID++;
  92. }
  93. }
  94. /**
  95. * Returns a unique id, as an integer, for this ColorType.
  96. *
  97. * @return a unique id, as an integer, for this ColorType.
  98. */
  99. public final int getID() {
  100. return index;
  101. }
  102. /**
  103. * Returns the textual description of this <code>ColorType</code>.
  104. *
  105. * @return description of the string.
  106. */
  107. public String toString() {
  108. return description;
  109. }
  110. }