01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings;
14:
15: import javax.swing.event.ChangeEvent;
16: import javax.swing.event.ChangeListener;
17: import javax.swing.event.EventListenerList;
18: import java.io.Serializable;
19:
20: /**
21: * Default implementation of {@link SButtonModel}
22: */
23: public class SDefaultButtonModel implements SButtonModel, Serializable {
24: public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
25:
26: private boolean selected;
27: private EventListenerList listeners = null;
28:
29: protected transient ChangeEvent changeEvent = null;
30:
31: public boolean isSelected() {
32: return selected;
33: }
34:
35: public void setSelected(boolean selected) {
36: if (this .selected == selected) {
37: return;
38: }
39: this .selected = selected;
40: fireStateChanged();
41: }
42:
43: public Object[] getSelectedObjects() {
44: return new Object[0];
45: }
46:
47: public void addChangeListener(ChangeListener listener) {
48: if (listeners == null)
49: listeners = new EventListenerList();
50: listeners.add(ChangeListener.class, listener);
51: }
52:
53: public void removeChangeListener(ChangeListener listener) {
54: if (listeners == null)
55: return;
56: listeners.remove(ChangeListener.class, listener);
57: }
58:
59: public ChangeListener[] getChangeListeners() {
60: return (ChangeListener[]) listeners
61: .getListeners(ChangeListener.class);
62: }
63:
64: protected final Object[] getListenerList() {
65: if (listeners == null) {
66: return EMPTY_OBJECT_ARRAY;
67: } else {
68: return listeners.getListenerList();
69: } // end of else
70: }
71:
72: protected void fireStateChanged() {
73: Object[] listeners = getListenerList();
74:
75: for (int i = listeners.length - 2; i >= 0; i -= 2) {
76: if (listeners[i] == ChangeListener.class) {
77: // Lazily create the event:
78: if (changeEvent == null)
79: changeEvent = new ChangeEvent(this );
80: ((ChangeListener) listeners[i + 1])
81: .stateChanged(changeEvent);
82: }
83: }
84: }
85: }
|