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