KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SButtonGroup


1 /*
2  * $Id: SButtonGroup.java,v 1.5 2005/05/26 13:18:08 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.session.SessionManager;
19
20 import javax.swing.event.EventListenerList JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * This class is used to create a multiple-exclusion scope for a set of
30  * buttons. Creating a set of buttons with the same ButtonGroup object means
31  * that turning "on" one of those buttons turns off all other buttons in the
32  * group.
33  * <p/>
34  * <p>A SButtonGroup can be used with any set of objects that inherit from
35  * {@link SAbstractButton}, because they support the selected state.
36  * <p/>
37  * <p>Initially, all buttons in the group are unselected. Once any button is
38  * selected, one button is always selected in the group. There is no way to
39  * turn a button programmatically to "off", in order to clear the button
40  * group.
41  * <p/>
42  * <p><em>Details:</em>The implementation of the button group is a
43  * bit tricky for the HTML generation. In HTML, groups of components are
44  * usually formed by giving them all the same name. The problem is, that
45  * any {@link SComponent}, especially the {@link SAbstractButton}, have globally
46  * <em>unique</em> names. So this implementation gives all buttons in the
47  * group the name of this SButtonGroup, and sets their <em>value</em> to
48  * their actual name. So a bit of dispatching is already done here.
49  *
50  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
51  * @version $Revision: 1.5 $
52  * @see javax.swing.ButtonGroup
53  */

54 public class SButtonGroup implements SDelayedEventModel {
55     private final transient static Log log = LogFactory.getLog(SButtonGroup.class);
56     public static final String JavaDoc SELECTION_CHANGED = "SelectionChanged";
57
58     protected final ArrayList JavaDoc buttons = new ArrayList JavaDoc(2);
59
60     private SAbstractButton selection = null;
61
62     /* */
63     private transient String JavaDoc componentId = null;
64
65     protected final EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
66
67     /**
68      * indicates if we should fire event immediately when they arise, or if we
69      * should collect them for a later delivery
70      */

71     private boolean delayEvents = false;
72
73     /**
74      * all delayed events are stored here.
75      */

76     protected final ArrayList JavaDoc delayedEvents = new ArrayList JavaDoc(2);
77
78     public SButtonGroup() {}
79
80     /**
81      * Return a jvm wide unique id.
82      *
83      * @return an id
84      */

85     public final String JavaDoc getComponentId() {
86         if (componentId == null)
87             componentId = SessionManager.getSession().createUniqueId();
88         return componentId;
89     }
90
91     protected void setSelection(SAbstractButton button) {
92         SAbstractButton oldSelection = selection;
93
94         selection = button;
95
96         if (oldSelection != null && oldSelection.getGroup() == this)
97             oldSelection.setSelected(false);
98
99         if (selection != null)
100             selection.setSelected(true);
101
102
103         fireActionPerformed(selection != null ? selection.getActionCommand() :
104                 SELECTION_CHANGED);
105     }
106
107     /*
108      * Konzeptionell richtiger waere hier SAbstractButton. Macht aber keinen
109      * Sinn. Macht nur Sinn abstract Checkboxes(eventuell nur RadioButtons ???).
110      */

111     public void add(SAbstractButton button) {
112         if (buttons != null && !buttons.contains(button)) {
113             buttons.add(button);
114             button.setGroup(this);
115             if (selection == null && button.isSelected()) {
116                 setSelection(button);
117             }
118         }
119     }
120
121     public void remove(SAbstractButton button) {
122         if (button == null || button.getGroup() != this)
123             return;
124
125         buttons.remove(button);
126         button.setGroup(null);
127
128         if (button == selection) {
129             setSelection(null);
130         }
131     }
132
133     public void removeAll() {
134         while (buttons.size() > 0)
135             remove((SAbstractButton) buttons.get(0));
136     }
137
138     public final SAbstractButton getSelection() {
139         return selection;
140     }
141
142     public void setSelected(SAbstractButton b, boolean selected) {
143         if (selected && b != selection && b != null) {
144             setSelection(b);
145         }
146         // if button should be set to unselected, clear selection
147
if (!selected && b != null && b.equals(selection)) {
148             setSelection(null);
149             
150         }
151     }
152
153     public boolean isSelected(SAbstractButton button) {
154         return button == getSelection();
155     }
156
157     public Iterator JavaDoc iterator() {
158         return buttons.iterator();
159     }
160
161     public Enumeration JavaDoc getElements() {
162         return Collections.enumeration(buttons);
163     }
164
165     public String JavaDoc getLowLevelEventId() {
166         return getComponentId();
167     }
168
169     /**
170      * Adds an action listener to this group of buttons.
171      * If one of the buttons contained in this group gets selected, an action event will occur.
172      *
173      * @param listener
174      */

175     public void addActionListener(ActionListener JavaDoc listener) {
176         listenerList.add(ActionListener JavaDoc.class, listener);
177     }
178
179     public void removeActionListener(ActionListener JavaDoc listener) {
180         listenerList.remove(ActionListener JavaDoc.class, listener);
181     }
182
183
184     /**
185      * Fire an ActionEvent at each registered listener.
186      */

187     protected void fireActionPerformed(String JavaDoc command) {
188         fireActionEvent(new ActionEvent JavaDoc(this, ActionEvent.ACTION_PERFORMED,
189                 command));
190     }
191
192     /**
193      * Fire an ActionEvent at each registered listener.
194      */

195     protected void fireActionEvent(ActionEvent JavaDoc e) {
196         if (e == null)
197             return;
198
199         if (delayEvents) {
200             delayedEvents.add(e);
201             return;
202         }
203
204         // Guaranteed to return a non-null array
205
Object JavaDoc[] listeners = listenerList.getListenerList();
206         // Process the listeners last to first, notifying
207
// those that are interested in this event
208
for (int i = listeners.length - 2; i >= 0; i -= 2) {
209             if (listeners[i] == ActionListener JavaDoc.class) {
210                 ((ActionListener JavaDoc) listeners[i + 1]).actionPerformed(e);
211             }
212         }
213     }
214
215     public void setDelayEvents(boolean b) {
216         delayEvents = b;
217     }
218
219     public boolean getDelayEvents() {
220         return delayEvents;
221     }
222
223
224     public void fireDelayedIntermediateEvents() {}
225
226     public void fireDelayedFinalEvents() {
227         for (Iterator JavaDoc iter = delayedEvents.iterator(); iter.hasNext();) {
228             ActionEvent JavaDoc e = (ActionEvent JavaDoc) iter.next();
229
230             fireActionEvent(e);
231         }
232         delayedEvents.clear();
233     }
234 }
235
236
237
Popular Tags