3 * $Id: AnagGUI.java,v 1.5 2002/08/11 12:58:09 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.5 2002/08/11 12:58:09 mdw
33 * Added support for regular expression matching, if supported by the C
36 * Revision 1.4 2001/02/19 19:19:11 mdw
37 * Add `help' button. Lowercase input to the command.
39 * Revision 1.3 2001/02/16 21:46:10 mdw
40 * Use a BufferedReader, not a LineNumberReader.
42 * Revision 1.2 2001/02/07 09:10:04 mdw
43 * Add a settings panel (currently only allows the wordlist to be
44 * changed). Move the buttons down the right-hand side of the list. Add a
45 * `Run' button which passes arguments through directly.
47 * Revision 1.1 2001/02/04 19:53:07 mdw
48 * Simple GUI front-end in Java.
52 /*----- Imports -----------------------------------------------------------*/
59 import java.awt.event.*;
61 /*----- Main code ---------------------------------------------------------*/
63 class Whinge extends Frame {
64 public Whinge(String gripe) {
65 super("Error from AnagGUI");
66 setLayout(new GridBagLayout());
67 GridBagConstraints g = new GridBagConstraints();
69 addWindowListener(new WindowAdapter() {
70 public void windowClosing(WindowEvent e) { dispose(); }
73 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
74 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
75 g.weightx = g.weighty = 1;
76 g.insets = new Insets(24, 24, 24, 24);
77 add(new Label(gripe), g);
79 Button b = new Button("Bummer");
80 b.addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent e) { dispose(); }
84 g.insets.top = 0; g.insets.bottom = 24;
91 class AnagPanel extends Panel {
97 class Settings extends Frame {
101 super("AnagGUI settings");
103 GridBagConstraints g = new GridBagConstraints();
104 this.setLayout(new GridBagLayout());
105 this.addWindowListener(new WindowAdapter() {
106 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
108 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
111 g.fill = GridBagConstraints.NONE;
112 g.gridwidth = 1; g.weightx = 0;
113 g.insets = new Insets(8, 8, 8, 8);
114 this.add(new Label("Word list"), g);
115 g.fill = GridBagConstraints.HORIZONTAL;
116 g.gridwidth = GridBagConstraints.REMAINDER; g.weightx = 1;
118 name = new TextField(20);
121 g.insets.left = 8; g.insets.top = 0; g.gridwidth = 1;
122 g.weightx = 0; this.add(new Panel(), g);
123 g.weightx = 1; this.add(new Panel(), g);
126 b = new Button("Cancel");
127 b.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
131 b = new Button("OK");
132 b.addActionListener(new ActionListener() {
133 public void actionPerformed(ActionEvent e) {
134 file = name.getText(); dispose(); sb = null;
143 void splat(String gripe) { new Whinge(gripe); }
144 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
146 void listen(Process p) throws IOException {
147 BufferedReader fout =
148 new BufferedReader(new InputStreamReader(p.getInputStream()));
149 BufferedReader ferr =
150 new BufferedReader(new InputStreamReader(p.getErrorStream()));
153 Vector v = new Vector();
154 while ((l = fout.readLine()) != null)
156 StringBuffer d = new StringBuffer();
157 while ((l = ferr.readLine()) != null)
158 d.append(l).append("\n");
165 String[] vv = new String[v.size()];
167 for (i = 0; i < vv.length; i++)
174 StringBuffer b = new StringBuffer();
175 b.append("anag -file ")
178 .append(word.getText());
179 Process p = Runtime.getRuntime().exec(b.toString());
181 } catch (IOException e) {
188 Process p = Runtime.getRuntime().exec("anag --help");
190 } catch (IOException e) {
195 void getlist(String tag) {
197 Vector v = new Vector();
199 v.addElement("anag");
200 v.addElement("-file");
203 v.addElement(word.getText().toLowerCase());
204 vv = new String[v.size()];
206 Process p = Runtime.getRuntime().exec(vv);
208 } catch (IOException e) {
215 setLayout(new GridBagLayout());
216 GridBagConstraints g = new GridBagConstraints();
219 file = System.getProperty("anag.dictionary", "/usr/dict/words");
222 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
223 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
224 g.weightx = 1; g.weighty = 0;
226 word = new TextField(20);
227 g.fill = GridBagConstraints.HORIZONTAL;
228 g.insets = new Insets(8, 8, 8, 8);
231 list = new java.awt.List(20);
232 g.fill = GridBagConstraints.BOTH;
234 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
235 g.weightx = g.weighty = 1;
238 g.fill = GridBagConstraints.BOTH;
239 g.weightx = 0; g.weighty = 1;
241 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
244 g.fill = GridBagConstraints.HORIZONTAL;
247 b = new Button("Anagram");
248 b.addActionListener(new ActionListener() {
249 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
253 b = new Button("Subgram");
254 b.addActionListener(new ActionListener() {
255 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
259 b = new Button("Wildcard");
260 b.addActionListener(new ActionListener() {
261 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
265 b = new Button("Regexp");
266 b.addActionListener(new ActionListener() {
267 public void actionPerformed(ActionEvent e) { getlist("-regexp"); }
271 b = new Button("Trackword");
272 b.addActionListener(new ActionListener() {
273 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
277 b = new Button("Run");
278 b.addActionListener(new ActionListener() {
279 public void actionPerformed(ActionEvent e) { run(); }
283 b = new Button("Help!");
284 b.addActionListener(new ActionListener() {
285 public void actionPerformed(ActionEvent e) { help(); }
289 b = new Button("Settings...");
290 b.addActionListener(new ActionListener() {
291 public void actionPerformed(ActionEvent e) { settings(); }
297 /*----- Program or applet -------------------------------------------------*/
299 public class AnagGUI extends Applet {
300 public static void main(String[] argv) {
301 Frame f = new Frame("Anagram solver");
302 f.addWindowListener(new WindowAdapter() {
303 public void windowClosing(WindowEvent e) { System.exit(0); }
305 AnagPanel p = new AnagPanel();
310 public AnagGUI() { super(); setLayout(new BorderLayout()); }
311 public void init() { /*add(new AnagPanel());*/ main(null); }
312 public void destroy() { /*removeAll();*/ }
315 /*----- That's all, folks -------------------------------------------------*/