LWToolkit.java revision 4655
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * This code is free software; you can redistribute it and/or modify it
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * under the terms of the GNU General Public License version 2 only, as
97dc19418e21b4b87280756668cf171a4332e498vboxsync * published by the Free Software Foundation. Oracle designates this
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * particular file as subject to the "Classpath" exception as provided
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * by Oracle in the LICENSE file that accompanied this code.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * This code is distributed in the hope that it will be useful, but WITHOUT
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * version 2 for more details (a copy is included in the LICENSE file that
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * accompanied this code).
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * You should have received a copy of the GNU General Public License version
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * 2 along with this work; if not, write to the Free Software Foundation,
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * or visit www.oracle.com if you need additional information or have any
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * questions.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsyncpublic abstract class LWToolkit extends SunToolkit implements Runnable {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync private static Map<PlatformWindow, LWWindowPeer> delegateMap =
a7327719907518e1a890c41ab95b5a58252afe3cvboxsync * This method is called by subclasses to start this toolkit
a7327719907518e1a890c41ab95b5a58252afe3cvboxsync * by launching the message loop.
a7327719907518e1a890c41ab95b5a58252afe3cvboxsync * This method waits for the toolkit to be completely initialized
a7327719907518e1a890c41ab95b5a58252afe3cvboxsync * and returns before the message pump is started.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected final void init() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync ThreadGroup mainTG = AccessController.doPrivileged(
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync ThreadGroup currentTG = Thread.currentThread().getThreadGroup();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public void run() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync Thread toolkitThread = new Thread(mainTG, this, "AWT-LW");
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync toolkitThread.setPriority(Thread.NORM_PRIORITY + 1);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Implemented in subclasses to initialize platform-dependent
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * part of the toolkit (open X display connection, create
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * toolkit HWND, etc.)
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * This method is called on the toolkit thread.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected abstract void platformInit();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Sends a request to stop the message pump.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public void shutdown() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Implemented in subclasses to release all the platform-
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * dependent resources. Called after the message loop is
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * terminated.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Could be called (always called?) on a non-toolkit thread.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected abstract void platformShutdown();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Implemented in subclasses to release all the platform
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * resources before the application is terminated.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * This method is called on the toolkit thread.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected abstract void platformCleanup();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync private synchronized int getRunState() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public boolean isTerminating() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync synchronized (this) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync // TODO: log
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public void run() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync //XXX: if there isn't native code on the stack, the VM just
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync //kills the thread right away. Do we expect to catch it
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync //nevertheless?
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync } catch (Throwable t) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync // TODO: log
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync System.err.println("Exception on the toolkit thread");
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync //XXX: if that's a secondary loop, jump back to the STATE_MESSAGELOOP
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Process the next message(s) from the native event queue.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Initially, all the LWToolkit implementations were supposed
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * to have the similar message loop sequence: check if any events
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * available, peek events, wait. However, the later analysis shown
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * that X11 and Windows implementations are really different, so
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * let the subclasses do whatever they require.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected abstract void platformRunMessage();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync // ---- TOPLEVEL PEERS ---- //
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * Note that LWWindowPeer implements WindowPeer, FramePeer
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync * and DialogPeer interfaces.
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync private LWWindowPeer createDelegatedPeer(Window target, PlatformComponent platformComponent,
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWWindowPeer peer = new LWWindowPeer(target, platformComponent, platformWindow);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformWindow platformWindow = createPlatformWindow(LWWindowPeer.PeerType.SIMPLEWINDOW);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return createDelegatedPeer(target, platformComponent, platformWindow);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformWindow platformWindow = createPlatformWindow(LWWindowPeer.PeerType.FRAME);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return createDelegatedPeer(target, platformComponent, platformWindow);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public LWWindowPeer createEmbeddedFrame(CEmbeddedFrame target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformWindow platformWindow = createPlatformWindow(LWWindowPeer.PeerType.EMBEDDEDFRAME);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return createDelegatedPeer(target, platformComponent, platformWindow);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync CPrinterDialogPeer createCPrinterDialog(CPrinterDialog target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformWindow platformWindow = createPlatformWindow(LWWindowPeer.PeerType.DIALOG);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync CPrinterDialogPeer peer = new CPrinterDialogPeer(target, platformComponent, platformWindow);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return createCPrinterDialog((CPrinterDialog)target);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformWindow platformWindow = createPlatformWindow(LWWindowPeer.PeerType.DIALOG);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return createDelegatedPeer(target, platformComponent, platformWindow);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public FileDialogPeer createFileDialog(FileDialog target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync FileDialogPeer peer = createFileDialogPeer(target);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync // ---- LIGHTWEIGHT COMPONENT PEERS ---- //
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWButtonPeer peer = new LWButtonPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public CheckboxPeer createCheckbox(Checkbox target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWCheckboxPeer peer = new LWCheckboxPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWChoicePeer peer = new LWChoicePeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWLabelPeer peer = new LWLabelPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWCanvasPeer peer = new LWCanvasPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWListPeer peer = new LWListPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public MenuItemPeer createMenuItem(MenuItem target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWPanelPeer peer = new LWPanelPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public PopupMenuPeer createPopupMenu(PopupMenu target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public ScrollPanePeer createScrollPane(ScrollPane target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWScrollPanePeer peer = new LWScrollPanePeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public ScrollbarPeer createScrollbar(Scrollbar target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWScrollBarPeer peer = new LWScrollBarPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public TextAreaPeer createTextArea(TextArea target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWTextAreaPeer peer = new LWTextAreaPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public TextFieldPeer createTextField(TextField target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync PlatformComponent platformComponent = createPlatformComponent();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync LWTextFieldPeer peer = new LWTextFieldPeer(target, platformComponent);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync // ---- NON-COMPONENT PEERS ---- //
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public ColorModel getColorModel() throws HeadlessException {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getColorModel();
97dc19418e21b4b87280756668cf171a4332e498vboxsync public boolean isDesktopSupported() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return true;
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected DesktopPeer createDesktopPeer(Desktop target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return new CDesktopPeer();
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync DragSourceContextPeer dscp = CDragSourceContextPeer.createDragSourceContextPeer(dge);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync return LWKeyboardFocusManagerPeer.getInstance(manager);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public synchronized MouseInfoPeer getMouseInfoPeer() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync protected MouseInfoPeer createMouseInfoPeerImpl() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public PrintJob getPrintJob(Frame frame, String doctitle, Properties props) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public PrintJob getPrintJob(Frame frame, String doctitle, JobAttributes jobAttributes, PageAttributes pageAttributes) {
29890600941d8c492d0c52cc0daefda9bad1b538vboxsync PrintJob2D printJob = new PrintJob2D(frame, doctitle, jobAttributes, pageAttributes);
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public RobotPeer createRobot(Robot target, GraphicsDevice screen) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public boolean isTraySupported() {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public SystemTrayPeer createSystemTray(SystemTray target) {
65acc9a737d682ad726d06327bf6f6ecc6eb7e83vboxsync public TrayIconPeer createTrayIcon(TrayIcon target) {
43bb4d90097ef9a9d7fc597315d0869427609694vboxsync SecurityManager security = System.getSecurityManager();
return clipboard;
public boolean needUpdateWindow() {