pcre.c, etc.: Support the PCRE2 library.
[anag] / AnagGUI.java
CommitLineData
dc460403 1/* -*-java-*-
2 *
dc460403 3 * Front-end GUI
4 *
5 * (c) 2001 Mark Wooding
6 */
7
0279756e 8/*----- Licensing notice --------------------------------------------------*
dc460403 9 *
10 * This file is part of Anag: a simple wordgame helper.
11 *
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.
0279756e 16 *
dc460403 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.
0279756e 21 *
dc460403 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.
25 */
26
dc460403 27/*----- Imports -----------------------------------------------------------*/
28
29import java.lang.*;
30import java.util.*;
31import java.io.*;
32import java.awt.*;
33import java.applet.*;
34import java.awt.event.*;
35
36/*----- Main code ---------------------------------------------------------*/
37
38class Whinge extends Frame {
39 public Whinge(String gripe) {
7b468eed 40 super("Error from AnagGUI");
41 setLayout(new GridBagLayout());
42 GridBagConstraints g = new GridBagConstraints();
dc460403 43
44 addWindowListener(new WindowAdapter() {
7b468eed 45 public void windowClosing(WindowEvent e) { dispose(); }
dc460403 46 });
47
dc460403 48 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
49 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
50 g.weightx = g.weighty = 1;
7b468eed 51 g.insets = new Insets(24, 24, 24, 24);
52 add(new Label(gripe), g);
dc460403 53
54 Button b = new Button("Bummer");
55 b.addActionListener(new ActionListener() {
7b468eed 56 public void actionPerformed(ActionEvent e) { dispose(); }
dc460403 57 });
58 g.weighty = 0;
59 g.insets.top = 0; g.insets.bottom = 24;
7b468eed 60 add(b, g);
dc460403 61 pack();
0279756e 62 setVisible(true);
dc460403 63 }
7b468eed 64};
dc460403 65
66class AnagPanel extends Panel {
67 TextField word;
68 java.awt.List list;
7b468eed 69 Settings sb;
0279756e
MW
70 String anag = System.getProperty("anag.program", "anag");
71 String file = System.getProperty("anag.dictionary",
72 "/usr/share/dict/words");
dc460403 73
7b468eed 74 class Settings extends Frame {
75 TextField name;
0279756e 76
7b468eed 77 public Settings() {
78 super("AnagGUI settings");
79 Button b;
80 GridBagConstraints g = new GridBagConstraints();
81 this.setLayout(new GridBagLayout());
82 this.addWindowListener(new WindowAdapter() {
83 public void windowClosing(WindowEvent e) { dispose(); sb = null; }
84 });
85 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
86 g.gridheight = 1;
87 g.weighty = 0;
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;
94 g.insets.left = 0;
95 name = new TextField(20);
96 name.setText(file);
97 this.add(name, g);
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);
101 g.weightx = 0;
102 g.insets.left = 0;
103 b = new Button("Cancel");
104 b.addActionListener(new ActionListener() {
105 public void actionPerformed(ActionEvent e) { dispose(); sb = null; }
106 });
107 this.add(b, g);
108 b = new Button("OK");
109 b.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 file = name.getText(); dispose(); sb = null;
112 }
113 });
114 this.add(b, g);
115 this.pack();
0279756e 116 this.setVisible(true);
7b468eed 117 }
118 };
119
120 void splat(String gripe) { new Whinge(gripe); }
121 void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
122
123 void listen(Process p) throws IOException {
162ffb5e 124 BufferedReader fout =
125 new BufferedReader(new InputStreamReader(p.getInputStream()));
126 BufferedReader ferr =
127 new BufferedReader(new InputStreamReader(p.getErrorStream()));
7b468eed 128
129 String l;
0279756e 130 Vector<String> v = new Vector<String>();
7b468eed 131 while ((l = fout.readLine()) != null)
132 v.addElement(l);
133 StringBuffer d = new StringBuffer();
134 while ((l = ferr.readLine()) != null)
135 d.append(l).append("\n");
136 l = d.toString();
137 if (l.length() > 0)
138 splat(l);
139 else {
140 list.removeAll();
141 int i;
142 String[] vv = new String[v.size()];
143 v.copyInto(vv);
144 for (i = 0; i < vv.length; i++)
145 list.add(vv[i]);
146 }
147 }
148
149 void run() {
150 try {
151 StringBuffer b = new StringBuffer();
0279756e
MW
152 b.append(anag)
153 .append(" -file ").append(file)
154 .append(" ").append(word.getText());
7b468eed 155 Process p = Runtime.getRuntime().exec(b.toString());
156 listen(p);
157 } catch (IOException e) {
158 splat(e.toString());
159 }
dc460403 160 }
161
435ab388 162 void help() {
163 try {
0279756e 164 Process p = Runtime.getRuntime().exec(anag + " --help");
435ab388 165 listen(p);
166 } catch (IOException e) {
167 splat(e.toString());
168 }
169 }
170
dc460403 171 void getlist(String tag) {
172 try {
0279756e 173 Vector<String> v = new Vector<String>();
7b468eed 174 String[] vv;
0279756e 175 v.addElement(anag);
7b468eed 176 v.addElement("-file");
177 v.addElement(file);
178 v.addElement(tag);
435ab388 179 v.addElement(word.getText().toLowerCase());
7b468eed 180 vv = new String[v.size()];
181 v.copyInto(vv);
182 Process p = Runtime.getRuntime().exec(vv);
183 listen(p);
dc460403 184 } catch (IOException e) {
7b468eed 185 splat(e.toString());
dc460403 186 }
187 }
188
189 public AnagPanel() {
190 super();
7b468eed 191 setLayout(new GridBagLayout());
192 GridBagConstraints g = new GridBagConstraints();
dc460403 193 Button b;
194
7b468eed 195 sb = null;
196
dc460403 197 g.gridx = g.gridy = GridBagConstraints.RELATIVE;
7b468eed 198 g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1;
dc460403 199 g.weightx = 1; g.weighty = 0;
200
7b468eed 201 word = new TextField(20);
dc460403 202 g.fill = GridBagConstraints.HORIZONTAL;
203 g.insets = new Insets(8, 8, 8, 8);
7b468eed 204 add(word, g);
dc460403 205
7b468eed 206 list = new java.awt.List(20);
207 g.fill = GridBagConstraints.BOTH;
208 g.insets.top = 0;
209 g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER;
210 g.weightx = g.weighty = 1;
211 add(list, g);
212
213 g.fill = GridBagConstraints.BOTH;
214 g.weightx = 0; g.weighty = 1;
dc460403 215 g.insets.left = 0;
7b468eed 216 g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER;
217 add(new Panel(), g);
218
219 g.fill = GridBagConstraints.HORIZONTAL;
220 g.weighty = 0;
221
dc460403 222 b = new Button("Anagram");
223 b.addActionListener(new ActionListener() {
7b468eed 224 public void actionPerformed(ActionEvent e) { getlist("-anagram"); }
dc460403 225 });
7b468eed 226 add(b, g);
227
dc460403 228 b = new Button("Subgram");
229 b.addActionListener(new ActionListener() {
7b468eed 230 public void actionPerformed(ActionEvent e) { getlist("-subgram"); }
dc460403 231 });
7b468eed 232 add(b, g);
233
dc460403 234 b = new Button("Wildcard");
235 b.addActionListener(new ActionListener() {
7b468eed 236 public void actionPerformed(ActionEvent e) { getlist("-wildcard"); }
dc460403 237 });
7b468eed 238 add(b, g);
239
d9af4a2b 240 b = new Button("Regular expression");
a10122de 241 b.addActionListener(new ActionListener() {
242 public void actionPerformed(ActionEvent e) { getlist("-regexp"); }
243 });
244 add(b, g);
245
d9af4a2b 246 b = new Button("Perl regexp");
247 b.addActionListener(new ActionListener() {
248 public void actionPerformed(ActionEvent e) { getlist("-pcre"); }
249 });
250 add(b, g);
251
252 b = new Button("Monoalphabetic");
253 b.addActionListener(new ActionListener() {
254 public void actionPerformed(ActionEvent e) { getlist("-mono"); }
255 });
256 add(b, g);
257
dc460403 258 b = new Button("Trackword");
259 b.addActionListener(new ActionListener() {
7b468eed 260 public void actionPerformed(ActionEvent e) { getlist("-trackword"); }
dc460403 261 });
7b468eed 262 add(b, g);
dc460403 263
7b468eed 264 b = new Button("Run");
265 b.addActionListener(new ActionListener() {
266 public void actionPerformed(ActionEvent e) { run(); }
267 });
268 add(b, g);
269
435ab388 270 b = new Button("Help!");
271 b.addActionListener(new ActionListener() {
272 public void actionPerformed(ActionEvent e) { help(); }
273 });
274 add(b, g);
275
7b468eed 276 b = new Button("Settings...");
277 b.addActionListener(new ActionListener() {
278 public void actionPerformed(ActionEvent e) { settings(); }
279 });
280 add(b, g);
dc460403 281 }
7b468eed 282};
dc460403 283
284/*----- Program or applet -------------------------------------------------*/
285
286public class AnagGUI extends Applet {
287 public static void main(String[] argv) {
288 Frame f = new Frame("Anagram solver");
289 f.addWindowListener(new WindowAdapter() {
0279756e 290 public void windowClosing(WindowEvent e) { System.exit(0); }
dc460403 291 });
292 AnagPanel p = new AnagPanel();
293 f.add(p);
294 f.pack();
0279756e 295 f.setVisible(true);
dc460403 296 }
297 public AnagGUI() { super(); setLayout(new BorderLayout()); }
435ab388 298 public void init() { /*add(new AnagPanel());*/ main(null); }
299 public void destroy() { /*removeAll();*/ }
dc460403 300};
301
302/*----- That's all, folks -------------------------------------------------*/