SunToolkit.java revision 881
82a2fc4751cef28c0bdc327d02012bf8796083b9James Carlson * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * This code is free software; you can redistribute it and/or modify it
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * under the terms of the GNU General Public License version 2 only, as
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * published by the Free Software Foundation. Sun designates this
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * particular file as subject to the "Classpath" exception as provided
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * by Sun in the LICENSE file that accompanied this code.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * This code is distributed in the hope that it will be useful, but WITHOUT
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * version 2 for more details (a copy is included in the LICENSE file that
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * accompanied this code).
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * You should have received a copy of the GNU General Public License version
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * 2 along with this work; if not, write to the Free Software Foundation,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * CA 95054 USA or visit www.sun.com if you need additional information or
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * have any questions.
fb91fd8a302dfb13e250bbefb6a3970c2edc3ae3zf ComponentFactory, InputMethodSupport, KeyboardFocusManagerPeerProvider {
fb91fd8a302dfb13e250bbefb6a3970c2edc3ae3zf private static final Logger log = Logger.getLogger("sun.awt.SunToolkit");
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* Load debug settings for native code */
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (AccessController.doPrivileged(new GetBooleanAction("sun.awt.nativedebug"))) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Special mask for the UngrabEvent events, in addition to the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * public masks defined in AWTEvent. Should be used as the mask
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * value for Toolkit.addAWTEventListener.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* The key to put()/get() the PostEventQueue into/from the AppContext.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc private static final String POST_EVENT_QUEUE_KEY = "PostEventQueue";
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* If awt.threadgroup is set to class name the instance of
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * this class is created (should be subclass of ThreadGroup)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * and EventDispatchThread is created inside of it
fb91fd8a302dfb13e250bbefb6a3970c2edc3ae3zf * If loaded class overrides uncaughtException instance
fb91fd8a302dfb13e250bbefb6a3970c2edc3ae3zf * handles all uncaught exception on EventDispatchThread
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc String tgName = System.getProperty("awt.threadgroup", "");
a399b7655a1d835aa8606c2b29e4e777baac8635zf threadGroup = (ThreadGroup)ctor.newInstance(new Object[] {"AWT-ThreadGroup"});
a399b7655a1d835aa8606c2b29e4e777baac8635zf } catch (Exception e) {
a399b7655a1d835aa8606c2b29e4e777baac8635zf System.err.println("Failed loading " + tgName + ": " + e);
a399b7655a1d835aa8606c2b29e4e777baac8635zf public void run () {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc String eqName = System.getProperty("AWT.EventQueueClass",
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc "java.awt.EventQueue");
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc eventQueue = (EventQueue)Class.forName(eqName).newInstance();
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc } catch (Exception e) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc System.err.println("Failed loading " + eqName + ": " + e);
82a2fc4751cef28c0bdc327d02012bf8796083b9James Carlson PostEventQueue postEventQueue = new PostEventQueue(eventQueue);
82a2fc4751cef28c0bdc327d02012bf8796083b9James Carlson appContext.put(POST_EVENT_QUEUE_KEY, postEventQueue);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Thread eqInitThread = new Thread(threadGroup, initEQ, "EventQueue-Init");
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public boolean useBufferPerWindow() {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return false;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract TextFieldPeer createTextField(TextField target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract ListPeer createList(java.awt.List target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract CheckboxPeer createCheckbox(Checkbox target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract ScrollbarPeer createScrollbar(Scrollbar target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract ScrollPanePeer createScrollPane(ScrollPane target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract TextAreaPeer createTextArea(TextArea target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract FileDialogPeer createFileDialog(FileDialog target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract MenuBarPeer createMenuBar(MenuBar target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract PopupMenuPeer createPopupMenu(PopupMenu target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract MenuItemPeer createMenuItem(MenuItem target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract CheckboxMenuItemPeer createCheckboxMenuItem(
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract DragSourceContextPeer createDragSourceContextPeer(
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract TrayIconPeer createTrayIcon(TrayIcon target)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract SystemTrayPeer createSystemTray(SystemTray target);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract boolean isTraySupported();
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract FontPeer getFontPeer(String name, int style);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public abstract RobotPeer createRobot(Robot target, GraphicsDevice screen)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) throws HeadlessException {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc KeyboardFocusManagerPeerImpl peer = new KeyboardFocusManagerPeerImpl(manager);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * The AWT lock is typically only used on Unix platforms to synchronize
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * access to Xlib, OpenGL, etc. However, these methods are implemented
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * in SunToolkit so that they can be called from shared code (e.g.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * from the OGL pipeline) or from the X11 pipeline regardless of whether
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * XToolkit or MToolkit is currently in use. There are native macros
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx * (such as AWT_LOCK) defined in awt.h, so if the implementation of these
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx * methods is changed, make sure it is compatible with the native macros.
239e91abc172c1397b1e94869c5d0e8ab67bfc22hx * Note: The following methods (awtLock(), awtUnlock(), etc) should be
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * used in place of:
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * synchronized (getAWTLock()) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * By factoring these methods out specially, we are able to change the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * implementation of these methods (e.g. use more advanced locking
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * mechanisms) without impacting calling code.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * Sample usage:
public static final void awtLock() {
public static final boolean awtTryLock() {
public static final void awtUnlock() {
public static final void awtLockWait()
throws InterruptedException
throws InterruptedException
public static final void awtLockNotify() {
public static final void awtLockNotifyAll() {
public static final boolean isAWTLockHeldByCurrentThread() {
} catch (Exception e) {
return appContext;
return field;
} catch (SecurityException e) {
} catch (NoSuchFieldException e) {
return null;
return method;
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
return null;
} catch (InvocationTargetException e){
} catch (IllegalAccessException e) {
return null;
// Maps from non-Component/MenuComponent to AppContext.
} catch( IllegalAccessException e){
} catch( IllegalAccessException e){
return retObj;
return null;
// target is not a Component/MenuComponent, try the
return context;
} catch( IllegalAccessException e){
if (isSwingCont) {
if (isSwingCont) {
if (isSwingCont) {
catch (ClassNotFoundException e) {
catch (InstantiationException e) {
catch (IllegalAccessException e) {
return policy;
// Target is not a Component/MenuComponent, use the private Map
* on the toolkit (AWT-Windows/AWT-Motif) thread. This function should
throw new NullPointerException();
public void run() {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
public static void flushPendingEvents() {
public static boolean isPostEventQueueEmpty() {
final long when) {
public long getWhen(){
return when;
public static void invokeLaterOnAppContext(
class AWTInvocationLock {}
synchronized (lock) {
} catch( IllegalAccessException e){
return result;
} catch( IllegalAccessException e){
return result;
protected abstract int getScreenWidth();
protected abstract int getScreenHeight();
return hardwiredFontList;
public static boolean getSunAwtNoerasebackground() {
public static boolean getSunAwtErasebackgroundonresize() {
// fallback to checkRead/checkConnect for pre 1.2
} else if ((perm instanceof
throw se;
} catch (Exception e) {
return img;
} catch (Exception e) {
return img;
// fallback to checkRead/checkConnect for pre 1.2
} else if ((perm instanceof
throw se;
int repbits;
if (o != null) {
public static BufferedImage getScaledIconImage(java.util.List<Image> imageList, int width, int height) {
return null;
int iw;
int ih;
} catch (Exception e){
return null;
g.dispose();
return bimage;
public static DataBufferInt getScaledIconData(java.util.List<Image> imageList, int width, int height) {
return null;
return getSystemEventQueueImplPP();
return theEventQueue;
public boolean enableInputMethodsForTextComponent() {
return startupLocale;
return getStartupLocale();
return dataTransfererClassName;
return windowClosingListener;
return null;
return null;
return mPeer;
public static boolean needsXEmbed() {
protected boolean needsXEmbedImpl() {
DEFAULT_MODAL_EXCLUSION_TYPE = (Dialog.ModalExclusionType)AccessController.doPrivileged(new PrivilegedAction() {
f.setAccessible(true);
} catch (Exception e) {
return defaultType;
protected final boolean isXEmbedServerRequested() {
public static boolean isModalExcludedSupported()
protected boolean isModalExcludedSupportedImpl()
m.setAccessible(true);
return res;
super(msg);
public OperationTimedOut() {
super(msg);
public IllegalThreadException() {
throw new IllegalThreadException("The SunToolkit.realSync() method cannot be used on the event dispatch thread (EDT).");
sync();
// generate new events. Example: MapNotify/XSetInputFocus.
iters++;
iters++;
throw new InfiniteLoop();
iters++;
iters++;
throw new InfiniteLoop();
bigLoop++;
private boolean eventDispatched = false;
private boolean queueEmpty = false;
private boolean isEQEmpty() {
synchronized(SunToolkit.class) {
} catch (Exception e) {
e.printStackTrace();
queueEmpty = false;
eventDispatched = false;
synchronized(waitLock) {
public void dispatch() {
iters++;
iters++;
synchronized(waitLock) {
eventDispatched = true;
while (!eventDispatched) {
synchronized (waitLock) {
public static native void closeSplashScreen();
private void fireDesktopFontPropertyChanges() {
private static boolean checkedSystemAAFontSettings;
private static boolean useSystemAAFontSettings;
private static boolean lastExtraCondition = true;
if (checkedSystemAAFontSettings) {
checkedSystemAAFontSettings = false;
return map;
return null;
private static boolean useSystemAAFontSettings() {
if (!checkedSystemAAFontSettings) {
if (!useSystemAAFontSettings) {
if (useSystemAAFontSettings) {
checkedSystemAAFontSettings = true;
return useSystemAAFontSettings;
return null;
if (useSystemAAFontSettings()) {
return null;
return null;
public abstract boolean isDesktopSupported();
keyEvent);
public boolean isNativeGTKAvailable() {
* Java EventQueue. The toolkit Thread (AWT-Windows/AWT-Motif) posts
class PostEventQueue {
public boolean noEvents() {
public synchronized void flush() {