1. /*
  2. * @(#)AbstractColorChooserPanel.java 1.19 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 javax.swing.colorchooser;
  8. import java.awt.*;
  9. import java.io.Serializable;
  10. import javax.swing.*;
  11. import javax.swing.event.*;
  12. /**
  13. * This is the abstract superclass for color choosers. If you want to add
  14. * a new color chooser panel into a <code>JColorChooser</code>, subclass
  15. * this class.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is
  20. * appropriate for short term storage or RMI between applications running
  21. * the same version of Swing. As of 1.4, support for long term storage
  22. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  23. * has been added to the <code>java.beans</code> package.
  24. * Please see {@link java.beans.XMLEncoder}.
  25. *
  26. * @version 1.19 01/23/03
  27. * @author Tom Santos
  28. * @author Steve Wilson
  29. */
  30. public abstract class AbstractColorChooserPanel extends JPanel {
  31. /**
  32. *
  33. */
  34. private JColorChooser chooser;
  35. /**
  36. *
  37. */
  38. private ChangeListener colorListener;
  39. /**
  40. *
  41. */
  42. private boolean dirty = true;
  43. /**
  44. * Invoked automatically when the model's state changes.
  45. * It is also called by <code>installChooserPanel</code> to allow
  46. * you to set up the initial state of your chooser.
  47. * Override this method to update your <code>ChooserPanel</code>.
  48. */
  49. public abstract void updateChooser();
  50. /**
  51. * Builds a new chooser panel.
  52. */
  53. protected abstract void buildChooser();
  54. /**
  55. * Returns a string containing the display name of the panel.
  56. * @return the name of the display panel
  57. */
  58. public abstract String getDisplayName();
  59. /**
  60. * Provides a hint to the look and feel as to the
  61. * <code>KeyEvent.VK</code> constant that can be used as a mnemonic to
  62. * access the panel. A return value <= 0 indicates there is no mnemonic.
  63. * <p>
  64. * The return value here is a hint, it is ultimately up to the look
  65. * and feel to honor the return value in some meaningful way.
  66. * <p>
  67. * This implementation returns 0, indicating the
  68. * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
  69. * subclasses wishing a mnemonic will need to override this.
  70. *
  71. * @return KeyEvent.VK constant identifying the mnemonic; <= 0 for no
  72. * mnemonic
  73. * @see #getDisplayedMnemonicIndex
  74. * @since 1.4
  75. */
  76. public int getMnemonic() {
  77. return 0;
  78. }
  79. /**
  80. * Provides a hint to the look and feel as to the index of the character in
  81. * <code>getDisplayName</code> that should be visually identified as the
  82. * mnemonic. The look and feel should only use this if
  83. * <code>getMnemonic</code> returns a value > 0.
  84. * <p>
  85. * The return value here is a hint, it is ultimately up to the look
  86. * and feel to honor the return value in some meaningful way. For example,
  87. * a look and feel may wish to render each
  88. * <code>AbstractColorChooserPanel</code> in a <code>JTabbedPane</code>,
  89. * and further use this return value to underline a character in
  90. * the <code>getDisplayName</code>.
  91. * <p>
  92. * This implementation returns -1, indicating the
  93. * <code>AbstractColorChooserPanel</code> does not support a mnemonic,
  94. * subclasses wishing a mnemonic will need to override this.
  95. *
  96. * @return Character index to render mnemonic for; -1 to provide no
  97. * visual identifier for this panel.
  98. * @see #getMnemonic
  99. * @since 1.4
  100. */
  101. public int getDisplayedMnemonicIndex() {
  102. return -1;
  103. }
  104. /**
  105. * Returns the small display icon for the panel.
  106. * @return the small display icon
  107. */
  108. public abstract Icon getSmallDisplayIcon();
  109. /**
  110. * Returns the large display icon for the panel.
  111. * @return the large display icon
  112. */
  113. public abstract Icon getLargeDisplayIcon();
  114. /**
  115. * Invoked when the panel is added to the chooser.
  116. * If you override this, be sure to call <code>super</code>.
  117. * @param enclosingChooser the panel to be added
  118. * @exception RuntimeException if the chooser panel has already been
  119. * installed
  120. */
  121. public void installChooserPanel(JColorChooser enclosingChooser) {
  122. if (chooser != null) {
  123. throw new RuntimeException ("This chooser panel is already installed");
  124. }
  125. chooser = enclosingChooser;
  126. buildChooser();
  127. updateChooser();
  128. colorListener = new ModelListener();
  129. getColorSelectionModel().addChangeListener(colorListener);
  130. }
  131. /**
  132. * Invoked when the panel is removed from the chooser.
  133. * If override this, be sure to call <code>super</code>.
  134. */
  135. public void uninstallChooserPanel(JColorChooser enclosingChooser) {
  136. getColorSelectionModel().removeChangeListener(colorListener);
  137. chooser = null;
  138. }
  139. /**
  140. * Returns the model that the chooser panel is editing.
  141. * @return the <code>ColorSelectionModel</code> model this panel
  142. * is editing
  143. */
  144. public ColorSelectionModel getColorSelectionModel() {
  145. return chooser.getSelectionModel();
  146. }
  147. /**
  148. * Returns the color that is currently selected.
  149. * @return the <code>Color</code> that is selected
  150. */
  151. protected Color getColorFromModel() {
  152. return getColorSelectionModel().getSelectedColor();
  153. }
  154. /**
  155. * Draws the panel.
  156. * @param g the <code>Graphics</code> object
  157. */
  158. public void paint(Graphics g) {
  159. if (dirty) {
  160. updateChooser();
  161. dirty = false;
  162. }
  163. super.paint(g);
  164. }
  165. /**
  166. * Returns an integer from the defaults table. If <code>key</code> does
  167. * not map to a valid <code>Integer</code>, <code>default</code> is
  168. * returned.
  169. *
  170. * @param key an <code>Object</code> specifying the int
  171. * @param defaultValue Returned value if <code>key</code> is not available,
  172. * or is not an Integer
  173. * @return the int
  174. */
  175. static int getInt(Object key, int defaultValue) {
  176. Object value = UIManager.get(key);
  177. if (value instanceof Integer) {
  178. return ((Integer)value).intValue();
  179. }
  180. if (value instanceof String) {
  181. try {
  182. return Integer.parseInt((String)value);
  183. } catch (NumberFormatException nfe) {}
  184. }
  185. return defaultValue;
  186. }
  187. /**
  188. *
  189. */
  190. class ModelListener implements ChangeListener, Serializable {
  191. public void stateChanged(ChangeEvent e) {
  192. if (isShowing()) { // isVisible
  193. updateChooser();
  194. dirty = false;
  195. } else {
  196. dirty = true;
  197. }
  198. }
  199. }
  200. }