1. /*
  2. * @(#)AbstractColorChooserPanel.java 1.13 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.colorchooser;
  11. import java.awt.*;
  12. import java.io.Serializable;
  13. import javax.swing.*;
  14. import javax.swing.event.*;
  15. /**
  16. * This is the abstract superclass for color choosers. If you want to add
  17. * a new color chooser panel into a <code>JColorChooser</code>, subclass
  18. * this class.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.13 02/02/00
  28. * @author Tom Santos
  29. * @author Steve Wilson
  30. */
  31. public abstract class AbstractColorChooserPanel extends JPanel {
  32. /**
  33. *
  34. */
  35. private JColorChooser chooser;
  36. /**
  37. *
  38. */
  39. private ChangeListener colorListener;
  40. /**
  41. *
  42. */
  43. private boolean dirty = true;
  44. /**
  45. * Invoked automatically when the model's state changes.
  46. * It is also called by <code>installChooserPanel</code> to allow
  47. * you to set up the initial state of your chooser.
  48. * Override this method to update your <code>ChooserPanel</code>.
  49. */
  50. public abstract void updateChooser();
  51. /**
  52. * Builds a new chooser panel.
  53. */
  54. protected abstract void buildChooser();
  55. /**
  56. * Returns a string containing the display name of the panel.
  57. * @return the name of the display panel
  58. */
  59. public abstract String getDisplayName();
  60. /**
  61. * Returns the large display icon for the panel.
  62. * @return the large display icon
  63. */
  64. public abstract Icon getSmallDisplayIcon();
  65. /**
  66. * Returns the small display icon for the panel.
  67. * @return the small display icon
  68. */
  69. public abstract Icon getLargeDisplayIcon();
  70. /**
  71. * Invoked when the panel is added to the chooser.
  72. * If you're going to override this, be sure to call super.
  73. * @param enclosingChooser the panel to be added
  74. * @exception RuntimeException if the chooser panel has already been
  75. * installed
  76. */
  77. public void installChooserPanel(JColorChooser enclosingChooser) {
  78. if (chooser != null) {
  79. throw new RuntimeException ("This chooser panel is already installed");
  80. }
  81. chooser = enclosingChooser;
  82. buildChooser();
  83. updateChooser();
  84. colorListener = new ModelListener();
  85. getColorSelectionModel().addChangeListener(colorListener);
  86. }
  87. /**
  88. * Invoked when the panel is removed from the chooser.
  89. * If you're going to override this, be sure to call super.
  90. */
  91. public void uninstallChooserPanel(JColorChooser enclosingChooser) {
  92. getColorSelectionModel().removeChangeListener(colorListener);
  93. chooser = null;
  94. }
  95. /**
  96. * Returns the model that the chooser panel is editing.
  97. * @return the <code>ColorSelectionModel</code> model this panel
  98. * is editing
  99. */
  100. public ColorSelectionModel getColorSelectionModel() {
  101. return chooser.getSelectionModel();
  102. }
  103. /**
  104. * Returns the color that is currently selected.
  105. * @return the <code>Color</code> that is selected
  106. */
  107. protected Color getColorFromModel() {
  108. return getColorSelectionModel().getSelectedColor();
  109. }
  110. /**
  111. * Draws the panel.
  112. * @param g the <code>Graphics</code> object
  113. */
  114. public void paint(Graphics g) {
  115. if (dirty) {
  116. updateChooser();
  117. dirty = false;
  118. }
  119. super.paint(g);
  120. }
  121. /**
  122. *
  123. */
  124. class ModelListener implements ChangeListener, Serializable {
  125. public void stateChanged(ChangeEvent e) {
  126. if (isShowing()) { // isVisible
  127. updateChooser();
  128. dirty = false;
  129. } else {
  130. dirty = true;
  131. }
  132. }
  133. }
  134. }