KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > DelegateAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import javax.swing.AbstractAction JavaDoc;
25 import javax.swing.Action JavaDoc;
26
27 /**
28  * Action that delegates on delegate action.
29  *
30  * @author Miloslav Metelka, Martin Roskanin
31  */

32 public class DelegateAction extends AbstractAction JavaDoc {
33
34     protected Action JavaDoc delegate;
35     private PropertyChangeListener JavaDoc pcl;
36
37     public DelegateAction() {
38         this(null);
39     }
40
41     public DelegateAction(Action JavaDoc delegate) {
42         this.delegate = delegate;
43         pcl = new PropertyChangeListener JavaDoc(){
44              public void propertyChange(PropertyChangeEvent JavaDoc evt){
45                  
46                  if(evt!=null){
47                      if ("enabled".equals(evt.getPropertyName())){ //NOI18N
48
setEnabled(((Boolean JavaDoc)evt.getNewValue()).booleanValue());
49                      }else{
50                          firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
51                      }
52                  }
53              }
54         };
55     }
56
57     protected final Action JavaDoc getDelegate() {
58         return delegate;
59     }
60     
61     protected void setDelegate(Action JavaDoc delegate){
62         
63         if (this.delegate == delegate) return;
64         
65         if (delegate == this) throw new IllegalStateException JavaDoc("Cannot delegate on the same action"); // NOI18N
66

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 JavaDoc e) {
80         if (delegate != null){
81             delegate.actionPerformed(e);
82         }
83     }
84     
85     
86     public Object JavaDoc getValue(String JavaDoc 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 JavaDoc key, Object JavaDoc 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