summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/DialogAction.java
blob: b0092bec3864d5b3d558122fc0fcf652cf52edd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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();
      }
   }
}