KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > DelegatingAction


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 JavaDoc;
40 import java.util.ListIterator JavaDoc;
41 import java.util.LinkedList JavaDoc;
42
43 public class DelegatingAction implements Action {
44   /**
45    * These keys will be copied from the delegatee. All other keys are held in this action itself.
46    */

47   private static final String JavaDoc[] KEYS_TO_DELEGATE = {
48     //DEFAULT,
49
NAME,
50     //SHORT_DESCRIPTION,
51
//LONG_DESCRIPTION,
52
//SMALL_ICON,
53
//ACTION_COMMAND_KEY,
54
//ACCELERATOR_KEY,
55
//MNEMONIC_KEY,
56
};
57
58   /** Can't use a more specific type parameter because of Action interface. */
59   private HashMap JavaDoc<String JavaDoc, Object JavaDoc> _localProperties = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
60
61   /** The action to delegate to. If it's null, this action is disabled and all method calls will result in
62    * IllegalStateExceptions.
63    */

64   private Action _delegatee;
65   private final LinkedList JavaDoc<PropertyChangeListener> _listenerList =
66     new LinkedList JavaDoc<PropertyChangeListener>();
67
68   /**
69    * Returns value of the key, from delegatee is it's in {@link #KEYS_TO_DELEGATE}
70    * or from this if not.
71    */

72   public Object JavaDoc getValue(String JavaDoc 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 JavaDoc 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 JavaDoc key, Object JavaDoc value) {
91     _checkState();
92
93     if (_isDelegatedKey(key)) {
94       _delegatee.putValue(key, value);
95     }
96     else {
97       Object JavaDoc old = _localProperties.get(key);
98       _localProperties.put(key, value);
99       ListIterator JavaDoc 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 JavaDoc("setDelegatee(null) is not allowed!");
140     }
141
142     // create property change notifications
143
Boolean JavaDoc 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 JavaDoc oldValue = _delegatee.getValue(KEYS_TO_DELEGATE[i]);
155         Object JavaDoc 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     // remove listeners from old and add to new
165
ListIterator JavaDoc 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       // fire property change events for all properties
176

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 JavaDoc("delegatee is null!");
192     }
193   }
194 }
195
196
197
Popular Tags