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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.colorchooser; 27 28 import javax.swing.*; 29 import javax.swing.event.*; 30 import java.awt.Color; 31 import 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 */ 40 @SuppressWarnings("serial") // Same-version serialization only 41 public class DefaultColorSelectionModel implements ColorSelectionModel, Serializable { 42 43 /** 44 * Only one <code>ChangeEvent</code> is needed per model instance 45 * since the event's only (read-only) state is the source property. 46 * The source of events generated here is always "this". 47 */ 48 protected transient ChangeEvent changeEvent = null; 49 50 /** 51 * The listener list. 52 */ 53 protected EventListenerList listenerList = new EventListenerList(); 54 55 private Color selectedColor; 56 57 /** 58 * Creates a <code>DefaultColorSelectionModel</code> with the 59 * current color set to <code>Color.white</code>. This is 60 * the default constructor. 61 */ 62 public DefaultColorSelectionModel() { 63 selectedColor = Color.white; 64 } 65 66 /** 67 * Creates a <code>DefaultColorSelectionModel</code> with the 68 * current color set to <code>color</code>, which should be 69 * non-<code>null</code>. Note that setting the color to 70 * <code>null</code> is undefined and may have unpredictable 71 * results. 72 * 73 * @param color the new <code>Color</code> 74 */ 75 public DefaultColorSelectionModel(Color color) { 76 selectedColor = color; 77 } 78 79 /** 80 * Returns the selected <code>Color</code> which should be 81 * non-<code>null</code>. 82 * 83 * @return the selected <code>Color</code> 84 */ 85 public Color getSelectedColor() { 86 return selectedColor; 87 } 88 89 /** 90 * Sets the selected color to <code>color</code>. 91 * Note that setting the color to <code>null</code> 92 * is undefined and may have unpredictable results. 93 * This method fires a state changed event if it sets the 94 * current color to a new non-<code>null</code> color; 95 * if the new color is the same as the current color, 96 * no event is fired. 97 * 98 * @param color the new <code>Color</code> 99 */ 100 public void setSelectedColor(Color color) { 101 if (color != null && !selectedColor.equals(color)) { 102 selectedColor = color; 103 fireStateChanged(); 104 } 105 } 106 107 108 /** 109 * Adds a <code>ChangeListener</code> to the model. 110 * 111 * @param l the <code>ChangeListener</code> to be added 112 */ 113 public void addChangeListener(ChangeListener l) { 114 listenerList.add(ChangeListener.class, l); 115 } 116 117 /** 118 * Removes a <code>ChangeListener</code> from the model. 119 * @param l the <code>ChangeListener</code> to be removed 120 */ 121 public void removeChangeListener(ChangeListener l) { 122 listenerList.remove(ChangeListener.class, l); 123 } 124 125 /** 126 * Returns an array of all the <code>ChangeListener</code>s added 127 * to this <code>DefaultColorSelectionModel</code> with 128 * <code>addChangeListener</code>. 129 * 130 * @return all of the <code>ChangeListener</code>s added, or an empty 131 * array if no listeners have been added 132 * @since 1.4 133 */ 134 public ChangeListener[] getChangeListeners() { 135 return listenerList.getListeners(ChangeListener.class); 136 } 137 138 /** 139 * Runs each <code>ChangeListener</code>'s 140 * <code>stateChanged</code> method. 141 * 142 * <!-- @see #setRangeProperties //bad link--> 143 * @see EventListenerList 144 */ 145 protected void fireStateChanged() 146 { 147 Object[] listeners = listenerList.getListenerList(); 148 for (int i = listeners.length - 2; i >= 0; i -=2 ) { 149 if (listeners[i] == ChangeListener.class) { 150 if (changeEvent == null) { 151 changeEvent = new ChangeEvent(this); 152 } 153 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 154 } 155 } 156 } 157 158 } | 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.colorchooser; 27 28 import javax.swing.*; 29 import javax.swing.event.*; 30 import java.awt.Color; 31 import java.io.Serializable; 32 33 /** 34 * A generic implementation of {@code ColorSelectionModel}. 35 * 36 * @author Steve Wilson 37 * 38 * @see java.awt.Color 39 */ 40 @SuppressWarnings("serial") // Same-version serialization only 41 public class DefaultColorSelectionModel implements ColorSelectionModel, Serializable { 42 43 /** 44 * Only one {@code ChangeEvent} is needed per model instance 45 * since the event's only (read-only) state is the source property. 46 * The source of events generated here is always "this". 47 */ 48 protected transient ChangeEvent changeEvent = null; 49 50 /** 51 * The listener list. 52 */ 53 protected EventListenerList listenerList = new EventListenerList(); 54 55 private Color selectedColor; 56 57 /** 58 * Creates a {@code DefaultColorSelectionModel} with the 59 * current color set to {@code Color.white}. This is 60 * the default constructor. 61 */ 62 public DefaultColorSelectionModel() { 63 selectedColor = Color.white; 64 } 65 66 /** 67 * Creates a {@code DefaultColorSelectionModel} with the 68 * current color set to {@code color}, which should be 69 * non-{@code null}. Note that setting the color to 70 * {@code null} is undefined and may have unpredictable 71 * results. 72 * 73 * @param color the new {@code Color} 74 */ 75 public DefaultColorSelectionModel(Color color) { 76 selectedColor = color; 77 } 78 79 /** 80 * Returns the selected {@code Color} which should be 81 * non-{@code null}. 82 * 83 * @return the selected {@code Color} 84 */ 85 public Color getSelectedColor() { 86 return selectedColor; 87 } 88 89 /** 90 * Sets the selected color to {@code color}. 91 * Note that setting the color to {@code null} 92 * is undefined and may have unpredictable results. 93 * This method fires a state changed event if it sets the 94 * current color to a new non-{@code null} color; 95 * if the new color is the same as the current color, 96 * no event is fired. 97 * 98 * @param color the new {@code Color} 99 */ 100 public void setSelectedColor(Color color) { 101 if (color != null && !selectedColor.equals(color)) { 102 selectedColor = color; 103 fireStateChanged(); 104 } 105 } 106 107 108 /** 109 * Adds a {@code ChangeListener} to the model. 110 * 111 * @param l the {@code ChangeListener} to be added 112 */ 113 public void addChangeListener(ChangeListener l) { 114 listenerList.add(ChangeListener.class, l); 115 } 116 117 /** 118 * Removes a {@code ChangeListener} from the model. 119 * @param l the {@code ChangeListener} to be removed 120 */ 121 public void removeChangeListener(ChangeListener l) { 122 listenerList.remove(ChangeListener.class, l); 123 } 124 125 /** 126 * Returns an array of all the {@code ChangeListener}s added 127 * to this {@code DefaultColorSelectionModel} with 128 * {@code addChangeListener}. 129 * 130 * @return all of the {@code ChangeListener}s added, or an empty 131 * array if no listeners have been added 132 * @since 1.4 133 */ 134 public ChangeListener[] getChangeListeners() { 135 return listenerList.getListeners(ChangeListener.class); 136 } 137 138 /** 139 * Runs each {@code ChangeListener}'s 140 * {@code stateChanged} method. 141 * 142 * <!-- @see #setRangeProperties //bad link--> 143 * @see EventListenerList 144 */ 145 protected void fireStateChanged() 146 { 147 Object[] listeners = listenerList.getListenerList(); 148 for (int i = listeners.length - 2; i >= 0; i -=2 ) { 149 if (listeners[i] == ChangeListener.class) { 150 if (changeEvent == null) { 151 changeEvent = new ChangeEvent(this); 152 } 153 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 154 } 155 } 156 } 157 158 } |