package NET.worlds.console; import java.awt.Button; import java.awt.Event; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; public class YesNoCancelDialog extends PolledDialog { private static final long serialVersionUID = 4497394464783317237L; private Button yesButton = new Button(Console.message("Yes")); private Button noButton = new Button(Console.message("No")); private Button cancelButton = new Button(Console.message("Cancel")); private GridBagLayout gbag = new GridBagLayout(); private String prompt; private int choice = -2; public static final int UNDECIDED = -2; public static final int CANCEL = -1; public static final int NO = 0; public static final int YES = 1; public YesNoCancelDialog(java.awt.Window parent, DialogReceiver receiver, String title, String prompt) { super(parent, receiver, title, true); this.prompt = prompt; this.setLayout(this.gbag); this.ready(); } public int getChoice() { return this.choice; } @Override protected void build() { GridBagConstraints c = new GridBagConstraints(); c.weightx = 1.0; c.weighty = 1.0; c.gridwidth = 0; this.add(this.gbag, new MultiLineLabel(this.prompt, 5, 5), c); c.gridwidth = 3; c.weightx = 1.0; c.weighty = 0.0; this.add(this.gbag, this.yesButton, c); this.add(this.gbag, this.noButton, c); this.add(this.gbag, this.cancelButton, c); } @Override public void show() { super.show(); this.yesButton.requestFocus(); } private boolean yes() { this.choice = 1; return this.done(true); } private boolean no() { this.choice = 0; return this.done(false); } private boolean cancel() { this.choice = -1; return this.done(false); } @Override public boolean handleEvent(Event event) { return event.id == 201 ? this.cancel() : super.handleEvent(event); } @Override public boolean action(Event event, Object what) { Object target = event.target; if (target == this.yesButton) { return this.yes(); } else if (target == this.noButton) { return this.no(); } else { return target == this.cancelButton ? this.cancel() : false; } } @Override public boolean keyDown(Event event, int key) { return key == 27 ? this.cancel() : super.keyDown(event, key); } }