01: /*
02: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.awt;
27:
28: import java.awt.event.FocusEvent;
29: import java.awt.Component;
30:
31: /**
32: * This class represents FocusEvents with a known "cause" - reason why this event happened. It can
33: * be mouse press, traversal, activation, and so on - all causes are described as Cause enum. The
34: * event with the cause can be constructed in two ways - explicitly through constructor of
35: * CausedFocusEvent class or implicitly, by calling appropriate requestFocusXXX method with "cause"
36: * parameter. The default cause is UNKNOWN.
37: */
38: public class CausedFocusEvent extends FocusEvent {
39: public enum Cause {
40: UNKNOWN, MOUSE_EVENT, TRAVERSAL, TRAVERSAL_UP, TRAVERSAL_DOWN, TRAVERSAL_FORWARD, TRAVERSAL_BACKWARD, MANUAL_REQUEST, AUTOMATIC_TRAVERSE, ROLLBACK, NATIVE_SYSTEM, ACTIVATION, CLEAR_GLOBAL_FOCUS_OWNER, RETARGETED
41: };
42:
43: private final Cause cause;
44:
45: public Cause getCause() {
46: return cause;
47: }
48:
49: public String toString() {
50: return "java.awt.FocusEvent[" + super .paramString() + ",cause="
51: + cause + "] on " + getSource();
52: }
53:
54: public CausedFocusEvent(Component source, int id,
55: boolean temporary, Component opposite, Cause cause) {
56: super (source, id, temporary, opposite);
57: if (cause == null) {
58: cause = Cause.UNKNOWN;
59: }
60: this .cause = cause;
61: }
62:
63: /**
64: * Retargets the original focus event to the new target. If the
65: * original focus event is CausedFocusEvent, it remains such and
66: * cause is copied. Otherwise, new CausedFocusEvent is created,
67: * with cause as RETARGETED.
68: * @return retargeted event, or null if e is null
69: */
70: public static FocusEvent retarget(FocusEvent e, Component newSource) {
71: if (e == null)
72: return null;
73:
74: return new CausedFocusEvent(
75: newSource,
76: e.getID(),
77: e.isTemporary(),
78: e.getOppositeComponent(),
79: (e instanceof CausedFocusEvent) ? ((CausedFocusEvent) e)
80: .getCause()
81: : Cause.RETARGETED);
82: }
83: }
|