View Javadoc
1   /*
2    * Copyright (c) 2003, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * 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  @SuppressWarnings("serial")
39  public class CausedFocusEvent extends FocusEvent {
40      public enum Cause {
41          UNKNOWN,
42          MOUSE_EVENT,
43          TRAVERSAL,
44          TRAVERSAL_UP,
45          TRAVERSAL_DOWN,
46          TRAVERSAL_FORWARD,
47          TRAVERSAL_BACKWARD,
48          MANUAL_REQUEST,
49          AUTOMATIC_TRAVERSE,
50          ROLLBACK,
51          NATIVE_SYSTEM,
52          ACTIVATION,
53          CLEAR_GLOBAL_FOCUS_OWNER,
54          RETARGETED
55      };
56  
57      private final Cause cause;
58  
59      public Cause getCause() {
60          return cause;
61      }
62  
63      public String toString() {
64          return "java.awt.FocusEvent[" + super.paramString() + ",cause=" + cause + "] on " + getSource();
65      }
66  
67      public CausedFocusEvent(Component source, int id, boolean temporary,
68                              Component opposite, Cause cause) {
69          super(source, id, temporary, opposite);
70          if (cause == null) {
71              cause = Cause.UNKNOWN;
72          }
73          this.cause = cause;
74      }
75  
76      /**
77       * Retargets the original focus event to the new target.  If the
78       * original focus event is CausedFocusEvent, it remains such and
79       * cause is copied.  Otherwise, new CausedFocusEvent is created,
80       * with cause as RETARGETED.
81       * @return retargeted event, or null if e is null
82       */
83      public static FocusEvent retarget(FocusEvent e, Component newSource) {
84          if (e == null) return null;
85  
86          return new CausedFocusEvent(newSource, e.getID(), e.isTemporary(), e.getOppositeComponent(),
87                                      (e instanceof CausedFocusEvent) ? ((CausedFocusEvent)e).getCause() : Cause.RETARGETED);
88      }
89  }