KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > focus > FocusManager


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.addressbook.gui.focus;
17
18 import java.awt.event.FocusEvent JavaDoc;
19 import java.awt.event.FocusListener JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.columba.core.gui.action.AbstractColumbaAction;
26
27 /**
28  *
29  * Every {@link FocusOwner}should register at the <code>FocusManager</code>.
30  * <p>
31  * FocusManager enables and disables the following Actions:
32  * <ul>
33  * <li>CutAction</li>
34  * <li>CopyAction</li>
35  * <li>PasteAction</li>
36  * <li>DeleteAction</li>
37  * <li>SelectAllAction</li>
38  * </ul>
39  *
40  *
41  * @author fdietz
42  *
43  */

44 public class FocusManager implements FocusListener JavaDoc {
45     /**
46      * list of focus owners
47      */

48     List JavaDoc list;
49
50     /**
51      * map associating focus listener ui with focus owner
52      */

53     Map JavaDoc map;
54
55     /**
56      * all actions
57      */

58     AbstractColumbaAction cutAction;
59
60     AbstractColumbaAction copyAction;
61
62     AbstractColumbaAction pasteAction;
63
64     AbstractColumbaAction deleteAction;
65
66     AbstractColumbaAction selectAllAction;
67
68     AbstractColumbaAction undoAction;
69
70     AbstractColumbaAction redoAction;
71
72     /**
73      * current focus owner
74      */

75     FocusOwner current = null;
76
77     /**
78      * last available focus owner
79      */

80     FocusOwner last = null;
81     
82     private static FocusManager instance = new FocusManager();
83
84     public FocusManager() {
85         list = new Vector JavaDoc();
86         map = new HashMap JavaDoc();
87     }
88     
89     public static FocusManager getInstance() {
90         return instance;
91     }
92
93     /**
94      * register FocusOwner and add FocusListener
95      *
96      * @param c
97      * focus owner
98      */

99     public void registerComponent(FocusOwner c) {
100         list.add(c);
101
102         // associate ui component with FocusOwner
103
map.put(c.getComponent(), c);
104
105         c.getComponent().addFocusListener(this);
106     }
107
108     /**
109      * Get current focus owner
110      *
111      * Try first current owner. If this fails, try the last available one.
112      *
113      * @return current focus owner
114      */

115     public FocusOwner getCurrentOwner() {
116         if (current != null) {
117             return current;
118         }
119
120         if (last != null) {
121             return last;
122         }
123
124         return null;
125     }
126
127     /**
128      *
129      * FocusOwner objects should call this method on selection changes in their
130      * view component to enable/disable the actions
131      *
132      */

133     public void updateActions() {
134         // TODO: fix updateActions
135
//enableActions(getCurrentOwner());
136
}
137
138     /**
139      * enable/disable actions
140      *
141      * @param o
142      * current focus owner
143      */

144     protected void enableActions(FocusOwner o) {
145         if (o == null) {
146             // no component has the focus
147
// -> disable all actions
148
cutAction.setEnabled(false);
149             copyAction.setEnabled(false);
150             pasteAction.setEnabled(false);
151             deleteAction.setEnabled(false);
152             undoAction.setEnabled(false);
153             redoAction.setEnabled(false);
154             selectAllAction.setEnabled(false);
155
156             return;
157         }
158
159         cutAction.setEnabled(o.isCutActionEnabled());
160         copyAction.setEnabled(o.isCopyActionEnabled());
161         pasteAction.setEnabled(o.isPasteActionEnabled());
162         deleteAction.setEnabled(o.isDeleteActionEnabled());
163         undoAction.setEnabled(o.isUndoActionEnabled());
164         redoAction.setEnabled(o.isRedoActionEnabled());
165         selectAllAction.setEnabled(o.isSelectAllActionEnabled());
166     }
167
168     /**
169      * Component gained focus
170      *
171      */

172     public void focusGained(FocusEvent JavaDoc event) {
173         current = (FocusOwner) map.get(event.getSource());
174
175         updateActions();
176     }
177
178     /**
179      * Component lost focus
180      */

181     public void focusLost(FocusEvent JavaDoc event) {
182         //FocusOwner lost = (FocusOwner) map.get(event.getSource());
183

184         last = current;
185
186         current = null;
187
188         //current = lost;
189
updateActions();
190     }
191
192     /**
193      * execute cut action of currently available focus owner
194      *
195      */

196     public void cut() {
197         getCurrentOwner().cut();
198
199         enableActions(getCurrentOwner());
200     }
201
202     /**
203      * execute copy action of currently available focus owner
204      *
205      */

206     public void copy() {
207         getCurrentOwner().copy();
208
209         enableActions(getCurrentOwner());
210     }
211
212     /**
213      * execute paste action of currently available focus owner
214      *
215      */

216     public void paste() {
217         getCurrentOwner().paste();
218
219         enableActions(getCurrentOwner());
220     }
221
222     /**
223      * execute delete action of currently available focus owner
224      *
225      */

226     public void delete() {
227         getCurrentOwner().delete();
228
229         enableActions(getCurrentOwner());
230     }
231
232     /**
233      * execute redo action of currentyl available focus owner
234      *
235      */

236     public void redo() {
237         getCurrentOwner().redo();
238         enableActions(getCurrentOwner());
239     }
240
241     /**
242      * execute undo action of currentyl available focus owner
243      *
244      */

245     public void undo() {
246         getCurrentOwner().undo();
247         enableActions(getCurrentOwner());
248     }
249
250     /**
251      * execute selectAll action of currentyl available focus owner
252      *
253      */

254     public void selectAll() {
255         getCurrentOwner().selectAll();
256         enableActions(getCurrentOwner());
257     }
258
259     /** *********************** setter of actions ********************* */
260     /**
261      * @param action
262      */

263     public void setCopyAction(AbstractColumbaAction action) {
264         copyAction = action;
265     }
266
267     /**
268      * @param action
269      */

270     public void setCutAction(AbstractColumbaAction action) {
271         cutAction = action;
272     }
273
274     /**
275      * @param action
276      */

277     public void setDeleteAction(AbstractColumbaAction action) {
278         deleteAction = action;
279     }
280
281     /**
282      * @param action
283      */

284     public void setPasteAction(AbstractColumbaAction action) {
285         pasteAction = action;
286     }
287
288     /**
289      * @param action
290      */

291     public void setRedoAction(AbstractColumbaAction action) {
292         redoAction = action;
293     }
294
295     /**
296      * @param action
297      */

298     public void setSelectAllAction(AbstractColumbaAction action) {
299         selectAllAction = action;
300     }
301
302     /**
303      * @param action
304      */

305     public void setUndoAction(AbstractColumbaAction action) {
306         undoAction = action;
307     }
308 }
Popular Tags