jdk/src/share/classes/sun/awt/SunToolkit.java
changeset 3084 67ca55732362
parent 2810 fa49c6a06baf
child 3237 7c6c2d9361d9
equal deleted inserted replaced
3083:1954edd3b7a7 3084:67ca55732362
    75      * public masks defined in AWTEvent.  Should be used as the mask
    75      * public masks defined in AWTEvent.  Should be used as the mask
    76      * value for Toolkit.addAWTEventListener.
    76      * value for Toolkit.addAWTEventListener.
    77      */
    77      */
    78     public static final int GRAB_EVENT_MASK = 0x80000000;
    78     public static final int GRAB_EVENT_MASK = 0x80000000;
    79 
    79 
    80     private static Field syncLWRequestsField;
       
    81     private static Method  wakeupMethod;
    80     private static Method  wakeupMethod;
    82     private static Field componentKeyField;
       
    83     private static Field menuComponentKeyField;
       
    84     private static Field trayIconKeyField;
       
    85     private static Field componentAppContextField;
       
    86     private static Field menuComponentAppContextField;
       
    87     private static Field isPostedField;
       
    88     /* The key to put()/get() the PostEventQueue into/from the AppContext.
    81     /* The key to put()/get() the PostEventQueue into/from the AppContext.
    89      */
    82      */
    90     private static final String POST_EVENT_QUEUE_KEY = "PostEventQueue";
    83     private static final String POST_EVENT_QUEUE_KEY = "PostEventQueue";
    91 
    84 
    92     /**
    85     /**
   420     // Maps from non-Component/MenuComponent to AppContext.
   413     // Maps from non-Component/MenuComponent to AppContext.
   421     // WeakHashMap<Component,AppContext>
   414     // WeakHashMap<Component,AppContext>
   422     private static final Map appContextMap =
   415     private static final Map appContextMap =
   423         Collections.synchronizedMap(new WeakHashMap());
   416         Collections.synchronizedMap(new WeakHashMap());
   424 
   417 
   425 
       
   426     /**
   418     /**
   427      * Sets the appContext field of target. If target is not a Component or
   419      * Sets the appContext field of target. If target is not a Component or
   428      * MenuComponent, this returns false.
   420      * MenuComponent, this returns false.
   429      */
   421      */
   430     private static boolean setAppContext(Object target, AppContext context)
   422     private static boolean setAppContext(Object target,
   431     {
   423                                          AppContext context) {
   432         if (!(target instanceof Component) && !(target instanceof MenuComponent)) {
   424         if (target instanceof Component) {
       
   425             AWTAccessor.getComponentAccessor().
       
   426                 setAppContext((Component)target, context);
       
   427         } else if (target instanceof MenuComponent) {
       
   428             AWTAccessor.getMenuComponentAccessor().
       
   429                 setAppContext((MenuComponent)target, context);
       
   430         } else {
   433             return false;
   431             return false;
   434         }
   432         }
   435         try{
       
   436             if (target instanceof Component){
       
   437                 if (componentAppContextField == null) {
       
   438                     componentAppContextField = getField(Component.class, "appContext");
       
   439                 }
       
   440                 componentAppContextField.set(target, context);
       
   441             } else if (target instanceof MenuComponent) {
       
   442                 if (menuComponentAppContextField == null) {
       
   443                     menuComponentAppContextField = getField(MenuComponent.class, "appContext");
       
   444                 }
       
   445                 menuComponentAppContextField.set(target, context);
       
   446             }
       
   447         } catch( IllegalAccessException e){
       
   448             assert false;
       
   449         }
       
   450 
       
   451         return true;
   433         return true;
   452     }
   434     }
   453 
   435 
   454     /**
   436     /**
   455      * Returns the appContext field for target. If target is not a
   437      * Returns the appContext field for target. If target is not a
   456      * Component or MenuComponent this returns null.
   438      * Component or MenuComponent this returns null.
   457      */
   439      */
   458     private static AppContext getAppContext(Object target) {
   440     private static AppContext getAppContext(Object target) {
   459         AppContext retObj = null;
   441         if (target instanceof Component) {
   460         try{
   442             return AWTAccessor.getComponentAccessor().
   461             if (target instanceof Component){
   443                        getAppContext((Component)target);
   462                 if (componentAppContextField == null) {
   444         } else if (target instanceof MenuComponent) {
   463                     componentAppContextField = getField(Component.class, "appContext");
   445             return AWTAccessor.getMenuComponentAccessor().
   464                 }
   446                        getAppContext((MenuComponent)target);
   465                 retObj = (AppContext) componentAppContextField.get(target);
   447         } else {
   466             } else if (target instanceof MenuComponent) {
   448             return null;
   467                 if (menuComponentAppContextField == null) {
   449         }
   468                     menuComponentAppContextField = getField(MenuComponent.class, "appContext");
       
   469                 }
       
   470                 retObj = (AppContext) menuComponentAppContextField.get(target);
       
   471             }
       
   472         } catch( IllegalAccessException e){
       
   473             assert false;
       
   474         }
       
   475         return retObj;
       
   476     }
   450     }
   477 
   451 
   478     /*
   452     /*
   479      * Fetch the AppContext associated with the given target.
   453      * Fetch the AppContext associated with the given target.
   480      * This can be used to determine things like which EventQueue
   454      * This can be used to determine things like which EventQueue
   518       *             should be set
   492       *             should be set
   519       * @param    status the value of lightweight focus request status
   493       * @param    status the value of lightweight focus request status
   520       */
   494       */
   521 
   495 
   522     public static void setLWRequestStatus(Window changed,boolean status){
   496     public static void setLWRequestStatus(Window changed,boolean status){
   523         if (syncLWRequestsField == null){
   497         AWTAccessor.getWindowAccessor().setLWRequestStatus(changed, status);
   524             syncLWRequestsField = getField(Window.class, "syncLWRequests");
       
   525         }
       
   526         try{
       
   527             if (syncLWRequestsField != null){
       
   528                 syncLWRequestsField.setBoolean(changed, status);
       
   529             }
       
   530         } catch( IllegalAccessException e){
       
   531             assert false;
       
   532         }
       
   533     };
   498     };
   534 
   499 
   535     public static void checkAndSetPolicy(Container cont, boolean isSwingCont)
   500     public static void checkAndSetPolicy(Container cont, boolean isSwingCont)
   536     {
   501     {
   537         FocusTraversalPolicy defaultPolicy = KeyboardFocusManager
   502         FocusTraversalPolicy defaultPolicy = KeyboardFocusManager
   635 
   600 
   636     /*
   601     /*
   637      * Post AWTEvent of high priority.
   602      * Post AWTEvent of high priority.
   638      */
   603      */
   639     public static void postPriorityEvent(final AWTEvent e) {
   604     public static void postPriorityEvent(final AWTEvent e) {
   640         if (isPostedField == null) {
       
   641             isPostedField = getField(AWTEvent.class, "isPosted");
       
   642         }
       
   643         PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() {
   605         PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() {
   644                 public void run() {
   606                 public void run() {
   645                     try {
   607                     AWTAccessor.getAWTEventAccessor().setPosted(e);
   646                         isPostedField.setBoolean(e, true);
       
   647                     } catch (IllegalArgumentException e) {
       
   648                         assert(false);
       
   649                     } catch (IllegalAccessException e) {
       
   650                         assert(false);
       
   651                     }
       
   652                     ((Component)e.getSource()).dispatchEvent(e);
   608                     ((Component)e.getSource()).dispatchEvent(e);
   653                 }
   609                 }
   654             }, PeerEvent.ULTIMATE_PRIORITY_EVENT);
   610             }, PeerEvent.ULTIMATE_PRIORITY_EVENT);
   655         postEvent(targetToAppContext(e.getSource()), pe);
   611         postEvent(targetToAppContext(e.getSource()), pe);
   656     }
   612     }
   755             throw new InvocationTargetException(eventThrowable);
   711             throw new InvocationTargetException(eventThrowable);
   756         }
   712         }
   757     }
   713     }
   758 
   714 
   759     /*
   715     /*
   760      * Returns next queue for the given EventQueue which has private access
       
   761      */
       
   762     private static EventQueue getNextQueue(final Object o) {
       
   763         EventQueue result = null;
       
   764         try{
       
   765             Field nextQueueField = getField(EventQueue.class,
       
   766                                             "nextQueue");
       
   767             result = (EventQueue)nextQueueField.get(o);
       
   768         } catch( IllegalAccessException e){
       
   769             assert false;
       
   770         }
       
   771         return result;
       
   772     }
       
   773 
       
   774     /*
       
   775      * Returns dispatch thread for the given EventQueue which has private access
       
   776      */
       
   777     private static Thread getDispatchThread(final Object o) {
       
   778         Thread result = null;
       
   779         try{
       
   780             Field dispatchThreadField = getField(EventQueue.class,
       
   781                                                  "dispatchThread");
       
   782             result = (Thread)dispatchThreadField.get(o);
       
   783         } catch( IllegalAccessException e){
       
   784             assert false;
       
   785         }
       
   786         return result;
       
   787     }
       
   788 
       
   789     /*
       
   790      * Returns true if the calling thread is the event dispatch thread
   716      * Returns true if the calling thread is the event dispatch thread
   791      * contained within AppContext which associated with the given target.
   717      * contained within AppContext which associated with the given target.
   792      * Use this call to ensure that a given task is being executed
   718      * Use this call to ensure that a given task is being executed
   793      * (or not being) on the event dispatch thread for the given target.
   719      * (or not being) on the event dispatch thread for the given target.
   794      */
   720      */
   795     public static boolean isDispatchThreadForAppContext(Object target) {
   721     public static boolean isDispatchThreadForAppContext(Object target) {
   796         AppContext appContext = targetToAppContext(target);
   722         AppContext appContext = targetToAppContext(target);
   797         EventQueue eq = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
   723         EventQueue eq = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
   798 
   724 
   799         EventQueue next = getNextQueue(eq);
   725         AWTAccessor.EventQueueAccessor accessor = AWTAccessor.getEventQueueAccessor();
       
   726         EventQueue next = accessor.getNextQueue(eq);
   800         while (next != null) {
   727         while (next != null) {
   801             eq = next;
   728             eq = next;
   802             next = getNextQueue(eq);
   729             next = accessor.getNextQueue(eq);
   803         }
   730         }
   804 
   731 
   805         return (Thread.currentThread() == getDispatchThread(eq));
   732         return (Thread.currentThread() == accessor.getDispatchThread(eq));
   806     }
   733     }
   807 
   734 
   808     public Dimension getScreenSize() {
   735     public Dimension getScreenSize() {
   809         return new Dimension(getScreenWidth(), getScreenHeight());
   736         return new Dimension(getScreenWidth(), getScreenHeight());
   810     }
   737     }
  1354      */
  1281      */
  1355     protected boolean needsXEmbedImpl() {
  1282     protected boolean needsXEmbedImpl() {
  1356         return false;
  1283         return false;
  1357     }
  1284     }
  1358 
  1285 
  1359     private static Dialog.ModalExclusionType DEFAULT_MODAL_EXCLUSION_TYPE;
  1286     private static Dialog.ModalExclusionType DEFAULT_MODAL_EXCLUSION_TYPE = null;
  1360 
       
  1361     static {
       
  1362         DEFAULT_MODAL_EXCLUSION_TYPE = (Dialog.ModalExclusionType)AccessController.doPrivileged(new PrivilegedAction() {
       
  1363             public Object run() {
       
  1364                 Dialog.ModalExclusionType defaultType = Dialog.ModalExclusionType.NO_EXCLUDE;
       
  1365                 try {
       
  1366                     java.lang.reflect.Field f = Dialog.class.getDeclaredField("DEFAULT_MODAL_EXCLUSION_TYPE");
       
  1367                     f.setAccessible(true);
       
  1368                     defaultType = (Dialog.ModalExclusionType)f.get(null);
       
  1369                 } catch (Exception e) {
       
  1370                 }
       
  1371                 return defaultType;
       
  1372             }
       
  1373         });
       
  1374     }
       
  1375 
  1287 
  1376     /**
  1288     /**
  1377      * Returns whether the XEmbed server feature is requested by
  1289      * Returns whether the XEmbed server feature is requested by
  1378      * developer.  If true, Toolkit should return an
  1290      * developer.  If true, Toolkit should return an
  1379      * XEmbed-server-enabled CanvasPeer instead of the ordinary CanvasPeer.
  1291      * XEmbed-server-enabled CanvasPeer instead of the ordinary CanvasPeer.
  1428      * @see sun.awt.SunToolkit#isModalExcludedSupported
  1340      * @see sun.awt.SunToolkit#isModalExcludedSupported
  1429      * @see sun.awt.SunToolkit#isModalExcluded(java.awt.Window)
  1341      * @see sun.awt.SunToolkit#isModalExcluded(java.awt.Window)
  1430      */
  1342      */
  1431     public static void setModalExcluded(Window window)
  1343     public static void setModalExcluded(Window window)
  1432     {
  1344     {
       
  1345         if (DEFAULT_MODAL_EXCLUSION_TYPE == null) {
       
  1346             DEFAULT_MODAL_EXCLUSION_TYPE = Dialog.ModalExclusionType.APPLICATION_EXCLUDE;
       
  1347         }
  1433         window.setModalExclusionType(DEFAULT_MODAL_EXCLUSION_TYPE);
  1348         window.setModalExclusionType(DEFAULT_MODAL_EXCLUSION_TYPE);
  1434     }
  1349     }
  1435 
  1350 
  1436     /*
  1351     /*
  1437      * Returns whether the specified window is blocked by modal dialogs.
  1352      * Returns whether the specified window is blocked by modal dialogs.
  1449      *
  1364      *
  1450      * @since 1.5
  1365      * @since 1.5
  1451      */
  1366      */
  1452     public static boolean isModalExcluded(Window window)
  1367     public static boolean isModalExcluded(Window window)
  1453     {
  1368     {
       
  1369         if (DEFAULT_MODAL_EXCLUSION_TYPE == null) {
       
  1370             DEFAULT_MODAL_EXCLUSION_TYPE = Dialog.ModalExclusionType.APPLICATION_EXCLUDE;
       
  1371         }
  1454         return window.getModalExclusionType().compareTo(DEFAULT_MODAL_EXCLUSION_TYPE) >= 0;
  1372         return window.getModalExclusionType().compareTo(DEFAULT_MODAL_EXCLUSION_TYPE) >= 0;
  1455     }
  1373     }
  1456 
  1374 
  1457     /**
  1375     /**
  1458      * Overridden in XToolkit and WToolkit
  1376      * Overridden in XToolkit and WToolkit
  2102      * Descendants of the SunToolkit should override and put their own logic here.
  2020      * Descendants of the SunToolkit should override and put their own logic here.
  2103      */
  2021      */
  2104     public int getNumberOfButtons(){
  2022     public int getNumberOfButtons(){
  2105         return 3;
  2023         return 3;
  2106     }
  2024     }
       
  2025 
       
  2026     /**
       
  2027      * Checks that the given object implements/extends the given
       
  2028      * interface/class.
       
  2029      *
       
  2030      * Note that using the instanceof operator causes a class to be loaded.
       
  2031      * Using this method doesn't load a class and it can be used instead of
       
  2032      * the instanceof operator for performance reasons.
       
  2033      *
       
  2034      * @param obj Object to be checked
       
  2035      * @param type The name of the interface/class. Must be
       
  2036      * fully-qualified interface/class name.
       
  2037      * @return true, if this object implements/extends the given
       
  2038      *         interface/class, false, otherwise, or if obj or type is null
       
  2039      */
       
  2040     public static boolean isInstanceOf(Object obj, String type) {
       
  2041         if (obj == null) return false;
       
  2042         if (type == null) return false;
       
  2043 
       
  2044         return isInstanceOf(obj.getClass(), type);
       
  2045     }
       
  2046 
       
  2047     private static boolean isInstanceOf(Class cls, String type) {
       
  2048         if (cls == null) return false;
       
  2049 
       
  2050         if (cls.getName().equals(type)) {
       
  2051             return true;
       
  2052         }
       
  2053 
       
  2054         for (Class c : cls.getInterfaces()) {
       
  2055             if (c.getName().equals(type)) {
       
  2056                 return true;
       
  2057             }
       
  2058         }
       
  2059         return isInstanceOf(cls.getSuperclass(), type);
       
  2060     }
  2107 } // class SunToolkit
  2061 } // class SunToolkit
  2108 
  2062 
  2109 
  2063 
  2110 /*
  2064 /*
  2111  * PostEventQueue is a Thread that runs in the same AppContext as the
  2065  * PostEventQueue is a Thread that runs in the same AppContext as the