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