1. /*
  2. * @(#)AbstractColorChooserPanel.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 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. * The is the abstract superclass for color choosers. If you want to add a new color chooser
  14. * panel into a JColorChooser, sublclass this class.
  15. * <p>
  16. * <strong>Warning:</strong>
  17. * Serialized objects of this class will not be compatible with
  18. * future Swing releases. The current serialization support is appropriate
  19. * for short term storage or RMI between applications running the same
  20. * version of Swing. A future release of Swing will provide support for
  21. * long term persistence.
  22. *
  23. * @version 1.11 11/29/01
  24. * @author Tom Santos
  25. * @author Steve Wilson
  26. */
  27. public abstract class AbstractColorChooserPanel extends JPanel {
  28. private JColorChooser chooser;
  29. private ChangeListener colorListener;
  30. private boolean dirty = true;
  31. /**
  32. * override this method to update your ChooserPanel
  33. * This method will be automatically called when the model's state
  34. * changes.
  35. * It is also called by installChooserPanel to allow you to set up
  36. * the initial state of your chooser
  37. */
  38. public abstract void updateChooser();
  39. protected abstract void buildChooser();
  40. public abstract String getDisplayName();
  41. public abstract Icon getSmallDisplayIcon();
  42. public abstract Icon getLargeDisplayIcon();
  43. /**
  44. * This get called when the panel is added to the chooser.
  45. *
  46. * if you're going to override this, be sure to call super.
  47. */
  48. public void installChooserPanel(JColorChooser enclosingChooser) {
  49. if (chooser != null) {
  50. throw new RuntimeException ("This chooser panel is already installed");
  51. }
  52. chooser = enclosingChooser;
  53. buildChooser();
  54. updateChooser();
  55. colorListener = new ModelListener();
  56. getColorSelectionModel().addChangeListener(colorListener);
  57. }
  58. /**
  59. * This get called when the panel is removed from the chooser.
  60. *
  61. * if you're going to override this, be sure to call super.
  62. */
  63. public void uninstallChooserPanel(JColorChooser enclosingChooser) {
  64. getColorSelectionModel().removeChangeListener(colorListener);
  65. chooser = null;
  66. }
  67. /**
  68. * @return The model this panel is editing
  69. */
  70. public ColorSelectionModel getColorSelectionModel() {
  71. return chooser.getSelectionModel();
  72. }
  73. protected Color getColorFromModel() {
  74. return getColorSelectionModel().getSelectedColor();
  75. }
  76. public void paint(Graphics g) {
  77. if (dirty) {
  78. updateChooser();
  79. dirty = false;
  80. }
  81. super.paint(g);
  82. }
  83. class ModelListener implements ChangeListener, Serializable {
  84. public void stateChanged(ChangeEvent e) {
  85. if (isShowing()) { // isVisible
  86. updateChooser();
  87. dirty = false;
  88. } else {
  89. dirty = true;
  90. }
  91. }
  92. }
  93. }