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: package javax.swing;
19:
20: import java.io.Serializable;
21: import java.util.EventListener;
22: import javax.swing.event.ChangeEvent;
23: import javax.swing.event.ChangeListener;
24: import javax.swing.event.EventListenerList;
25:
26: /**
27: * <p>
28: * <i>DefaultSingleSelectionModel</i>
29: * </p>
30: * <h3>Implementation Notes:</h3>
31: * <ul>
32: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
33: * optimization, not as a guarantee of serialization compatibility.</li>
34: * </ul>
35: */
36: public class DefaultSingleSelectionModel implements Serializable,
37: SingleSelectionModel {
38: private static final long serialVersionUID = -8733648221309386264L;
39:
40: protected ChangeEvent changeEvent;
41:
42: protected EventListenerList listenerList = new EventListenerList();
43:
44: private int selectedIndex = -1;
45:
46: public void clearSelection() {
47: selectedIndex = -1;
48: fireStateChanged();
49: }
50:
51: public void setSelectedIndex(int index) {
52: selectedIndex = index;
53: fireStateChanged();
54: }
55:
56: public int getSelectedIndex() {
57: return selectedIndex;
58: }
59:
60: public boolean isSelected() {
61: return selectedIndex > -1;
62: }
63:
64: public void addChangeListener(ChangeListener l) {
65: listenerList.add(ChangeListener.class, l);
66: }
67:
68: public void removeChangeListener(ChangeListener l) {
69: listenerList.remove(ChangeListener.class, l);
70: }
71:
72: public <T extends EventListener> T[] getListeners(
73: Class<T> listenerType) {
74: return listenerList.getListeners(listenerType);
75: }
76:
77: public ChangeListener[] getChangeListeners() {
78: return getListeners(ChangeListener.class);
79: }
80:
81: protected void fireStateChanged() {
82: ChangeListener[] changeListeners = getChangeListeners();
83: if (changeListeners.length > 0) {
84: ChangeEvent event = getChangeEvent();
85: for (int i = 0; i < changeListeners.length; i++) {
86: changeListeners[i].stateChanged(event);
87: }
88: }
89: }
90:
91: private ChangeEvent getChangeEvent() {
92: if (changeEvent == null) {
93: changeEvent = new ChangeEvent(this);
94: }
95: return changeEvent;
96: }
97: }
|