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); 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 KeyEvent class 407 */ 408 public interface KeyEventAccessor { 409 /** 410 * Sets rawCode field for KeyEvent 411 */ 412 void setRawCode(KeyEvent ev, long rawCode); 413 414 /** 415 * Sets primaryLevelUnicode field for KeyEvent 416 */ 417 void setPrimaryLevelUnicode(KeyEvent ev, long primaryLevelUnicode); 418 419 /** 420 * Sets extendedKeyCode field for KeyEvent 421 */ 422 void setExtendedKeyCode(KeyEvent ev, long extendedKeyCode); 423 424 /** 425 * Returns an extended key code for the event. 426 */ 427 int getExtendedKeyCode(KeyEvent ev); 428 429 /** 430 * Returns an extended key code for a unicode character. 431 */ 432 int getExtendedKeyCodeForChar(int c); 433 } 434 435 /** 436 * An accessor for the ClientPropertyKey class 437 */ 438 public interface ClientPropertyKeyAccessor { 439 /** 440 * Retrieves JComponent_TRANSFER_HANDLER enum object 441 */ 442 Object getJComponent_TRANSFER_HANDLER(); 443 } 444 445 /** 446 * An accessor for the DefaultKeyboardFocusManager class 447 */ 448 public interface DefaultKeyboardFocusManagerAccessor { 449 void consumeNextKeyTyped(DefaultKeyboardFocusManager dkfm, KeyEvent e); 450 } 451 452 /* 453 * Accessor instances are initialized in the static initializers of 454 * corresponding AWT classes by using setters defined below. 455 */ 456 /* The java.awt.Component class accessor object. 457 */ 458 private static ComponentAccessor componentAccessor; 459 private static KeyboardFocusManagerAccessor kfmAccessor; 460 /* 461 * The java.awt.Window class accessor object. 462 */ 463 private static WindowAccessor windowAccessor; 464 465 /* 466 * The java.awt.AWTEvent class accessor object. 467 */ 468 private static AWTEventAccessor awtEventAccessor; 469 private static MenuComponentAccessor menuComponentAccessor; 470 private static EventQueueAccessor eventQueueAccessor; 471 private static PopupMenuAccessor popupMenuAccessor; 472 private static ScrollPaneAdjustableAccessor scrollPaneAdjustableAccessor; 473 private static CheckboxMenuItemAccessor checkboxMenuItemAccessor; 474 private static CursorAccessor cursorAccessor; 475 private static MenuBarAccessor menuBarAccessor; 476 private static MenuItemAccessor menuItemAccessor; 477 private static MenuAccessor menuAccessor; 478 private static KeyEventAccessor keyEventAccessor; 479 private static ClientPropertyKeyAccessor clientPropertyKeyAccessor; 480 private static DefaultKeyboardFocusManagerAccessor defaultKeyboardFocusManagerAccessor; 481 482 /** 483 * Set an accessor object for the java.awt.Window class. 484 */ 485 public static void setWindowAccessor(WindowAccessor wa) { 486 windowAccessor = wa; 487 } 488 489 /** 490 * Retrieve the accessor object for the java.awt.Window class. 491 */ 492 public static WindowAccessor getWindowAccessor() { 493 if (windowAccessor == null) { 494 unsafe.ensureClassInitialized(Window.class); 495 } 496 497 return windowAccessor; 498 } 499 500 /* 501 * Set an accessor object for the java.awt.Component class. 502 */ 503 public static void setComponentAccessor(ComponentAccessor ca) { 504 componentAccessor = ca; 505 } 506 507 /* 508 * Retrieve the accessor object for the java.awt.Component class. 509 */ 510 public static ComponentAccessor getComponentAccessor() { 511 if (componentAccessor == null) { 512 unsafe.ensureClassInitialized(Component.class); 513 } 514 515 return componentAccessor; 516 } 517 518 /** 519 * Set an accessor object for the java.awt.KeyboardFocusManager class. 520 */ 521 public static void setKeyboardFocusManagerAccessor(KeyboardFocusManagerAccessor kfma) { 522 kfmAccessor = kfma; 523 } 524 525 /** 526 * Retrieve the accessor object for the java.awt.KeyboardFocusManager class. 527 */ 528 public static KeyboardFocusManagerAccessor getKeyboardFocusManagerAccessor() { 529 if (kfmAccessor == null) { 530 unsafe.ensureClassInitialized(KeyboardFocusManager.class); 531 } 532 return kfmAccessor; 533 } 534 535 /* 536 * Set an accessor object for the java.awt.AWTEvent class. 537 */ 538 public static void setAWTEventAccessor(AWTEventAccessor aea) { 539 awtEventAccessor = aea; 540 } 541 542 /* 543 * Retrieve the accessor object for the java.awt.AWTEvent class. 544 */ 545 public static AWTEventAccessor getAWTEventAccessor() { 546 if (awtEventAccessor == null) { 547 unsafe.ensureClassInitialized(AWTEvent.class); 548 } 549 return awtEventAccessor; 550 } 551 552 /** 553 * Set an accessor object for the java.awt.MenuComponent class. 554 */ 555 public static void setMenuComponentAccessor(MenuComponentAccessor mca) { 556 menuComponentAccessor = mca; 557 } 558 559 /** 560 * Retrieve the accessor object for the java.awt.MenuComponent class. 561 */ 562 public static MenuComponentAccessor getMenuComponentAccessor() { 563 if (menuComponentAccessor == null) { 564 unsafe.ensureClassInitialized(MenuComponent.class); 565 } 566 567 return menuComponentAccessor; 568 } 569 570 /** 571 * Set an accessor object for the java.awt.EventQueue class. 572 */ 573 public static void setEventQueueAccessor(EventQueueAccessor eqa) { 574 eventQueueAccessor = eqa; 575 } 576 577 /** 578 * Retrieve the accessor object for the java.awt.EventQueue class. 579 */ 580 public static EventQueueAccessor getEventQueueAccessor() { 581 if (eventQueueAccessor == null) { 582 unsafe.ensureClassInitialized(EventQueue.class); 583 } 584 return eventQueueAccessor; 585 } 586 587 /** 588 * Set an accessor object for the java.awt.PopupMenu class. 589 */ 590 public static void setPopupMenuAccessor(PopupMenuAccessor pma) { 591 popupMenuAccessor = pma; 592 } 593 594 /** 595 * Retrieve the accessor object for the java.awt.PopupMenu class. 596 */ 597 public static PopupMenuAccessor getPopupMenuAccessor() { 598 if (popupMenuAccessor == null) { 599 unsafe.ensureClassInitialized(PopupMenu.class); 600 } 601 return popupMenuAccessor; 602 } 603 604 /** 605 * Set an accessor object for the java.awt.ScrollPaneAdjustable class. 606 */ 607 public static void setScrollPaneAdjustableAccessor(ScrollPaneAdjustableAccessor adj) { 608 scrollPaneAdjustableAccessor = adj; 609 } 610 611 /** 612 * Retrieve the accessor object for the java.awt.ScrollPaneAdjustable 613 * class. 614 */ 615 public static ScrollPaneAdjustableAccessor getScrollPaneAdjustableAccessor() { 616 if (scrollPaneAdjustableAccessor == null) { 617 unsafe.ensureClassInitialized(ScrollPaneAdjustable.class); 618 } 619 return scrollPaneAdjustableAccessor; 620 } 621 622 /** 623 * Set an accessor object for the java.awt.CheckboxMenuItem class. 624 */ 625 public static void setCheckboxMenuItemAccessor(CheckboxMenuItemAccessor cmia) { 626 checkboxMenuItemAccessor = cmia; 627 } 628 629 /** 630 * Retrieve the accessor object for the java.awt.CheckboxMenuItem class. 631 */ 632 public static CheckboxMenuItemAccessor getCheckboxMenuItemAccessor() { 633 if (checkboxMenuItemAccessor == null) { 634 unsafe.ensureClassInitialized(CheckboxMenuItemAccessor.class); 635 } 636 return checkboxMenuItemAccessor; 637 } 638 639 /** 640 * Set an accessor object for the java.awt.Cursor class. 641 */ 642 public static void setCursorAccessor(CursorAccessor ca) { 643 cursorAccessor = ca; 644 } 645 646 /** 647 * Retrieve the accessor object for the java.awt.Cursor class. 648 */ 649 public static CursorAccessor getCursorAccessor() { 650 if (cursorAccessor == null) { 651 unsafe.ensureClassInitialized(CursorAccessor.class); 652 } 653 return cursorAccessor; 654 } 655 656 /** 657 * Set an accessor object for the java.awt.MenuBar class. 658 */ 659 public static void setMenuBarAccessor(MenuBarAccessor mba) { 660 menuBarAccessor = mba; 661 } 662 663 /** 664 * Retrieve the accessor object for the java.awt.MenuBar class. 665 */ 666 public static MenuBarAccessor getMenuBarAccessor() { 667 if (menuBarAccessor == null) { 668 unsafe.ensureClassInitialized(MenuBarAccessor.class); 669 } 670 return menuBarAccessor; 671 } 672 673 /** 674 * Set an accessor object for the java.awt.MenuItem class. 675 */ 676 public static void setMenuItemAccessor(MenuItemAccessor mia) { 677 menuItemAccessor = mia; 678 } 679 680 /** 681 * Retrieve the accessor object for the java.awt.MenuItem class. 682 */ 683 public static MenuItemAccessor getMenuItemAccessor() { 684 if (menuItemAccessor == null) { 685 unsafe.ensureClassInitialized(MenuItemAccessor.class); 686 } 687 return menuItemAccessor; 688 } 689 690 /** 691 * Set an accessor object for the java.awt.Menu class. 692 */ 693 public static void setMenuAccessor(MenuAccessor ma) { 694 menuAccessor = ma; 695 } 696 697 /** 698 * Retrieve the accessor object for the java.awt.Menu class. 699 */ 700 public static MenuAccessor getMenuAccessor() { 701 if (menuAccessor == null) { 702 unsafe.ensureClassInitialized(MenuAccessor.class); 703 } 704 return menuAccessor; 705 } 706 707 /** 708 * Set an accessor object for the java.awt.event.KeyEvent class. 709 */ 710 public static void setKeyEventAccessor(KeyEventAccessor kea) { 711 keyEventAccessor = kea; 712 } 713 714 /** 715 * Retrieve the accessor object for the java.awt.event.KeyEvent class. 716 */ 717 public static KeyEventAccessor getKeyEventAccessor() { 718 if (keyEventAccessor == null) { 719 unsafe.ensureClassInitialized(KeyEventAccessor.class); 720 } 721 return keyEventAccessor; 722 } 723 724 /** 725 * Set an accessor object for the javax.swing.ClientPropertyKey class. 726 */ 727 public static void setClientPropertyKeyAccessor(ClientPropertyKeyAccessor cpka) { 728 clientPropertyKeyAccessor = cpka; 729 } 730 731 /** 732 * Retrieve the accessor object for the javax.swing.ClientPropertyKey class. 733 */ 734 public static ClientPropertyKeyAccessor getClientPropertyKeyAccessor() { 735 if (clientPropertyKeyAccessor == null) { 736 unsafe.ensureClassInitialized(ClientPropertyKeyAccessor.class); 737 } 738 return clientPropertyKeyAccessor; 739 } 740 741 /** 742 * Set an accessor object for the java.awt.DefaultKeyboardFocusManager class. 743 */ 744 public static void setDefaultKeyboardFocusManagerAccessor( 745 DefaultKeyboardFocusManagerAccessor dkfma) { 746 defaultKeyboardFocusManagerAccessor = dkfma; 747 } 748 749 /** 750 * Retrieve the accessor object for the java.awt.DefaultKeyboardFocusManager class. 751 */ 752 public static DefaultKeyboardFocusManagerAccessor getDefaultKeyboardFocusManagerAccessor() { 753 if (defaultKeyboardFocusManagerAccessor == null) { 754 unsafe.ensureClassInitialized(DefaultKeyboardFocusManagerAccessor.class); 755 } 756 return defaultKeyboardFocusManagerAccessor; 757 } 758 }