3 * $Id: AnagGUI.java,v 1.4 2001/02/19 19:19:11 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: AnagGUI.java,v $
32 * Revision 1.4 2001/02/19 19:19:11 mdw
33 * Add `help' button. Lowercase input to the command.
35 * Revision 1.3 2001/02/16 21:46:10 mdw
36 * Use a BufferedReader, not a LineNumberReader.
38 * Revision 1.2 2001/02/07 09:10:04 mdw
39 * Add a settings panel (currently only allows the wordlist to be
40 * changed). Move the buttons down the right-hand side of the list. Add a
41 * `Run' button which passes arguments through directly.
43 * Revision 1.1 2001/02/04 19:53:07 mdw
44 * Simple GUI front-end in Java.
48 /*----- Imports -----------------------------------------------------------*/
55 import java.awt.event.*;
57 /*----- Main code ---------------------------------------------------------*/
59 class Whinge extends Frame {
60 public Whinge(String gripe) {
61 super("Error from AnagGUI");
62 setLayout(new GridBagLayout());
63 GridBagConstraints g = new GridBagConstraints();
65 addWindowListener(new WindowAdapter() {
66 public void windowClosing(WindowEvent e) { dispose(); }
69 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
70 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
71 g.weightx = g.weighty = 1;
72 g.insets = new Insets(24, 24, 24, 24);
73 add(new Label(gripe), g);
75 Button b = new Button("Bummer");
76 b.addActionListener(new ActionListener() {
77 public void actionPerformed(ActionEvent e) { dispose(); }
80 g.insets.top = 0; g.insets.bottom = 24;
87 class AnagPanel extends Panel {
93 class Settings extends Frame {
97 super("AnagGUI settings");
99 GridBagConstraints g = new GridBagConstraints();
100 this.setLayout(new GridBagLayout());
101 this.addWindowListener(new WindowAdapter() {
102 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
104 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
107 g.fill = GridBagConstraints.NONE;
108 g.gridwidth = 1; g.weightx = 0;
109 g.insets = new Insets(8, 8, 8, 8);
110 this.add(new Label("Word list"), g);
111 g.fill = GridBagConstraints.HORIZONTAL;
112 g.gridwidth = GridBagConstraints.REMAINDER; g.weightx = 1;
114 name = new TextField(20);
117 g.insets.left = 8; g.insets.top = 0; g.gridwidth = 1;
118 g.weightx = 0; this.add(new Panel(), g);
119 g.weightx = 1; this.add(new Panel(), g);
122 b = new Button("Cancel");
123 b.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
127 b = new Button("OK");
128 b.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
130 file = name.getText(); dispose(); sb = null;
139 void splat(String gripe) { new Whinge(gripe); }
140 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
142 void listen(Process p) throws IOException {
143 BufferedReader fout =
144 new BufferedReader(new InputStreamReader(p.getInputStream()));
145 BufferedReader ferr =
146 new BufferedReader(new InputStreamReader(p.getErrorStream()));
149 Vector v = new Vector();
150 while ((l = fout.readLine()) != null)
152 StringBuffer d = new StringBuffer();
153 while ((l = ferr.readLine()) != null)
154 d.append(l).append("\n");
161 String[] vv = new String[v.size()];
163 for (i = 0; i < vv.length; i++)
170 StringBuffer b = new StringBuffer();
171 b.append("anag -file ")
174 .append(word.getText());
175 Process p = Runtime.getRuntime().exec(b.toString());
177 } catch (IOException e) {
184 Process p = Runtime.getRuntime().exec("anag --help");
186 } catch (IOException e) {
191 void getlist(String tag) {
193 Vector v = new Vector();
195 v.addElement("anag");
196 v.addElement("-file");
199 v.addElement(word.getText().toLowerCase());
200 vv = new String[v.size()];
202 Process p = Runtime.getRuntime().exec(vv);
204 } catch (IOException e) {
211 setLayout(new GridBagLayout());
212 GridBagConstraints g = new GridBagConstraints();
215 file = System.getProperty("anag.dictionary", "/usr/dict/words");
218 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
219 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
220 g.weightx = 1; g.weighty = 0;
222 word = new TextField(20);
223 g.fill = GridBagConstraints.HORIZONTAL;
224 g.insets = new Insets(8, 8, 8, 8);
227 list = new java.awt.List(20);
228 g.fill = GridBagConstraints.BOTH;
230 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
231 g.weightx = g.weighty = 1;
234 g.fill = GridBagConstraints.BOTH;
235 g.weightx = 0; g.weighty = 1;
237 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
240 g.fill = GridBagConstraints.HORIZONTAL;
243 b = new Button("Anagram");
244 b.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
249 b = new Button("Subgram");
250 b.addActionListener(new ActionListener() {
251 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
255 b = new Button("Wildcard");
256 b.addActionListener(new ActionListener() {
257 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
261 b = new Button("Trackword");
262 b.addActionListener(new ActionListener() {
263 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
267 b = new Button("Run");
268 b.addActionListener(new ActionListener() {
269 public void actionPerformed(ActionEvent e) { run(); }
273 b = new Button("Help!");
274 b.addActionListener(new ActionListener() {
275 public void actionPerformed(ActionEvent e) { help(); }
279 b = new Button("Settings...");
280 b.addActionListener(new ActionListener() {
281 public void actionPerformed(ActionEvent e) { settings(); }
287 /*----- Program or applet -------------------------------------------------*/
289 public class AnagGUI extends Applet {
290 public static void main(String[] argv) {
291 Frame f = new Frame("Anagram solver");
292 f.addWindowListener(new WindowAdapter() {
293 public void windowClosing(WindowEvent e) { System.exit(0); }
295 AnagPanel p = new AnagPanel();
300 public AnagGUI() { super(); setLayout(new BorderLayout()); }
301 public void init() { /*add(new AnagPanel());*/ main(null); }
302 public void destroy() { /*removeAll();*/ }
305 /*----- That's all, folks -------------------------------------------------*/