blob: e3cf261bd024efe014abf2619f9e40620efd335f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing.colorchooser;
27
28import javax.swing.*;
29import javax.swing.event.*;
30import java.awt.Color;
31import java.io.Serializable;
32
33/**
34 * A generic implementation of <code>ColorSelectionModel</code>.
35 *
36 * @author Steve Wilson
37 *
38 * @see java.awt.Color
39 */
40public class DefaultColorSelectionModel implements ColorSelectionModel, Serializable {
41
42 /**
43 * Only one <code>ChangeEvent</code> is needed per model instance
44 * since the event's only (read-only) state is the source property.
45 * The source of events generated here is always "this".
46 */
47 protected transient ChangeEvent changeEvent = null;
48
49 protected EventListenerList listenerList = new EventListenerList();
50
51 private Color selectedColor;
52
53 /**
54 * Creates a <code>DefaultColorSelectionModel</code> with the
55 * current color set to <code>Color.white</code>. This is
56 * the default constructor.
57 */
58 public DefaultColorSelectionModel() {
59 selectedColor = Color.white;
60 }
61
62 /**
63 * Creates a <code>DefaultColorSelectionModel</code> with the
64 * current color set to <code>color</code>, which should be
65 * non-<code>null</code>. Note that setting the color to
66 * <code>null</code> is undefined and may have unpredictable
67 * results.
68 *
69 * @param color the new <code>Color</code>
70 */
71 public DefaultColorSelectionModel(Color color) {
72 selectedColor = color;
73 }
74
75 /**
76 * Returns the selected <code>Color</code> which should be
77 * non-<code>null</code>.
78 *
79 * @return the selected <code>Color</code>
80 */
81 public Color getSelectedColor() {
82 return selectedColor;
83 }
84
85 /**
86 * Sets the selected color to <code>color</code>.
87 * Note that setting the color to <code>null</code>
88 * is undefined and may have unpredictable results.
89 * This method fires a state changed event if it sets the
90 * current color to a new non-<code>null</code> color;
91 * if the new color is the same as the current color,
92 * no event is fired.
93 *
94 * @param color the new <code>Color</code>
95 */
96 public void setSelectedColor(Color color) {
97 if (color != null && !selectedColor.equals(color)) {
98 selectedColor = color;
99 fireStateChanged();
100 }
101 }
102
103
104 /**
105 * Adds a <code>ChangeListener</code> to the model.
106 *
107 * @param l the <code>ChangeListener</code> to be added
108 */
109 public void addChangeListener(ChangeListener l) {
110 listenerList.add(ChangeListener.class, l);
111 }
112
113 /**
114 * Removes a <code>ChangeListener</code> from the model.
115 * @param l the <code>ChangeListener</code> to be removed
116 */
117 public void removeChangeListener(ChangeListener l) {
118 listenerList.remove(ChangeListener.class, l);
119 }
120
121 /**
122 * Returns an array of all the <code>ChangeListener</code>s added
123 * to this <code>DefaultColorSelectionModel</code> with
124 * <code>addChangeListener</code>.
125 *
126 * @return all of the <code>ChangeListener</code>s added, or an empty
127 * array if no listeners have been added
128 * @since 1.4
129 */
130 public ChangeListener[] getChangeListeners() {
131 return (ChangeListener[])listenerList.getListeners(
132 ChangeListener.class);
133 }
134
135 /**
136 * Runs each <code>ChangeListener</code>'s
137 * <code>stateChanged</code> method.
138 *
139 * <!-- @see #setRangeProperties //bad link-->
140 * @see EventListenerList
141 */
142 protected void fireStateChanged()
143 {
144 Object[] listeners = listenerList.getListenerList();
145 for (int i = listeners.length - 2; i >= 0; i -=2 ) {
146 if (listeners[i] == ChangeListener.class) {
147 if (changeEvent == null) {
148 changeEvent = new ChangeEvent(this);
149 }
150 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
151 }
152 }
153 }
154
155}