package NET.worlds.scape; import NET.worlds.console.Console; import NET.worlds.console.DialogReceiver; import NET.worlds.console.PolledDialog; import java.io.IOException; public abstract class DialogAction extends Action implements DialogReceiver { boolean showDialog = true; boolean cancelOnly = false; private static PolledDialog dialogUp = null; private PolledDialog thisDialog; protected static boolean doCancel = false; private static Object classCookie = new Object(); @Override public void dialogDone(Object who, boolean confirmed) { if (dialogUp == who) { doCancel = true; if (confirmed) { this.doIt(); } } } public abstract void doIt(); public abstract PolledDialog getDialog(); @Override public Persister trigger(Event e, Persister seqID) { Console console = Console.getActive(); if (console == null) { return null; } else if (!this.showDialog && !this.cancelOnly) { if (console != null) { this.doIt(); } return null; } else if (dialogUp == null) { if (this.cancelOnly) { return null; } else { this.thisDialog = dialogUp = this.getDialog(); doCancel = false; return this; } } else { if (this.thisDialog == dialogUp && seqID != null) { if (doCancel) { dialogUp.closeIt(false); dialogUp = null; this.thisDialog = null; return null; } } else { doCancel = true; } return this; } } @Override public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException { Object ret = null; switch (index - offset) { case 0: if (mode == 0) { ret = BooleanPropertyEditor.make(new Property(this, index, "Display Dialog Box"), "Don't ask", "Ask user to ok choice"); } else if (mode == 1) { ret = new Boolean(this.showDialog); } else if (mode == 2) { this.showDialog = (Boolean)value; } break; case 1: if (mode == 0) { ret = BooleanPropertyEditor.make(new Property(this, index, "Cancel only"), "Regular operation", "Cancels dialog, doesn't do action"); } else if (mode == 1) { ret = new Boolean(this.cancelOnly); } else if (mode == 2) { this.cancelOnly = (Boolean)value; } break; default: ret = super.properties(index, offset + 2, mode, value); } return ret; } @Override public void saveState(Saver s) throws IOException { s.saveVersion(0, classCookie); super.saveState(s); s.saveBoolean(this.showDialog); s.saveBoolean(this.cancelOnly); } protected void dialogActionSkipRestore(Restorer r) throws IOException, TooNewException { super.restoreState(r); } @Override public void restoreState(Restorer r) throws IOException, TooNewException { switch (r.restoreVersion(classCookie)) { case 0: super.restoreState(r); this.showDialog = r.restoreBoolean(); this.cancelOnly = r.restoreBoolean(); return; default: throw new TooNewException(); } } }