35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
|
|
|
38 |
package javax.swing.colorchooser; |
package javax.swing.colorchooser; |
39 |
|
|
40 |
import java.awt.Color; |
import java.awt.Color; |
43 |
import javax.swing.event.ChangeListener; |
import javax.swing.event.ChangeListener; |
44 |
import javax.swing.event.EventListenerList; |
import javax.swing.event.EventListenerList; |
45 |
|
|
46 |
|
|
47 |
/** |
/** |
48 |
* DefaultColorSelectionModel |
* This is the default implementation of the ColorSelectionModel interface |
49 |
* @author Andrew Selkirk |
* that JColorChoosers use. |
50 |
* @version 1.0 |
* |
51 |
|
* @author Andrew Selkirk |
52 |
|
* @version 1.0 |
53 |
*/ |
*/ |
54 |
public class DefaultColorSelectionModel |
public class DefaultColorSelectionModel implements ColorSelectionModel, |
55 |
implements ColorSelectionModel, Serializable |
Serializable |
56 |
{ |
{ |
57 |
|
/** DOCUMENT ME! */ |
58 |
private static final long serialVersionUID = -8117143602864778804L; |
private static final long serialVersionUID = -8117143602864778804L; |
59 |
|
|
60 |
|
/** The currently selected color. */ |
61 |
private Color selectedColor; |
private Color selectedColor; |
62 |
|
|
63 |
protected transient ChangeEvent changeEvent = new ChangeEvent (this); |
/** The ChangeEvent fired to all ChangeListeners. */ |
64 |
protected EventListenerList listenerList = new EventListenerList (); |
protected transient ChangeEvent changeEvent = new ChangeEvent(this); |
65 |
|
|
66 |
|
/** The list of listeners. */ |
67 |
|
protected EventListenerList listenerList = new EventListenerList(); |
68 |
|
|
69 |
/** |
/** |
70 |
* Creates a new color selection model. |
* Creates a new color selection model with the default white color. |
71 |
*/ |
*/ |
72 |
public DefaultColorSelectionModel() |
public DefaultColorSelectionModel() |
73 |
{ |
{ |
74 |
this (Color.white); |
this(Color.white); |
75 |
} |
} |
76 |
|
|
77 |
/** |
/** |
78 |
* Creates a new color selection model with a given selected color. |
* Creates a new color selection model with a given selected color. |
79 |
* |
* |
80 |
* @param color The selected color. |
* @param color The initial color. |
81 |
|
* |
82 |
|
* @throws Error If the color is null. |
83 |
*/ |
*/ |
84 |
public DefaultColorSelectionModel (Color color) |
public DefaultColorSelectionModel(Color color) |
85 |
{ |
{ |
86 |
super(); |
super(); |
87 |
|
if (color == null) |
88 |
|
throw new Error("ColorSelectionModel cannot be set to have null color."); |
89 |
this.selectedColor = color; |
this.selectedColor = color; |
90 |
} |
} |
91 |
|
|
92 |
/** |
/** |
93 |
* Returns the selected color. |
* Returns the selected color. |
94 |
* |
* |
95 |
* @return The selected color. |
* @return The selected color. |
96 |
*/ |
*/ |
97 |
public Color getSelectedColor() |
public Color getSelectedColor() |
100 |
} |
} |
101 |
|
|
102 |
/** |
/** |
103 |
|
* This method sets the color. |
104 |
|
* |
105 |
* @param color The color to set. |
* @param color The color to set. |
106 |
|
* |
107 |
|
* @throws Error If the color is set. |
108 |
*/ |
*/ |
109 |
public void setSelectedColor (Color color) |
public void setSelectedColor(Color color) |
110 |
{ |
{ |
111 |
this.selectedColor = color; |
if (color == null) |
112 |
|
throw new Error("ColorSelectionModel cannot be set to have null color."); |
113 |
|
if (color != selectedColor) |
114 |
|
{ |
115 |
|
this.selectedColor = color; |
116 |
|
fireStateChanged(); |
117 |
|
} |
118 |
} |
} |
119 |
|
|
120 |
/** |
/** |
121 |
* Adds a listener to this model. |
* Adds a listener to this model. |
122 |
* |
* |
123 |
* @param listener The listener to add. |
* @param listener The listener to add. |
124 |
*/ |
*/ |
125 |
public void addChangeListener (ChangeListener listener) |
public void addChangeListener(ChangeListener listener) |
126 |
{ |
{ |
127 |
listenerList.add (ChangeListener.class, listener); |
listenerList.add(ChangeListener.class, listener); |
128 |
} |
} |
129 |
|
|
130 |
/** |
/** |
131 |
* Removes a listener from this model. |
* Removes a listener from this model. |
132 |
* |
* |
133 |
* @param listener The listener to remove. |
* @param listener The listener to remove. |
134 |
*/ |
*/ |
135 |
public void removeChangeListener (ChangeListener listener) |
public void removeChangeListener(ChangeListener listener) |
136 |
{ |
{ |
137 |
listenerList.remove (ChangeListener.class, listener); |
listenerList.remove(ChangeListener.class, listener); |
138 |
} |
} |
139 |
|
|
140 |
/** |
/** |
144 |
*/ |
*/ |
145 |
public ChangeListener[] getChangeListeners() |
public ChangeListener[] getChangeListeners() |
146 |
{ |
{ |
147 |
return (ChangeListener[]) listenerList.getListeners (ChangeListener.class); |
return (ChangeListener[]) listenerList.getListeners(ChangeListener.class); |
148 |
} |
} |
149 |
|
|
150 |
/** |
/** |
151 |
* Calls all the <code>stateChanged()</code> method of all added |
* Calls all the <code>stateChanged()</code> method of all added |
152 |
* <code>ChangeListener</code> objects with <code>changeEvent</code> |
* <code>ChangeListener</code> objects with <code>changeEvent</code> as |
153 |
* as argument. |
* argument. |
154 |
*/ |
*/ |
155 |
protected void fireStateChanged() |
protected void fireStateChanged() |
156 |
{ |
{ |
157 |
ChangeListener[] listeners = getChangeListeners(); |
ChangeListener[] listeners = getChangeListeners(); |
158 |
|
|
159 |
for (int i = 0; i < listeners.length; i++) |
for (int i = 0; i < listeners.length; i++) |
160 |
listeners [i].stateChanged (changeEvent); |
listeners[i].stateChanged(changeEvent); |
161 |
} |
} |
162 |
} |
} |