3 * $Id: AnagGUI.java,v 1.2 2001/02/07 09:10:04 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.2 2001/02/07 09:10:04 mdw
33 * Add a settings panel (currently only allows the wordlist to be
34 * changed). Move the buttons down the right-hand side of the list. Add a
35 * `Run' button which passes arguments through directly.
37 * Revision 1.1 2001/02/04 19:53:07 mdw
38 * Simple GUI front-end in Java.
42 /*----- Imports -----------------------------------------------------------*/
49 import java.awt.event.*;
51 /*----- Main code ---------------------------------------------------------*/
53 class Whinge extends Frame {
54 public Whinge(String gripe) {
55 super("Error from AnagGUI");
56 setLayout(new GridBagLayout());
57 GridBagConstraints g = new GridBagConstraints();
59 addWindowListener(new WindowAdapter() {
60 public void windowClosing(WindowEvent e) { dispose(); }
63 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
64 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
65 g.weightx = g.weighty = 1;
66 g.insets = new Insets(24, 24, 24, 24);
67 add(new Label(gripe), g);
69 Button b = new Button("Bummer");
70 b.addActionListener(new ActionListener() {
71 public void actionPerformed(ActionEvent e) { dispose(); }
74 g.insets.top = 0; g.insets.bottom = 24;
81 class AnagPanel extends Panel {
87 class Settings extends Frame {
91 super("AnagGUI settings");
93 GridBagConstraints g = new GridBagConstraints();
94 this.setLayout(new GridBagLayout());
95 this.addWindowListener(new WindowAdapter() {
96 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
98 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
101 g.fill = GridBagConstraints.NONE;
102 g.gridwidth = 1; g.weightx = 0;
103 g.insets = new Insets(8, 8, 8, 8);
104 this.add(new Label("Word list"), g);
105 g.fill = GridBagConstraints.HORIZONTAL;
106 g.gridwidth = GridBagConstraints.REMAINDER; g.weightx = 1;
108 name = new TextField(20);
111 g.insets.left = 8; g.insets.top = 0; g.gridwidth = 1;
112 g.weightx = 0; this.add(new Panel(), g);
113 g.weightx = 1; this.add(new Panel(), g);
116 b = new Button("Cancel");
117 b.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
121 b = new Button("OK");
122 b.addActionListener(new ActionListener() {
123 public void actionPerformed(ActionEvent e) {
124 file = name.getText(); dispose(); sb = null;
133 void splat(String gripe) { new Whinge(gripe); }
134 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
136 void listen(Process p) throws IOException {
137 LineNumberReader fout =
138 new LineNumberReader(new InputStreamReader(p.getInputStream()));
139 LineNumberReader ferr =
140 new LineNumberReader(new InputStreamReader(p.getErrorStream()));
143 Vector v = new Vector();
144 while ((l = fout.readLine()) != null)
146 StringBuffer d = new StringBuffer();
147 while ((l = ferr.readLine()) != null)
148 d.append(l).append("\n");
155 String[] vv = new String[v.size()];
157 for (i = 0; i < vv.length; i++)
164 StringBuffer b = new StringBuffer();
165 b.append("anag -file ")
168 .append(word.getText());
169 Process p = Runtime.getRuntime().exec(b.toString());
171 } catch (IOException e) {
176 void getlist(String tag) {
178 Vector v = new Vector();
180 v.addElement("anag");
181 v.addElement("-file");
184 v.addElement(word.getText());
185 vv = new String[v.size()];
187 Process p = Runtime.getRuntime().exec(vv);
189 } catch (IOException e) {
196 setLayout(new GridBagLayout());
197 GridBagConstraints g = new GridBagConstraints();
200 file = "/usr/dict/words";
203 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
204 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
205 g.weightx = 1; g.weighty = 0;
207 word = new TextField(20);
208 g.fill = GridBagConstraints.HORIZONTAL;
209 g.insets = new Insets(8, 8, 8, 8);
212 list = new java.awt.List(20);
213 g.fill = GridBagConstraints.BOTH;
215 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
216 g.weightx = g.weighty = 1;
219 g.fill = GridBagConstraints.BOTH;
220 g.weightx = 0; g.weighty = 1;
222 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
225 g.fill = GridBagConstraints.HORIZONTAL;
228 b = new Button("Anagram");
229 b.addActionListener(new ActionListener() {
230 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
234 b = new Button("Subgram");
235 b.addActionListener(new ActionListener() {
236 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
240 b = new Button("Wildcard");
241 b.addActionListener(new ActionListener() {
242 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
246 b = new Button("Trackword");
247 b.addActionListener(new ActionListener() {
248 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
252 b = new Button("Run");
253 b.addActionListener(new ActionListener() {
254 public void actionPerformed(ActionEvent e) { run(); }
258 b = new Button("Settings...");
259 b.addActionListener(new ActionListener() {
260 public void actionPerformed(ActionEvent e) { settings(); }
266 /*----- Program or applet -------------------------------------------------*/
268 public class AnagGUI extends Applet {
269 public static void main(String[] argv) {
270 Frame f = new Frame("Anagram solver");
271 f.addWindowListener(new WindowAdapter() {
272 public void windowClosing(WindowEvent e) { System.exit(0); }
274 AnagPanel p = new AnagPanel();
279 public AnagGUI() { super(); setLayout(new BorderLayout()); }
280 public void init() { add(new AnagPanel()); }
281 public void destroy() { removeAll(); }
284 /*----- That's all, folks -------------------------------------------------*/