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