chiark / gitweb /
Quick lick of paint before we really get started.
[anag] / AnagGUI.java
1 /* -*-java-*-
2  *
3  * Front-end GUI
4  *
5  * (c) 2001 Mark Wooding
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Anag: a simple wordgame helper.
11  *
12  * Anag is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Anag is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Anag; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Imports -----------------------------------------------------------*/
28
29 import java.lang.*;
30 import java.util.*;
31 import java.io.*;
32 import java.awt.*;
33 import java.applet.*;
34 import java.awt.event.*;
35
36 /*----- Main code ---------------------------------------------------------*/
37
38 class Whinge extends Frame {
39   public Whinge(String gripe) {
40     super("Error from AnagGUI");
41     setLayout(new GridBagLayout());
42     GridBagConstraints g = new GridBagConstraints();
43
44     addWindowListener(new WindowAdapter() {
45       public void windowClosing(WindowEvent e) { dispose(); }
46     });
47
48     g.gridx = g.gridy = GridBagConstraints.RELATIVE;
49     g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
50     g.weightx = g.weighty = 1;
51     g.insets = new Insets(24, 24, 24, 24);
52     add(new Label(gripe), g);
53
54     Button b = new Button("Bummer");
55     b.addActionListener(new ActionListener() {
56       public void actionPerformed(ActionEvent e) { dispose(); }
57     });
58     g.weighty = 0;
59     g.insets.top = 0; g.insets.bottom = 24;
60     add(b, g);
61     pack();
62     setVisible(true);
63   }
64 };
65
66 class AnagPanel extends Panel {
67   TextField word;
68   java.awt.List list;
69   Settings sb;
70   String anag = System.getProperty("anag.program", "anag");
71   String file = System.getProperty("anag.dictionary",
72                                    "/usr/share/dict/words");
73
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.setVisible(true);
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 {
124     BufferedReader fout =
125       new BufferedReader(new InputStreamReader(p.getInputStream()));
126     BufferedReader ferr =
127       new BufferedReader(new InputStreamReader(p.getErrorStream()));
128
129     String l;
130     Vector<String> v = new Vector<String>();
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)
153        .append(" -file ").append(file)
154        .append(" ").append(word.getText());
155       Process p = Runtime.getRuntime().exec(b.toString());
156       listen(p);
157     } catch (IOException e) {
158       splat(e.toString());
159     }
160   }
161
162   void help() {
163     try {
164       Process p = Runtime.getRuntime().exec(anag + " --help");
165       listen(p);
166     } catch (IOException e) {
167       splat(e.toString());
168     }
169   }
170
171   void getlist(String tag) {
172     try {
173       Vector<String> v = new Vector<String>();
174       String[] vv;
175       v.addElement(anag);
176       v.addElement("-file");
177       v.addElement(file);
178       v.addElement(tag);
179       v.addElement(word.getText().toLowerCase());
180       vv = new String[v.size()];
181       v.copyInto(vv);
182       Process p = Runtime.getRuntime().exec(vv);
183       listen(p);
184     } catch (IOException e) {
185       splat(e.toString());
186     }
187   }
188
189   public AnagPanel() {
190     super();
191     setLayout(new GridBagLayout());
192     GridBagConstraints g = new GridBagConstraints();
193     Button b;
194
195     sb = null;
196
197     g.gridx = g.gridy = GridBagConstraints.RELATIVE;
198     g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
199     g.weightx = 1; g.weighty = 0;
200
201     word = new TextField(20);
202     g.fill = GridBagConstraints.HORIZONTAL;
203     g.insets = new Insets(8, 8, 8, 8);
204     add(word, g);
205
206     list = new java.awt.List(20);
207     g.fill = GridBagConstraints.BOTH;
208     g.insets.top = 0;
209     g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
210     g.weightx = g.weighty = 1;
211     add(list, g);
212
213     g.fill = GridBagConstraints.BOTH;
214     g.weightx = 0; g.weighty = 1;
215     g.insets.left = 0;
216     g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
217     add(new Panel(), g);
218
219     g.fill = GridBagConstraints.HORIZONTAL;
220     g.weighty = 0;
221
222     b = new Button("Anagram");
223     b.addActionListener(new ActionListener() {
224       public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
225     });
226     add(b, g);
227
228     b = new Button("Subgram");
229     b.addActionListener(new ActionListener() {
230       public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
231     });
232     add(b, g);
233
234     b = new Button("Wildcard");
235     b.addActionListener(new ActionListener() {
236       public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
237     });
238     add(b, g);
239
240     b = new Button("Regular expression");
241     b.addActionListener(new ActionListener() {
242       public void actionPerformed(ActionEvent e) { getlist("-regexp"); }
243     });
244     add(b, g);
245
246     b = new Button("Perl regexp");
247     b.addActionListener(new ActionListener() {
248       public void actionPerformed(ActionEvent e) { getlist("-pcre"); }
249     });
250     add(b, g);
251
252     b = new Button("Monoalphabetic");
253     b.addActionListener(new ActionListener() {
254       public void actionPerformed(ActionEvent e) { getlist("-mono"); }
255     });
256     add(b, g);
257
258     b = new Button("Trackword");
259     b.addActionListener(new ActionListener() {
260       public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
261     });
262     add(b, g);
263
264     b = new Button("Run");
265     b.addActionListener(new ActionListener() {
266       public void actionPerformed(ActionEvent e) { run(); }
267     });
268     add(b, g);
269
270     b = new Button("Help!");
271     b.addActionListener(new ActionListener() {
272       public void actionPerformed(ActionEvent e) { help(); }
273     });
274     add(b, g);
275
276     b = new Button("Settings...");
277     b.addActionListener(new ActionListener() {
278       public void actionPerformed(ActionEvent e) { settings(); }
279     });
280     add(b, g);
281   }
282 };
283
284 /*----- Program or applet -------------------------------------------------*/
285
286 public class AnagGUI extends Applet {
287   public static void main(String[] argv) {
288     Frame f = new Frame("Anagram solver");
289     f.addWindowListener(new WindowAdapter() {
290       public void windowClosing(WindowEvent e) { System.exit(0); }
291     });
292     AnagPanel p = new AnagPanel();
293     f.add(p);
294     f.pack();
295     f.setVisible(true);
296   }
297   public AnagGUI() { super(); setLayout(new BorderLayout()); }
298   public void init() { /*add(new AnagPanel());*/ main(null); }
299   public void destroy() { /*removeAll();*/ }
300 };
301
302 /*----- That's all, folks -------------------------------------------------*/