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