1. /*
  2. * @(#)DefaultColorSelectionModel.java 1.14 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 javax.swing.*;
  9. import javax.swing.event.*;
  10. import java.awt.Color;
  11. import java.io.Serializable;
  12. /**
  13. * A generic implementation of <code>ColorSelectionModel</code>.
  14. *
  15. * @version 1.14 01/23/03
  16. * @author Steve Wilson
  17. *
  18. * @see java.awt.Color
  19. */
  20. public class DefaultColorSelectionModel implements ColorSelectionModel, Serializable {
  21. /**
  22. * Only one <code>ChangeEvent</code> is needed per model instance
  23. * since the event's only (read-only) state is the source property.
  24. * The source 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. * Creates a <code>DefaultColorSelectionModel</code> with the
  31. * current color set to <code>Color.white</code>. This is
  32. * the default constructor.
  33. */
  34. public DefaultColorSelectionModel() {
  35. selectedColor = Color.white;
  36. }
  37. /**
  38. * Creates a <code>DefaultColorSelectionModel</code> with the
  39. * current color set to <code>color</code>, which should be
  40. * non-<code>null</code>. Note that setting the color to
  41. * <code>null</code> is undefined and may have unpredictable
  42. * results.
  43. *
  44. * @param color the new <code>Color</code>
  45. */
  46. public DefaultColorSelectionModel(Color color) {
  47. selectedColor = color;
  48. }
  49. /**
  50. * Returns the selected <code>Color</code> which should be
  51. * non-<code>null</code>.
  52. *
  53. * @return the selected <code>Color</code>
  54. */
  55. public Color getSelectedColor() {
  56. return selectedColor;
  57. }
  58. /**
  59. * Sets the selected color to <code>color</code>.
  60. * Note that setting the color to <code>null</code>
  61. * is undefined and may have unpredictable results.
  62. * This method fires a state changed event if it sets the
  63. * current color to a new non-<code>null</code> color;
  64. * if the new color is the same as the current color,
  65. * no event is fired.
  66. *
  67. * @param color the new <code>Color</code>
  68. */
  69. public void setSelectedColor(Color color) {
  70. if (color != null && !selectedColor.equals(color)) {
  71. selectedColor = color;
  72. fireStateChanged();
  73. }
  74. }
  75. /**
  76. * Adds a <code>ChangeListener</code> to the model.
  77. *
  78. * @param l the <code>ChangeListener</code> to be added
  79. */
  80. public void addChangeListener(ChangeListener l) {
  81. listenerList.add(ChangeListener.class, l);
  82. }
  83. /**
  84. * Removes a <code>ChangeListener</code> from the model.
  85. * @param l the <code>ChangeListener</code> to be removed
  86. */
  87. public void removeChangeListener(ChangeListener l) {
  88. listenerList.remove(ChangeListener.class, l);
  89. }
  90. /**
  91. * Returns an array of all the <code>ChangeListener</code>s added
  92. * to this <code>DefaultColorSelectionModel</code> with
  93. * <code>addChangeListener</code>.
  94. *
  95. * @return all of the <code>ChangeListener</code>s added, or an empty
  96. * array if no listeners have been added
  97. * @since 1.4
  98. */
  99. public ChangeListener[] getChangeListeners() {
  100. return (ChangeListener[])listenerList.getListeners(
  101. ChangeListener.class);
  102. }
  103. /**
  104. * Runs each <code>ChangeListener</code>'s
  105. * <code>stateChanged</code> method.
  106. *
  107. * <!-- @see #setRangeProperties //bad link-->
  108. * @see EventListenerList
  109. */
  110. protected void fireStateChanged()
  111. {
  112. Object[] listeners = listenerList.getListenerList();
  113. for (int i = listeners.length - 2; i >= 0; i -=2 ) {
  114. if (listeners[i] == ChangeListener.class) {
  115. if (changeEvent == null) {
  116. changeEvent = new ChangeEvent(this);
  117. }
  118. ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  119. }
  120. }
  121. }
  122. }