J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * 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, |
| 41 | MOUSE_EVENT, |
| 42 | TRAVERSAL, |
| 43 | TRAVERSAL_UP, |
| 44 | TRAVERSAL_DOWN, |
| 45 | TRAVERSAL_FORWARD, |
| 46 | TRAVERSAL_BACKWARD, |
| 47 | MANUAL_REQUEST, |
| 48 | AUTOMATIC_TRAVERSE, |
| 49 | ROLLBACK, |
| 50 | NATIVE_SYSTEM, |
| 51 | ACTIVATION, |
| 52 | CLEAR_GLOBAL_FOCUS_OWNER, |
| 53 | RETARGETED |
| 54 | }; |
| 55 | |
| 56 | private final Cause cause; |
| 57 | |
| 58 | public Cause getCause() { |
| 59 | return cause; |
| 60 | } |
| 61 | |
| 62 | public String toString() { |
| 63 | return "java.awt.FocusEvent[" + super.paramString() + ",cause=" + cause + "] on " + getSource(); |
| 64 | } |
| 65 | |
| 66 | public CausedFocusEvent(Component source, int id, boolean temporary, |
| 67 | Component opposite, Cause cause) { |
| 68 | super(source, id, temporary, opposite); |
| 69 | if (cause == null) { |
| 70 | cause = Cause.UNKNOWN; |
| 71 | } |
| 72 | this.cause = cause; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Retargets the original focus event to the new target. If the |
| 77 | * original focus event is CausedFocusEvent, it remains such and |
| 78 | * cause is copied. Otherwise, new CausedFocusEvent is created, |
| 79 | * with cause as RETARGETED. |
| 80 | * @return retargeted event, or null if e is null |
| 81 | */ |
| 82 | public static FocusEvent retarget(FocusEvent e, Component newSource) { |
| 83 | if (e == null) return null; |
| 84 | |
| 85 | return new CausedFocusEvent(newSource, e.getID(), e.isTemporary(), e.getOppositeComponent(), |
| 86 | (e instanceof CausedFocusEvent) ? ((CausedFocusEvent)e).getCause() : Cause.RETARGETED); |
| 87 | } |
| 88 | } |