dc460403 |
1 | /* -*-java-*- |
2 | * |
3 | * $Id: AnagGUI.java,v 1.1 2001/02/04 19:53:07 mdw Exp $ |
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 $ |
32 | * Revision 1.1 2001/02/04 19:53:07 mdw |
33 | * Simple GUI front-end in Java. |
34 | * |
35 | */ |
36 | |
37 | /*----- Imports -----------------------------------------------------------*/ |
38 | |
39 | import java.lang.*; |
40 | import java.util.*; |
41 | import java.io.*; |
42 | import java.awt.*; |
43 | import java.applet.*; |
44 | import java.awt.event.*; |
45 | |
46 | /*----- Main code ---------------------------------------------------------*/ |
47 | |
48 | class Whinge extends Frame { |
49 | public Whinge(String gripe) { |
50 | super("Error!"); |
51 | GridBagLayout gb = new GridBagLayout(); |
52 | setLayout(gb); |
53 | |
54 | addWindowListener(new WindowAdapter() { |
55 | public void windowClosing(WindowEvent e) { dispose(); } |
56 | }); |
57 | |
58 | GridBagConstraints g = new GridBagConstraints(); |
59 | g.gridx = g.gridy = GridBagConstraints.RELATIVE; |
60 | g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1; |
61 | g.weightx = g.weighty = 1; |
62 | g.insets = new Insets(24, 24, 8, 24); |
63 | Label l = new Label(gripe); |
64 | add(l); gb.setConstraints(l, g); |
65 | |
66 | Button b = new Button("Bummer"); |
67 | b.addActionListener(new ActionListener() { |
68 | public void actionPerformed(ActionEvent e) { dispose(); } |
69 | }); |
70 | g.weighty = 0; |
71 | g.insets.top = 0; g.insets.bottom = 24; |
72 | add(b); gb.setConstraints(l, g); |
73 | pack(); |
74 | show(); |
75 | } |
76 | } |
77 | |
78 | class AnagPanel extends Panel { |
79 | TextField word; |
80 | java.awt.List list; |
81 | |
82 | void splat(String gripe) { |
83 | new Whinge(gripe); |
84 | } |
85 | |
86 | void getlist(String tag) { |
87 | try { |
88 | Vector v = new Vector(); |
89 | String[] vv; |
90 | v.addElement("anag"); |
91 | v.addElement(tag); |
92 | v.addElement(word.getText()); |
93 | vv = new String[v.size()]; |
94 | v.copyInto(vv); |
95 | Process p = Runtime.getRuntime().exec(vv); |
96 | LineNumberReader fout = |
97 | new LineNumberReader(new InputStreamReader(p.getInputStream())); |
98 | LineNumberReader ferr = |
99 | new LineNumberReader(new InputStreamReader(p.getErrorStream())); |
100 | |
101 | String l; |
102 | v = new Vector(); |
103 | while ((l = fout.readLine()) != null) |
104 | v.addElement(l); |
105 | StringBuffer d = new StringBuffer(); |
106 | while ((l = ferr.readLine()) != null) |
107 | d.append(l).append("\n"); |
108 | l = d.toString(); |
109 | if (l.length() > 0) |
110 | splat(l); |
111 | else { |
112 | list.removeAll(); |
113 | int i; |
114 | vv = new String[v.size()]; |
115 | v.copyInto(vv); |
116 | for (i = 0; i < vv.length; i++) |
117 | list.add(vv[i]); |
118 | } |
119 | } catch (IOException e) { |
120 | splat(e.toString()); |
121 | } |
122 | } |
123 | |
124 | public AnagPanel() { |
125 | super(); |
126 | GridBagLayout gb = new GridBagLayout(); |
127 | setLayout(gb); |
128 | Button b; |
129 | |
130 | GridBagConstraints g = new GridBagConstraints(); |
131 | g.gridx = g.gridy = GridBagConstraints.RELATIVE; |
132 | g.gridwidth = g.gridheight = 1; |
133 | g.weightx = 1; g.weighty = 0; |
134 | |
135 | word = new TextField(40); |
136 | g.fill = GridBagConstraints.HORIZONTAL; |
137 | g.insets = new Insets(8, 8, 8, 8); |
138 | add(word); gb.setConstraints(word, g); |
139 | |
140 | g.fill = GridBagConstraints.NONE; |
141 | g.weightx = g.weighty = 0; |
142 | g.insets.left = 0; |
143 | b = new Button("Anagram"); |
144 | b.addActionListener(new ActionListener() { |
145 | public void actionPerformed(ActionEvent e) { getlist("-anagram"); } |
146 | }); |
147 | add(b); gb.setConstraints(b, g); |
148 | b = new Button("Subgram"); |
149 | b.addActionListener(new ActionListener() { |
150 | public void actionPerformed(ActionEvent e) { getlist("-subgram"); } |
151 | }); |
152 | add(b); gb.setConstraints(b, g); |
153 | b = new Button("Wildcard"); |
154 | b.addActionListener(new ActionListener() { |
155 | public void actionPerformed(ActionEvent e) { getlist("-wildcard"); } |
156 | }); |
157 | add(b); gb.setConstraints(b, g); |
158 | b = new Button("Trackword"); |
159 | b.addActionListener(new ActionListener() { |
160 | public void actionPerformed(ActionEvent e) { getlist("-trackword"); } |
161 | }); |
162 | g.gridwidth = GridBagConstraints.REMAINDER; |
163 | add(b); gb.setConstraints(b, g); |
164 | |
165 | list = new java.awt.List(20); |
166 | g.fill = GridBagConstraints.BOTH; |
167 | g.insets.left = 8; g.insets.top = 0; |
168 | g.gridwidth = 5; |
169 | g.weightx = g.weighty = 1; |
170 | add(list); gb.setConstraints(list, g); |
171 | } |
172 | } |
173 | |
174 | /*----- Program or applet -------------------------------------------------*/ |
175 | |
176 | public class AnagGUI extends Applet { |
177 | public static void main(String[] argv) { |
178 | Frame f = new Frame("Anagram solver"); |
179 | f.addWindowListener(new WindowAdapter() { |
180 | public void windowClosing(WindowEvent e) { System.exit(0); } |
181 | }); |
182 | AnagPanel p = new AnagPanel(); |
183 | f.add(p); |
184 | f.pack(); |
185 | f.show(); |
186 | } |
187 | public AnagGUI() { super(); setLayout(new BorderLayout()); } |
188 | public void init() { add(new AnagPanel()); } |
189 | public void destroy() { removeAll(); } |
190 | }; |
191 | |
192 | /*----- That's all, folks -------------------------------------------------*/ |