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 
CausedFocusEvent(Component source, int id, boolean temporary, Component opposite, Cause cause)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 
readResolve()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 }
134