summaryrefslogtreecommitdiff
path: root/NET/worlds/console/OkCancelDialog.java
blob: 2b2f6d2bb35aed95978528a6d12da2ad891fb228 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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);
      }
   }
}