3 * $Id: AnagGUI.java,v 1.7 2004/04/08 01:36:18 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 /*----- Imports -----------------------------------------------------------*/
36 import java.awt.event.*;
38 /*----- Main code ---------------------------------------------------------*/
40 class Whinge extends Frame {
41 public Whinge(String gripe) {
42 super("Error from AnagGUI");
43 setLayout(new GridBagLayout());
44 GridBagConstraints g = new GridBagConstraints();
46 addWindowListener(new WindowAdapter() {
47 public void windowClosing(WindowEvent e) { dispose(); }
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);
56 Button b = new Button("Bummer");
57 b.addActionListener(new ActionListener() {
58 public void actionPerformed(ActionEvent e) { dispose(); }
61 g.insets.top = 0; g.insets.bottom = 24;
68 class AnagPanel extends Panel {
74 class Settings extends Frame {
78 super("AnagGUI settings");
80 GridBagConstraints g = new GridBagConstraints();
81 this.setLayout(new GridBagLayout());
82 this.addWindowListener(new WindowAdapter() {
83 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
85 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
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;
95 name = new TextField(20);
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);
103 b = new Button("Cancel");
104 b.addActionListener(new ActionListener() {
105 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
108 b = new Button("OK");
109 b.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 file = name.getText(); dispose(); sb = null;
120 void splat(String gripe) { new Whinge(gripe); }
121 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
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()));
130 Vector v = new Vector();
131 while ((l = fout.readLine()) != null)
133 StringBuffer d = new StringBuffer();
134 while ((l = ferr.readLine()) != null)
135 d.append(l).append("\n");
142 String[] vv = new String[v.size()];
144 for (i = 0; i < vv.length; i++)
151 StringBuffer b = new StringBuffer();
152 b.append("anag -file ")
155 .append(word.getText());
156 Process p = Runtime.getRuntime().exec(b.toString());
158 } catch (IOException e) {
165 Process p = Runtime.getRuntime().exec("anag --help");
167 } catch (IOException e) {
172 void getlist(String tag) {
174 Vector v = new Vector();
176 v.addElement("anag");
177 v.addElement("-file");
180 v.addElement(word.getText().toLowerCase());
181 vv = new String[v.size()];
183 Process p = Runtime.getRuntime().exec(vv);
185 } catch (IOException e) {
192 setLayout(new GridBagLayout());
193 GridBagConstraints g = new GridBagConstraints();
196 file = System.getProperty("anag.dictionary", "/usr/dict/words");
199 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
200 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
201 g.weightx = 1; g.weighty = 0;
203 word = new TextField(20);
204 g.fill = GridBagConstraints.HORIZONTAL;
205 g.insets = new Insets(8, 8, 8, 8);
208 list = new java.awt.List(20);
209 g.fill = GridBagConstraints.BOTH;
211 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
212 g.weightx = g.weighty = 1;
215 g.fill = GridBagConstraints.BOTH;
216 g.weightx = 0; g.weighty = 1;
218 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
221 g.fill = GridBagConstraints.HORIZONTAL;
224 b = new Button("Anagram");
225 b.addActionListener(new ActionListener() {
226 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
230 b = new Button("Subgram");
231 b.addActionListener(new ActionListener() {
232 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
236 b = new Button("Wildcard");
237 b.addActionListener(new ActionListener() {
238 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
242 b = new Button("Regular expression");
243 b.addActionListener(new ActionListener() {
244 public void actionPerformed(ActionEvent e) { getlist("-regexp"); }
248 b = new Button("Perl regexp");
249 b.addActionListener(new ActionListener() {
250 public void actionPerformed(ActionEvent e) { getlist("-pcre"); }
254 b = new Button("Monoalphabetic");
255 b.addActionListener(new ActionListener() {
256 public void actionPerformed(ActionEvent e) { getlist("-mono"); }
260 b = new Button("Trackword");
261 b.addActionListener(new ActionListener() {
262 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
266 b = new Button("Run");
267 b.addActionListener(new ActionListener() {
268 public void actionPerformed(ActionEvent e) { run(); }
272 b = new Button("Help!");
273 b.addActionListener(new ActionListener() {
274 public void actionPerformed(ActionEvent e) { help(); }
278 b = new Button("Settings...");
279 b.addActionListener(new ActionListener() {
280 public void actionPerformed(ActionEvent e) { settings(); }
286 /*----- Program or applet -------------------------------------------------*/
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); }
294 AnagPanel p = new AnagPanel();
299 public AnagGUI() { super(); setLayout(new BorderLayout()); }
300 public void init() { /*add(new AnagPanel());*/ main(null); }
301 public void destroy() { /*removeAll();*/ }
304 /*----- That's all, folks -------------------------------------------------*/