1 /* 2 * Copyright (c) 2008, 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.*; 29 30 import sun.misc.Unsafe; 31 32 import java.lang.reflect.InvocationTargetException; 33 import java.security.AccessControlContext; 34 35 import java.util.Vector; 36 37 import java.awt.event.KeyEvent; 38 import java.awt.geom.Point2D; 39 import java.awt.image.BufferedImage; 40 41 /** 42 * The AWTAccessor utility class. 43 * The main purpose of this class is to enable accessing 44 * private and package-private fields of classes from 45 * different classes/packages. See sun.misc.SharedSecretes 46 * for another example. 47 */ 48 public final class AWTAccessor { 49 50 private static final Unsafe unsafe = Unsafe.getUnsafe(); 51 52 /* 53 * We don't need any objects of this class. 54 * It's rather a collection of static methods 55 * and interfaces. 56 */ 57 private AWTAccessor() { 58 } 59 60 /** 61 * An interface of an accessor for java.awt.Window class. 62 */ 63 public interface WindowAccessor { 64 /* 65 * Get opacity level of the given window. 66 */ 67 float getOpacity(Window window); 68 /* 69 * Set opacity level to the given window. 70 */ 71 void setOpacity(Window window, float opacity); 72 /* 73 * Get a shape assigned to the given window. 74 */ 75 Shape getShape(Window window); 76 /* 77 * Set a shape to the given window. 78 */ 79 void setShape(Window window, Shape shape); 80 /* 81 * Identify whether the given window is opaque (true) 82 * or translucent (false). 83 */ 84 boolean isOpaque(Window window); 85 /* 86 * Set the opaque preoperty to the given window. 87 */ 88 void setOpaque(Window window, boolean isOpaque); 89 /* 90 * Update the image of a non-opaque (translucent) window. 91 */ 92 void updateWindow(Window window, BufferedImage backBuffer); 93 /** 94 * Sets the synchronous status of focus requests on lightweight 95 * components in the specified window to the specified value. 96 */ 97 void setLWRequestStatus(Window changed, boolean status); 98 99 /** Get the size of the security warning. 100 */ 101 Dimension getSecurityWarningSize(Window w); 102 103 /** 104 * Set the size of the security warning. 105 */ 106 void setSecurityWarningSize(Window w, int width, int height); 107 108 /** Set the position of the security warning. 109 */ 110 void setSecurityWarningPosition(Window w, Point2D point, 111 float alignmentX, float alignmentY); 112 113 /** Request to recalculate the new position of the security warning for 114 * the given window size/location as reported by the native system. 115 */ 116 Point2D calculateSecurityWarningPosition(Window window, 117 double x, double y, double w, double h); 118 } 119 120 /* 121 * An interface of accessor for the java.awt.Component class. 122 */ 123 public interface ComponentAccessor { 124 /** 125 * Returns the appContext of the component. 126 */ 127 AppContext getAppContext(Component comp); 128 129 /** 130 * Sets the appContext of the component. 131 */ 132 void setAppContext(Component comp, AppContext appContext); 133 134 /* 135 * Sets whether the native background erase for a component 136 * has been disabled via SunToolkit.disableBackgroundErase(). 137 */ 138 void setBackgroundEraseDisabled(Component comp, boolean disabled); 139 /* 140 * Indicates whether the native background erase for a 141 * component has been disabled via 142 * SunToolkit.disableBackgroundErase(). 143 */ 144 boolean getBackgroundEraseDisabled(Component comp); 145 /* 146 * 147 * Gets the bounds of this component in the form of a 148 * <code>Rectangle</code> object. The bounds specify this 149 * component's width, height, and location relative to 150 * its parent. 151 */ 152 Rectangle getBounds(Component comp); 153 /* 154 * Sets the shape of a lw component to cut out from hw components. 155 * 156 * See 6797587, 6776743, 6768307, and 6768332 for details 157 */ 158 void setMixingCutoutShape(Component comp, Shape shape); 159 160 /* 161 * Returns the acc this component was constructed with. 162 */ 163 AccessControlContext getAccessControlContext(Component comp); 164 165 /** 166 * Requests that this Component get the input focus, if this 167 * Component's top-level ancestor is already the focused Window 168 */ 169 boolean requestFocusInWindow(Component comp, CausedFocusEvent.Cause cause); 170 171 /** 172 * Requests that this Component get the input focus, providing the cause 173 */ 174 void requestFocus(Component comp, CausedFocusEvent.Cause cause); 175 176 /** 177 * Returns whether the component is visible without invoking 178 * any client code. 179 */ 180 boolean isVisible_NoClientCode(Component comp); 181 } 182 183 /** 184 * An interface of accessor for the KeyboardFocusManager class. 185 */ 186 public interface KeyboardFocusManagerAccessor { 187 /** 188 * Indicates whether the native implementation should 189 * proceed with a pending focus request for the heavyweight. 190 */ 191 int shouldNativelyFocusHeavyweight(Component heavyweight, 192 Component descendant, 193 boolean temporary, 194 boolean focusedWindowChangeAllowed, 195 long time, 196 CausedFocusEvent.Cause cause); 197 198 void removeLastFocusRequest(Component heavyweight); 199 } 200 201 /* 202 * An accessor for the AWTEvent class. 203 */ 204 public interface AWTEventAccessor { 205 /** 206 * Marks the event as posted. 207 */ 208 void setPosted(AWTEvent ev); 209 210 /** 211 * Sets the flag on this AWTEvent indicating that it was 212 * generated by the system. 213 */ 214 void setSystemGenerated(AWTEvent ev); 215 216 /** 217 * Indicates whether this AWTEvent was generated by the system. 218 */ 219 boolean isSystemGenerated(AWTEvent ev); 220 221 /* 222 * Returns the acc this event was constructed with. 223 */ 224 AccessControlContext getAccessControlContext(AWTEvent ev); 225 226 /** 227 * Returns binary data associated with this event; 228 */ 229 byte[] getBData(AWTEvent ev); 230 231 /** 232 * Associates binary data with this event; 233 */ 234 void setBData(AWTEvent ev, byte[] bdata); 235 } 236 237 /** 238 * An accessor for the MenuComponent class. 239 */ 240 public interface MenuComponentAccessor { 241 /** 242 * Returns the appContext of the menu component. 243 */ 244 AppContext getAppContext(MenuComponent menuComp); 245 246 /** 247 * Sets the appContext of the menu component. 248 */ 249 void setAppContext(MenuComponent menuComp, AppContext appContext); 250 251 /** 252 * Returns the parent container for this menu component. 253 */ 254 MenuContainer getParent(MenuComponent menuComp); 255 256 /** 257 * Gets the font used for this menu component. 258 */ 259 Font getFont_NoClientCode(MenuComponent menuComp); 260 } 261 262 /** An accessor for the EventQueue class 263 */ 264 public interface EventQueueAccessor { 265 /** 266 * Returns whether an event is pending on any of the separate Queues. 267 */ 268 boolean noEvents(EventQueue eventQueue); 269 270 /** 271 * Returns dispatch thread for the given EventQueue which has private access 272 */ 273 Thread getDispatchThread(EventQueue eventQueue); 274 275 /** 276 * Returns next queue for the given EventQueue which has private access 277 */ 278 EventQueue getNextQueue(EventQueue eventQueue); 279 280 /** 281 * Removes any pending events for the specified source object. 282 */ 283 void removeSourceEvents(EventQueue eventQueue, Object source, 284 boolean removeAllEvents); 285 /** 286 * Static in EventQueue 287 */ 288 void invokeAndWait(Object source, Runnable r) 289 throws InterruptedException, InvocationTargetException; 290 291 /** 292 * Gets most recent event time in the EventQueue 293 */ 294 long getMostRecentEventTime(EventQueue eventQueue); 295 } 296 297 /** 298 * An accessor for the PopupMenu class 299 */ 300 public interface PopupMenuAccessor { 301 /** 302 * Returns whether the popup menu is attached to a tray 303 */ 304 boolean isTrayIconPopup(PopupMenu popupMenu); 305 } 306 307 /** 308 * An accessor for the ScrollPaneAdjustable class. 309 */ 310 public interface ScrollPaneAdjustableAccessor { 311 /** 312 * Sets the value of this scrollbar to the specified value. 313 */ 314 void setTypedValue(final ScrollPaneAdjustable adj, final int v, 315 final int type); 316 } 317 318 /** 319 * An accessor for the CheckboxMenuItem class 320 */ 321 public interface CheckboxMenuItemAccessor { 322 /** 323 * Returns whether menu item is checked 324 */ 325 boolean getState(CheckboxMenuItem cmi); 326 } 327 328 /** 329 * An accessor for the Cursor class 330 */ 331 public interface CursorAccessor { 332 /** 333 * Returns pData of the Cursor class 334 */ 335 long getPData(Cursor cursor); 336 337 /** 338 * Sets pData to the Cursor class 339 */ 340 void setPData(Cursor cursor, long pData); 341 342 /** 343 * Return type of the Cursor class 344 */ 345 int getType(Cursor cursor); 346 } 347 348 /** 349 * An accessor for the MenuBar class 350 */ 351 public interface MenuBarAccessor { 352 /** 353 * Returns help menu 354 */ 355 Menu getHelpMenu(MenuBar menuBar); 356 357 /** 358 * Returns menus 359 */ 360 Vector getMenus(MenuBar menuBar); 361 } 362 363 /** 364 * An accessor for the MenuItem class 365 */ 366 public interface MenuItemAccessor { 367 /** 368 * Returns whether menu item is enabled 369 */ 370 boolean isEnabled(MenuItem item); 371 372 /** 373 * Gets the command name of the action event that is fired 374 * by this menu item. 375 */ 376 String getActionCommandImpl(MenuItem item); 377 378 /** 379 * Returns true if the item and all its ancestors are 380 * enabled, false otherwise 381 */ 382 boolean isItemEnabled(MenuItem item); 383 384 /** 385 * Returns label 386 */ 387 String getLabel(MenuItem item); 388 389 /** 390 * Returns shortcut 391 */ 392 MenuShortcut getShortcut(MenuItem item); 393 } 394 395 /** 396 * An accessor for the Menu class 397 */ 398 public interface MenuAccessor { 399 /** 400 * Returns vector of the items that are part of the Menu 401 */ 402 Vector getItems(Menu menu); 403 } 404 405 /** 406 * An accessor for the ClientPropertyKey class 407 */ 408 public interface ClientPropertyKeyAccessor { 409 /** 410 * Retrieves JComponent_TRANSFER_HANDLER enum object 411 */ 412 Object getJComponent_TRANSFER_HANDLER(); 413 } 414 415 /** 416 * An accessor for the DefaultKeyboardFocusManager class 417 */ 418 public interface DefaultKeyboardFocusManagerAccessor { 419 void consumeNextKeyTyped(DefaultKeyboardFocusManager dkfm, KeyEvent e); 420 } 421 422 /* 423 * Accessor instances are initialized in the static initializers of 424 * corresponding AWT classes by using setters defined below. 425 */ 426 /* The java.awt.Component class accessor object. 427 */ 428 private static ComponentAccessor componentAccessor; 429 private static KeyboardFocusManagerAccessor kfmAccessor; 430 /* 431 * The java.awt.Window class accessor object. 432 */ 433 private static WindowAccessor windowAccessor; 434 435 /* 436 * The java.awt.AWTEvent class accessor object. 437 */ 438 private static AWTEventAccessor awtEventAccessor; 439 private static MenuComponentAccessor menuComponentAccessor; 440 private static EventQueueAccessor eventQueueAccessor; 441 private static PopupMenuAccessor popupMenuAccessor; 442 private static ScrollPaneAdjustableAccessor scrollPaneAdjustableAccessor; 443 private static CheckboxMenuItemAccessor checkboxMenuItemAccessor; 444 private static CursorAccessor cursorAccessor; 445 private static MenuBarAccessor menuBarAccessor; 446 private static MenuItemAccessor menuItemAccessor; 447 private static MenuAccessor menuAccessor; 448 private static ClientPropertyKeyAccessor clientPropertyKeyAccessor; 449 private static DefaultKeyboardFocusManagerAccessor defaultKeyboardFocusManagerAccessor; 450 451 /** 452 * Set an accessor object for the java.awt.Window class. 453 */ 454 public static void setWindowAccessor(WindowAccessor wa) { 455 windowAccessor = wa; 456 } 457 458 /** 459 * Retrieve the accessor object for the java.awt.Window class. 460 */ 461 public static WindowAccessor getWindowAccessor() { 462 if (windowAccessor == null) { 463 unsafe.ensureClassInitialized(Window.class); 464 } 465 466 return windowAccessor; 467 } 468 469 /* 470 * Set an accessor object for the java.awt.Component class. 471 */ 472 public static void setComponentAccessor(ComponentAccessor ca) { 473 componentAccessor = ca; 474 } 475 476 /* 477 * Retrieve the accessor object for the java.awt.Component class. 478 */ 479 public static ComponentAccessor getComponentAccessor() { 480 if (componentAccessor == null) { 481 unsafe.ensureClassInitialized(Component.class); 482 } 483 484 return componentAccessor; 485 } 486 487 /** 488 * Set an accessor object for the java.awt.KeyboardFocusManager class. 489 */ 490 public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) { 491 kfmAccessor = kfma; 492 } 493 494 /** 495 * Retrieve the accessor object for the java.awt.KeyboardFocusManager class. 496 */ 497 public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() { 498 if (kfmAccessor == null) { 499 unsafe.ensureClassInitialized(KeyboardFocusManager.class); 500 } 501 return kfmAccessor; 502 } 503 504 /* 505 * Set an accessor object for the java.awt.AWTEvent class. 506 */ 507 public static void setAWTEventAccessor(AWTEventAccessor aea) { 508 awtEventAccessor = aea; 509 } 510 511 /* 512 * Retrieve the accessor object for the java.awt.AWTEvent class. 513 */ 514 public static AWTEventAccessor getAWTEventAccessor() { 515 if (awtEventAccessor == null) { 516 unsafe.ensureClassInitialized(AWTEvent.class); 517 } 518 return awtEventAccessor; 519 } 520 521 /** 522 * Set an accessor object for the java.awt.MenuComponent class. 523 */ 524 public static void setMenuComponentAccessor(MenuComponentAccessor mca) { 525 menuComponentAccessor = mca; 526 } 527 528 /** 529 * Retrieve the accessor object for the java.awt.MenuComponent class. 530 */ 531 public static MenuComponentAccessor getMenuComponentAccessor() { 532 if (menuComponentAccessor == null) { 533 unsafe.ensureClassInitialized(MenuComponent.class); 534 } 535 536 return menuComponentAccessor; 537 } 538 539 /** 540 * Set an accessor object for the java.awt.EventQueue class. 541 */ 542 public static void setEventQueueAccessor(EventQueueAccessor eqa) { 543 eventQueueAccessor = eqa; 544 } 545 546 /** 547 * Retrieve the accessor object for the java.awt.EventQueue class. 548 */ 549 public static EventQueueAccessor getEventQueueAccessor() { 550 if (eventQueueAccessor == null) { 551 unsafe.ensureClassInitialized(EventQueue.class); 552 } 553 return eventQueueAccessor; 554 } 555 556 /** 557 * Set an accessor object for the java.awt.PopupMenu class. 558 */ 559 public static void setPopupMenuAccessor(PopupMenuAccessor pma) { 560 popupMenuAccessor = pma; 561 } 562 563 /** 564 * Retrieve the accessor object for the java.awt.PopupMenu class. 565 */ 566 public static PopupMenuAccessor getPopupMenuAccessor() { 567 if (popupMenuAccessor == null) { 568 unsafe.ensureClassInitialized(PopupMenu.class); 569 } 570 return popupMenuAccessor; 571 } 572 573 /** 574 * Set an accessor object for the java.awt.ScrollPaneAdjustable class. 575 */ 576 public static void setScrollPaneAdjustableAccessor(ScrollPaneAdjustableAccessor adj) { 577 scrollPaneAdjustableAccessor = adj; 578 } 579 580 /** 581 * Retrieve the accessor object for the java.awt.ScrollPaneAdjustable 582 * class. 583 */ 584 public static ScrollPaneAdjustableAccessor getScrollPaneAdjustableAccessor() { 585 if (scrollPaneAdjustableAccessor == null) { 586 unsafe.ensureClassInitialized(ScrollPaneAdjustable.class); 587 } 588 return scrollPaneAdjustableAccessor; 589 } 590 591 /** 592 * Set an accessor object for the java.awt.CheckboxMenuItem class. 593 */ 594 public static void setCheckboxMenuItemAccessor(CheckboxMenuItemAccessor cmia) { 595 checkboxMenuItemAccessor = cmia; 596 } 597 598 /** 599 * Retrieve the accessor object for the java.awt.CheckboxMenuItem class. 600 */ 601 public static CheckboxMenuItemAccessor getCheckboxMenuItemAccessor() { 602 if (checkboxMenuItemAccessor == null) { 603 unsafe.ensureClassInitialized(CheckboxMenuItemAccessor.class); 604 } 605 return checkboxMenuItemAccessor; 606 } 607 608 /** 609 * Set an accessor object for the java.awt.Cursor class. 610 */ 611 public static void setCursorAccessor(CursorAccessor ca) { 612 cursorAccessor = ca; 613 } 614 615 /** 616 * Retrieve the accessor object for the java.awt.Cursor class. 617 */ 618 public static CursorAccessor getCursorAccessor() { 619 if (cursorAccessor == null) { 620 unsafe.ensureClassInitialized(CursorAccessor.class); 621 } 622 return cursorAccessor; 623 } 624 625 /** 626 * Set an accessor object for the java.awt.MenuBar class. 627 */ 628 public static void setMenuBarAccessor(MenuBarAccessor mba) { 629 menuBarAccessor = mba; 630 } 631 632 /** 633 * Retrieve the accessor object for the java.awt.MenuBar class. 634 */ 635 public static MenuBarAccessor getMenuBarAccessor() { 636 if (menuBarAccessor == null) { 637 unsafe.ensureClassInitialized(MenuBarAccessor.class); 638 } 639 return menuBarAccessor; 640 } 641 642 /** 643 * Set an accessor object for the java.awt.MenuItem class. 644 */ 645 public static void setMenuItemAccessor(MenuItemAccessor mia) { 646 menuItemAccessor = mia; 647 } 648 649 /** 650 * Retrieve the accessor object for the java.awt.MenuItem class. 651 */ 652 public static MenuItemAccessor getMenuItemAccessor() { 653 if (menuItemAccessor == null) { 654 unsafe.ensureClassInitialized(MenuItemAccessor.class); 655 } 656 return menuItemAccessor; 657 } 658 659 /** 660 * Set an accessor object for the java.awt.Menu class. 661 */ 662 public static void setMenuAccessor(MenuAccessor ma) { 663 menuAccessor = ma; 664 } 665 666 /** 667 * Retrieve the accessor object for the java.awt.Menu class. 668 */ 669 public static MenuAccessor getMenuAccessor() { 670 if (menuAccessor == null) { 671 unsafe.ensureClassInitialized(MenuAccessor.class); 672 } 673 return menuAccessor; 674 } 675 676 /** 677 * Set an accessor object for the javax.swing.ClientPropertyKey class. 678 */ 679 public static void setClientPropertyKeyAccessor(ClientPropertyKeyAccessor cpka) { 680 clientPropertyKeyAccessor = cpka; 681 } 682 683 /** 684 * Retrieve the accessor object for the javax.swing.ClientPropertyKey class. 685 */ 686 public static ClientPropertyKeyAccessor getClientPropertyKeyAccessor() { 687 if (clientPropertyKeyAccessor == null) { 688 unsafe.ensureClassInitialized(ClientPropertyKeyAccessor.class); 689 } 690 return clientPropertyKeyAccessor; 691 } 692 693 /** 694 * Set an accessor object for the java.awt.DefaultKeyboardFocusManager class. 695 */ 696 public static void setDefaultKeyboardFocusManagerAccessor( 697 DefaultKeyboardFocusManagerAccessor dkfma) { 698 defaultKeyboardFocusManagerAccessor = dkfma; 699 } 700 701 /** 702 * Retrieve the accessor object for the java.awt.DefaultKeyboardFocusManager class. 703 */ 704 public static DefaultKeyboardFocusManagerAccessor getDefaultKeyboardFocusManagerAccessor() { 705 if (defaultKeyboardFocusManagerAccessor == null) { 706 unsafe.ensureClassInitialized(DefaultKeyboardFocusManagerAccessor.class); 707 } 708 return defaultKeyboardFocusManagerAccessor; 709 } 710 }