1 /*
   2  * Copyright (c) 1998, 2014, 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 @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     protected EventListenerList listenerList = new EventListenerList();
  51 
  52     private Color selectedColor;
  53 
  54     /**
  55      * Creates a <code>DefaultColorSelectionModel</code> with the
  56      * current color set to <code>Color.white</code>.  This is
  57      * the default constructor.
  58      */
  59     public DefaultColorSelectionModel() {
  60         selectedColor = Color.white;
  61     }
  62 
  63     /**
  64      * Creates a <code>DefaultColorSelectionModel</code> with the
  65      * current color set to <code>color</code>, which should be
  66      * non-<code>null</code>.  Note that setting the color to
  67      * <code>null</code> is undefined and may have unpredictable
  68      * results.
  69      *
  70      * @param color the new <code>Color</code>
  71      */
  72     public DefaultColorSelectionModel(Color color) {
  73         selectedColor = color;
  74     }
  75 
  76     /**
  77      * Returns the selected <code>Color</code> which should be
  78      * non-<code>null</code>.
  79      *
  80      * @return the selected <code>Color</code>
  81      */
  82     public Color getSelectedColor() {
  83         return selectedColor;
  84     }
  85 
  86     /**
  87      * Sets the selected color to <code>color</code>.
  88      * Note that setting the color to <code>null</code>
  89      * is undefined and may have unpredictable results.
  90      * This method fires a state changed event if it sets the
  91      * current color to a new non-<code>null</code> color;
  92      * if the new color is the same as the current color,
  93      * no event is fired.
  94      *
  95      * @param color the new <code>Color</code>
  96      */
  97     public void setSelectedColor(Color color) {
  98         if (color != null && !selectedColor.equals(color)) {
  99             selectedColor = color;
 100             fireStateChanged();
 101         }
 102     }
 103 
 104 
 105     /**
 106      * Adds a <code>ChangeListener</code> to the model.
 107      *
 108      * @param l the <code>ChangeListener</code> to be added
 109      */
 110     public void addChangeListener(ChangeListener l) {
 111         listenerList.add(ChangeListener.class, l);
 112     }
 113 
 114     /**
 115      * Removes a <code>ChangeListener</code> from the model.
 116      * @param l the <code>ChangeListener</code> to be removed
 117      */
 118     public void removeChangeListener(ChangeListener l) {
 119         listenerList.remove(ChangeListener.class, l);
 120     }
 121 
 122     /**
 123      * Returns an array of all the <code>ChangeListener</code>s added
 124      * to this <code>DefaultColorSelectionModel</code> with
 125      * <code>addChangeListener</code>.
 126      *
 127      * @return all of the <code>ChangeListener</code>s added, or an empty
 128      *         array if no listeners have been added
 129      * @since 1.4
 130      */
 131     public ChangeListener[] getChangeListeners() {
 132         return listenerList.getListeners(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 }