01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Dennis Ushakov
19: * @version $Revision$
20: */package javax.swing.colorchooser;
21:
22: import java.awt.Color;
23: import java.io.Serializable;
24:
25: import javax.swing.event.ChangeEvent;
26: import javax.swing.event.ChangeListener;
27: import javax.swing.event.EventListenerList;
28:
29: public class DefaultColorSelectionModel implements ColorSelectionModel,
30: Serializable {
31: protected transient ChangeEvent changeEvent;
32: protected EventListenerList listenerList = new EventListenerList();
33:
34: private Color color;
35:
36: public DefaultColorSelectionModel() {
37: this (Color.WHITE);
38: }
39:
40: public DefaultColorSelectionModel(final Color color) {
41: this .color = color;
42: }
43:
44: public Color getSelectedColor() {
45: return color;
46: }
47:
48: public void setSelectedColor(final Color color) {
49: if (color == null) {
50: return;
51: }
52: Color oldColor = this .color;
53: this .color = color;
54: if (!oldColor.equals(color)) {
55: fireStateChanged();
56: }
57: }
58:
59: public void addChangeListener(final ChangeListener listener) {
60: listenerList.add(ChangeListener.class, listener);
61: }
62:
63: public void removeChangeListener(final ChangeListener listener) {
64: listenerList.remove(ChangeListener.class, listener);
65: }
66:
67: public ChangeListener[] getChangeListeners() {
68: return (ChangeListener[]) listenerList
69: .getListeners(ChangeListener.class);
70: }
71:
72: protected void fireStateChanged() {
73: if (changeEvent == null) {
74: changeEvent = new ChangeEvent(this );
75: }
76: ChangeListener[] listeners = getChangeListeners();
77: for (int i = 0; i < listeners.length; i++) {
78: listeners[i].stateChanged(changeEvent);
79: }
80: }
81:
82: }
|