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