1aee3590f14a3bb85e81119ca770f3246dca72f1
[anag] / AnagGUI.java
1 /* -*-java-*-
2 *
3 * $Id: AnagGUI.java,v 1.4 2001/02/19 19:19:11 mdw Exp $
4 *
5 * Front-end GUI
6 *
7 * (c) 2001 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Anag: a simple wordgame helper.
13 *
14 * Anag is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * Anag is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Anag; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: AnagGUI.java,v $
32 * Revision 1.4 2001/02/19 19:19:11 mdw
33 * Add `help' button. Lowercase input to the command.
34 *
35 * Revision 1.3 2001/02/16 21:46:10 mdw
36 * Use a BufferedReader, not a LineNumberReader.
37 *
38 * Revision 1.2 2001/02/07 09:10:04 mdw
39 * Add a settings panel (currently only allows the wordlist to be
40 * changed). Move the buttons down the right-hand side of the list. Add a
41 * `Run' button which passes arguments through directly.
42 *
43 * Revision 1.1 2001/02/04 19:53:07 mdw
44 * Simple GUI front-end in Java.
45 *
46 */
47
48 /*----- Imports -----------------------------------------------------------*/
49
50 import java.lang.*;
51 import java.util.*;
52 import java.io.*;
53 import java.awt.*;
54 import java.applet.*;
55 import java.awt.event.*;
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 class Whinge extends Frame {
60 public Whinge(String gripe) {
61 super("Error from AnagGUI");
62 setLayout(new GridBagLayout());
63 GridBagConstraints g = new GridBagConstraints();
64
65 addWindowListener(new WindowAdapter() {
66 public void windowClosing(WindowEvent e) { dispose(); }
67 });
68
69 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
70 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
71 g.weightx = g.weighty = 1;
72 g.insets = new Insets(24, 24, 24, 24);
73 add(new Label(gripe), g);
74
75 Button b = new Button("Bummer");
76 b.addActionListener(new ActionListener() {
77 public void actionPerformed(ActionEvent e) { dispose(); }
78 });
79 g.weighty = 0;
80 g.insets.top = 0; g.insets.bottom = 24;
81 add(b, g);
82 pack();
83 show();
84 }
85 };
86
87 class AnagPanel extends Panel {
88 TextField word;
89 java.awt.List list;
90 String file;
91 Settings sb;
92
93 class Settings extends Frame {
94 TextField name;
95
96 public Settings() {
97 super("AnagGUI settings");
98 Button b;
99 GridBagConstraints g = new GridBagConstraints();
100 this.setLayout(new GridBagLayout());
101 this.addWindowListener(new WindowAdapter() {
102 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
103 });
104 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
105 g.gridheight = 1;
106 g.weighty = 0;
107 g.fill = GridBagConstraints.NONE;
108 g.gridwidth = 1; g.weightx = 0;
109 g.insets = new Insets(8, 8, 8, 8);
110 this.add(new Label("Word list"), g);
111 g.fill = GridBagConstraints.HORIZONTAL;
112 g.gridwidth = GridBagConstraints.REMAINDER; g.weightx = 1;
113 g.insets.left = 0;
114 name = new TextField(20);
115 name.setText(file);
116 this.add(name, g);
117 g.insets.left = 8; g.insets.top = 0; g.gridwidth = 1;
118 g.weightx = 0; this.add(new Panel(), g);
119 g.weightx = 1; this.add(new Panel(), g);
120 g.weightx = 0;
121 g.insets.left = 0;
122 b = new Button("Cancel");
123 b.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
125 });
126 this.add(b, g);
127 b = new Button("OK");
128 b.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
130 file = name.getText(); dispose(); sb = null;
131 }
132 });
133 this.add(b, g);
134 this.pack();
135 this.show();
136 }
137 };
138
139 void splat(String gripe) { new Whinge(gripe); }
140 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
141
142 void listen(Process p) throws IOException {
143 BufferedReader fout =
144 new BufferedReader(new InputStreamReader(p.getInputStream()));
145 BufferedReader ferr =
146 new BufferedReader(new InputStreamReader(p.getErrorStream()));
147
148 String l;
149 Vector v = new Vector();
150 while ((l = fout.readLine()) != null)
151 v.addElement(l);
152 StringBuffer d = new StringBuffer();
153 while ((l = ferr.readLine()) != null)
154 d.append(l).append("\n");
155 l = d.toString();
156 if (l.length() > 0)
157 splat(l);
158 else {
159 list.removeAll();
160 int i;
161 String[] vv = new String[v.size()];
162 v.copyInto(vv);
163 for (i = 0; i < vv.length; i++)
164 list.add(vv[i]);
165 }
166 }
167
168 void run() {
169 try {
170 StringBuffer b = new StringBuffer();
171 b.append("anag -file ")
172 .append(file)
173 .append(" ")
174 .append(word.getText());
175 Process p = Runtime.getRuntime().exec(b.toString());
176 listen(p);
177 } catch (IOException e) {
178 splat(e.toString());
179 }
180 }
181
182 void help() {
183 try {
184 Process p = Runtime.getRuntime().exec("anag --help");
185 listen(p);
186 } catch (IOException e) {
187 splat(e.toString());
188 }
189 }
190
191 void getlist(String tag) {
192 try {
193 Vector v = new Vector();
194 String[] vv;
195 v.addElement("anag");
196 v.addElement("-file");
197 v.addElement(file);
198 v.addElement(tag);
199 v.addElement(word.getText().toLowerCase());
200 vv = new String[v.size()];
201 v.copyInto(vv);
202 Process p = Runtime.getRuntime().exec(vv);
203 listen(p);
204 } catch (IOException e) {
205 splat(e.toString());
206 }
207 }
208
209 public AnagPanel() {
210 super();
211 setLayout(new GridBagLayout());
212 GridBagConstraints g = new GridBagConstraints();
213 Button b;
214
215 file = System.getProperty("anag.dictionary", "/usr/dict/words");
216 sb = null;
217
218 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
219 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
220 g.weightx = 1; g.weighty = 0;
221
222 word = new TextField(20);
223 g.fill = GridBagConstraints.HORIZONTAL;
224 g.insets = new Insets(8, 8, 8, 8);
225 add(word, g);
226
227 list = new java.awt.List(20);
228 g.fill = GridBagConstraints.BOTH;
229 g.insets.top = 0;
230 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
231 g.weightx = g.weighty = 1;
232 add(list, g);
233
234 g.fill = GridBagConstraints.BOTH;
235 g.weightx = 0; g.weighty = 1;
236 g.insets.left = 0;
237 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
238 add(new Panel(), g);
239
240 g.fill = GridBagConstraints.HORIZONTAL;
241 g.weighty = 0;
242
243 b = new Button("Anagram");
244 b.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
246 });
247 add(b, g);
248
249 b = new Button("Subgram");
250 b.addActionListener(new ActionListener() {
251 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
252 });
253 add(b, g);
254
255 b = new Button("Wildcard");
256 b.addActionListener(new ActionListener() {
257 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
258 });
259 add(b, g);
260
261 b = new Button("Trackword");
262 b.addActionListener(new ActionListener() {
263 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
264 });
265 add(b, g);
266
267 b = new Button("Run");
268 b.addActionListener(new ActionListener() {
269 public void actionPerformed(ActionEvent e) { run(); }
270 });
271 add(b, g);
272
273 b = new Button("Help!");
274 b.addActionListener(new ActionListener() {
275 public void actionPerformed(ActionEvent e) { help(); }
276 });
277 add(b, g);
278
279 b = new Button("Settings...");
280 b.addActionListener(new ActionListener() {
281 public void actionPerformed(ActionEvent e) { settings(); }
282 });
283 add(b, g);
284 }
285 };
286
287 /*----- Program or applet -------------------------------------------------*/
288
289 public class AnagGUI extends Applet {
290 public static void main(String[] argv) {
291 Frame f = new Frame("Anagram solver");
292 f.addWindowListener(new WindowAdapter() {
293 public void windowClosing(WindowEvent e) { System.exit(0); }
294 });
295 AnagPanel p = new AnagPanel();
296 f.add(p);
297 f.pack();
298 f.show();
299 }
300 public AnagGUI() { super(); setLayout(new BorderLayout()); }
301 public void init() { /*add(new AnagPanel());*/ main(null); }
302 public void destroy() { /*removeAll();*/ }
303 };
304
305 /*----- That's all, folks -------------------------------------------------*/