1 19 20 package org.netbeans.editor; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import javax.swing.AbstractAction ; 25 import javax.swing.Action ; 26 27 32 public class DelegateAction extends AbstractAction { 33 34 protected Action delegate; 35 private PropertyChangeListener pcl; 36 37 public DelegateAction() { 38 this(null); 39 } 40 41 public DelegateAction(Action delegate) { 42 this.delegate = delegate; 43 pcl = new PropertyChangeListener (){ 44 public void propertyChange(PropertyChangeEvent evt){ 45 46 if(evt!=null){ 47 if ("enabled".equals(evt.getPropertyName())){ setEnabled(((Boolean )evt.getNewValue()).booleanValue()); 49 }else{ 50 firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue()); 51 } 52 } 53 } 54 }; 55 } 56 57 protected final Action getDelegate() { 58 return delegate; 59 } 60 61 protected void setDelegate(Action delegate){ 62 63 if (this.delegate == delegate) return; 64 65 if (delegate == this) throw new IllegalStateException ("Cannot delegate on the same action"); 67 if (this.delegate != null){ 68 this.delegate.removePropertyChangeListener(pcl); 69 } 70 if (delegate != null) { 71 delegate.addPropertyChangeListener(pcl); 72 } 73 74 this.delegate = delegate; 75 76 setEnabled((delegate != null) ? delegate.isEnabled() : false); 77 } 78 79 public void actionPerformed(java.awt.event.ActionEvent e) { 80 if (delegate != null){ 81 delegate.actionPerformed(e); 82 } 83 } 84 85 86 public Object getValue(String key) { 87 if (delegate != null){ 88 return delegate.getValue(key); 89 }else{ 90 return super.getValue(key); 91 } 92 } 93 94 95 public void putValue(String key, Object value) { 96 if (delegate != null){ 97 delegate.putValue(key, value); 98 }else{ 99 super.putValue(key, value); 100 } 101 } 102 103 104 } 105 | Popular Tags |