< prev index next >

src/java.desktop/share/classes/sun/awt/CausedFocusEvent.java

Print this page


   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 }
   1 /*
   2  * Copyright (c) 2003, 2016, 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 import java.io.ObjectStreamException;
  31 import java.lang.reflect.Field;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 
  35 /**
  36  * This class exists for deserialization compatibility only.




  37  */
  38 class CausedFocusEvent extends FocusEvent {
  39     private static final long serialVersionUID = -3647309088427840738L;
  40 
  41     private enum Cause {
  42         UNKNOWN,
  43         MOUSE_EVENT,
  44         TRAVERSAL,
  45         TRAVERSAL_UP,
  46         TRAVERSAL_DOWN,
  47         TRAVERSAL_FORWARD,
  48         TRAVERSAL_BACKWARD,
  49         MANUAL_REQUEST,
  50         AUTOMATIC_TRAVERSE,
  51         ROLLBACK,
  52         NATIVE_SYSTEM,
  53         ACTIVATION,
  54         CLEAR_GLOBAL_FOCUS_OWNER,
  55         RETARGETED;
  56     };
  57 
  58     @SuppressWarnings("serial")
  59     private static final Component dummy = new Component(){};
  60 
  61     private final Cause cause;
  62 
  63     private CausedFocusEvent(Component source, int id, boolean temporary,
  64                             Component opposite, Cause cause) {
  65         super(source, id, temporary, opposite);
  66         throw new IllegalStateException();
  67     }
  68 
  69     Object readResolve() throws ObjectStreamException {
  70         FocusEvent.Cause newCause;
  71         switch (cause) {
  72             case UNKNOWN:
  73                 newCause = FocusEvent.Cause.UNKNOWN;
  74                 break;
  75             case MOUSE_EVENT:
  76                 newCause = FocusEvent.Cause.MOUSE_EVENT;
  77                 break;
  78             case TRAVERSAL:
  79                 newCause = FocusEvent.Cause.TRAVERSAL;
  80                 break;
  81             case TRAVERSAL_UP:
  82                 newCause = FocusEvent.Cause.TRAVERSAL_UP;
  83                 break;
  84             case TRAVERSAL_DOWN:
  85                 newCause = FocusEvent.Cause.TRAVERSAL_DOWN;
  86                 break;
  87             case TRAVERSAL_FORWARD:
  88                 newCause = FocusEvent.Cause.TRAVERSAL_FORWARD;
  89                 break;
  90             case TRAVERSAL_BACKWARD:
  91                 newCause = FocusEvent.Cause.TRAVERSAL_BACKWARD;
  92                 break;
  93             case ROLLBACK:
  94                 newCause = FocusEvent.Cause.ROLLBACK;
  95                 break;
  96             case NATIVE_SYSTEM:
  97                 newCause = FocusEvent.Cause.UNEXPECTED;
  98                 break;
  99             case ACTIVATION:
 100                 newCause = FocusEvent.Cause.ACTIVATION;
 101                 break;
 102             case CLEAR_GLOBAL_FOCUS_OWNER:
 103                 newCause = FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER;
 104                 break;
 105             default:
 106                 newCause = FocusEvent.Cause.UNKNOWN;
 107         }
 108 
 109         FocusEvent focusEvent = new FocusEvent(dummy, getID(), isTemporary(),
 110                         getOppositeComponent(), newCause);
 111         focusEvent.setSource(null);
 112         try {
 113             final Field consumedField = FocusEvent.class.getField("consumed");
 114             AccessController.doPrivileged(new PrivilegedAction<Object>() {
 115                 @Override
 116                 public Object run() {
 117                     consumedField.setAccessible(true);
 118                     try {
 119                         consumedField.set(focusEvent, consumed);
 120                     } catch (IllegalAccessException e) {
 121                     }
 122                     return null;
 123                 }
 124             });
 125         } catch (NoSuchFieldException e) {
 126         }
 127 
 128         AWTAccessor.AWTEventAccessor accessor =
 129                                            AWTAccessor.getAWTEventAccessor();
 130         accessor.setBData(focusEvent, accessor.getBData(this));
 131         return focusEvent;








 132     }
 133 }
< prev index next >