package sun.lwawt;
import java.awt.*;
import java.awt.List;
import java.awt.datatransfer.*;
import java.awt.dnd.DropTarget;
import java.awt.image.*;
import java.awt.peer.*;
import java.security.*;
import java.util.*;
import sun.awt.*;
import sun.print.*;
import sun.awt.util.ThreadGroupUtils;
import static sun.lwawt.LWWindowPeer.PeerType;
public abstract class LWToolkit extends SunToolkit implements Runnable {
private static final int STATE_NONE = 0;
private static final int STATE_INIT = 1;
private static final int STATE_MESSAGELOOP = 2;
private static final int STATE_SHUTDOWN = 3;
private static final int STATE_CLEANUP = 4;
private static final int STATE_DONE = 5;
private int runState = STATE_NONE;
private Clipboard clipboard;
private MouseInfoPeer mouseInfoPeer;
private volatile boolean dynamicLayoutSetting = true;
protected LWToolkit() {
}
protected final void init() {
AWTAutoShutdown.notifyToolkitThreadBusy();
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
Runnable shutdownRunnable = () -> {
shutdown();
waitForRunState(STATE_CLEANUP);
};
Thread shutdown = new Thread(
ThreadGroupUtils.getRootThreadGroup(), shutdownRunnable,
"AWT-Shutdown", 0, false);
shutdown.setContextClassLoader(null);
Runtime.getRuntime().addShutdownHook(shutdown);
String name = "AWT-LW";
Thread toolkitThread = new Thread(
ThreadGroupUtils.getRootThreadGroup(), this, name, 0, false);
toolkitThread.setDaemon(true);
toolkitThread.setPriority(Thread.NORM_PRIORITY + 1);
toolkitThread.start();
return null;
});
waitForRunState(STATE_MESSAGELOOP);
}
protected abstract void platformInit();
public final void shutdown() {
setRunState(STATE_SHUTDOWN);
platformShutdown();
}
protected abstract void platformShutdown();
protected abstract void platformCleanup();
private synchronized int getRunState() {
return runState;
}
private synchronized void setRunState(int state) {
runState = state;
notifyAll();
}
public final boolean isTerminating() {
return getRunState() >= STATE_SHUTDOWN;
}
private void waitForRunState(int state) {
while (getRunState() < state) {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException z) {
break;
}
}
}
@Override
public final void run() {
setRunState(STATE_INIT);
platformInit();
AWTAutoShutdown.notifyToolkitThreadFree();
setRunState(STATE_MESSAGELOOP);
while (getRunState() < STATE_SHUTDOWN) {
try {
platformRunMessage();
if (Thread.currentThread().isInterrupted()) {
if (AppContext.getAppContext().isDisposed()) {
break;
}
}
} catch (ThreadDeath td) {
break;
} catch (Throwable t) {
System.err.println("Exception on the toolkit thread");
t.printStackTrace(System.err);
}
}
setRunState(STATE_CLEANUP);
AWTAutoShutdown.notifyToolkitThreadFree();
platformCleanup();
setRunState(STATE_DONE);
}
protected abstract void platformRunMessage();
public static LWToolkit getLWToolkit() {
return (LWToolkit)Toolkit.getDefaultToolkit();
}
protected LWWindowPeer createDelegatedPeer(Window target,
PlatformComponent platformComponent,
PlatformWindow platformWindow,
PeerType peerType) {
LWWindowPeer peer = new LWWindowPeer(target, platformComponent, platformWindow, peerType);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final FramePeer createLightweightFrame(LightweightFrame target) {
PlatformComponent platformComponent = createLwPlatformComponent();
PlatformWindow platformWindow = createPlatformWindow(PeerType.LW_FRAME);
LWLightweightFramePeer peer = new LWLightweightFramePeer(target,
platformComponent,
platformWindow);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final WindowPeer createWindow(Window target) {
PlatformComponent platformComponent = createPlatformComponent();
PlatformWindow platformWindow = createPlatformWindow(PeerType.SIMPLEWINDOW);
return createDelegatedPeer(target, platformComponent, platformWindow, PeerType.SIMPLEWINDOW);
}
@Override
public final FramePeer createFrame(Frame target) {
PlatformComponent platformComponent = createPlatformComponent();
PlatformWindow platformWindow = createPlatformWindow(PeerType.FRAME);
return createDelegatedPeer(target, platformComponent, platformWindow, PeerType.FRAME);
}
@Override
public DialogPeer createDialog(Dialog target) {
PlatformComponent platformComponent = createPlatformComponent();
PlatformWindow platformWindow = createPlatformWindow(PeerType.DIALOG);
return createDelegatedPeer(target, platformComponent, platformWindow, PeerType.DIALOG);
}
@Override
public final FileDialogPeer createFileDialog(FileDialog target) {
FileDialogPeer peer = createFileDialogPeer(target);
targetCreatedPeer(target, peer);
return peer;
}
@Override
public final ButtonPeer createButton(Button target) {
PlatformComponent platformComponent = createPlatformComponent();
LWButtonPeer peer = new LWButtonPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final CheckboxPeer createCheckbox(Checkbox target) {
PlatformComponent platformComponent = createPlatformComponent();
LWCheckboxPeer peer = new LWCheckboxPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final ChoicePeer createChoice(Choice target) {
PlatformComponent platformComponent = createPlatformComponent();
LWChoicePeer peer = new LWChoicePeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final LabelPeer createLabel(Label target) {
PlatformComponent platformComponent = createPlatformComponent();
LWLabelPeer peer = new LWLabelPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final CanvasPeer createCanvas(Canvas target) {
PlatformComponent platformComponent = createPlatformComponent();
LWCanvasPeer<?, ?> peer = new LWCanvasPeer<>(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final ListPeer createList(List target) {
PlatformComponent platformComponent = createPlatformComponent();
LWListPeer peer = new LWListPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final PanelPeer createPanel(Panel target) {
PlatformComponent platformComponent = createPlatformComponent();
LWPanelPeer peer = new LWPanelPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final ScrollPanePeer createScrollPane(ScrollPane target) {
PlatformComponent platformComponent = createPlatformComponent();
LWScrollPanePeer peer = new LWScrollPanePeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final ScrollbarPeer createScrollbar(Scrollbar target) {
PlatformComponent platformComponent = createPlatformComponent();
LWScrollBarPeer peer = new LWScrollBarPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final TextAreaPeer createTextArea(TextArea target) {
PlatformComponent platformComponent = createPlatformComponent();
LWTextAreaPeer peer = new LWTextAreaPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final TextFieldPeer createTextField(TextField target) {
PlatformComponent platformComponent = createPlatformComponent();
LWTextFieldPeer peer = new LWTextFieldPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
@Override
public final boolean isDesktopSupported() {
return true;
}
@Override
public final boolean isTaskbarSupported() {
return true;
}
@Override
public final KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() {
return LWKeyboardFocusManagerPeer.getInstance();
}
@Override
public final synchronized MouseInfoPeer getMouseInfoPeer() {
if (mouseInfoPeer == null) {
mouseInfoPeer = createMouseInfoPeerImpl();
}
return mouseInfoPeer;
}
protected final MouseInfoPeer createMouseInfoPeerImpl() {
return new LWMouseInfoPeer();
}
protected abstract PlatformWindow getPlatformWindowUnderMouse();
@Override
public final PrintJob getPrintJob(Frame frame, String doctitle,
Properties props) {
return getPrintJob(frame, doctitle, null, null);
}
@Override
public final PrintJob getPrintJob(Frame frame, String doctitle,
JobAttributes jobAttributes,
PageAttributes pageAttributes) {
if (frame == null) {
throw new NullPointerException("frame must not be null");
}
if (GraphicsEnvironment.isHeadless()) {
throw new IllegalArgumentException();
}
PrintJob2D printJob = new PrintJob2D(frame, doctitle, jobAttributes, pageAttributes);
if (!printJob.printDialog()) {
printJob = null;
}
return printJob;
}
@Override
public final Clipboard getSystemClipboard() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
synchronized (this) {
if (clipboard == null) {
clipboard = createPlatformClipboard();
}
}
return clipboard;
}
protected abstract SecurityWarningWindow createSecurityWarning(
Window ownerWindow, LWWindowPeer ownerPeer);
public abstract Clipboard createPlatformClipboard();
protected abstract PlatformWindow createPlatformWindow(PeerType peerType);
protected abstract PlatformComponent createPlatformComponent();
protected abstract PlatformComponent createLwPlatformComponent();
protected abstract FileDialogPeer createFileDialogPeer(FileDialog target);
protected abstract PlatformDropTarget createDropTarget(DropTarget dropTarget,
Component component,
LWComponentPeer<?, ?> peer);
public static final Object targetToPeer(Object target) {
return SunToolkit.targetToPeer(target);
}
public static final void targetDisposedPeer(Object target, Object peer) {
SunToolkit.targetDisposedPeer(target, peer);
}
public abstract LWCursorManager getCursorManager();
public static void postEvent(AWTEvent event) {
postEvent(targetToAppContext(event.getSource()), event);
}
@Override
public final void grab(final Window w) {
final Object peer = AWTAccessor.getComponentAccessor().getPeer(w);
if (peer != null) {
((LWWindowPeer) peer).grab();
}
}
@Override
public final void ungrab(final Window w) {
final Object peer = AWTAccessor.getComponentAccessor().getPeer(w);
if (peer != null) {
((LWWindowPeer) peer).ungrab(false);
}
}
@Override
protected final Object lazilyLoadDesktopProperty(final String name) {
if (name.equals("awt.dynamicLayoutSupported")) {
return isDynamicLayoutSupported();
}
return super.lazilyLoadDesktopProperty(name);
}
@Override
public final void setDynamicLayout(final boolean dynamic) {
dynamicLayoutSetting = dynamic;
}
@Override
protected final boolean isDynamicLayoutSet() {
return dynamicLayoutSetting;
}
@Override
public final boolean isDynamicLayoutActive() {
return isDynamicLayoutSupported();
}
protected final boolean isDynamicLayoutSupported() {
return true;
}
}