001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.addressbook.gui.focus;
017:
018: import java.awt.event.FocusEvent;
019: import java.awt.event.FocusListener;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Vector;
024:
025: import org.columba.core.gui.action.AbstractColumbaAction;
026:
027: /**
028: *
029: * Every {@link FocusOwner}should register at the <code>FocusManager</code>.
030: * <p>
031: * FocusManager enables and disables the following Actions:
032: * <ul>
033: * <li>CutAction</li>
034: * <li>CopyAction</li>
035: * <li>PasteAction</li>
036: * <li>DeleteAction</li>
037: * <li>SelectAllAction</li>
038: * </ul>
039: *
040: *
041: * @author fdietz
042: *
043: */
044: public class FocusManager implements FocusListener {
045: /**
046: * list of focus owners
047: */
048: List list;
049:
050: /**
051: * map associating focus listener ui with focus owner
052: */
053: Map map;
054:
055: /**
056: * all actions
057: */
058: AbstractColumbaAction cutAction;
059:
060: AbstractColumbaAction copyAction;
061:
062: AbstractColumbaAction pasteAction;
063:
064: AbstractColumbaAction deleteAction;
065:
066: AbstractColumbaAction selectAllAction;
067:
068: AbstractColumbaAction undoAction;
069:
070: AbstractColumbaAction redoAction;
071:
072: /**
073: * current focus owner
074: */
075: FocusOwner current = null;
076:
077: /**
078: * last available focus owner
079: */
080: FocusOwner last = null;
081:
082: private static FocusManager instance = new FocusManager();
083:
084: public FocusManager() {
085: list = new Vector();
086: map = new HashMap();
087: }
088:
089: public static FocusManager getInstance() {
090: return instance;
091: }
092:
093: /**
094: * register FocusOwner and add FocusListener
095: *
096: * @param c
097: * focus owner
098: */
099: 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 event) {
173: current = (FocusOwner) map.get(event.getSource());
174:
175: updateActions();
176: }
177:
178: /**
179: * Component lost focus
180: */
181: public void focusLost(FocusEvent 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: }
|