KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > colorchooser > DefaultColorSelectionModel


1 /*
2  * @(#)DefaultColorSelectionModel.java 1.15 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.colorchooser;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import java.awt.Color JavaDoc;
13 import java.io.Serializable JavaDoc;
14
15 /**
16  * A generic implementation of <code>ColorSelectionModel</code>.
17  *
18  * @version 1.15 12/19/03
19  * @author Steve Wilson
20  *
21  * @see java.awt.Color
22  */

23 public class DefaultColorSelectionModel implements ColorSelectionModel JavaDoc, Serializable JavaDoc {
24
25     /**
26      * Only one <code>ChangeEvent</code> is needed per model instance
27      * since the event's only (read-only) state is the source property.
28      * The source of events generated here is always "this".
29      */

30     protected transient ChangeEvent changeEvent = null;
31
32     protected EventListenerList listenerList = new EventListenerList();
33
34     private Color JavaDoc selectedColor;
35
36     /**
37      * Creates a <code>DefaultColorSelectionModel</code> with the
38      * current color set to <code>Color.white</code>. This is
39      * the default constructor.
40      */

41     public DefaultColorSelectionModel() {
42         selectedColor = Color.white;
43     }
44
45     /**
46      * Creates a <code>DefaultColorSelectionModel</code> with the
47      * current color set to <code>color</code>, which should be
48      * non-<code>null</code>. Note that setting the color to
49      * <code>null</code> is undefined and may have unpredictable
50      * results.
51      *
52      * @param color the new <code>Color</code>
53      */

54     public DefaultColorSelectionModel(Color JavaDoc color) {
55         selectedColor = color;
56     }
57
58     /**
59      * Returns the selected <code>Color</code> which should be
60      * non-<code>null</code>.
61      *
62      * @return the selected <code>Color</code>
63      */

64     public Color JavaDoc getSelectedColor() {
65         return selectedColor;
66     }
67
68     /**
69      * Sets the selected color to <code>color</code>.
70      * Note that setting the color to <code>null</code>
71      * is undefined and may have unpredictable results.
72      * This method fires a state changed event if it sets the
73      * current color to a new non-<code>null</code> color;
74      * if the new color is the same as the current color,
75      * no event is fired.
76      *
77      * @param color the new <code>Color</code>
78      */

79     public void setSelectedColor(Color JavaDoc color) {
80         if (color != null && !selectedColor.equals(color)) {
81             selectedColor = color;
82             fireStateChanged();
83         }
84     }
85
86
87     /**
88      * Adds a <code>ChangeListener</code> to the model.
89      *
90      * @param l the <code>ChangeListener</code> to be added
91      */

92     public void addChangeListener(ChangeListener l) {
93     listenerList.add(ChangeListener.class, l);
94     }
95
96     /**
97      * Removes a <code>ChangeListener</code> from the model.
98      * @param l the <code>ChangeListener</code> to be removed
99      */

100     public void removeChangeListener(ChangeListener l) {
101     listenerList.remove(ChangeListener.class, l);
102     }
103
104     /**
105      * Returns an array of all the <code>ChangeListener</code>s added
106      * to this <code>DefaultColorSelectionModel</code> with
107      * <code>addChangeListener</code>.
108      *
109      * @return all of the <code>ChangeListener</code>s added, or an empty
110      * array if no listeners have been added
111      * @since 1.4
112      */

113     public ChangeListener[] getChangeListeners() {
114         return (ChangeListener[])listenerList.getListeners(
115                 ChangeListener.class);
116     }
117
118     /**
119      * Runs each <code>ChangeListener</code>'s
120      * <code>stateChanged</code> method.
121      *
122      * <!-- @see #setRangeProperties //bad link-->
123      * @see EventListenerList
124      */

125     protected void fireStateChanged()
126     {
127         Object JavaDoc[] listeners = listenerList.getListenerList();
128         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
129             if (listeners[i] == ChangeListener.class) {
130                 if (changeEvent == null) {
131                     changeEvent = new ChangeEvent(this);
132                 }
133                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
134             }
135         }
136     }
137
138 }
139
Popular Tags