blob: af3abcf8a9581402a36e41f2a6d5d9090d9ac269 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2007 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
26package sun.awt;
27
28import java.awt.Component;
29import java.awt.Container;
30import java.awt.AWTEvent;
31import java.awt.Font;
32import java.awt.Color;
33import java.awt.Cursor;
34import java.awt.Point;
35
36import java.awt.peer.ComponentPeer;
37
38import java.lang.reflect.Field;
39import java.lang.reflect.Method;
40import java.lang.reflect.InvocationTargetException;
41
42import java.util.logging.Logger;
43import java.util.logging.Level;
44
45import java.security.AccessController;
46import java.security.PrivilegedAction;
47
48/**
49 * A collection of methods for modifying package private fields in AWT components.
50 * This class is meant to be used by Peer code only. Previously peer code
51 * got around this problem by modifying fields from native code. However
52 * as we move away from native code to Pure-java peers we need this class.
53 *
54 * @author Bino George
55 */
56
57
58public class ComponentAccessor
59{
60 private static Class componentClass;
61 private static Field fieldX;
62 private static Field fieldY;
63 private static Field fieldWidth;
64 private static Field fieldHeight;
65 private static Method methodGetParentNoClientCode;
66 private static Method methodGetFontNoClientCode;
67 private static Method methodProcessEvent;
68 private static Method methodEnableEvents;
69 private static Field fieldParent;
70 private static Field fieldBackground;
71 private static Field fieldForeground;
72 private static Field fieldFont;
73 private static Field fieldPacked;
74 private static Field fieldIgnoreRepaint;
75 private static Field fieldPeer;
76 private static Method methodResetGC;
77 private static Field fieldVisible;
78 private static Method methodIsEnabledImpl;
79 private static Method methodGetCursorNoClientCode;
80 private static Method methodLocationNoClientCode;
81
82 private static final Logger log = Logger.getLogger("sun.awt.ComponentAccessor");
83
84 private ComponentAccessor() {
85 }
86
87 static {
88 AccessController.doPrivileged( new PrivilegedAction() {
89 public Object run() {
90 try {
91 componentClass = Class.forName("java.awt.Component");
92 fieldX = componentClass.getDeclaredField("x");
93 fieldX.setAccessible(true);
94 fieldY = componentClass.getDeclaredField("y");
95 fieldY.setAccessible(true);
96 fieldWidth = componentClass.getDeclaredField("width");
97 fieldWidth.setAccessible(true);
98 fieldHeight = componentClass.getDeclaredField("height");
99 fieldHeight.setAccessible(true);
100 fieldForeground = componentClass.getDeclaredField("foreground");
101 fieldForeground.setAccessible(true);
102 fieldBackground = componentClass.getDeclaredField("background");
103 fieldBackground.setAccessible(true);
104 fieldFont = componentClass.getDeclaredField("font");
105 fieldFont.setAccessible(true);
106 methodGetParentNoClientCode = componentClass.getDeclaredMethod("getParent_NoClientCode", (Class[]) null);
107 methodGetParentNoClientCode.setAccessible(true);
108 methodGetFontNoClientCode = componentClass.getDeclaredMethod("getFont_NoClientCode", (Class[]) null);
109 methodGetFontNoClientCode.setAccessible(true);
110 Class[] argTypes = { AWTEvent.class };
111 methodProcessEvent = componentClass.getDeclaredMethod("processEvent",argTypes);
112 methodProcessEvent.setAccessible(true);
113 Class[] argTypesForMethodEnableEvents = { Long.TYPE };
114 methodEnableEvents = componentClass.getDeclaredMethod("enableEvents",argTypesForMethodEnableEvents);
115 methodEnableEvents.setAccessible(true);
116
117 fieldParent = componentClass.getDeclaredField("parent");
118 fieldParent.setAccessible(true);
119 fieldPacked = componentClass.getDeclaredField("isPacked");
120 fieldPacked.setAccessible(true);
121 fieldIgnoreRepaint = componentClass.getDeclaredField("ignoreRepaint");
122 fieldIgnoreRepaint.setAccessible(true);
123
124 fieldPeer = componentClass.getDeclaredField("peer");
125 fieldPeer.setAccessible(true);
126
127 methodResetGC = componentClass.getDeclaredMethod("resetGC", (Class[]) null);
128 methodResetGC.setAccessible(true);
129
130 fieldVisible = componentClass.getDeclaredField("visible");
131 fieldVisible.setAccessible(true);
132
133 methodIsEnabledImpl = componentClass.getDeclaredMethod("isEnabledImpl", (Class[]) null);
134 methodIsEnabledImpl.setAccessible(true);
135
136 methodGetCursorNoClientCode = componentClass.getDeclaredMethod("getCursor_NoClientCode", (Class[]) null);
137 methodGetCursorNoClientCode.setAccessible(true);
138
139 methodLocationNoClientCode = componentClass.getDeclaredMethod("location_NoClientCode", (Class[]) null);
140 methodLocationNoClientCode.setAccessible(true);
141 }
142 catch (NoSuchFieldException e) {
143 log.log(Level.FINE, "Unable to initialize ComponentAccessor", e);
144 }
145 catch (ClassNotFoundException e) {
146 log.log(Level.FINE, "Unable to initialize ComponentAccessor", e);
147 }
148 catch (NoSuchMethodException e) {
149 log.log(Level.FINE, "Unable to initialize ComponentAccessor", e);
150 }
151 // to please javac
152 return null;
153 }
154 });
155 }
156
157 public static void setX(Component c, int x)
158 {
159 try {
160 fieldX.setInt(c,x);
161 }
162 catch (IllegalAccessException e)
163 {
164 log.log(Level.FINE, "Unable to access the Component object", e);
165 }
166 }
167
168 public static void setY(Component c, int y)
169 {
170 try {
171 fieldY.setInt(c,y);
172 }
173 catch (IllegalAccessException e)
174 {
175 log.log(Level.FINE, "Unable to access the Component object", e);
176 }
177 }
178
179 public static void setWidth(Component c, int width)
180 {
181 try {
182 fieldWidth.setInt(c,width);
183 }
184 catch (IllegalAccessException e)
185 {
186 log.log(Level.FINE, "Unable to access the Component object", e);
187 }
188 }
189
190 public static void setHeight(Component c, int height)
191 {
192 try {
193 fieldHeight.setInt(c,height);
194 }
195 catch (IllegalAccessException e)
196 {
197 log.log(Level.FINE, "Unable to access the Component object", e);
198 }
199 }
200
201 public static void setBounds(Component c, int x, int y, int width, int height)
202 {
203 try {
204 fieldX.setInt(c,x);
205 fieldY.setInt(c,y);
206 fieldWidth.setInt(c,width);
207 fieldHeight.setInt(c,height);
208 }
209 catch (IllegalAccessException e)
210 {
211 log.log(Level.FINE, "Unable to access the Component object", e);
212 }
213 }
214
215 public static int getX(Component c) {
216 try {
217 return fieldX.getInt(c);
218 }
219 catch (IllegalAccessException e)
220 {
221 log.log(Level.FINE, "Unable to access the Component object", e);
222 }
223 return 0;
224 }
225
226 public static int getY(Component c) {
227 try {
228 return fieldY.getInt(c);
229 }
230 catch (IllegalAccessException e)
231 {
232 log.log(Level.FINE, "Unable to access the Component object", e);
233 }
234 return 0;
235 }
236
237 public static int getWidth(Component c) {
238 try {
239 return fieldWidth.getInt(c);
240 }
241 catch (IllegalAccessException e)
242 {
243 log.log(Level.FINE, "Unable to access the Component object", e);
244 }
245 return 0;
246 }
247
248 public static int getHeight(Component c) {
249 try {
250 return fieldHeight.getInt(c);
251 }
252 catch (IllegalAccessException e)
253 {
254 log.log(Level.FINE, "Unable to access the Component object", e);
255 }
256 return 0;
257 }
258
259 public static boolean getIsPacked(Component c) {
260 try {
261 return fieldPacked.getBoolean(c);
262 }
263 catch (IllegalAccessException e)
264 {
265 log.log(Level.FINE, "Unable to access the Component object", e);
266 }
267 return false;
268 }
269
270 public static Container getParent_NoClientCode(Component c) {
271 Container parent=null;
272
273 try {
274 parent = (Container) methodGetParentNoClientCode.invoke(c, (Object[]) null);
275 }
276 catch (IllegalAccessException e)
277 {
278 log.log(Level.FINE, "Unable to access the Component object", e);
279 }
280 catch (InvocationTargetException e) {
281 log.log(Level.FINE, "Unable to invoke on the Component object", e);
282 }
283
284 return parent;
285 }
286
287 public static Font getFont_NoClientCode(Component c) {
288 Font font=null;
289
290 try {
291 font = (Font) methodGetFontNoClientCode.invoke(c, (Object[]) null);
292 }
293 catch (IllegalAccessException e)
294 {
295 log.log(Level.FINE, "Unable to access the Component object", e);
296 }
297 catch (InvocationTargetException e) {
298 log.log(Level.FINE, "Unable to invoke on the Component object", e);
299 }
300
301 return font;
302 }
303
304 public static void processEvent(Component c, AWTEvent event) {
305 Font font=null;
306
307 try {
308 Object[] args = new Object[1];
309 args[0] = event;
310 methodProcessEvent.invoke(c,args);
311 }
312 catch (IllegalAccessException e)
313 {
314 log.log(Level.FINE, "Unable to access the Component object", e);
315 }
316 catch (InvocationTargetException e) {
317 log.log(Level.FINE, "Unable to invoke on the Component object", e);
318 }
319 }
320
321 public static void enableEvents(Component c, long event_mask) {
322 try {
323 Object[] args = new Object[1];
324 args[0] = Long.valueOf(event_mask);
325 methodEnableEvents.invoke(c,args);
326 }
327 catch (IllegalAccessException e)
328 {
329 log.log(Level.FINE, "Unable to access the Component object", e);
330 }
331 catch (InvocationTargetException e) {
332 log.log(Level.FINE, "Unable to invoke on the Component object", e);
333 }
334 }
335
336 public static void setParent(Component c, Container parent)
337 {
338 try {
339 fieldParent.set(c,parent);
340 }
341 catch (IllegalAccessException e)
342 {
343 log.log(Level.FINE, "Unable to access the Component object", e);
344 }
345 }
346
347 public static Color getForeground(Component c)
348 {
349 Color color = null;
350 try {
351 color = (Color) fieldForeground.get(c);
352 }
353 catch (IllegalAccessException e)
354 {
355 log.log(Level.FINE, "Unable to access the Component object", e);
356 }
357 return color;
358 }
359
360 public static Color getBackground(Component c)
361 {
362 Color color = null;
363 try {
364 color = (Color) fieldBackground.get(c);
365 }
366 catch (IllegalAccessException e)
367 {
368 log.log(Level.FINE, "Unable to access the Component object", e);
369 }
370 return color;
371 }
372
373 public static void setBackground(Component c, Color color) {
374 try {
375 fieldBackground.set(c, color);
376 }
377 catch (IllegalAccessException e)
378 {
379 log.log(Level.FINE, "Unable to access the Component object", e);
380 }
381 }
382
383 public static Font getFont(Component c)
384 {
385 Font f = null;
386 try {
387 f = (Font) fieldFont.get(c);
388 }
389 catch (IllegalAccessException e)
390 {
391 log.log(Level.FINE, "Unable to access the Component object", e);
392 }
393 return f;
394 }
395
396 public static ComponentPeer getPeer(Component c) {
397 ComponentPeer peer = null;
398 try {
399 peer = (ComponentPeer)fieldPeer.get(c);
400 }
401 catch (IllegalAccessException e)
402 {
403 log.log(Level.FINE, "Unable to access the Component object", e);
404 }
405 return peer;
406 }
407
408 public static void setPeer(Component c, ComponentPeer peer) {
409 try {
410 fieldPeer.set(c, peer);
411 } catch (IllegalAccessException e)
412 {
413 log.log(Level.FINE, "Unable to access the Component object", e);
414 }
415 }
416
417 public static boolean getIgnoreRepaint(Component comp) {
418 try {
419 return fieldIgnoreRepaint.getBoolean(comp);
420 }
421 catch (IllegalAccessException e) {
422 log.log(Level.FINE, "Unable to access the Component object", e);
423 }
424
425 return false;
426 }
427
428 public static void resetGC(Component c) {
429 try {
430 methodResetGC.invoke(c, (Object[]) null);
431 }
432 catch (IllegalAccessException e) {
433 log.log(Level.FINE, "Unable to access the Component object", e);
434 }
435 catch (InvocationTargetException e) {
436 log.log(Level.FINE, "Unable to invoke on the Component object", e);
437 }
438 }
439
440 public static boolean getVisible(Component c) {
441 try {
442 return fieldVisible.getBoolean(c);
443 }
444 catch (IllegalAccessException e)
445 {
446 log.log(Level.FINE, "Unable to access the Component object", e);
447 }
448 return false;
449 }
450
451 public static boolean isEnabledImpl(Component c) {
452 boolean enabled = true;
453 try {
454 enabled = (Boolean) methodIsEnabledImpl.invoke(c, (Object[]) null);
455 }
456 catch (IllegalAccessException e)
457 {
458 log.log(Level.FINE, "Unable to access the Component object", e);
459 }
460 catch (InvocationTargetException e) {
461 log.log(Level.FINE, "Unable to invoke on the Component object", e);
462 }
463 return enabled;
464 }
465
466 public static Cursor getCursor_NoClientCode(Component c) {
467 Cursor cursor = null;
468
469 try {
470 cursor = (Cursor) methodGetCursorNoClientCode.invoke(c, (Object[]) null);
471 }
472 catch (IllegalAccessException e)
473 {
474 log.log(Level.FINE, "Unable to access the Component object", e);
475 }
476 catch (InvocationTargetException e) {
477 log.log(Level.FINE, "Unable to invoke on the Component object", e);
478 }
479
480 return cursor;
481 }
482
483 public static Point getLocation_NoClientCode(Component c) {
484 Point loc = null;
485
486 try {
487 loc = (Point) methodLocationNoClientCode.invoke(c, (Object[]) null);
488 }
489 catch (IllegalAccessException e)
490 {
491 log.log(Level.FINE, "Unable to access the Component object", e);
492 }
493 catch (InvocationTargetException e) {
494 log.log(Level.FINE, "Unable to invoke on the Component object", e);
495 }
496
497 return loc;
498 }
499}