package NET.worlds.console; import java.awt.Component; import java.awt.Container; import java.awt.Dialog; import java.awt.Dimension; import java.awt.Event; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Point; import java.awt.Toolkit; public abstract class PolledDialog extends Dialog implements MainCallback, DialogDisabled { private static final long serialVersionUID = -2608298148446880055L; public static final int CENTER = 0; public static final int BOTTOM = 1; public static final int RIGHT = 2; public static final int LEFT = 3; private boolean active; private boolean built; private boolean restarting; private boolean confirmed; private boolean killed; private boolean disableParent; private boolean toBeDisposed; private java.awt.Window parent; private DialogReceiver receiver; private int alignment = 0; private Point initialOffset = new Point(0, 0); private Object startupWaiter; private boolean started; protected PolledDialog(java.awt.Window parent, DialogReceiver receiver, String title, boolean disableParent) { super(findFrame(parent), title, false); this.parent = parent; this.receiver = receiver; if (disableParent) { this.disableParent(); } } protected void disableParent() { this.disableParent = this.parent instanceof DialogDisabled; if (this.disableParent) { ((DialogDisabled)this.parent).dialogDisable(true); } } protected void ready() { this.killed = false; if (this.built && !this.active) { this.restarting = true; } Main.register(this); this.setResizable(true); } protected void readySetGo() { this.startupWaiter = new Object(); this.started = false; synchronized (this.startupWaiter) { this.ready(); while (!this.started) { try { this.startupWaiter.wait(); } catch (InterruptedException var3) { } } } this.startupWaiter = null; this.started = false; } protected void setAlignment(int alignment) { this.setAlignment(alignment, 0, 0); } protected void setAlignment(int alignment, int offsetX, int offsetY) { this.alignment = alignment; this.initialOffset.x = offsetX; this.initialOffset.y = offsetY; } private static Frame findFrame(Container container) { while (!(container instanceof Frame) && container != null) { container = container.getParent(); } return (Frame)container; } protected abstract void build(); @Override public final synchronized void mainCallback() { if (!this.killed) { if (!this.built) { try { this.build(); this.pack(); this.restoreFrame(); this.position(); this.show(); this.built = true; this.active = true; } catch (Exception var2) { System.out.println("Caught exception building dialog " + this.toString() + " " + var2.getMessage()); } } else if (this.restarting) { this.restoreFrame(); this.show(); this.restarting = false; this.active = true; } } if (!this.killed && this.active) { this.activeCallback(); } else { if (this.toBeDisposed) { this.setVisible(false); this.parent.requestFocus(); if (this.isDisplayable()) { this.dispose(); } this.toBeDisposed = false; } Main.unregister(this); if (this.receiver != null) { this.receiver.dialogDone(this, this.confirmed); } } } protected void activeCallback() { } private void restoreFrame() { int handle = Window.getFrameHandle(); if (handle != 0 && Window.getWindowState(handle) == 1) { Window.setWindowState(handle, 0); } } @Override public void show() { try { super.show(); } catch (Exception var3) { System.out.println("Caught exception in PolledDialog::show: " + var3.getMessage()); } if (this.startupWaiter != null) { synchronized (this.startupWaiter) { this.started = true; this.startupWaiter.notify(); } } } public void closeIt(boolean state) { this.done(state); } protected synchronized boolean done(boolean confirmed) { if (!this.killed && this.disableParent) { ((DialogDisabled)this.parent).dialogDisable(false); } this.killed = true; this.toBeDisposed = true; if (this.active) { this.savePosAndSize(new PolledDialogSaver(this)); this.active = false; } this.confirmed = confirmed; return true; } @Override public boolean isActive() { return this.active; } protected void add(GridBagLayout gbag, Component comp, GridBagConstraints c) { if (comp != null && c != null) { gbag.setConstraints(comp, c); this.add(comp); } else { System.out.println("Bad parameter passed to PolledDialog::add, how bizarre."); } } @Override public boolean handleEvent(Event event) { if (event.id == 201) { return this.done(false); } else { if (event.id == 401 || event.id == 501) { Console.wake(); } return super.handleEvent(event); } } @Override public void dialogDisable(boolean disable) { this.setVisible(!disable); } public boolean getConfirmed() { return this.confirmed; } protected void position() { Dimension mySize = this.getSize(); this.initialSize(mySize.width, mySize.height); } protected void initialSize(int width, int height) { if (!PolledDialogSaver.restorePosAndSize(this.restorePosAndSize(), this)) { Point loc = this.parent.location(); Dimension size = null; if (this.parent instanceof Frame) { int hwnd = Window.getFrameHandle(); if (hwnd != 0) { size = new Dimension(Window.getWindowWidth(hwnd), Window.getWindowHeight(hwnd)); } } if (size == null) { size = this.parent.getSize(); } int x; if (this.alignment == 2) { x = loc.x + this.initialOffset.x + size.width; } else if (this.alignment == 3) { x = loc.x + this.initialOffset.x - width; } else { x = loc.x + this.initialOffset.x + (size.width - width) / 2; } int y; if (this.alignment == 1) { y = loc.y + this.initialOffset.y + size.height - height; } else { y = loc.y + this.initialOffset.y + (size.height - height) / 2; } Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); if (y + height > screenSize.height) { y = screenSize.height - height; } if (y < 0) { y = 0; } if (x + width > screenSize.width) { x = screenSize.width - width; } if (x < 0) { x = 0; } this.reshape(x, y, width, height); } } public void savePosAndSize(Object o) { } public Object restorePosAndSize() { return null; } }