5 * (c) 2001 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Anag: a simple wordgame helper.
12 * Anag is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Anag is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Anag; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Imports -----------------------------------------------------------*/
34 import java.awt.event.*;
36 /*----- Main code ---------------------------------------------------------*/
38 class Whinge extends Frame {
39 public Whinge(String gripe) {
40 super("Error from AnagGUI");
41 setLayout(new GridBagLayout());
42 GridBagConstraints g = new GridBagConstraints();
44 addWindowListener(new WindowAdapter() {
45 public void windowClosing(WindowEvent e) { dispose(); }
48 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
49 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
50 g.weightx = g.weighty = 1;
51 g.insets = new Insets(24, 24, 24, 24);
52 add(new Label(gripe), g);
54 Button b = new Button("Bummer");
55 b.addActionListener(new ActionListener() {
56 public void actionPerformed(ActionEvent e) { dispose(); }
59 g.insets.top = 0; g.insets.bottom = 24;
66 class AnagPanel extends Panel {
70 String anag = System.getProperty("anag.program", "anag");
71 String file = System.getProperty("anag.dictionary",
72 "/usr/share/dict/words");
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;
116 this.setVisible(true);
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<String> v = new Vector<String>();
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();
153 .append(" -file ").append(file)
154 .append(" ").append(word.getText());
155 Process p = Runtime.getRuntime().exec(b.toString());
157 } catch (IOException e) {
164 Process p = Runtime.getRuntime().exec(anag + " --help");
166 } catch (IOException e) {
171 void getlist(String tag) {
173 Vector<String> v = new Vector<String>();
176 v.addElement("-file");
179 v.addElement(word.getText().toLowerCase());
180 vv = new String[v.size()];
182 Process p = Runtime.getRuntime().exec(vv);
184 } catch (IOException e) {
191 setLayout(new GridBagLayout());
192 GridBagConstraints g = new GridBagConstraints();
197 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
198 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
199 g.weightx = 1; g.weighty = 0;
201 word = new TextField(20);
202 g.fill = GridBagConstraints.HORIZONTAL;
203 g.insets = new Insets(8, 8, 8, 8);
206 list = new java.awt.List(20);
207 g.fill = GridBagConstraints.BOTH;
209 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
210 g.weightx = g.weighty = 1;
213 g.fill = GridBagConstraints.BOTH;
214 g.weightx = 0; g.weighty = 1;
216 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
219 g.fill = GridBagConstraints.HORIZONTAL;
222 b = new Button("Anagram");
223 b.addActionListener(new ActionListener() {
224 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
228 b = new Button("Subgram");
229 b.addActionListener(new ActionListener() {
230 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
234 b = new Button("Wildcard");
235 b.addActionListener(new ActionListener() {
236 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
240 b = new Button("Regular expression");
241 b.addActionListener(new ActionListener() {
242 public void actionPerformed(ActionEvent e) { getlist("-regexp"); }
246 b = new Button("Perl regexp");
247 b.addActionListener(new ActionListener() {
248 public void actionPerformed(ActionEvent e) { getlist("-pcre"); }
252 b = new Button("Monoalphabetic");
253 b.addActionListener(new ActionListener() {
254 public void actionPerformed(ActionEvent e) { getlist("-mono"); }
258 b = new Button("Trackword");
259 b.addActionListener(new ActionListener() {
260 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
264 b = new Button("Run");
265 b.addActionListener(new ActionListener() {
266 public void actionPerformed(ActionEvent e) { run(); }
270 b = new Button("Help!");
271 b.addActionListener(new ActionListener() {
272 public void actionPerformed(ActionEvent e) { help(); }
276 b = new Button("Settings...");
277 b.addActionListener(new ActionListener() {
278 public void actionPerformed(ActionEvent e) { settings(); }
284 /*----- Program or applet -------------------------------------------------*/
286 public class AnagGUI extends Applet {
287 public static void main(String[] argv) {
288 Frame f = new Frame("Anagram solver");
289 f.addWindowListener(new WindowAdapter() {
290 public void windowClosing(WindowEvent e) { System.exit(0); }
292 AnagPanel p = new AnagPanel();
297 public AnagGUI() { super(); setLayout(new BorderLayout()); }
298 public void init() { /*add(new AnagPanel());*/ main(null); }
299 public void destroy() { /*removeAll();*/ }
302 /*----- That's all, folks -------------------------------------------------*/