chiark / gitweb /
Extract Subversion ignore data.
[anag] / AnagGUI.java
1 /* -*-java-*-
2  *
3  * $Id: AnagGUI.java,v 1.7 2004/04/08 01:36:18 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 /*----- Imports -----------------------------------------------------------*/
30
31 import java.lang.*;
32 import java.util.*;
33 import java.io.*;
34 import java.awt.*;
35 import java.applet.*;
36 import java.awt.event.*;
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 class Whinge extends Frame {
41   public Whinge(String gripe) {
42     super("Error from AnagGUI");
43     setLayout(new GridBagLayout());
44     GridBagConstraints g = new GridBagConstraints();
45
46     addWindowListener(new WindowAdapter() {
47       public void windowClosing(WindowEvent e) { dispose(); }
48     });
49
50     g.gridx = g.gridy = GridBagConstraints.RELATIVE;
51     g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
52     g.weightx = g.weighty = 1;
53     g.insets = new Insets(24, 24, 24, 24);
54     add(new Label(gripe), g);
55
56     Button b = new Button("Bummer");
57     b.addActionListener(new ActionListener() {
58       public void actionPerformed(ActionEvent e) { dispose(); }
59     });
60     g.weighty = 0;
61     g.insets.top = 0; g.insets.bottom = 24;
62     add(b, g);
63     pack();
64     show();    
65   }
66 };
67
68 class AnagPanel extends Panel {
69   TextField word;
70   java.awt.List list;
71   String file;
72   Settings sb;
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.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 {
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 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     }
161   }
162
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
172   void getlist(String tag) {
173     try {
174       Vector v = new Vector();
175       String[] vv;
176       v.addElement("anag");
177       v.addElement("-file");
178       v.addElement(file);
179       v.addElement(tag);
180       v.addElement(word.getText().toLowerCase());
181       vv = new String[v.size()];
182       v.copyInto(vv);
183       Process p = Runtime.getRuntime().exec(vv);
184       listen(p);
185     } catch (IOException e) {
186       splat(e.toString());
187     }
188   }
189
190   public AnagPanel() {
191     super();
192     setLayout(new GridBagLayout());
193     GridBagConstraints g = new GridBagConstraints();
194     Button b;
195
196     file = System.getProperty("anag.dictionary", "/usr/dict/words");
197     sb = null;
198
199     g.gridx = g.gridy = GridBagConstraints.RELATIVE;
200     g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
201     g.weightx = 1; g.weighty = 0;
202
203     word = new TextField(20);
204     g.fill = GridBagConstraints.HORIZONTAL;
205     g.insets = new Insets(8, 8, 8, 8);
206     add(word, g);
207
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;
217     g.insets.left = 0;
218     g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
219     add(new Panel(), g);
220
221     g.fill = GridBagConstraints.HORIZONTAL;
222     g.weighty = 0;
223
224     b = new Button("Anagram");
225     b.addActionListener(new ActionListener() {
226       public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
227     });
228     add(b, g);
229
230     b = new Button("Subgram");
231     b.addActionListener(new ActionListener() {
232       public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
233     });
234     add(b, g);
235
236     b = new Button("Wildcard");
237     b.addActionListener(new ActionListener() {
238       public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
239     });
240     add(b, g);
241
242     b = new Button("Regular expression");
243     b.addActionListener(new ActionListener() {
244       public void actionPerformed(ActionEvent e) { getlist("-regexp"); }
245     });
246     add(b, g);
247
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
260     b = new Button("Trackword");
261     b.addActionListener(new ActionListener() {
262       public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
263     });
264     add(b, g);
265
266     b = new Button("Run");
267     b.addActionListener(new ActionListener() {
268       public void actionPerformed(ActionEvent e) { run(); }
269     });
270     add(b, g);
271
272     b = new Button("Help!");
273     b.addActionListener(new ActionListener() {
274       public void actionPerformed(ActionEvent e) { help(); }
275     });
276     add(b, g);
277
278     b = new Button("Settings...");
279     b.addActionListener(new ActionListener() {
280       public void actionPerformed(ActionEvent e) { settings(); }
281     });
282     add(b, g);
283   }
284 };
285
286 /*----- Program or applet -------------------------------------------------*/
287
288 public 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()); }
300   public void init() { /*add(new AnagPanel());*/ main(null); }
301   public void destroy() { /*removeAll();*/ }
302 };
303
304 /*----- That's all, folks -------------------------------------------------*/