1 33 34 package edu.rice.cs.util.swing; 35 36 import javax.swing.*; 37 import java.awt.event.*; 38 import java.beans.*; 39 import java.util.HashMap ; 40 import java.util.ListIterator ; 41 import java.util.LinkedList ; 42 43 public class DelegatingAction implements Action { 44 47 private static final String [] KEYS_TO_DELEGATE = { 48 NAME, 50 }; 57 58 59 private HashMap <String , Object > _localProperties = new HashMap <String , Object >(); 60 61 64 private Action _delegatee; 65 private final LinkedList <PropertyChangeListener> _listenerList = 66 new LinkedList <PropertyChangeListener>(); 67 68 72 public Object getValue(String key) { 73 _checkState(); 74 75 if (_isDelegatedKey(key)) { 76 return _delegatee.getValue(key); 77 } 78 else { 79 return _localProperties.get(key); 80 } 81 } 82 83 private boolean _isDelegatedKey(String key) { 84 for (int i = 0; i < KEYS_TO_DELEGATE.length; i++) { 85 if (KEYS_TO_DELEGATE[i].equals(key)) return true; 86 } 87 return false; 88 } 89 90 public void putValue(String key, Object value) { 91 _checkState(); 92 93 if (_isDelegatedKey(key)) { 94 _delegatee.putValue(key, value); 95 } 96 else { 97 Object old = _localProperties.get(key); 98 _localProperties.put(key, value); 99 ListIterator itor = _listenerList.listIterator(); 100 101 PropertyChangeEvent event = new PropertyChangeEvent(this, key, old, value); 102 103 while (itor.hasNext()) { 104 PropertyChangeListener listener = (PropertyChangeListener) itor.next(); 105 listener.propertyChange(event); 106 } 107 } 108 } 109 110 public void setEnabled(boolean b) { 111 _checkState(); 112 _delegatee.setEnabled(b); 113 } 114 115 public boolean isEnabled() { 116 _checkState(); 117 return _delegatee.isEnabled(); 118 } 119 120 public void addPropertyChangeListener(PropertyChangeListener listener) { 121 _checkState(); 122 _delegatee.addPropertyChangeListener(listener); 123 _listenerList.add(listener); 124 } 125 126 public void removePropertyChangeListener(PropertyChangeListener listener) { 127 _checkState(); 128 _delegatee.removePropertyChangeListener(listener); 129 _listenerList.remove(listener); 130 } 131 132 public void actionPerformed(ActionEvent ae) { 133 _checkState(); 134 _delegatee.actionPerformed(ae); 135 } 136 137 public void setDelegatee(final Action newDelegatee) { 138 if (newDelegatee == null) { 139 throw new IllegalArgumentException ("setDelegatee(null) is not allowed!"); 140 } 141 142 Boolean isEnabled = newDelegatee.isEnabled() ? Boolean.TRUE : Boolean.FALSE; 144 145 PropertyChangeEvent enabledEvent 146 =new PropertyChangeEvent(newDelegatee, "enabled", Boolean.FALSE, isEnabled); 147 148 PropertyChangeEvent[] events = null; 149 150 if (_delegatee != null) { 151 events = new PropertyChangeEvent[KEYS_TO_DELEGATE.length]; 152 153 for (int i = 0; i < KEYS_TO_DELEGATE.length; i++) { 154 Object oldValue = _delegatee.getValue(KEYS_TO_DELEGATE[i]); 155 Object newValue = newDelegatee.getValue(KEYS_TO_DELEGATE[i]); 156 157 events[i] = new PropertyChangeEvent(newDelegatee, 158 KEYS_TO_DELEGATE[i], 159 oldValue, 160 newValue); 161 } 162 } 163 164 ListIterator itor = _listenerList.listIterator(); 166 while (itor.hasNext()) { 167 PropertyChangeListener listener = (PropertyChangeListener) itor.next(); 168 169 if (_delegatee != null) { 170 _delegatee.removePropertyChangeListener(listener); 171 } 172 173 newDelegatee.addPropertyChangeListener(listener); 174 175 177 if (events != null) { 178 for (int i = 0; i < events.length; i++) { 179 listener.propertyChange(events[i]); 180 } 181 } 182 183 listener.propertyChange(enabledEvent); 184 } 185 186 _delegatee = newDelegatee; 187 } 188 189 private void _checkState() { 190 if (_delegatee == null) { 191 throw new IllegalStateException ("delegatee is null!"); 192 } 193 } 194 } 195 196 197 | Popular Tags |