1 7 8 package org.jdesktop.swing.actions; 9 10 import java.awt.Component ; 11 12 import java.awt.event.ActionEvent ; 13 14 import java.beans.PropertyChangeEvent ; 15 import java.beans.PropertyChangeListener ; 16 import java.beans.PropertyChangeSupport ; 17 18 import java.util.ArrayList ; 19 import java.util.EventObject ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.swing.Action ; 24 import javax.swing.ActionMap ; 25 import javax.swing.JComponent ; 26 import javax.swing.FocusManager ; 27 28 import org.jdesktop.swing.Application; 29 30 31 77 public class TargetManager { 78 79 private static TargetManager INSTANCE; 80 private List targetList; 81 private Targetable target; 82 private PropertyChangeSupport propertySupport; 83 84 89 public TargetManager() { 90 propertySupport = new PropertyChangeSupport (this); 91 } 92 93 96 public static TargetManager getInstance() { 97 if (INSTANCE == null) { 98 INSTANCE = new TargetManager(); 99 } 100 return INSTANCE; 101 } 102 103 114 public void addTarget(Targetable target, boolean prepend) { 115 if (targetList == null) { 116 targetList = new ArrayList (); 117 } 118 if (prepend) { 119 targetList.add(0, target); 120 } else { 121 targetList.add(target); 122 } 123 } 125 126 130 public void addTarget(Targetable target) { 131 addTarget(target, false); 132 } 133 134 137 public void removeTarget(Targetable target) { 138 if (targetList != null) { 139 targetList.remove(target); 140 } 141 } 142 143 150 public Targetable[] getTargets() { 151 Targetable[] targets; 152 if (targetList == null) { 153 targets = new Targetable[0]; 154 } else { 155 targets = new Targetable[targetList.size()]; 156 targets = (Targetable[])targetList.toArray(new Targetable[targetList.size()]); 157 } 158 return targets; 159 } 160 161 173 public void setTarget(Targetable newTarget) { 174 Targetable oldTarget = target; 175 if (oldTarget != newTarget) { 176 target = newTarget; 177 propertySupport.firePropertyChange("target", oldTarget, newTarget); 178 } 179 } 180 181 187 public Targetable getTarget() { 188 return target; 189 } 190 191 public void addPropertyChangeListener(PropertyChangeListener listener) { 192 propertySupport.addPropertyChangeListener(listener); 193 } 194 195 public void removePropertyChangeListener(PropertyChangeListener listener) { 196 propertySupport.removePropertyChangeListener(listener); 197 } 198 199 225 226 238 public boolean doCommand(Object command, Object value) { 239 if (target != null) { 241 if (target.hasCommand(command) && target.doCommand(command, value)) { 242 return true; 243 } 244 } 245 246 if (targetList != null) { 248 Iterator iter = targetList.iterator(); 249 while (iter.hasNext()) { 250 Targetable target = (Targetable)iter.next(); 251 if (target.hasCommand(command) && 252 target.doCommand(command, value)) { 253 return true; 254 } 255 } 256 } 257 258 ActionEvent evt = null; 259 if (value instanceof ActionEvent ) { 260 evt = (ActionEvent )value; 261 } 262 263 Component comp = FocusManager.getCurrentManager().getPermanentFocusOwner(); 266 while (comp != null) { 267 if (comp instanceof JComponent ) { 268 ActionMap map = ((JComponent )comp).getActionMap(); 269 Action action = map.get(command); 270 if (action != null) { 271 if (evt == null) { 272 evt = new ActionEvent (comp, 0, command.toString()); 273 } 274 action.actionPerformed(evt); 275 276 return true; 277 } 278 } 279 comp = comp.getParent(); 280 } 281 282 Application app = Application.getInstance(); 283 if (app != null) { 284 ActionMap map = app.getActionMap(); 285 Action action = map.get(command); 286 if (action != null) { 287 if (evt == null) { 288 evt = new ActionEvent (comp, 0, command.toString()); 289 } 290 action.actionPerformed(evt); 291 return true; 292 } 293 } 294 return false; 295 } 296 297 301 void reset() { 302 if (targetList != null) { 303 targetList.clear(); 304 targetList = null; 305 } 306 target = null; 307 308 PropertyChangeListener [] listeners = propertySupport.getPropertyChangeListeners(); 309 for (int i = 0; i < listeners.length; i++) { 310 propertySupport.removePropertyChangeListener(listeners[i]); 311 } 312 INSTANCE = null; 313 } 314 315 } 316 | Popular Tags |