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