1 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 ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 28 54 public class SButtonGroup implements SDelayedEventModel { 55 private final transient static Log log = LogFactory.getLog(SButtonGroup.class); 56 public static final String SELECTION_CHANGED = "SelectionChanged"; 57 58 protected final ArrayList buttons = new ArrayList (2); 59 60 private SAbstractButton selection = null; 61 62 63 private transient String componentId = null; 64 65 protected final EventListenerList listenerList = new EventListenerList (); 66 67 71 private boolean delayEvents = false; 72 73 76 protected final ArrayList delayedEvents = new ArrayList (2); 77 78 public SButtonGroup() {} 79 80 85 public final String 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 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 (!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 iterator() { 158 return buttons.iterator(); 159 } 160 161 public Enumeration getElements() { 162 return Collections.enumeration(buttons); 163 } 164 165 public String getLowLevelEventId() { 166 return getComponentId(); 167 } 168 169 175 public void addActionListener(ActionListener listener) { 176 listenerList.add(ActionListener .class, listener); 177 } 178 179 public void removeActionListener(ActionListener listener) { 180 listenerList.remove(ActionListener .class, listener); 181 } 182 183 184 187 protected void fireActionPerformed(String command) { 188 fireActionEvent(new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 189 command)); 190 } 191 192 195 protected void fireActionEvent(ActionEvent e) { 196 if (e == null) 197 return; 198 199 if (delayEvents) { 200 delayedEvents.add(e); 201 return; 202 } 203 204 Object [] listeners = listenerList.getListenerList(); 206 for (int i = listeners.length - 2; i >= 0; i -= 2) { 209 if (listeners[i] == ActionListener .class) { 210 ((ActionListener ) 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 iter = delayedEvents.iterator(); iter.hasNext();) { 228 ActionEvent e = (ActionEvent ) iter.next(); 229 230 fireActionEvent(e); 231 } 232 delayedEvents.clear(); 233 } 234 } 235 236 237 | Popular Tags |