1 /* 2 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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 public 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 listenerList.getListeners(ChangeListener.class); 132 } 133 134 /** 135 * Runs each <code>ChangeListener</code>'s 136 * <code>stateChanged</code> method. 137 * 138 * <!-- @see #setRangeProperties //bad link--> 139 * @see EventListenerList 140 */ 141 protected void fireStateChanged() 142 { 143 Object[] listeners = listenerList.getListenerList(); 144 for (int i = listeners.length - 2; i >= 0; i -=2 ) { 145 if (listeners[i] == ChangeListener.class) { 146 if (changeEvent == null) { 147 changeEvent = new ChangeEvent(this); 148 } 149 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 150 } 151 } 152 } 153 154 }