1 19 24 25 package org.netbeans.core.output2.ui; 26 27 import org.openide.util.WeakListeners; 28 29 import javax.swing.*; 30 import java.awt.event.ActionEvent ; 31 import java.beans.PropertyChangeEvent ; 32 import java.beans.PropertyChangeListener ; 33 import java.lang.ref.Reference ; 34 import java.lang.ref.WeakReference ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 39 44 class WeakAction implements Action, PropertyChangeListener { 45 private Reference original; 46 private Icon icon; 47 private List listeners = new ArrayList (); 48 private String name = null; 49 50 51 public WeakAction(Action original) { 52 wasEnabled = original.isEnabled(); 53 icon = (Icon) original.getValue (SMALL_ICON); 54 name = (String ) original.getValue (NAME); 55 this.original = new WeakReference (original); 56 original.addPropertyChangeListener(WeakListeners.propertyChange(this, original)); 57 } 58 59 public void actionPerformed(ActionEvent actionEvent) { 60 Action orig = getOriginal(); 61 if (orig != null) { 62 orig.actionPerformed (actionEvent); 63 } 64 } 65 66 public synchronized void addPropertyChangeListener(PropertyChangeListener pce) { 67 listeners.add (pce); 68 } 69 70 public Object getValue(String str) { 71 if (SMALL_ICON.equals(str)) { 72 return icon; 73 } else { 74 Action orig = getOriginal(); 75 if (orig != null) { 76 return orig.getValue(str); 77 } else if (NAME.equals(str)) { 78 return name; 80 } 81 } 82 return null; 83 } 84 85 private boolean wasEnabled = true; 86 public boolean isEnabled() { 87 Action orig = getOriginal(); 88 if (orig != null) { 89 wasEnabled = orig.isEnabled(); 90 return wasEnabled; 91 } 92 return false; 93 } 94 95 public void putValue(String str, Object obj) { 96 if (SMALL_ICON.equals(str)) { 97 icon = (Icon) obj; 98 } else { 99 Action orig = getOriginal(); 100 if (orig != null) { 101 orig.putValue(str, obj); 102 } 103 } 104 } 105 106 public synchronized void removePropertyChangeListener(PropertyChangeListener pce) { 107 listeners.remove (pce); 108 } 109 110 public void setEnabled(boolean val) { 111 Action orig = getOriginal(); 112 if (orig != null) { 113 orig.setEnabled(val); 114 } 115 } 116 117 private boolean hadOriginal = true; 118 private Action getOriginal() { 119 Action result = (Action) original.get(); 120 if (result == null && hadOriginal && wasEnabled) { 121 hadOriginal = false; 122 firePropertyChange ("enabled", Boolean.TRUE, Boolean.FALSE); } 124 return result; 125 } 126 127 private synchronized void firePropertyChange(String nm, Object old, Object nue) { 128 PropertyChangeEvent pce = new PropertyChangeEvent (this, nm, old, nue); 129 for (Iterator i=listeners.iterator(); i.hasNext();) { 130 PropertyChangeListener pcl = (PropertyChangeListener ) i.next(); 131 pcl.propertyChange(pce); 132 } 133 } 134 135 public void propertyChange(java.beans.PropertyChangeEvent pce) { 136 firePropertyChange (pce.getPropertyName(), pce.getOldValue(), 137 pce.getNewValue()); 138 if ("enabled".equals(pce.getPropertyName())) { wasEnabled = Boolean.TRUE.equals(pce.getNewValue()); 140 } 141 } 142 143 } 144 | Popular Tags |