1. /*
  2. * @(#)DefaultColorSelectionModel.java 1.6 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 javax.swing.*;
  9. import javax.swing.event.*;
  10. import java.awt.Color;
  11. import java.io.Serializable;
  12. /**
  13. * A generic implementation of ColorSelectionModel.
  14. *
  15. * @version 1.6 11/29/01
  16. * @author Steve Wilson
  17. *
  18. * @see java.awt.Color
  19. */
  20. public class DefaultColorSelectionModel implements ColorSelectionModel, Serializable {
  21. /**
  22. * Only one ChangeEvent is needed per model instance since the
  23. * event's only (read-only) state is the source property. The source
  24. * of events generated here is always "this".
  25. */
  26. protected transient ChangeEvent changeEvent = null;
  27. protected EventListenerList listenerList = new EventListenerList();
  28. private Color selectedColor;
  29. /**
  30. * Default constructor. Initializes selectedColor to Color.white
  31. */
  32. public DefaultColorSelectionModel() {
  33. selectedColor = Color.white;
  34. }
  35. /**
  36. *Initializes selectedColor to <I>color</I>
  37. */
  38. public DefaultColorSelectionModel(Color color) {
  39. selectedColor = color;
  40. }
  41. public Color getSelectedColor() {
  42. return selectedColor;
  43. }
  44. public void setSelectedColor(Color color) {
  45. if ( !selectedColor.equals(color) ) {
  46. selectedColor = color;
  47. fireStateChanged();
  48. }
  49. }
  50. /**
  51. * Adds a ChangeListener to the model.
  52. */
  53. public void addChangeListener(ChangeListener l) {
  54. listenerList.add(ChangeListener.class, l);
  55. }
  56. /**
  57. * Removes a ChangeListener from the model.
  58. */
  59. public void removeChangeListener(ChangeListener l) {
  60. listenerList.remove(ChangeListener.class, l);
  61. }
  62. /**
  63. * Run each ChangeListeners stateChanged() method.
  64. *
  65. * @see #setRangeProperties
  66. * @see EventListenerList
  67. */
  68. protected void fireStateChanged()
  69. {
  70. Object[] listeners = listenerList.getListenerList();
  71. for (int i = listeners.length - 2; i >= 0; i -=2 ) {
  72. if (listeners[i] == ChangeListener.class) {
  73. if (changeEvent == null) {
  74. changeEvent = new ChangeEvent(this);
  75. }
  76. ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  77. }
  78. }
  79. }
  80. }