Source code: rcsdesign/QueryDialog.java
1 package rcsdesign;
2
3 import java.awt.*;
4 import java.util.*;
5 import java.awt.event.*;
6
7 /*
8 *
9 * QueryDialog
10 *
11 */
12 public class QueryDialog extends Dialog implements ActionListener
13 {
14 GridBagLayout lmInner = null;
15 Panel innerPanel = null;
16 TextArea msgArea = null;
17 Button yesButton = null;
18 Button noButton = null;
19 Button yesToAllButton = null;
20 Button noToAllButton = null;
21
22 Button cancelButton = null;
23 FlowLayout lm = null;
24 public boolean done = false;
25 public boolean ok = false;
26 public boolean yes_to_all = false;
27 public boolean no_to_all = false;
28 static public boolean cancel = false;
29 static public boolean debug_on = false;
30
31
32 Dimension d = new Dimension(480,240);
33
34 QueryDialog(Frame parent, String title, String message)
35 {
36 super(parent, title, false);
37 lm = new FlowLayout(FlowLayout.LEFT);
38 setLayout(lm);
39 int text_length = 10;
40 int text_width = 40;
41
42 try
43 {
44 Font f = getFont();
45 int font_height = 8;
46 int font_width = 8;
47 if(null != f)
48 {
49 FontMetrics fm = getFontMetrics(f);
50 if(null != fm)
51 {
52 font_height = fm.getHeight();
53 if(font_height < 8)
54 {
55 font_height = 8;
56 }
57 int font_widths[] = fm.getWidths();
58 font_width = 0;
59 for(int i = 0; i < font_widths.length; i++)
60 {
61 if(font_width < font_widths[i])
62 {
63 font_width = font_widths[i];
64 }
65 }
66 if(font_width < 8)
67 {
68 font_width = 8;
69 }
70 }
71 }
72 if(debug_on)
73 {
74 System.out.println("font_height = "+font_height);
75 System.out.println("font_width = "+font_width);
76 }
77 text_length = d.height/font_height;
78 if(text_length < 8)
79 {
80 text_length = 8;
81 }
82 text_width = d.width/font_width;
83 if(text_width < 20)
84 {
85 text_width = 20;
86 }
87 }
88 catch(Exception e)
89 {
90 e.printStackTrace();
91 }
92 if(debug_on)
93 {
94 System.out.println("text_length = "+text_length);
95 System.out.println("text_width = "+text_width);
96 }
97 innerPanel = new Panel();
98 lmInner = new GridBagLayout();
99 innerPanel.setLayout(lmInner);
100 if(message.length() > text_width)
101 {
102 StringTokenizer messageTokenizer = new StringTokenizer(message,"\r\n");
103 String new_message = "";
104 while(messageTokenizer.hasMoreTokens())
105 {
106 String line = messageTokenizer.nextToken();
107 if(line.length() > text_width)
108 {
109 for(int offset = text_width-4; offset < line.length(); offset+=(text_width-4))
110 {
111 line = line.substring(0,offset)+"- \\\n"+line.substring(offset);
112 }
113 }
114 new_message += line +"\n";
115 }
116 message = new_message;
117 }
118 GridBagConstraints c = new GridBagConstraints();
119 msgArea = new TextArea(message,text_length/2,text_width -5);
120 msgArea.setEditable(false);
121 innerPanel.add(msgArea);
122 c.gridx = 0;
123 c.gridy = 0;
124 c.gridwidth = 5;
125 lmInner.setConstraints(msgArea,c);
126 yesButton = new Button("YES");
127 innerPanel.add(yesButton);
128 yesButton.addActionListener(this);
129 c.gridx = 0;
130 c.gridy = 1;
131 c.gridwidth = 1;
132 lmInner.setConstraints(yesButton,c);
133 noButton = new Button("NO");
134 innerPanel.add(noButton);
135 c.gridx = 1;
136 c.gridy = 1;
137 c.gridwidth = 1;
138 lmInner.setConstraints(noButton,c);
139 noButton.addActionListener(this);
140 yesToAllButton = new Button("YES TO ALL");
141 innerPanel.add(yesToAllButton);
142 c.gridx = 2;
143 c.gridy = 1;
144 c.gridwidth = 1;
145 lmInner.setConstraints(yesToAllButton,c);
146 yesToAllButton.addActionListener(this);
147 noToAllButton = new Button("NO TO ALL");
148 innerPanel.add(noToAllButton);
149 c.gridx = 3;
150 c.gridy = 1;
151 c.gridwidth = 1;
152 lmInner.setConstraints(noToAllButton,c);
153 noToAllButton.addActionListener(this);
154 cancelButton = new Button("CANCEL");
155 innerPanel.add(cancelButton);
156 c.gridx = 4;
157 c.gridy = 1;
158 c.gridwidth = 1;
159 lmInner.setConstraints(cancelButton,c);
160 cancelButton.addActionListener(this);
161 add(innerPanel);
162 setSize(d);
163 if(debug_on)
164 {
165 System.out.println("Showing QueryDialog with message = "+message);
166 }
167 }
168
169 /*
170 *
171 * This function is called after the user clicks any button or presses <ENTER> in any TextField.
172 */
173 public void actionPerformed(ActionEvent evt)
174 {
175 try
176 {
177 if(debug_on)
178 {
179 System.out.println("QueryDialog.actionPerformed("+evt+")");
180 }
181 if(evt.getSource() == yesButton)
182 {
183 if(debug_on)
184 {
185 System.out.println("yes");
186 }
187 ok = true;
188 cancel = false;
189 done=true;
190 dispose();
191 return;
192 }
193
194 if(evt.getSource() == noButton)
195 {
196 if(debug_on)
197 {
198 System.out.println("no");
199 }
200 ok = false;
201 done=true;
202 dispose();
203 return;
204 }
205
206 if(evt.getSource() == yesToAllButton)
207 {
208 if(debug_on)
209 {
210 System.out.println("yes to all");
211 }
212 ok = true;
213 cancel = false;
214 yes_to_all = true;
215 no_to_all = false;
216 done=true;
217 dispose();
218 return;
219 }
220 if(evt.getSource() == noToAllButton)
221 {
222 if(debug_on)
223 {
224 System.out.println("no to all");
225 }
226 ok = false;
227 yes_to_all = false;
228 no_to_all = true;
229 done=true;
230 dispose();
231 return;
232 }
233 if(evt.getSource() == cancelButton)
234 {
235 if(debug_on)
236 {
237 System.out.println("cancel");
238 }
239 ok = false;
240 cancel = true;
241 done=true;
242 dispose();
243 return;
244 }
245 }
246 catch(Exception e)
247 {
248 e.printStackTrace();
249 }
250 }
251
252 public Dimension getPreferredSize()
253 {
254 return d;
255 }
256
257 public Dimension getMinimumSize()
258 {
259 return d;
260 }
261 }
262