3 * $Id: AnagGUI.java,v 1.6 2003/11/29 23:38:37 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.6 2003/11/29 23:38:37 mdw
35 * Revision 1.5 2002/08/11 12:58:09 mdw
36 * Added support for regular expression matching, if supported by the C
39 * Revision 1.4 2001/02/19 19:19:11 mdw
40 * Add `help' button. Lowercase input to the command.
42 * Revision 1.3 2001/02/16 21:46:10 mdw
43 * Use a BufferedReader, not a LineNumberReader.
45 * Revision 1.2 2001/02/07 09:10:04 mdw
46 * Add a settings panel (currently only allows the wordlist to be
47 * changed). Move the buttons down the right-hand side of the list. Add a
48 * `Run' button which passes arguments through directly.
50 * Revision 1.1 2001/02/04 19:53:07 mdw
51 * Simple GUI front-end in Java.
55 /*----- Imports -----------------------------------------------------------*/
62 import java
.awt
.event
.*;
64 /*----- Main code ---------------------------------------------------------*/
66 class Whinge
extends Frame
{
67 public Whinge(String gripe
) {
68 super("Error from AnagGUI");
69 setLayout(new GridBagLayout());
70 GridBagConstraints g
= new GridBagConstraints();
72 addWindowListener(new WindowAdapter() {
73 public void windowClosing(WindowEvent e
) { dispose(); }
76 g
.gridx
= g
.gridy
= GridBagConstraints
.RELATIVE
;
77 g
.gridwidth
= GridBagConstraints
.REMAINDER
; g
.gridheight
= 1;
78 g
.weightx
= g
.weighty
= 1;
79 g
.insets
= new Insets(24, 24, 24, 24);
80 add(new Label(gripe
), g
);
82 Button b
= new Button("Bummer");
83 b
.addActionListener(new ActionListener() {
84 public void actionPerformed(ActionEvent e
) { dispose(); }
87 g
.insets
.top
= 0; g
.insets
.bottom
= 24;
94 class AnagPanel
extends Panel
{
100 class Settings
extends Frame
{
104 super("AnagGUI settings");
106 GridBagConstraints g
= new GridBagConstraints();
107 this.setLayout(new GridBagLayout());
108 this.addWindowListener(new WindowAdapter() {
109 public void windowClosing(WindowEvent e
) { dispose(); sb
= null
; }
111 g
.gridx
= g
.gridy
= GridBagConstraints
.RELATIVE
;
114 g
.fill
= GridBagConstraints
.NONE
;
115 g
.gridwidth
= 1; g
.weightx
= 0;
116 g
.insets
= new Insets(8, 8, 8, 8);
117 this.add(new Label("Word list"), g
);
118 g
.fill
= GridBagConstraints
.HORIZONTAL
;
119 g
.gridwidth
= GridBagConstraints
.REMAINDER
; g
.weightx
= 1;
121 name
= new TextField(20);
124 g
.insets
.left
= 8; g
.insets
.top
= 0; g
.gridwidth
= 1;
125 g
.weightx
= 0; this.add(new Panel(), g
);
126 g
.weightx
= 1; this.add(new Panel(), g
);
129 b
= new Button("Cancel");
130 b
.addActionListener(new ActionListener() {
131 public void actionPerformed(ActionEvent e
) { dispose(); sb
= null
; }
134 b
= new Button("OK");
135 b
.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e
) {
137 file
= name
.getText(); dispose(); sb
= null
;
146 void splat(String gripe
) { new Whinge(gripe
); }
147 void settings() { if (sb
!= null
) sb
.toFront(); else sb
= new Settings(); }
149 void listen(Process p
) throws IOException
{
150 BufferedReader fout
=
151 new BufferedReader(new InputStreamReader(p
.getInputStream()));
152 BufferedReader ferr
=
153 new BufferedReader(new InputStreamReader(p
.getErrorStream()));
156 Vector v
= new Vector();
157 while ((l
= fout
.readLine()) != null
)
159 StringBuffer d
= new StringBuffer();
160 while ((l
= ferr
.readLine()) != null
)
161 d
.append(l
).append("\n");
168 String
[] vv
= new String
[v
.size()];
170 for (i
= 0; i
< vv
.length
; i
++)
177 StringBuffer b
= new StringBuffer();
178 b
.append("anag -file ")
181 .append(word
.getText());
182 Process p
= Runtime
.getRuntime().exec(b
.toString());
184 } catch (IOException e
) {
191 Process p
= Runtime
.getRuntime().exec("anag --help");
193 } catch (IOException e
) {
198 void getlist(String tag
) {
200 Vector v
= new Vector();
202 v
.addElement("anag");
203 v
.addElement("-file");
206 v
.addElement(word
.getText().toLowerCase());
207 vv
= new String
[v
.size()];
209 Process p
= Runtime
.getRuntime().exec(vv
);
211 } catch (IOException e
) {
218 setLayout(new GridBagLayout());
219 GridBagConstraints g
= new GridBagConstraints();
222 file
= System
.getProperty("anag.dictionary", "/usr/dict/words");
225 g
.gridx
= g
.gridy
= GridBagConstraints
.RELATIVE
;
226 g
.gridwidth
= GridBagConstraints
.REMAINDER
; g
.gridheight
= 1;
227 g
.weightx
= 1; g
.weighty
= 0;
229 word
= new TextField(20);
230 g
.fill
= GridBagConstraints
.HORIZONTAL
;
231 g
.insets
= new Insets(8, 8, 8, 8);
234 list
= new java
.awt
.List(20);
235 g
.fill
= GridBagConstraints
.BOTH
;
237 g
.gridwidth
= 1; g
.gridheight
= GridBagConstraints
.REMAINDER
;
238 g
.weightx
= g
.weighty
= 1;
241 g
.fill
= GridBagConstraints
.BOTH
;
242 g
.weightx
= 0; g
.weighty
= 1;
244 g
.gridheight
= 1; g
.gridwidth
= GridBagConstraints
.REMAINDER
;
247 g
.fill
= GridBagConstraints
.HORIZONTAL
;
250 b
= new Button("Anagram");
251 b
.addActionListener(new ActionListener() {
252 public void actionPerformed(ActionEvent e
) { getlist("-anagram"); }
256 b
= new Button("Subgram");
257 b
.addActionListener(new ActionListener() {
258 public void actionPerformed(ActionEvent e
) { getlist("-subgram"); }
262 b
= new Button("Wildcard");
263 b
.addActionListener(new ActionListener() {
264 public void actionPerformed(ActionEvent e
) { getlist("-wildcard"); }
268 b
= new Button("Regular expression");
269 b
.addActionListener(new ActionListener() {
270 public void actionPerformed(ActionEvent e
) { getlist("-regexp"); }
274 b
= new Button("Perl regexp");
275 b
.addActionListener(new ActionListener() {
276 public void actionPerformed(ActionEvent e
) { getlist("-pcre"); }
280 b
= new Button("Monoalphabetic");
281 b
.addActionListener(new ActionListener() {
282 public void actionPerformed(ActionEvent e
) { getlist("-mono"); }
286 b
= new Button("Trackword");
287 b
.addActionListener(new ActionListener() {
288 public void actionPerformed(ActionEvent e
) { getlist("-trackword"); }
292 b
= new Button("Run");
293 b
.addActionListener(new ActionListener() {
294 public void actionPerformed(ActionEvent e
) { run(); }
298 b
= new Button("Help!");
299 b
.addActionListener(new ActionListener() {
300 public void actionPerformed(ActionEvent e
) { help(); }
304 b
= new Button("Settings...");
305 b
.addActionListener(new ActionListener() {
306 public void actionPerformed(ActionEvent e
) { settings(); }
312 /*----- Program or applet -------------------------------------------------*/
314 public class AnagGUI
extends Applet
{
315 public static void main(String
[] argv
) {
316 Frame f
= new Frame("Anagram solver");
317 f
.addWindowListener(new WindowAdapter() {
318 public void windowClosing(WindowEvent e
) { System
.exit(0); }
320 AnagPanel p
= new AnagPanel();
325 public AnagGUI() { super(); setLayout(new BorderLayout()); }
326 public void init() { /*add(new AnagPanel());*/ main(null
); }
327 public void destroy() { /*removeAll();*/ }
330 /*----- That's all, folks -------------------------------------------------*/