3 * $Id: AnagGUI.java,v 1.3 2001/02/16 21:46:10 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.3 2001/02/16 21:46:10 mdw
33 * Use a BufferedReader, not a LineNumberReader.
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.
40 * Revision 1.1 2001/02/04 19:53:07 mdw
41 * Simple GUI front-end in Java.
45 /*----- Imports -----------------------------------------------------------*/
52 import java.awt.event.*;
54 /*----- Main code ---------------------------------------------------------*/
56 class Whinge extends Frame {
57 public Whinge(String gripe) {
58 super("Error from AnagGUI");
59 setLayout(new GridBagLayout());
60 GridBagConstraints g = new GridBagConstraints();
62 addWindowListener(new WindowAdapter() {
63 public void windowClosing(WindowEvent e) { dispose(); }
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);
72 Button b = new Button("Bummer");
73 b.addActionListener(new ActionListener() {
74 public void actionPerformed(ActionEvent e) { dispose(); }
77 g.insets.top = 0; g.insets.bottom = 24;
84 class AnagPanel extends Panel {
90 class Settings extends Frame {
94 super("AnagGUI settings");
96 GridBagConstraints g = new GridBagConstraints();
97 this.setLayout(new GridBagLayout());
98 this.addWindowListener(new WindowAdapter() {
99 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
101 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
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;
111 name = new TextField(20);
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);
119 b = new Button("Cancel");
120 b.addActionListener(new ActionListener() {
121 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
124 b = new Button("OK");
125 b.addActionListener(new ActionListener() {
126 public void actionPerformed(ActionEvent e) {
127 file = name.getText(); dispose(); sb = null;
136 void splat(String gripe) { new Whinge(gripe); }
137 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
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()));
146 Vector v = new Vector();
147 while ((l = fout.readLine()) != null)
149 StringBuffer d = new StringBuffer();
150 while ((l = ferr.readLine()) != null)
151 d.append(l).append("\n");
158 String[] vv = new String[v.size()];
160 for (i = 0; i < vv.length; i++)
167 StringBuffer b = new StringBuffer();
168 b.append("anag -file ")
171 .append(word.getText());
172 Process p = Runtime.getRuntime().exec(b.toString());
174 } catch (IOException e) {
179 void getlist(String tag) {
181 Vector v = new Vector();
183 v.addElement("anag");
184 v.addElement("-file");
187 v.addElement(word.getText());
188 vv = new String[v.size()];
190 Process p = Runtime.getRuntime().exec(vv);
192 } catch (IOException e) {
199 setLayout(new GridBagLayout());
200 GridBagConstraints g = new GridBagConstraints();
203 file = "/usr/dict/words";
206 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
207 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
208 g.weightx = 1; g.weighty = 0;
210 word = new TextField(20);
211 g.fill = GridBagConstraints.HORIZONTAL;
212 g.insets = new Insets(8, 8, 8, 8);
215 list = new java.awt.List(20);
216 g.fill = GridBagConstraints.BOTH;
218 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
219 g.weightx = g.weighty = 1;
222 g.fill = GridBagConstraints.BOTH;
223 g.weightx = 0; g.weighty = 1;
225 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
228 g.fill = GridBagConstraints.HORIZONTAL;
231 b = new Button("Anagram");
232 b.addActionListener(new ActionListener() {
233 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
237 b = new Button("Subgram");
238 b.addActionListener(new ActionListener() {
239 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
243 b = new Button("Wildcard");
244 b.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
249 b = new Button("Trackword");
250 b.addActionListener(new ActionListener() {
251 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
255 b = new Button("Run");
256 b.addActionListener(new ActionListener() {
257 public void actionPerformed(ActionEvent e) { run(); }
261 b = new Button("Settings...");
262 b.addActionListener(new ActionListener() {
263 public void actionPerformed(ActionEvent e) { settings(); }
269 /*----- Program or applet -------------------------------------------------*/
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); }
277 AnagPanel p = new AnagPanel();
282 public AnagGUI() { super(); setLayout(new BorderLayout()); }
283 public void init() { add(new AnagPanel()); }
284 public void destroy() { removeAll(); }
287 /*----- That's all, folks -------------------------------------------------*/