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