This example Java source code file (LWCToolkit.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.lwawt.macosx;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.dnd.*;
import java.awt.dnd.peer.DragSourceContextPeer;
import java.awt.event.InputEvent;
import java.awt.event.InvocationEvent;
import java.awt.event.KeyEvent;
import java.awt.im.InputMethodHighlight;
import java.awt.peer.*;
import java.lang.reflect.*;
import java.security.*;
import java.util.*;
import java.util.concurrent.Callable;
import sun.awt.*;
import sun.lwawt.*;
import sun.lwawt.LWWindowPeer.PeerType;
import sun.security.action.GetBooleanAction;
import sun.util.CoreResourceBundleControl;
class NamedCursor extends Cursor {
NamedCursor(String name) {
super(name);
}
}
/**
* Mac OS X Cocoa-based AWT Toolkit.
*/
public final class LWCToolkit extends LWToolkit {
// While it is possible to enumerate all mouse devices
// and query them for the number of buttons, the code
// that does it is rather complex. Instead, we opt for
// the easy way and just support up to 5 mouse buttons,
// like Windows.
private static final int BUTTONS = 5;
private static native void initIDs();
private static CInputMethodDescriptor sInputMethodDescriptor;
static {
System.err.flush();
ResourceBundle platformResources = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ResourceBundle>() {
public ResourceBundle run() {
ResourceBundle platformResources = null;
try {
platformResources =
ResourceBundle.getBundle("sun.awt.resources.awtosx",
CoreResourceBundleControl.getRBControlInstance());
} catch (MissingResourceException e) {
// No resource file; defaults will be used.
}
System.loadLibrary("awt");
System.loadLibrary("fontmanager");
return platformResources;
}
});
AWTAccessor.getToolkitAccessor().setPlatformResources(platformResources);
if (!GraphicsEnvironment.isHeadless()) {
initIDs();
}
inAWT = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return !Boolean.parseBoolean(System.getProperty("javafx.embed.singleThread", "false"));
}
});
}
/*
* If true we operate in normal mode and nested runloop is executed in JavaRunLoopMode
* If false we operate in singleThreaded FX/AWT interop mode and nested loop uses NSDefaultRunLoopMode
*/
private static final boolean inAWT;
public LWCToolkit() {
SunToolkit.setDataTransfererClassName("sun.lwawt.macosx.CDataTransferer");
areExtraMouseButtonsEnabled = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons", "true"));
//set system property if not yet assigned
System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
}
/*
* System colors with default initial values, overwritten by toolkit if system values differ and are available.
*/
private final static int NUM_APPLE_COLORS = 3;
public final static int KEYBOARD_FOCUS_COLOR = 0;
public final static int INACTIVE_SELECTION_BACKGROUND_COLOR = 1;
public final static int INACTIVE_SELECTION_FOREGROUND_COLOR = 2;
private static int[] appleColors = {
0xFF808080, // keyboardFocusColor = Color.gray;
0xFFC0C0C0, // secondarySelectedControlColor
0xFF303030, // controlDarkShadowColor
};
private native void loadNativeColors(final int[] systemColors, final int[] appleColors);
protected void loadSystemColors(final int[] systemColors) {
if (systemColors == null) return;
loadNativeColors(systemColors, appleColors);
}
private static class AppleSpecificColor extends Color {
int index;
public AppleSpecificColor(int index) {
super(appleColors[index]);
this.index = index;
}
public int getRGB() {
return appleColors[index];
}
}
/**
* Returns Apple specific colors that we may expose going forward.
*
*/
public static Color getAppleColor(int color) {
return new AppleSpecificColor(color);
}
static void systemColorsChanged() {
// This is only called from native code.
EventQueue.invokeLater(new Runnable() {
public void run() {
AccessController.doPrivileged (new PrivilegedAction<Object>() {
public Object run() {
try {
final Method updateColorsMethod = SystemColor.class.getDeclaredMethod("updateSystemColors", new Class[0]);
updateColorsMethod.setAccessible(true);
updateColorsMethod.invoke(null, new Object[0]);
} catch (final Throwable e) {
e.printStackTrace();
// swallow this if something goes horribly wrong
}
return null;
}
});
}
});
}
public static LWCToolkit getLWCToolkit() {
return (LWCToolkit)Toolkit.getDefaultToolkit();
}
@Override
protected PlatformWindow createPlatformWindow(PeerType peerType) {
if (peerType == PeerType.EMBEDDED_FRAME) {
return new CPlatformEmbeddedFrame();
} else if (peerType == PeerType.VIEW_EMBEDDED_FRAME) {
return new CViewPlatformEmbeddedFrame();
} else if (peerType == PeerType.LW_FRAME) {
return new CPlatformLWWindow();
} else {
assert (peerType == PeerType.SIMPLEWINDOW || peerType == PeerType.DIALOG || peerType == PeerType.FRAME);
return new CPlatformWindow();
}
}
@Override
protected SecurityWarningWindow createSecurityWarning(Window ownerWindow, LWWindowPeer ownerPeer) {
return new CWarningWindow(ownerWindow, ownerPeer);
}
@Override
protected PlatformComponent createPlatformComponent() {
return new CPlatformComponent();
}
@Override
protected PlatformComponent createLwPlatformComponent() {
return new CPlatformLWComponent();
}
@Override
protected FileDialogPeer createFileDialogPeer(FileDialog target) {
return new CFileDialog(target);
}
@Override
public MenuPeer createMenu(Menu target) {
MenuPeer peer = new CMenu(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public MenuBarPeer createMenuBar(MenuBar target) {
MenuBarPeer peer = new CMenuBar(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public MenuItemPeer createMenuItem(MenuItem target) {
MenuItemPeer peer = new CMenuItem(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
CheckboxMenuItemPeer peer = new CCheckboxMenuItem(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public PopupMenuPeer createPopupMenu(PopupMenu target) {
PopupMenuPeer peer = new CPopupMenu(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public SystemTrayPeer createSystemTray(SystemTray target) {
SystemTrayPeer peer = new CSystemTray();
return peer;
}
@Override
public TrayIconPeer createTrayIcon(TrayIcon target) {
TrayIconPeer peer = new CTrayIcon(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public LWCursorManager getCursorManager() {
return CCursorManager.getInstance();
}
@Override
public Cursor createCustomCursor(final Image cursor, final Point hotSpot, final String name) throws IndexOutOfBoundsException, HeadlessException {
return new CCustomCursor(cursor, hotSpot, name);
}
@Override
public Dimension getBestCursorSize(final int preferredWidth, final int preferredHeight) throws HeadlessException {
return CCustomCursor.getBestCursorSize(preferredWidth, preferredHeight);
}
@Override
protected void platformCleanup() {
// TODO Auto-generated method stub
}
@Override
protected void platformInit() {
// TODO Auto-generated method stub
}
@Override
protected void platformRunMessage() {
// TODO Auto-generated method stub
}
@Override
protected void platformShutdown() {
// TODO Auto-generated method stub
}
class OSXPlatformFont extends sun.awt.PlatformFont
{
public OSXPlatformFont(String name, int style)
{
super(name, style);
}
protected char getMissingGlyphCharacter()
{
// Follow up for real implementation
return (char)0xfff8; // see http://developer.apple.com/fonts/LastResortFont/
}
}
public FontPeer getFontPeer(String name, int style) {
return new OSXPlatformFont(name, style);
}
@Override
protected int getScreenHeight() {
return GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration().getBounds().height;
}
@Override
protected int getScreenWidth() {
return GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration().getBounds().width;
}
@Override
protected void initializeDesktopProperties() {
super.initializeDesktopProperties();
Map <Object, Object> fontHints = new HashMap
Other Java examples (source code examples)
Here is a short list of links related to this Java LWCToolkit.java source code file: