jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java
author anashaty
Thu, 23 Apr 2015 17:33:19 +0300
changeset 30479 09cccadb5831
parent 30471 c1568a2416a8
child 30948 0a0972d3b58d
permissions -rw-r--r--
8078165: [macosx] NPE when attempting to get image from toolkit Reviewed-by: serb, alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4371
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    29
import java.awt.event.InputEvent;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    30
import java.awt.event.KeyEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.WindowEvent;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    32
import java.awt.image.BufferedImage;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    33
import java.awt.image.DataBuffer;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    34
import java.awt.image.DataBufferInt;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    35
import java.awt.image.ImageObserver;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    36
import java.awt.image.ImageProducer;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    37
import java.awt.image.Raster;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    38
import java.awt.peer.FramePeer;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    39
import java.awt.peer.KeyboardFocusManagerPeer;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    40
import java.awt.peer.MouseInfoPeer;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    41
import java.awt.peer.SystemTrayPeer;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    42
import java.awt.peer.TrayIconPeer;
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
    43
import java.io.File;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
    44
import java.io.IOException;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
    45
import java.io.InputStream;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    46
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.net.URL;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    48
import java.security.AccessController;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    49
import java.util.Collections;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    50
import java.util.Iterator;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    51
import java.util.Locale;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    52
import java.util.Map;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    53
import java.util.Vector;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    54
import java.util.WeakHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.concurrent.locks.Condition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.concurrent.locks.ReentrantLock;
12529
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
    58
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    59
import sun.awt.im.InputContext;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    60
import sun.awt.image.ByteArrayImageSource;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    61
import sun.awt.image.FileImageSource;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    62
import sun.awt.image.ImageRepresentation;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    63
import sun.awt.image.MultiResolutionImage;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    64
import sun.awt.image.MultiResolutionToolkitImage;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    65
import sun.awt.image.ToolkitImage;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    66
import sun.awt.image.URLImageSource;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.font.FontDesignMetrics;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    68
import sun.misc.SoftCache;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    69
import sun.security.action.GetBooleanAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.security.action.GetPropertyAction;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    71
import sun.util.logging.PlatformLogger;
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    72
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
    73
import static java.awt.RenderingHints.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public abstract class SunToolkit extends Toolkit
25554
8376663d83d7 8047799: Remove WindowClosingSupport
pchelko
parents: 25107
diff changeset
    76
    implements ComponentFactory, InputMethodSupport, KeyboardFocusManagerPeerProvider {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
18271
228440e64512 8014718: Netbeans IDE begins to throw a lot exceptions since 7u25 b10
leonidr
parents: 18247
diff changeset
    78
    // 8014718: logging has been removed from SunToolkit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* Load debug settings for native code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (AccessController.doPrivileged(new GetBooleanAction("sun.awt.nativedebug"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            DebugSettings.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Special mask for the UngrabEvent events, in addition to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * public masks defined in AWTEvent.  Should be used as the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * value for Toolkit.addAWTEventListener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static final int GRAB_EVENT_MASK = 0x80000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /* The key to put()/get() the PostEventQueue into/from the AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final String POST_EVENT_QUEUE_KEY = "PostEventQueue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
2810
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
    98
    /**
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
    99
     * Number of buttons.
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   100
     * By default it's taken from the system. If system value does not
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   101
     * fit into int type range, use our own MAX_BUTTONS_SUPPORT value.
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   102
     */
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   103
    protected static int numberOfButtons = 0;
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   104
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   105
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   106
    /* XFree standard mention 24 buttons as maximum:
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   107
     * http://www.xfree86.org/current/mouse.4.html
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   108
     * We workaround systems supporting more than 24 buttons.
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   109
     * Otherwise, we have to use long type values as masks
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   110
     * which leads to API change.
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   111
     * InputEvent.BUTTON_DOWN_MASK may contain only 21 masks due to
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   112
     * the 4-bytes limit for the int type. (CR 6799099)
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   113
     * One more bit is reserved for FIRST_HIGH_BIT.
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   114
     */
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   115
    public final static int MAX_BUTTONS_SUPPORTED = 20;
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
   116
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   117
    /**
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   118
     * Creates and initializes EventQueue instance for the specified
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   119
     * AppContext.
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   120
     * Note that event queue must be created from createNewAppContext()
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   121
     * only in order to ensure that EventQueue constructor obtains
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   122
     * the correct AppContext.
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   123
     * @param appContext AppContext to associate with the event queue
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 18202
diff changeset
   124
     */
11272
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   125
    private static void initEQ(AppContext appContext) {
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   126
        EventQueue eventQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
11272
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   128
        String eqName = System.getProperty("AWT.EventQueueClass",
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   129
                "java.awt.EventQueue");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
11272
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   131
        try {
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   132
            eventQueue = (EventQueue)Class.forName(eqName).newInstance();
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   133
        } catch (Exception e) {
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   134
            e.printStackTrace();
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   135
            System.err.println("Failed loading " + eqName + ": " + e);
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   136
            eventQueue = new EventQueue();
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   137
        }
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   138
        appContext.put(AppContext.EVENT_QUEUE_KEY, eventQueue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
11272
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   140
        PostEventQueue postEventQueue = new PostEventQueue(eventQueue);
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   141
        appContext.put(POST_EVENT_QUEUE_KEY, postEventQueue);
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   142
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
11272
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   144
    public SunToolkit() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public boolean useBufferPerWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
15983
26a673dec5b2 8006406: lightweight embedding in other Java UI toolkits
ant
parents: 13775
diff changeset
   151
    public abstract FramePeer createLightweightFrame(LightweightFrame target)
26a673dec5b2 8006406: lightweight embedding in other Java UI toolkits
ant
parents: 13775
diff changeset
   152
        throws HeadlessException;
26a673dec5b2 8006406: lightweight embedding in other Java UI toolkits
ant
parents: 13775
diff changeset
   153
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public abstract TrayIconPeer createTrayIcon(TrayIcon target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        throws HeadlessException, AWTException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public abstract SystemTrayPeer createSystemTray(SystemTray target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public abstract boolean isTraySupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   161
    @Override
13648
90effcfc064f 7124375: [macosx] Focus isn't transfered as expected between components
leonidr
parents: 13240
diff changeset
   162
    public abstract KeyboardFocusManagerPeer getKeyboardFocusManagerPeer()
2464
3c6755bdc55f 6806217: implement synthetic focus model for MS Windows
ant
parents: 2451
diff changeset
   163
        throws HeadlessException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * The AWT lock is typically only used on Unix platforms to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * access to Xlib, OpenGL, etc.  However, these methods are implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * in SunToolkit so that they can be called from shared code (e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * from the OGL pipeline) or from the X11 pipeline regardless of whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * XToolkit or MToolkit is currently in use.  There are native macros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * (such as AWT_LOCK) defined in awt.h, so if the implementation of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * methods is changed, make sure it is compatible with the native macros.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Note: The following methods (awtLock(), awtUnlock(), etc) should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * used in place of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *     synchronized (getAWTLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * By factoring these methods out specially, we are able to change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * implementation of these methods (e.g. use more advanced locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * mechanisms) without impacting calling code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Sample usage:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *     private void doStuffWithXlib() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *         assert !SunToolkit.isAWTLockHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         SunToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *             ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *             XlibWrapper.XDoStuff();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *         } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *             SunToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private static final ReentrantLock AWT_LOCK = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static final Condition AWT_LOCK_COND = AWT_LOCK.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public static final void awtLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        AWT_LOCK.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static final boolean awtTryLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return AWT_LOCK.tryLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public static final void awtUnlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        AWT_LOCK.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public static final void awtLockWait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        AWT_LOCK_COND.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public static final void awtLockWait(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        AWT_LOCK_COND.await(timeout, TimeUnit.MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static final void awtLockNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        AWT_LOCK_COND.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static final void awtLockNotifyAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        AWT_LOCK_COND.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public static final boolean isAWTLockHeldByCurrentThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return AWT_LOCK.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Create a new AppContext, along with its EventQueue, for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * new ThreadGroup.  Browser code, for example, would use this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * method to create an AppContext & EventQueue for an Applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static AppContext createNewAppContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 15983
diff changeset
   243
        return createNewAppContext(threadGroup);
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 15983
diff changeset
   244
    }
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 15983
diff changeset
   245
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 15983
diff changeset
   246
    static final AppContext createNewAppContext(ThreadGroup threadGroup) {
6821
f5949f5159ec 6979541: closed/javax/swing/plaf/basic/AWTEventListenerLeak/AWTEventListenerLeak.java fails
art
parents: 5944
diff changeset
   247
        // Create appContext before initialization of EventQueue, so all
f5949f5159ec 6979541: closed/javax/swing/plaf/basic/AWTEventListenerLeak/AWTEventListenerLeak.java fails
art
parents: 5944
diff changeset
   248
        // the calls to AppContext.getAppContext() from EventQueue ctor
f5949f5159ec 6979541: closed/javax/swing/plaf/basic/AWTEventListenerLeak/AWTEventListenerLeak.java fails
art
parents: 5944
diff changeset
   249
        // return correct values
f5949f5159ec 6979541: closed/javax/swing/plaf/basic/AWTEventListenerLeak/AWTEventListenerLeak.java fails
art
parents: 5944
diff changeset
   250
        AppContext appContext = new AppContext(threadGroup);
11272
d890af1d95b7 7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
anthony
parents: 11264
diff changeset
   251
        initEQ(appContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static void wakeupEventQueue(EventQueue q, boolean isShutdown){
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
   257
        AWTAccessor.getEventQueueAccessor().wakeup(q, isShutdown);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Fetch the peer associated with the given target (as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * in the peer creation method).  This can be used to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * things like what the parent peer is.  If the target is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * or the target can't be found (either because the a peer was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * never created for it or the peer was disposed), a null will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    protected static Object targetToPeer(Object target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (target != null && !GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return AWTAutoShutdown.getInstance().getPeer(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    protected static void targetCreatedPeer(Object target, Object peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (target != null && peer != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            !GraphicsEnvironment.isHeadless())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            AWTAutoShutdown.getInstance().registerPeer(target, peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected static void targetDisposedPeer(Object target, Object peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (target != null && peer != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            !GraphicsEnvironment.isHeadless())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            AWTAutoShutdown.getInstance().unregisterPeer(target, peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Maps from non-Component/MenuComponent to AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // WeakHashMap<Component,AppContext>
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   293
    private static final Map<Object, AppContext> appContextMap =
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   294
        Collections.synchronizedMap(new WeakHashMap<Object, AppContext>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Sets the appContext field of target. If target is not a Component or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * MenuComponent, this returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   300
    private static boolean setAppContext(Object target,
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   301
                                         AppContext context) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   302
        if (target instanceof Component) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   303
            AWTAccessor.getComponentAccessor().
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   304
                setAppContext((Component)target, context);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   305
        } else if (target instanceof MenuComponent) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   306
            AWTAccessor.getMenuComponentAccessor().
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   307
                setAppContext((MenuComponent)target, context);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   308
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns the appContext field for target. If target is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Component or MenuComponent this returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private static AppContext getAppContext(Object target) {
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   319
        if (target instanceof Component) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   320
            return AWTAccessor.getComponentAccessor().
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   321
                       getAppContext((Component)target);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   322
        } else if (target instanceof MenuComponent) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   323
            return AWTAccessor.getMenuComponentAccessor().
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   324
                       getAppContext((MenuComponent)target);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   325
        } else {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   326
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Fetch the AppContext associated with the given target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * This can be used to determine things like which EventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * to use for posting events to a Component.  If the target is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * null or the target can't be found, a null with be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public static AppContext targetToAppContext(Object target) {
26031
812d3082ba6f 8051359: JPopupMenu creation in headless mode with JDK9b23 causes NPE
alexsch
parents: 26011
diff changeset
   337
        if (target == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        AppContext context = getAppContext(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // target is not a Component/MenuComponent, try the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            // appContextMap.
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   344
            context = appContextMap.get(target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
      * Sets the synchronous status of focus requests on lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
      * components in the specified window to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
      * If the boolean parameter is <code>true</code> then the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
      * requests on lightweight components will be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
      * synchronously, if it is <code>false</code>, then asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
      * By default, all windows have their lightweight request status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
      * set to asynchronous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
      * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
      * The application can only set the status of lightweight focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
      * requests to synchronous for any of its windows if it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      * perform focus transfers between different heavyweight containers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
      * In this case the observable focus behaviour is the same as with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      * asynchronous status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      * If the application performs focus transfer between different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      * heavyweight containers and sets the lightweight focus request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      * status to synchronous for any of its windows, then further focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
      * behaviour is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
      * @param    w window for which the lightweight focus request status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      *             should be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      * @param    status the value of lightweight focus request status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public static void setLWRequestStatus(Window changed,boolean status){
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   375
        AWTAccessor.getWindowAccessor().setLWRequestStatus(changed, status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
12661
6cf8b7116579 7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application.
ant
parents: 12529
diff changeset
   378
    public static void checkAndSetPolicy(Container cont) {
6cf8b7116579 7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application.
ant
parents: 12529
diff changeset
   379
        FocusTraversalPolicy defaultPolicy = KeyboardFocusManager.
6cf8b7116579 7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application.
ant
parents: 12529
diff changeset
   380
            getCurrentKeyboardFocusManager().
6cf8b7116579 7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application.
ant
parents: 12529
diff changeset
   381
                getDefaultFocusTraversalPolicy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        cont.setFocusTraversalPolicy(defaultPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private static FocusTraversalPolicy createLayoutPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        FocusTraversalPolicy policy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        try {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   389
            Class<?> layoutPolicyClass =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                Class.forName("javax.swing.LayoutFocusTraversalPolicy");
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   391
            policy = (FocusTraversalPolicy)layoutPolicyClass.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Insert a mapping from target to AppContext, for later retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * via targetToAppContext() above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public static void insertTargetMapping(Object target, AppContext appContext) {
26031
812d3082ba6f 8051359: JPopupMenu creation in headless mode with JDK9b23 causes NPE
alexsch
parents: 26011
diff changeset
   411
        if (!setAppContext(target, appContext)) {
812d3082ba6f 8051359: JPopupMenu creation in headless mode with JDK9b23 causes NPE
alexsch
parents: 26011
diff changeset
   412
            // Target is not a Component/MenuComponent, use the private Map
812d3082ba6f 8051359: JPopupMenu creation in headless mode with JDK9b23 causes NPE
alexsch
parents: 26011
diff changeset
   413
            // instead.
812d3082ba6f 8051359: JPopupMenu creation in headless mode with JDK9b23 causes NPE
alexsch
parents: 26011
diff changeset
   414
            appContextMap.put(target, appContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Post an AWTEvent to the Java EventQueue, using the PostEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * to avoid possibly calling client code (EventQueueSubclass.postEvent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * on the toolkit (AWT-Windows/AWT-Motif) thread.  This function should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * not be called under another lock since it locks the EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * See bugids 4632918, 4526597.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public static void postEvent(AppContext appContext, AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (event == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   429
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   430
        AWTAccessor.SequencedEventAccessor sea = AWTAccessor.getSequencedEventAccessor();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   431
        if (sea != null && sea.isSequencedEvent(event)) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   432
            AWTEvent nested = sea.getNested(event);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   433
            if (nested.getID() == WindowEvent.WINDOW_LOST_FOCUS &&
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   434
                nested instanceof TimedWindowEvent)
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   435
            {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   436
                TimedWindowEvent twe = (TimedWindowEvent)nested;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   437
                ((SunToolkit)Toolkit.getDefaultToolkit()).
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   438
                    setWindowDeactivationTime((Window)twe.getSource(), twe.getWhen());
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   439
            }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   440
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
   441
6826
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
   442
        // All events posted via this method are system-generated.
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
   443
        // Placing the following call here reduces considerably the
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
   444
        // number of places throughout the toolkit that would
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
   445
        // otherwise have to be modified to precisely identify
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
   446
        // system-generated events.
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
   447
        setSystemGenerated(event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        AppContext eventContext = targetToAppContext(event.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (eventContext != null && !eventContext.equals(appContext)) {
18271
228440e64512 8014718: Netbeans IDE begins to throw a lot exceptions since 7u25 b10
leonidr
parents: 18247
diff changeset
   450
            throw new RuntimeException("Event posted on wrong app context : " + event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        PostEventQueue postEventQueue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY);
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   454
        if (postEventQueue != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            postEventQueue.postEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Post AWTEvent of high priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public static void postPriorityEvent(final AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() {
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   464
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                public void run() {
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   466
                    AWTAccessor.getAWTEventAccessor().setPosted(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    ((Component)e.getSource()).dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }, PeerEvent.ULTIMATE_PRIORITY_EVENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        postEvent(targetToAppContext(e.getSource()), pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Flush any pending events which haven't been posted to the AWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * EventQueue yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public static void flushPendingEvents()  {
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   478
        AppContext appContext = AppContext.getAppContext();
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   479
        flushPendingEvents(appContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   482
    /*
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   483
     * Flush the PostEventQueue for the right AppContext.
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   484
     * The default flushPendingEvents only flushes the thread-local context,
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   485
     * which is not always correct, c.f. 3746956
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   486
     */
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   487
    public static void flushPendingEvents(AppContext appContext) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        PostEventQueue postEventQueue =
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   489
                (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (postEventQueue != null) {
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
   491
            postEventQueue.flush();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Execute a chunk of code on the Java event handler thread for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * given target.  Does not wait for the execution to occur before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * returning to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public static void executeOnEventHandlerThread(Object target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                                   Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        executeOnEventHandlerThread(new PeerEvent(target, runnable, PeerEvent.PRIORITY_EVENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Fixed 5064013: the InvocationEvent time should be equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * the time of the ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   509
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public static void executeOnEventHandlerThread(Object target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                                                   Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                                   final long when) {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   513
        executeOnEventHandlerThread(
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   514
            new PeerEvent(target, runnable, PeerEvent.PRIORITY_EVENT) {
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   515
                @Override
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   516
                public long getWhen() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    return when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Execute a chunk of code on the Java event handler thread for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * given target.  Does not wait for the execution to occur before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * returning to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public static void executeOnEventHandlerThread(PeerEvent peerEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        postEvent(targetToAppContext(peerEvent.getSource()), peerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Execute a chunk of code on the Java event handler thread. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * method takes into account provided AppContext and sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <code>SunToolkit.getDefaultToolkit()</code> as a target of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * event. See 6451487 for detailes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Does not wait for the execution to occur before returning to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     public static void invokeLaterOnAppContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        AppContext appContext, Runnable dispatcher)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        postEvent(appContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            new PeerEvent(Toolkit.getDefaultToolkit(), dispatcher,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                PeerEvent.PRIORITY_EVENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Execute a chunk of code on the Java event handler thread for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * given target.  Waits for the execution to occur before returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    public static void executeOnEDTAndWait(Object target, Runnable runnable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        throws InterruptedException, InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            throw new Error("Cannot call executeOnEDTAndWait from any event dispatcher thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        class AWTInvocationLock {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        Object lock = new AWTInvocationLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        PeerEvent event = new PeerEvent(target, runnable, lock, true, PeerEvent.PRIORITY_EVENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            executeOnEventHandlerThread(event);
7955
4faac2af1f6e 7011443: ./share/classes/sun/awt/SunToolkit.java needs to avoid spurious wakeup
dcherepanov
parents: 7775
diff changeset
   566
            while(!event.isDispatched()) {
4faac2af1f6e 7011443: ./share/classes/sun/awt/SunToolkit.java needs to avoid spurious wakeup
dcherepanov
parents: 7775
diff changeset
   567
                lock.wait();
4faac2af1f6e 7011443: ./share/classes/sun/awt/SunToolkit.java needs to avoid spurious wakeup
dcherepanov
parents: 7775
diff changeset
   568
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        Throwable eventThrowable = event.getThrowable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (eventThrowable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            throw new InvocationTargetException(eventThrowable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns true if the calling thread is the event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * contained within AppContext which associated with the given target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Use this call to ensure that a given task is being executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * (or not being) on the event dispatch thread for the given target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public static boolean isDispatchThreadForAppContext(Object target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        AppContext appContext = targetToAppContext(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        EventQueue eq = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
   587
        AWTAccessor.EventQueueAccessor accessor = AWTAccessor.getEventQueueAccessor();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 3938
diff changeset
   588
        return accessor.isDispatchThreadImpl(eq);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   591
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public Dimension getScreenSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return new Dimension(getScreenWidth(), getScreenHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    protected abstract int getScreenWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    protected abstract int getScreenHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   598
    @Override
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   599
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public FontMetrics getFontMetrics(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return FontDesignMetrics.getMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   604
    @Override
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
   605
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public String[] getFontList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        String[] hardwiredFontList = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Font.DIALOG, Font.SANS_SERIF, Font.SERIF, Font.MONOSPACED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            Font.DIALOG_INPUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            // -- Obsolete font names from 1.0.2.  It was decided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            // -- getFontList should not return these old names:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            //    "Helvetica", "TimesRoman", "Courier", "ZapfDingbats"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return hardwiredFontList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   619
     * Disables erasing of background on the canvas before painting if
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   620
     * this is supported by the current toolkit. It is recommended to
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   621
     * call this method early, before the Canvas becomes displayable,
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   622
     * because some Toolkit implementations do not support changing
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   623
     * this property once the Canvas becomes displayable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public void disableBackgroundErase(Canvas canvas) {
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   626
        disableBackgroundEraseImpl(canvas);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   627
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   628
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   629
    /**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   630
     * Disables the native erasing of the background on the given
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   631
     * component before painting if this is supported by the current
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   632
     * toolkit. This only has an effect for certain components such as
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   633
     * Canvas, Panel and Window. It is recommended to call this method
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   634
     * early, before the Component becomes displayable, because some
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   635
     * Toolkit implementations do not support changing this property
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   636
     * once the Component becomes displayable.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   637
     */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   638
    public void disableBackgroundErase(Component component) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   639
        disableBackgroundEraseImpl(component);
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   640
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   641
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   642
    private void disableBackgroundEraseImpl(Component component) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
   643
        AWTAccessor.getComponentAccessor().setBackgroundEraseDisabled(component, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns the value of "sun.awt.noerasebackground" property. Default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * value is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public static boolean getSunAwtNoerasebackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return AccessController.doPrivileged(new GetBooleanAction("sun.awt.noerasebackground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Returns the value of "sun.awt.erasebackgroundonresize" property. Default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * value is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public static boolean getSunAwtErasebackgroundonresize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return AccessController.doPrivileged(new GetBooleanAction("sun.awt.erasebackgroundonresize"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
1190
f27065d0d9f0 6708392: Provide internal API to create OverrideRedirect windows, XToolkit
art
parents: 1181
diff changeset
   662
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 27715
diff changeset
   663
    @SuppressWarnings("deprecation")
4369
18b883ed2b58 6903354: deadlock involving Component.show & SunToolkit.getImageFromHash
ant
parents: 4366
diff changeset
   664
    static final SoftCache imgCache = new SoftCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
4369
18b883ed2b58 6903354: deadlock involving Component.show & SunToolkit.getImageFromHash
ant
parents: 4366
diff changeset
   666
    static Image getImageFromHash(Toolkit tk, URL url) {
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   667
        checkPermissions(url);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   668
        synchronized (imgCache) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   669
            Image img = (Image)imgCache.get(url);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   670
            if (img == null) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   671
                try {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   672
                    img = tk.createImage(new URLImageSource(url));
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   673
                    imgCache.put(url, img);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   674
                } catch (Exception e) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   675
                }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   676
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   677
            return img;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   678
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   679
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   680
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   681
    static Image getImageFromHash(Toolkit tk,
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   682
                                               String filename) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   683
        checkPermissions(filename);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   684
        synchronized (imgCache) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   685
            Image img = (Image)imgCache.get(filename);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   686
            if (img == null) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   687
                try {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   688
                    img = tk.createImage(new FileImageSource(filename));
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   689
                    imgCache.put(filename, img);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   690
                } catch (Exception e) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   691
                }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   692
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   693
            return img;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   694
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   695
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   696
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   697
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   698
    public Image getImage(String filename) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   699
        return getImageFromHash(this, filename);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   700
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   701
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   702
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   703
    public Image getImage(URL url) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   704
        return getImageFromHash(this, url);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   705
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   706
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   707
    protected Image getImageWithResolutionVariant(String fileName,
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   708
            String resolutionVariantName) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   709
        synchronized (imgCache) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   710
            Image image = getImageFromHash(this, fileName);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   711
            if (image instanceof MultiResolutionImage) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   712
                return image;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   713
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   714
            Image resolutionVariant = getImageFromHash(this, resolutionVariantName);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   715
            image = createImageWithResolutionVariant(image, resolutionVariant);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   716
            imgCache.put(fileName, image);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   717
            return image;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   718
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   719
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   720
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   721
    protected Image getImageWithResolutionVariant(URL url,
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   722
            URL resolutionVariantURL) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   723
        synchronized (imgCache) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   724
            Image image = getImageFromHash(this, url);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   725
            if (image instanceof MultiResolutionImage) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   726
                return image;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   727
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   728
            Image resolutionVariant = getImageFromHash(this, resolutionVariantURL);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   729
            image = createImageWithResolutionVariant(image, resolutionVariant);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   730
            imgCache.put(url, image);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   731
            return image;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   732
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   733
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   734
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   735
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   736
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   737
    public Image createImage(String filename) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   738
        checkPermissions(filename);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   739
        return createImage(new FileImageSource(filename));
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   740
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   741
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   742
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   743
    public Image createImage(URL url) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   744
        checkPermissions(url);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   745
        return createImage(new URLImageSource(url));
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   746
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   747
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   748
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   749
    public Image createImage(byte[] data, int offset, int length) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   750
        return createImage(new ByteArrayImageSource(data, offset, length));
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   751
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   752
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   753
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   754
    public Image createImage(ImageProducer producer) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   755
        return new ToolkitImage(producer);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   756
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   757
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   758
    public static Image createImageWithResolutionVariant(Image image,
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   759
            Image resolutionVariant) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   760
        return new MultiResolutionToolkitImage(image, resolutionVariant);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   761
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   762
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   763
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   764
    public int checkImage(Image img, int w, int h, ImageObserver o) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   765
        if (!(img instanceof ToolkitImage)) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   766
            return ImageObserver.ALLBITS;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   767
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   768
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   769
        ToolkitImage tkimg = (ToolkitImage)img;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   770
        int repbits;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   771
        if (w == 0 || h == 0) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   772
            repbits = ImageObserver.ALLBITS;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   773
        } else {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   774
            repbits = tkimg.getImageRep().check(o);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   775
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   776
        return (tkimg.check(o) | repbits) & checkResolutionVariant(img, w, h, o);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   777
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   778
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
   779
    @Override
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   780
    public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   781
        if (w == 0 || h == 0) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   782
            return true;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   783
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   784
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   785
        // Must be a ToolkitImage
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   786
        if (!(img instanceof ToolkitImage)) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   787
            return true;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   788
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   789
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   790
        ToolkitImage tkimg = (ToolkitImage)img;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   791
        if (tkimg.hasError()) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   792
            if (o != null) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   793
                o.imageUpdate(img, ImageObserver.ERROR|ImageObserver.ABORT,
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   794
                              -1, -1, -1, -1);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   795
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   796
            return false;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   797
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   798
        ImageRepresentation ir = tkimg.getImageRep();
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   799
        return ir.prepare(o) & prepareResolutionVariant(img, w, h, o);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   800
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   801
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   802
    private int checkResolutionVariant(Image img, int w, int h, ImageObserver o) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   803
        ToolkitImage rvImage = getResolutionVariant(img);
25107
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   804
        int rvw = getRVSize(w);
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   805
        int rvh = getRVSize(h);
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   806
        // Ignore the resolution variant in case of error
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   807
        return (rvImage == null || rvImage.hasError()) ? 0xFFFF :
25107
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   808
                checkImage(rvImage, rvw, rvh, MultiResolutionToolkitImage.
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   809
                                getResolutionVariantObserver(
25107
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   810
                                        img, o, w, h, rvw, rvh, true));
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   811
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   812
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   813
    private boolean prepareResolutionVariant(Image img, int w, int h,
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   814
            ImageObserver o) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   815
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   816
        ToolkitImage rvImage = getResolutionVariant(img);
25107
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   817
        int rvw = getRVSize(w);
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   818
        int rvh = getRVSize(h);
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   819
        // Ignore the resolution variant in case of error
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   820
        return rvImage == null || rvImage.hasError() || prepareImage(
25107
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   821
                rvImage, rvw, rvh,
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   822
                MultiResolutionToolkitImage.getResolutionVariantObserver(
25107
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   823
                        img, o, w, h, rvw, rvh, true));
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   824
    }
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   825
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   826
    private static int getRVSize(int size){
9a16a601de25 8040291: [macosx] Http-Images are not fully loaded when using ImageIcon
alexsch
parents: 24538
diff changeset
   827
        return size == -1 ? -1 : 2 * size;
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   828
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   829
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   830
    private static ToolkitImage getResolutionVariant(Image image) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   831
        if (image instanceof MultiResolutionToolkitImage) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   832
            Image resolutionVariant = ((MultiResolutionToolkitImage) image).
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   833
                    getResolutionVariant();
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   834
            if (resolutionVariant instanceof ToolkitImage) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   835
                return (ToolkitImage) resolutionVariant;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   836
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   837
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   838
        return null;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   839
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   840
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   841
    protected static boolean imageCached(Object key) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   842
        return imgCache.containsKey(key);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   843
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   844
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   845
    protected static boolean imageExists(String filename) {
30479
09cccadb5831 8078165: [macosx] NPE when attempting to get image from toolkit
anashaty
parents: 30471
diff changeset
   846
        if (filename != null) {
09cccadb5831 8078165: [macosx] NPE when attempting to get image from toolkit
anashaty
parents: 30471
diff changeset
   847
            checkPermissions(filename);
09cccadb5831 8078165: [macosx] NPE when attempting to get image from toolkit
anashaty
parents: 30471
diff changeset
   848
            return new File(filename).exists();
09cccadb5831 8078165: [macosx] NPE when attempting to get image from toolkit
anashaty
parents: 30471
diff changeset
   849
        }
09cccadb5831 8078165: [macosx] NPE when attempting to get image from toolkit
anashaty
parents: 30471
diff changeset
   850
        return false;
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   851
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   852
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   853
    @SuppressWarnings("try")
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   854
    protected static boolean imageExists(URL url) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   855
        if (url != null) {
30479
09cccadb5831 8078165: [macosx] NPE when attempting to get image from toolkit
anashaty
parents: 30471
diff changeset
   856
            checkPermissions(url);
23256
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   857
            try (InputStream is = url.openStream()) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   858
                return true;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   859
            }catch(IOException e){
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   860
                return false;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   861
            }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   862
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   863
        return false;
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   864
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   865
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   866
    private static void checkPermissions(String filename) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   867
        SecurityManager security = System.getSecurityManager();
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   868
        if (security != null) {
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   869
            security.checkRead(filename);
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   870
        }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   871
    }
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   872
d71287a532f8 8011059: [macosx] Support automatic @2x images loading on Mac OS X
alexsch
parents: 18275
diff changeset
   873
    private static void checkPermissions(URL url) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                java.security.Permission perm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    url.openConnection().getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                        sm.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                        // fallback to checkRead/checkConnect for pre 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        // security managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                        if ((perm instanceof java.io.FilePermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                            perm.getActions().indexOf("read") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                            sm.checkRead(perm.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                        } else if ((perm instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                            java.net.SocketPermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                            perm.getActions().indexOf("connect") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                            sm.checkConnect(url.getHost(), url.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                            throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    sm.checkConnect(url.getHost(), url.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Scans {@code imageList} for best-looking image of specified dimensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Image can be scaled and/or padded with transparency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    public static BufferedImage getScaledIconImage(java.util.List<Image> imageList, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (width == 0 || height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        Image bestImage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        int bestWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        int bestHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        double bestSimilarity = 3; //Impossibly high value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        double bestScaleFactor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        for (Iterator<Image> i = imageList.iterator();i.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            //Iterate imageList looking for best matching image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            //'Similarity' measure is defined as good scale factor and small insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            //best possible similarity is 0 (no scale, no insets).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            //It's found while the experiments that good-looking result is achieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            //with scale factors x1, x3/4, x2/3, xN, x1/N.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            Image im = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if (im == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (im instanceof ToolkitImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                ImageRepresentation ir = ((ToolkitImage)im).getImageRep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                ir.reconstruct(ImageObserver.ALLBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            int iw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            int ih;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                iw = im.getWidth(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                ih = im.getHeight(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            } catch (Exception e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            if (iw > 0 && ih > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                //Calc scale factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                double scaleFactor = Math.min((double)width / (double)iw,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                                              (double)height / (double)ih);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                //Calculate scaled image dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                //adjusting scale factor to nearest "good" value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                int adjw = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                int adjh = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                double scaleMeasure = 1; //0 - best (no) scale, 1 - impossibly bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                if (scaleFactor >= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    //Need to enlarge image more than twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    //Round down scale factor to multiply by integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    scaleFactor = Math.floor(scaleFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    adjw = iw * (int)scaleFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    adjh = ih * (int)scaleFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    scaleMeasure = 1.0 - 0.5 / scaleFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                } else if (scaleFactor >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    //Don't scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    scaleFactor = 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    adjw = iw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    adjh = ih;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    scaleMeasure = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                } else if (scaleFactor >= 0.75) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    //Multiply by 3/4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    scaleFactor = 0.75;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    adjw = iw * 3 / 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    adjh = ih * 3 / 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    scaleMeasure = 0.3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                } else if (scaleFactor >= 0.6666) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    //Multiply by 2/3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    scaleFactor = 0.6666;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    adjw = iw * 2 / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                    adjh = ih * 2 / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    scaleMeasure = 0.33;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    //Multiply size by 1/scaleDivider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    //where scaleDivider is minimum possible integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    //larger than 1/scaleFactor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    double scaleDivider = Math.ceil(1.0 / scaleFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    scaleFactor = 1.0 / scaleDivider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    adjw = (int)Math.round((double)iw / scaleDivider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    adjh = (int)Math.round((double)ih / scaleDivider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    scaleMeasure = 1.0 - 1.0 / scaleDivider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                double similarity = ((double)width - (double)adjw) / (double)width +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    ((double)height - (double)adjh) / (double)height + //Large padding is bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    scaleMeasure; //Large rescale is bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                if (similarity < bestSimilarity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    bestSimilarity = similarity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                    bestScaleFactor = scaleFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    bestImage = im;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    bestWidth = adjw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    bestHeight = adjh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                if (similarity == 0) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (bestImage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            //No images were found, possibly all are broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        BufferedImage bimage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        Graphics2D g = bimage.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                           RenderingHints.VALUE_INTERPOLATION_BILINEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            int x = (width - bestWidth) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            int y = (height - bestHeight) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            g.drawImage(bestImage, x, y, bestWidth, bestHeight, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return bimage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public static DataBufferInt getScaledIconData(java.util.List<Image> imageList, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        BufferedImage bimage = getScaledIconImage(imageList, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if (bimage == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        Raster raster = bimage.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        DataBuffer buffer = raster.getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        return (DataBufferInt)buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1024
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    protected EventQueue getSystemEventQueueImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        return getSystemEventQueueImplPP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    // Package private implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    static EventQueue getSystemEventQueueImplPP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        return getSystemEventQueueImplPP(AppContext.getAppContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public static EventQueue getSystemEventQueueImplPP(AppContext appContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        EventQueue theEventQueue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        return theEventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Give native peers the ability to query the native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * given a native component (eg the direct parent may be lightweight).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    public static Container getNativeContainer(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        return Toolkit.getNativeContainer(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    /**
4371
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1049
     * Gives native peers the ability to query the closest HW component.
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1050
     * If the given component is heavyweight, then it returns this. Otherwise,
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1051
     * it goes one level up in the hierarchy and tests next component.
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1052
     */
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1053
    public static Component getHeavyweightComponent(Component c) {
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1054
        while (c != null && AWTAccessor.getComponentAccessor().isLightweight(c)) {
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1055
            c = AWTAccessor.getComponentAccessor().getParent(c);
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1056
        }
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1057
        return c;
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1058
    }
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1059
dc9dcb8b0ae7 6823138: Need to replace ComponentAccessor with AWTAccessor
dcherepanov
parents: 4369
diff changeset
  1060
    /**
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1061
     * Returns key modifiers used by Swing to set up a focus accelerator key stroke.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1062
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1063
    public int getFocusAcceleratorKeyMask() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1064
        return InputEvent.ALT_MASK;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1065
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1066
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11272
diff changeset
  1067
    /**
12418
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1068
     * Tests whether specified key modifiers mask can be used to enter a printable
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1069
     * character. This is a default implementation of this method, which reflects
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1070
     * the way things work on Windows: here, pressing ctrl + alt allows user to enter
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1071
     * characters from the extended character set (like euro sign or math symbols)
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1072
     */
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1073
    public boolean isPrintableCharacterModifiersMask(int mods) {
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1074
        return ((mods & InputEvent.ALT_MASK) == (mods & InputEvent.CTRL_MASK));
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1075
    }
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1076
f749f1ed92ca 7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents: 12047
diff changeset
  1077
    /**
12529
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1078
     * Returns whether popup is allowed to be shown above the task bar.
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1079
     * This is a default implementation of this method, which checks
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1080
     * corresponding security permission.
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1081
     */
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1082
    public boolean canPopupOverlapTaskBar() {
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1083
        boolean result = true;
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1084
        try {
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1085
            SecurityManager sm = System.getSecurityManager();
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1086
            if (sm != null) {
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 18275
diff changeset
  1087
                sm.checkPermission(AWTPermissions.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
12529
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1088
            }
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1089
        } catch (SecurityException se) {
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1090
            // There is no permission to show popups over the task bar
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1091
            result = false;
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1092
        }
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1093
        return result;
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1094
    }
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1095
6da056faf511 7154480: [macosx] Not all popup menu items are visible
leonidr
parents: 12418
diff changeset
  1096
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * Returns a new input method window, with behavior as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * {@link java.awt.im.spi.InputMethodContext#createInputMethodWindow}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * If the inputContext is not null, the window should return it from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * getInputContext() method. The window needs to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * sun.awt.im.InputMethodWindow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * SunToolkit subclasses can override this method to return better input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * method windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     */
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1106
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    public Window createInputMethodWindow(String title, InputContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return new sun.awt.im.SimpleInputMethodWindow(title, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Returns whether enableInputMethods should be set to true for peered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * TextComponent instances on this platform. False by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1115
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    public boolean enableInputMethodsForTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    private static Locale startupLocale = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Returns the locale in which the runtime was started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    public static Locale getStartupLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (startupLocale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            String language, region, country, variant;
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1128
            language = AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                            new GetPropertyAction("user.language", "en"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            // for compatibility, check for old user.region property
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1131
            region = AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                            new GetPropertyAction("user.region"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            if (region != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                // region can be of form country, country_variant, or _variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                int i = region.indexOf('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    country = region.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    variant = region.substring(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                    country = region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    variant = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            } else {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1144
                country = AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                                new GetPropertyAction("user.country", ""));
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1146
                variant = AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                                new GetPropertyAction("user.variant", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            startupLocale = new Locale(language, country, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        return startupLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Returns the default keyboard locale of the underlying operating system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1157
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    public Locale getDefaultKeyboardLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        return getStartupLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    private static DefaultMouseInfoPeer mPeer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1164
    @Override
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1165
    public synchronized MouseInfoPeer getMouseInfoPeer() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        if (mPeer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            mPeer = new DefaultMouseInfoPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        return mPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * Returns whether default toolkit needs the support of the xembed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * from embedding host(if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @return <code>true</code>, if XEmbed is needed, <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    public static boolean needsXEmbed() {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1179
        String noxembed = AccessController.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            doPrivileged(new GetPropertyAction("sun.awt.noxembed", "false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        if ("true".equals(noxembed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        if (tk instanceof SunToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            // SunToolkit descendants should override this method to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            // concrete behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            return ((SunToolkit)tk).needsXEmbedImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            // Non-SunToolkit doubtly might support XEmbed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Returns whether this toolkit needs the support of the xembed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * from embedding host(if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * @return <code>true</code>, if XEmbed is needed, <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    protected boolean needsXEmbedImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1205
    private static Dialog.ModalExclusionType DEFAULT_MODAL_EXCLUSION_TYPE = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * Returns whether the XEmbed server feature is requested by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * developer.  If true, Toolkit should return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * XEmbed-server-enabled CanvasPeer instead of the ordinary CanvasPeer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    protected final boolean isXEmbedServerRequested() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        return AccessController.doPrivileged(new GetBooleanAction("sun.awt.xembedserver"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * Returns whether the modal exclusion API is supported by the current toolkit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * When it isn't supported, calling <code>setModalExcluded</code> has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * effect, and <code>isModalExcluded</code> returns false for all windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @return true if modal exclusion is supported by the toolkit, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @see sun.awt.SunToolkit#setModalExcluded(java.awt.Window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @see sun.awt.SunToolkit#isModalExcluded(java.awt.Window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    public static boolean isModalExcludedSupported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        return tk.isModalExclusionTypeSupported(DEFAULT_MODAL_EXCLUSION_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * Default implementation for isModalExcludedSupportedImpl(), returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @see sun.awt.windows.WToolkit#isModalExcludeSupportedImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * @see sun.awt.X11.XToolkit#isModalExcludeSupportedImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    protected boolean isModalExcludedSupportedImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * Sets this window to be excluded from being modally blocked. When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * toolkit supports modal exclusion and this method is called, input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * events, focus transfer and z-order will continue to work for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * window, it's owned windows and child components, even in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * presence of a modal dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * For details on which <code>Window</code>s are normally blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * by modal dialog, see {@link java.awt.Dialog}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Invoking this method when the modal exclusion API is not supported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * the current toolkit has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @param window Window to be marked as not modally blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @see java.awt.Dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @see java.awt.Dialog#setModal(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @see sun.awt.SunToolkit#isModalExcludedSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @see sun.awt.SunToolkit#isModalExcluded(java.awt.Window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    public static void setModalExcluded(Window window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    {
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1264
        if (DEFAULT_MODAL_EXCLUSION_TYPE == null) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1265
            DEFAULT_MODAL_EXCLUSION_TYPE = Dialog.ModalExclusionType.APPLICATION_EXCLUDE;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1266
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        window.setModalExclusionType(DEFAULT_MODAL_EXCLUSION_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Returns whether the specified window is blocked by modal dialogs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * If the modal exclusion API isn't supported by the current toolkit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * it returns false for all windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @param window Window to test for modal exclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @return true if the window is modal excluded, false otherwise. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * the modal exclusion isn't supported by the current Toolkit, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * @see sun.awt.SunToolkit#isModalExcludedSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * @see sun.awt.SunToolkit#setModalExcluded(java.awt.Window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    public static boolean isModalExcluded(Window window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    {
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1288
        if (DEFAULT_MODAL_EXCLUSION_TYPE == null) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1289
            DEFAULT_MODAL_EXCLUSION_TYPE = Dialog.ModalExclusionType.APPLICATION_EXCLUDE;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1290
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        return window.getModalExclusionType().compareTo(DEFAULT_MODAL_EXCLUSION_TYPE) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * Overridden in XToolkit and WToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     */
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1297
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    public boolean isModalityTypeSupported(Dialog.ModalityType modalityType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        return (modalityType == Dialog.ModalityType.MODELESS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
               (modalityType == Dialog.ModalityType.APPLICATION_MODAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * Overridden in XToolkit and WToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     */
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1306
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    public boolean isModalExclusionTypeSupported(Dialog.ModalExclusionType exclusionType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        return (exclusionType == Dialog.ModalExclusionType.NO_EXCLUDE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    ///////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    // The following is used by the Java Plug-in to coordinate dialog modality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    // between containing applications (browsers, ActiveX containers etc) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    // the AWT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    ///////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    private ModalityListenerList modalityListeners = new ModalityListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    public void addModalityListener(ModalityListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        modalityListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    public void removeModalityListener(ModalityListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        modalityListeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    public void notifyModalityPushed(Dialog dialog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        notifyModalityChange(ModalityEvent.MODALITY_PUSHED, dialog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    public void notifyModalityPopped(Dialog dialog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        notifyModalityChange(ModalityEvent.MODALITY_POPPED, dialog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    final void notifyModalityChange(int id, Dialog source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        ModalityEvent ev = new ModalityEvent(source, modalityListeners, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        ev.dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    static class ModalityListenerList implements ModalityListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        Vector<ModalityListener> listeners = new Vector<ModalityListener>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        void add(ModalityListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            listeners.addElement(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        void remove(ModalityListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            listeners.removeElement(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1354
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        public void modalityPushed(ModalityEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            Iterator<ModalityListener> it = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                it.next().modalityPushed(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1362
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        public void modalityPopped(ModalityEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            Iterator<ModalityListener> it = listeners.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                it.next().modalityPopped(ev);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    } // end of class ModalityListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    ///////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    // End Plug-in code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    ///////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    public static boolean isLightweightOrUnknown(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        if (comp.isLightweight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            || !(getDefaultToolkit() instanceof SunToolkit))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        return !(comp instanceof Button
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            || comp instanceof Canvas
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            || comp instanceof Checkbox
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            || comp instanceof Choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            || comp instanceof Label
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            || comp instanceof java.awt.List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            || comp instanceof Panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            || comp instanceof Scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            || comp instanceof ScrollPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            || comp instanceof TextArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            || comp instanceof TextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            || comp instanceof Window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1395
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    public static class OperationTimedOut extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        public OperationTimedOut(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        public OperationTimedOut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    }
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1403
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1404
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    public static class InfiniteLoop extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1408
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    public static class IllegalThreadException extends RuntimeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        public IllegalThreadException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        public IllegalThreadException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    public static final int DEFAULT_WAIT_TIME = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    private static final int MAX_ITERS = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    private static final int MIN_ITERS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    private static final int MINIMAL_EDELAY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * Parameterless version of realsync which uses default timout (see DEFAUL_WAIT_TIME).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    public void realSync() throws OperationTimedOut, InfiniteLoop {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        realSync(DEFAULT_WAIT_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * Forces toolkit to synchronize with the native windowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * sub-system, flushing all pending work and waiting for all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * events to be processed.  This method guarantees that after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * return no additional Java events will be generated, unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * cause by user. Obviously, the method cannot be used on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * event dispatch thread (EDT). In case it nevertheless gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * invoked on this thread, the method throws the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * IllegalThreadException runtime exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * <p> This method allows to write tests without explicit timeouts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * or wait for some event.  Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * Frame f = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * <p> After realSync, <code>f</code> will be completely visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * on the screen, its getLocationOnScreen will be returning the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * right result and it will be the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * <p> Another example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * b.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * <p> After realSync, <code>b</code> will be focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * <p> Notice that realSync isn't guaranteed to work if recurring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * actions occur, such as if during processing of some event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * another request which may generate some events occurs.  By
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * default, sync tries to perform as much as {@value MAX_ITERS}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * cycles of event processing, allowing for roughly {@value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * MAX_ITERS} additional requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * <p> For example, requestFocus() generates native request, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * generates one or two Java focus events, which then generate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * serie of paint events, a serie of Java focus events, which then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * generate a serie of paint events which then are processed -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * three cycles, minimum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * @param timeout the maximum time to wait in milliseconds, negative means "forever".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    public void realSync(final long timeout) throws OperationTimedOut, InfiniteLoop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            throw new IllegalThreadException("The SunToolkit.realSync() method cannot be used on the event dispatch thread (EDT).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        int bigLoop = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            // Let's do sync first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            // During the wait process, when we were processing incoming
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            // events, we could have made some new request, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            // generate new events.  Example: MapNotify/XSetInputFocus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            // Therefore, we dispatch them as long as there is something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            // to dispatch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            int iters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            while (iters < MIN_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                syncNativeQueue(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                iters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            while (syncNativeQueue(timeout) && iters < MAX_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                iters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            if (iters >= MAX_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                throw new InfiniteLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            // native requests were dispatched by X/Window Manager or Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            // Moreover, we processed them all on Toolkit thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            // Now wait while EDT processes them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            // During processing of some events (focus, for example),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            // some other events could have been generated.  So, after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            // waitForIdle, we may end up with full EventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            iters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            while (iters < MIN_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                waitForIdle(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                iters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            while (waitForIdle(timeout) && iters < MAX_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                iters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (iters >= MAX_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                throw new InfiniteLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            bigLoop++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            // Again, for Java events, it was simple to check for new Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            // events by checking event queue, but what if Java events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            // resulted in native requests?  Therefor, check native events again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        } while ((syncNativeQueue(timeout) || waitForIdle(timeout)) && bigLoop < MAX_ITERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * Platform toolkits need to implement this method to perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * sync of the native queue.  The method should wait until native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * requests are processed, all native events are processed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * corresponding Java events are generated.  Should return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * <code>true</code> if some events were processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    protected abstract boolean syncNativeQueue(final long timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    private boolean eventDispatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    private boolean queueEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    private final Object waitLock = "Wait Lock";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    private boolean isEQEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        EventQueue queue = getSystemEventQueueImpl();
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1543
        return AWTAccessor.getEventQueueAccessor().noEvents(queue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * Waits for the Java event queue to empty.  Ensures that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * events are processed (including paint events), and that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * recursive events were generated, they are also processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * Should return <code>true</code> if more processing is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * necessary, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     */
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1553
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    protected final boolean waitForIdle(final long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        boolean queueWasEmpty = isEQEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        queueEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        eventDispatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        synchronized(waitLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            postEvent(AppContext.getAppContext(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                      new PeerEvent(getSystemEventQueueImpl(), null, PeerEvent.LOW_PRIORITY_EVENT) {
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28231
diff changeset
  1562
                          @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                          public void dispatch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                              // Here we block EDT.  It could have some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                              // events, it should have dispatched them by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                              // now.  So native requests could have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                              // generated.  First, dispatch them.  Then,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                              // flush Java events again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                              int iters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                              while (iters < MIN_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                                  syncNativeQueue(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                                  iters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                              while (syncNativeQueue(timeout) && iters < MAX_ITERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                                  iters++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                              flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                              synchronized(waitLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                  queueEmpty = isEQEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                  eventDispatched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                                  waitLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                      });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                while (!eventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                    waitLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            Thread.sleep(MINIMAL_EDELAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            throw new RuntimeException("Interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        // Lock to force write-cache flush for queueEmpty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        synchronized (waitLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            return !(queueEmpty && isEQEmpty() && queueWasEmpty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * Grabs the mouse input for the given window.  The window must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * visible.  The window or its children do not receive any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * additional mouse events besides those targeted to them.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * other events will be dispatched as before - to the respective
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * targets.  This Window will receive UngrabEvent when automatic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * ungrab is about to happen.  The event can be listened to by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * installing AWTEventListener with WINDOW_EVENT_MASK.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * UngrabEvent class for the list of conditions when ungrab is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * about to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @see UngrabEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    public abstract void grab(Window w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * Forces ungrab.  No event will be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    public abstract void ungrab(Window w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * Locates the splash screen library in a platform dependent way and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * the splash screen. Should be invoked on first top-level frame display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * @see java.awt.SplashScreen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    public static native void closeSplashScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    /* The following methods and variables are to support retrieving
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * desktop text anti-aliasing settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    /* Need an instance method because setDesktopProperty(..) is protected. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    private void fireDesktopFontPropertyChanges() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        setDesktopProperty(SunToolkit.DESKTOPFONTHINTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                           SunToolkit.getDesktopFontHints());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    private static boolean checkedSystemAAFontSettings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    private static boolean useSystemAAFontSettings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    private static boolean lastExtraCondition = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    private static RenderingHints desktopFontHints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
    /* Since Swing is the reason for this "extra condition" logic its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * worth documenting it in some detail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * First, a goal is for Swing and applications to both retrieve and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * use the same desktop property value so that there is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * consistency between the settings used by JDK's Swing implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * and 3rd party custom Swing components, custom L&Fs and any general
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * text rendering that wants to be consistent with these.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * But by default on Solaris & Linux Swing will not use AA text over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * remote X11 display (unless Xrender can be used which is TBD and may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * always be available anyway) as that is a noticeable performance hit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * So there needs to be a way to express that extra condition so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * it is seen by all clients of the desktop property API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * If this were the only condition it could be handled here as it would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * be the same for any L&F and could reasonably be considered to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * a static behaviour of those systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * But GTK currently has an additional test based on locale which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * not applied by Metal. So mixing GTK in a few locales with Metal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * would mean the last one wins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * This could be stored per-app context which would work
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * for different applets, but wouldn't help for a single application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * using GTK and some other L&F concurrently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * But it is expected this will be addressed within GTK and the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * system so is a temporary and somewhat unlikely harmless corner case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    public static void setAAFontSettingsCondition(boolean extraCondition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        if (extraCondition != lastExtraCondition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            lastExtraCondition = extraCondition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            if (checkedSystemAAFontSettings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                /* Someone already asked for this info, under a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                 * condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                 * We'll force re-evaluation instead of replicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                 * logic, then notify any listeners of any change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                checkedSystemAAFontSettings = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                if (tk instanceof SunToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                     ((SunToolkit)tk).fireDesktopFontPropertyChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    /* "false", "off", ""default" aren't explicitly tested, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * just fall through to produce a null return which all are equated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * "false".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    private static RenderingHints getDesktopAAHintsByName(String hintname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        Object aaHint = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        hintname = hintname.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        if (hintname.equals("on")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            aaHint = VALUE_TEXT_ANTIALIAS_ON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        } else if (hintname.equals("gasp")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            aaHint = VALUE_TEXT_ANTIALIAS_GASP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        } else if (hintname.equals("lcd") || hintname.equals("lcd_hrgb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            aaHint = VALUE_TEXT_ANTIALIAS_LCD_HRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        } else if (hintname.equals("lcd_hbgr")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            aaHint = VALUE_TEXT_ANTIALIAS_LCD_HBGR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        } else if (hintname.equals("lcd_vrgb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            aaHint = VALUE_TEXT_ANTIALIAS_LCD_VRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        } else if (hintname.equals("lcd_vbgr")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            aaHint = VALUE_TEXT_ANTIALIAS_LCD_VBGR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        if (aaHint != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            RenderingHints map = new RenderingHints(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            map.put(KEY_TEXT_ANTIALIASING, aaHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    /* This method determines whether to use the system font settings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * or ignore them if a L&F has specified they should be ignored, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * to override both of these with a system property specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * If the toolkit isn't a SunToolkit, (eg may be headless) then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * system property isn't applied as desktop properties are considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * to be inapplicable in that case. In that headless case although
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * this method will return "true" the toolkit will return a null map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
    private static boolean useSystemAAFontSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        if (!checkedSystemAAFontSettings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            useSystemAAFontSettings = true; /* initially set this true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            String systemAAFonts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            if (tk instanceof SunToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                systemAAFonts =
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1738
                    AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                         new GetPropertyAction("awt.useSystemAAFontSettings"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            if (systemAAFonts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                useSystemAAFontSettings =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                    Boolean.valueOf(systemAAFonts).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                /* If it is anything other than "true", then it may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                 * a hint name , or it may be "off, "default", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                if (!useSystemAAFontSettings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                    desktopFontHints = getDesktopAAHintsByName(systemAAFonts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            /* If its still true, apply the extra condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            if (useSystemAAFontSettings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                 useSystemAAFontSettings = lastExtraCondition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            checkedSystemAAFontSettings = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        return useSystemAAFontSettings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    /* A variable defined for the convenience of JDK code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    public static final String DESKTOPFONTHINTS = "awt.font.desktophints";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    /* Overridden by subclasses to return platform/desktop specific values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    protected RenderingHints getDesktopAAHints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    /* Subclass desktop property loading methods call this which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * in turn calls the appropriate subclass implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * getDesktopAAHints() when system settings are being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * Its public rather than protected because subclasses may delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * to a helper class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    public static RenderingHints getDesktopFontHints() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        if (useSystemAAFontSettings()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
             Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
             if (tk instanceof SunToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                 Object map = ((SunToolkit)tk).getDesktopAAHints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                 return (RenderingHints)map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
             } else { /* Headless Toolkit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                 return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        } else if (desktopFontHints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            /* cloning not necessary as the return value is cloned later, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
             * its harmless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            return (RenderingHints)(desktopFontHints.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    public abstract boolean isDesktopSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * consumeNextKeyTyped() method is not currently used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * however Swing could use it in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    public static synchronized void consumeNextKeyTyped(KeyEvent keyEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        try {
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1802
            AWTAccessor.getDefaultKeyboardFocusManagerAccessor().consumeNextKeyTyped(
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1803
                (DefaultKeyboardFocusManager)KeyboardFocusManager.
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1804
                    getCurrentKeyboardFocusManager(),
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1805
                keyEvent);
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1806
        } catch (ClassCastException cce) {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1807
             cce.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3237
diff changeset
  1811
    protected static void dumpPeers(final PlatformLogger aLog) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        AWTAutoShutdown.getInstance().dumpPeers(aLog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1815
    /**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1816
     * Returns the <code>Window</code> ancestor of the component <code>comp</code>.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1817
     * @return Window ancestor of the component or component by itself if it is Window;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1818
     *         null, if component is not a part of window hierarchy
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1819
     */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1820
    public static Window getContainingWindow(Component comp) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1821
        while (comp != null && !(comp instanceof Window)) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1822
            comp = comp.getParent();
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1823
        }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1824
        return (Window)comp;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1825
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1826
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1827
    private static Boolean sunAwtDisableMixing = null;
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1828
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1829
    /**
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1830
     * Returns the value of "sun.awt.disableMixing" property. Default
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1831
     * value is {@code false}.
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1832
     */
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1833
    public synchronized static boolean getSunAwtDisableMixing() {
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1834
        if (sunAwtDisableMixing == null) {
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1835
            sunAwtDisableMixing = AccessController.doPrivileged(
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 13240
diff changeset
  1836
                                      new GetBooleanAction("sun.awt.disableMixing"));
1978
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1837
        }
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1838
        return sunAwtDisableMixing.booleanValue();
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1839
    }
8b981ce05cd0 6797195: Forward-port enhancements for hw/lw mixing from 6u12 to 7
anthony
parents: 1973
diff changeset
  1840
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * Returns true if the native GTK libraries are available.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * default implementation returns false, but UNIXToolkit overrides this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * method to provide a more specific answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    public boolean isNativeGTKAvailable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    }
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1849
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1850
    private static final Object DEACTIVATION_TIMES_MAP_KEY = new Object();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1851
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1852
    public synchronized void setWindowDeactivationTime(Window w, long time) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1853
        AppContext ctx = getAppContext(w);
27715
58e23e39e064 8004148: NPE in sun.awt.SunToolkit.getWindowDeactivationTime
ant
parents: 26353
diff changeset
  1854
        if (ctx == null) {
58e23e39e064 8004148: NPE in sun.awt.SunToolkit.getWindowDeactivationTime
ant
parents: 26353
diff changeset
  1855
            return;
58e23e39e064 8004148: NPE in sun.awt.SunToolkit.getWindowDeactivationTime
ant
parents: 26353
diff changeset
  1856
        }
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 23900
diff changeset
  1857
        @SuppressWarnings("unchecked")
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1858
        WeakHashMap<Window, Long> map = (WeakHashMap<Window, Long>)ctx.get(DEACTIVATION_TIMES_MAP_KEY);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1859
        if (map == null) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1860
            map = new WeakHashMap<Window, Long>();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1861
            ctx.put(DEACTIVATION_TIMES_MAP_KEY, map);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1862
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1863
        map.put(w, time);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1864
    }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1865
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1866
    public synchronized long getWindowDeactivationTime(Window w) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1867
        AppContext ctx = getAppContext(w);
27715
58e23e39e064 8004148: NPE in sun.awt.SunToolkit.getWindowDeactivationTime
ant
parents: 26353
diff changeset
  1868
        if (ctx == null) {
58e23e39e064 8004148: NPE in sun.awt.SunToolkit.getWindowDeactivationTime
ant
parents: 26353
diff changeset
  1869
            return -1;
58e23e39e064 8004148: NPE in sun.awt.SunToolkit.getWindowDeactivationTime
ant
parents: 26353
diff changeset
  1870
        }
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 23900
diff changeset
  1871
        @SuppressWarnings("unchecked")
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1872
        WeakHashMap<Window, Long> map = (WeakHashMap<Window, Long>)ctx.get(DEACTIVATION_TIMES_MAP_KEY);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1873
        if (map == null) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1874
            return -1;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1875
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1876
        Long time = map.get(w);
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1877
        return time == null ? -1 : time;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1878
    }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13648
diff changeset
  1879
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1880
    // Cosntant alpha
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1881
    public boolean isWindowOpacitySupported() {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1882
        return false;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1883
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1884
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1885
    // Shaping
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1886
    public boolean isWindowShapingSupported() {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1887
        return false;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1888
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1889
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1890
    // Per-pixel alpha
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1891
    public boolean isWindowTranslucencySupported() {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1892
        return false;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1893
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1894
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1895
    public boolean isTranslucencyCapable(GraphicsConfiguration gc) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1896
        return false;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1897
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1898
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1899
    /**
13233
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1900
     * Returns true if swing backbuffer should be translucent.
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1901
     */
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1902
    public boolean isSwingBackbufferTranslucencySupported() {
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1903
        return false;
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1904
    }
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1905
4d45f7ebc0d7 7124244: [macosx] Shaped windows support
serb
parents: 12661
diff changeset
  1906
    /**
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1907
     * Returns whether or not a containing top level window for the passed
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1908
     * component is
2648
aa45a227fce3 6802853: API: shaped & translucent windows
anthony
parents: 2464
diff changeset
  1909
     * {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT PERPIXEL_TRANSLUCENT}.
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1910
     *
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1911
     * @param c a Component which toplevel's to check
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1912
     * @return {@code true}  if the passed component is not null and has a
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1913
     * containing toplevel window which is opaque (so per-pixel translucency
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1914
     * is not enabled), {@code false} otherwise
2648
aa45a227fce3 6802853: API: shaped & translucent windows
anthony
parents: 2464
diff changeset
  1915
     * @see GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1916
     */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1917
    public static boolean isContainingTopLevelOpaque(Component c) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1918
        Window w = getContainingWindow(c);
3237
7c6c2d9361d9 6837446: Introduce Window.isOpaque() method
anthony
parents: 3084
diff changeset
  1919
        return w != null && w.isOpaque();
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1920
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1921
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1922
    /**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1923
     * Returns whether or not a containing top level window for the passed
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1924
     * component is
2648
aa45a227fce3 6802853: API: shaped & translucent windows
anthony
parents: 2464
diff changeset
  1925
     * {@link GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT}.
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1926
     *
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1927
     * @param c a Component which toplevel's to check
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1928
     * @return {@code true} if the passed component is not null and has a
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1929
     * containing toplevel window which has opacity less than
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1930
     * 1.0f (which means that it is translucent), {@code false} otherwise
2648
aa45a227fce3 6802853: API: shaped & translucent windows
anthony
parents: 2464
diff changeset
  1931
     * @see GraphicsDevice.WindowTranslucency#TRANSLUCENT
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1932
     */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1933
    public static boolean isContainingTopLevelTranslucent(Component c) {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1934
        Window w = getContainingWindow(c);
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1935
        return w != null && w.getOpacity() < 1.0f;
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1936
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1937
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1938
    /**
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1939
     * Returns whether the native system requires using the peer.updateWindow()
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1940
     * method to update the contents of a non-opaque window, or if usual
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1941
     * painting procedures are sufficient. The default return value covers
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1942
     * the X11 systems. On MS Windows this method is overriden in WToolkit
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1943
     * to return true.
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1944
     */
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1945
    public boolean needUpdateWindow() {
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1946
        return false;
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1947
    }
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1978
diff changeset
  1948
2810
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
  1949
    /**
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
  1950
     * Descendants of the SunToolkit should override and put their own logic here.
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
  1951
     */
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
  1952
    public int getNumberOfButtons(){
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
  1953
        return 3;
fa49c6a06baf 6799099: All automatic regression tests that create Robot fail on X11
dav
parents: 2648
diff changeset
  1954
    }
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1955
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1956
    /**
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1957
     * Checks that the given object implements/extends the given
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1958
     * interface/class.
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1959
     *
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1960
     * Note that using the instanceof operator causes a class to be loaded.
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1961
     * Using this method doesn't load a class and it can be used instead of
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1962
     * the instanceof operator for performance reasons.
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1963
     *
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1964
     * @param obj Object to be checked
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1965
     * @param type The name of the interface/class. Must be
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1966
     * fully-qualified interface/class name.
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1967
     * @return true, if this object implements/extends the given
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1968
     *         interface/class, false, otherwise, or if obj or type is null
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1969
     */
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1970
    public static boolean isInstanceOf(Object obj, String type) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1971
        if (obj == null) return false;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1972
        if (type == null) return false;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1973
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1974
        return isInstanceOf(obj.getClass(), type);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1975
    }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1976
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1977
    private static boolean isInstanceOf(Class<?> cls, String type) {
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1978
        if (cls == null) return false;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1979
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1980
        if (cls.getName().equals(type)) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1981
            return true;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1982
        }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1983
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 9035
diff changeset
  1984
        for (Class<?> c : cls.getInterfaces()) {
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1985
            if (c.getName().equals(type)) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1986
                return true;
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1987
            }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1988
        }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1989
        return isInstanceOf(cls.getSuperclass(), type);
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2810
diff changeset
  1990
    }
6826
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  1991
26342
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1992
    protected static LightweightFrame getLightweightFrame(Component c) {
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1993
        for (; c != null; c = c.getParent()) {
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1994
            if (c instanceof LightweightFrame) {
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1995
                return (LightweightFrame)c;
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1996
            }
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1997
            if (c instanceof Window) {
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1998
                // Don't traverse owner windows
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  1999
                return null;
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  2000
            }
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  2001
        }
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  2002
        return null;
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  2003
    }
3637212ae8f2 8049065: [JLightweightFrame] Support DnD for SwingNode
anthony
parents: 26031
diff changeset
  2004
6826
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2005
    ///////////////////////////////////////////////////////////////////////////
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2006
    //
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2007
    // The following methods help set and identify whether a particular
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2008
    // AWTEvent object was produced by the system or by user code. As of this
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2009
    // writing the only consumer is the Java Plug-In, although this information
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2010
    // could be useful to more clients and probably should be formalized in
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2011
    // the public API.
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2012
    //
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2013
    ///////////////////////////////////////////////////////////////////////////
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2014
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2015
    public static void setSystemGenerated(AWTEvent e) {
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2016
        AWTAccessor.getAWTEventAccessor().setSystemGenerated(e);
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2017
    }
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2018
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2019
    public static boolean isSystemGenerated(AWTEvent e) {
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2020
        return AWTAccessor.getAWTEventAccessor().isSystemGenerated(e);
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2021
    }
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 6821
diff changeset
  2022
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
} // class SunToolkit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
 * PostEventQueue is a Thread that runs in the same AppContext as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
 * Java EventQueue.  It is a queue of AWTEvents to be posted to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
 * Java EventQueue.  The toolkit Thread (AWT-Windows/AWT-Motif) posts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
 * events to this queue, which then calls EventQueue.postEvent().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
 * We do this because EventQueue.postEvent() may be overridden by client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
 * code, and we mustn't ever call client code from the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
class PostEventQueue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    private EventQueueItem queueHead = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    private EventQueueItem queueTail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    private final EventQueue eventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2040
    private Thread flushThread = null;
13240
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2041
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    PostEventQueue(EventQueue eq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        eventQueue = eq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    /*
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  2047
     * Continually post pending AWTEvents to the Java EventQueue. The method
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  2048
     * is synchronized to ensure the flush is completed before a new event
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  2049
     * can be posted to this queue.
13240
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2050
     *
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2051
     * 7177040: The method couldn't be wholly synchronized because of calls
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2052
     * of EventQueue.postEvent() that uses pushPopLock, otherwise it could
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2053
     * potentially lead to deadlock
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     */
13240
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2055
    public void flush() {
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2056
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2057
        Thread newThread = Thread.currentThread();
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2058
13240
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2059
        try {
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2060
            EventQueueItem tempQueue;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2061
            synchronized (this) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2062
                // Avoid method recursion
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2063
                if (newThread == flushThread) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2064
                    return;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2065
                }
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2066
                // Wait for other threads' flushing
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2067
                while (flushThread != null) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2068
                    wait();
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2069
                }
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2070
                // Skip everything if queue is empty
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2071
                if (queueHead == null) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2072
                    return;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2073
                }
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2074
                // Remember flushing thread
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2075
                flushThread = newThread;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2076
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2077
                tempQueue = queueHead;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2078
                queueHead = queueTail = null;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2079
            }
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2080
            try {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2081
                while (tempQueue != null) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2082
                    eventQueue.postEvent(tempQueue.event);
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2083
                    tempQueue = tempQueue.next;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2084
                }
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2085
            }
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2086
            finally {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2087
                // Only the flushing thread can get here
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2088
                synchronized (this) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2089
                    // Forget flushing thread, inform other pending threads
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2090
                    flushThread = null;
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2091
                    notifyAll();
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2092
                }
13240
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2093
            }
5f9e6f436f0b 7177040: Deadlock between PostEventQueue.noEvents, EventQueue.isDispatchThread and SwingUtilities.invokeLater
bagiras
parents: 13233
diff changeset
  2094
        }
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2095
        catch (InterruptedException e) {
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2096
            // Couldn't allow exception go up, so at least recover the flag
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  2097
            newThread.interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * Enqueue an AWTEvent to be posted to the Java EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    void postEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        EventQueueItem item = new EventQueueItem(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            if (queueHead == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                queueHead = queueTail = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                queueTail.next = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                queueTail = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        SunToolkit.wakeupEventQueue(eventQueue, event.getSource() == AWTAutoShutdown.getInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
} // class PostEventQueue