1 7 8 46 package org.jdesktop.swing.actions; 47 48 import java.awt.event.ActionEvent ; 49 import java.awt.event.ActionListener ; 50 import java.awt.event.ItemEvent ; 51 import java.awt.event.ItemListener ; 52 53 import java.beans.EventHandler ; 54 import java.beans.Statement ; 55 56 import java.util.EventListener ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 60 import javax.swing.Action ; 61 import javax.swing.Icon ; 62 63 import javax.swing.event.EventListenerList ; 64 65 74 public class BoundAction extends AbstractActionExt { 75 76 private EventListenerList listeners; 78 79 public BoundAction() { 80 this("BoundAction"); 81 } 82 83 public BoundAction(String name) { 84 super(name); 85 } 86 87 91 public BoundAction(String name, String command) { 92 super(name, command); 93 } 94 95 public BoundAction(String name, Icon icon) { 96 super(name, icon); 97 } 98 99 104 public BoundAction(String name, String command, Icon icon) { 105 super(name, command, icon); 106 } 107 108 121 public void setCallback(String callback) { 122 String [] elems = callback.split("#", 2); 123 if (elems.length == 2) { 124 try { 125 Class clz = Class.forName(elems[0]); 126 127 Object obj = clz.newInstance(); 130 131 registerCallback(obj, elems[1]); 132 } catch (Exception ex) { 133 System.out.println("ERROR: setCallback(" + callback 134 + ") - " + ex.getMessage()); 135 } 136 } 137 } 138 139 152 public void registerCallback(Object handler, String method) { 153 if (isStateAction()) { 154 addItemListener(new BooleanInvocationHandler(handler, method)); 156 } else { 157 addActionListener((ActionListener )EventHandler.create(ActionListener .class, 159 handler, method)); 160 } 161 } 162 163 169 private class BooleanInvocationHandler implements ItemListener { 170 171 private Statement falseStatement; 172 private Statement trueStatement; 173 174 public BooleanInvocationHandler(Object target, String methodName) { 175 falseStatement = new Statement (target, methodName, 177 new Object [] { Boolean.FALSE }); 178 179 trueStatement = new Statement (target, methodName, 180 new Object [] { Boolean.TRUE }); 181 } 182 183 public void itemStateChanged(ItemEvent evt) { 184 Statement statement = (evt.getStateChange() == ItemEvent.DESELECTED) ? 185 falseStatement : trueStatement; 186 187 try { 188 statement.execute(); 189 } catch (Exception ex) { 190 ex.printStackTrace(); 191 } 192 } 193 } 194 195 197 private void addListener(Class clz, EventListener listener) { 198 if (listeners == null) { 199 listeners = new EventListenerList (); 200 } 201 listeners.add(clz, listener); 202 } 203 204 private void removeListener(Class clz, EventListener listener) { 205 if (listeners != null) { 206 listeners.remove(clz, listener); 207 } 208 } 209 210 private EventListener [] getListeners(Class clz) { 211 if (listeners == null) { 212 return null; 213 } 214 return listeners.getListeners(clz); 215 } 216 217 220 public void addActionListener(ActionListener listener) { 221 addListener(ActionListener .class, listener); 222 } 223 224 public void removeActionListener(ActionListener listener) { 225 removeListener(ActionListener .class, listener); 226 } 227 228 public ActionListener [] getActionListeners() { 229 return (ActionListener [])getListeners(ActionListener .class); 230 } 231 232 235 public void addItemListener(ItemListener listener) { 236 addListener(ItemListener .class, listener); 237 } 238 239 public void removeItemListener(ItemListener listener) { 240 removeListener(ItemListener .class, listener); 241 } 242 243 public ItemListener [] getItemListeners() { 244 return (ItemListener [])getListeners(ItemListener .class); 245 } 246 247 249 252 public void actionPerformed(ActionEvent evt) { 253 ActionListener [] alist = getActionListeners(); 254 if (alist != null) { 255 for (int i = 0 ; i < alist.length; i++) { 256 alist[i].actionPerformed(evt); 257 } 258 } 259 } 260 261 264 public void itemStateChanged(ItemEvent evt) { 265 boolean newValue; 267 boolean oldValue = isSelected(); 268 269 if (evt.getStateChange() == ItemEvent.SELECTED) { 270 newValue = true; 271 } else { 272 newValue = false; 273 } 274 275 if (oldValue != newValue) { 276 setSelected(newValue); 277 278 ItemListener [] ilist = getItemListeners(); 280 if (ilist != null) { 281 for (int i = 0; i < ilist.length; i++) { 282 ilist[i].itemStateChanged(evt); 283 } 284 } 285 } 286 } 287 288 } 289 | Popular Tags |