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