package NET.worlds.console; import java.awt.Button; import java.awt.Event; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; public class OkCancelDialog extends PolledDialog { private static final long serialVersionUID = -232374191442133438L; protected Button okButton = new Button(Console.message("OK")); protected Button cancelButton = new Button(Console.message("Cancel")); protected GridBagLayout gbag = new GridBagLayout(); private String prompt; protected int cancelKey = 27; protected int confirmKey = 10; protected static Font font = new Font(Console.message("GammaTextFont"), 0, 12); protected static Font bfont = new Font(Console.message("ButtonFont"), 0, 12); protected OkCancelDialog(java.awt.Window parent, String title) { this(parent, (DialogReceiver)parent, title); } protected OkCancelDialog(java.awt.Window parent, String title, String cancel, String ok) { this(parent, (DialogReceiver)parent, title, cancel, ok); } protected OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title) { this(parent, target, title, true); } protected OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title, boolean modal) { super(parent, target, title, modal); this.setLayout(this.gbag); } protected OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title, String cancel, String ok) { this(parent, target, title, cancel, ok, true); } protected OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title, String cancel, String ok, boolean modal) { this(parent, target, title, modal); if (ok != null) { this.okButton.setFont(bfont); this.okButton.setLabel(ok); } else { this.okButton = null; } if (cancel != null) { this.cancelButton.setFont(bfont); this.cancelButton.setLabel(cancel); } else { this.cancelButton = null; } } public OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title, String cancel, String ok, String prompt) { this(parent, target, title, cancel, ok, prompt, true); } public OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title, String cancel, String ok, String prompt, boolean modal) { this(parent, target, title, cancel, ok, modal); this.prompt = prompt; this.ready(); } public OkCancelDialog(java.awt.Window parent, DialogReceiver target, String title, String cancel, String ok, String prompt, boolean modal, int alignment) { this(parent, target, title, cancel, ok, modal); this.prompt = prompt; this.setAlignment(alignment); this.ready(); } @Override protected void build() { GridBagConstraints c = new GridBagConstraints(); if (this.prompt != null) { c.weightx = 1.0; c.weighty = 1.0; c.gridwidth = 0; MultiLineLabel mll = new MultiLineLabel(this.prompt, 5, 5); mll.setFont(font); this.add(this.gbag, mll, c); } int count = 0; if (this.okButton != null) { count++; } if (this.cancelButton != null) { count++; } c.gridwidth = count; c.weightx = 1.0; c.weighty = 0.0; if (this.okButton != null) { this.okButton.setFont(bfont); this.add(this.gbag, this.okButton, c); } if (this.cancelButton != null) { this.cancelButton.setFont(bfont); this.add(this.gbag, this.cancelButton, c); } } @Override public boolean action(Event event, Object what) { Object target = event.target; if (target == this.okButton && this.setValue()) { return this.done(true); } else { return target == this.cancelButton ? this.done(false) : false; } } protected boolean setValue() { return true; } public void setCancelKey(int key) { this.cancelKey = key; } public void setConfirmKey(int key) { this.confirmKey = key; } @Override public boolean keyDown(Event event, int key) { if (key == this.cancelKey) { return this.done(false); } else { if (key == this.confirmKey) { if (this.okButton == null) { return this.done(false); } if (this.setValue()) { return this.done(true); } } return super.keyDown(event, key); } } }