X-Git-Url: https://git.distorted.org.uk/~mdw/anag/blobdiff_plain/dc46040365c43901d325c1989471d41a629126a3..0279756ef8eb60878927c9e325a9ad25a4b0f150:/AnagGUI.java diff --git a/AnagGUI.java b/AnagGUI.java index 01f91ee..dde7b2f 100644 --- a/AnagGUI.java +++ b/AnagGUI.java @@ -1,13 +1,11 @@ /* -*-java-*- * - * $Id: AnagGUI.java,v 1.1 2001/02/04 19:53:07 mdw Exp $ - * * Front-end GUI * * (c) 2001 Mark Wooding */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Anag: a simple wordgame helper. * @@ -15,25 +13,17 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * Anag is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with Anag; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: AnagGUI.java,v $ - * Revision 1.1 2001/02/04 19:53:07 mdw - * Simple GUI front-end in Java. - * - */ - /*----- Imports -----------------------------------------------------------*/ import java.lang.*; @@ -47,129 +37,249 @@ import java.awt.event.*; class Whinge extends Frame { public Whinge(String gripe) { - super("Error!"); - GridBagLayout gb = new GridBagLayout(); - setLayout(gb); + super("Error from AnagGUI"); + setLayout(new GridBagLayout()); + GridBagConstraints g = new GridBagConstraints(); addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { dispose(); } + public void windowClosing(WindowEvent e) { dispose(); } }); - GridBagConstraints g = new GridBagConstraints(); g.gridx = g.gridy = GridBagConstraints.RELATIVE; g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1; g.weightx = g.weighty = 1; - g.insets = new Insets(24, 24, 8, 24); - Label l = new Label(gripe); - add(l); gb.setConstraints(l, g); + g.insets = new Insets(24, 24, 24, 24); + add(new Label(gripe), g); Button b = new Button("Bummer"); b.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { dispose(); } + public void actionPerformed(ActionEvent e) { dispose(); } }); g.weighty = 0; g.insets.top = 0; g.insets.bottom = 24; - add(b); gb.setConstraints(l, g); + add(b, g); pack(); - show(); + setVisible(true); } -} +}; class AnagPanel extends Panel { TextField word; java.awt.List list; + Settings sb; + String anag = System.getProperty("anag.program", "anag"); + String file = System.getProperty("anag.dictionary", + "/usr/share/dict/words"); + + class Settings extends Frame { + TextField name; + + public Settings() { + super("AnagGUI settings"); + Button b; + GridBagConstraints g = new GridBagConstraints(); + this.setLayout(new GridBagLayout()); + this.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { dispose(); sb = null; } + }); + g.gridx = g.gridy = GridBagConstraints.RELATIVE; + g.gridheight = 1; + g.weighty = 0; + g.fill = GridBagConstraints.NONE; + g.gridwidth = 1; g.weightx = 0; + g.insets = new Insets(8, 8, 8, 8); + this.add(new Label("Word list"), g); + g.fill = GridBagConstraints.HORIZONTAL; + g.gridwidth = GridBagConstraints.REMAINDER; g.weightx = 1; + g.insets.left = 0; + name = new TextField(20); + name.setText(file); + this.add(name, g); + g.insets.left = 8; g.insets.top = 0; g.gridwidth = 1; + g.weightx = 0; this.add(new Panel(), g); + g.weightx = 1; this.add(new Panel(), g); + g.weightx = 0; + g.insets.left = 0; + b = new Button("Cancel"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { dispose(); sb = null; } + }); + this.add(b, g); + b = new Button("OK"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + file = name.getText(); dispose(); sb = null; + } + }); + this.add(b, g); + this.pack(); + this.setVisible(true); + } + }; + + void splat(String gripe) { new Whinge(gripe); } + void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); } - void splat(String gripe) { - new Whinge(gripe); + void listen(Process p) throws IOException { + BufferedReader fout = + new BufferedReader(new InputStreamReader(p.getInputStream())); + BufferedReader ferr = + new BufferedReader(new InputStreamReader(p.getErrorStream())); + + String l; + Vector v = new Vector(); + while ((l = fout.readLine()) != null) + v.addElement(l); + StringBuffer d = new StringBuffer(); + while ((l = ferr.readLine()) != null) + d.append(l).append("\n"); + l = d.toString(); + if (l.length() > 0) + splat(l); + else { + list.removeAll(); + int i; + String[] vv = new String[v.size()]; + v.copyInto(vv); + for (i = 0; i < vv.length; i++) + list.add(vv[i]); + } + } + + void run() { + try { + StringBuffer b = new StringBuffer(); + b.append(anag) + .append(" -file ").append(file) + .append(" ").append(word.getText()); + Process p = Runtime.getRuntime().exec(b.toString()); + listen(p); + } catch (IOException e) { + splat(e.toString()); + } + } + + void help() { + try { + Process p = Runtime.getRuntime().exec(anag + " --help"); + listen(p); + } catch (IOException e) { + splat(e.toString()); + } } void getlist(String tag) { try { - Vector v = new Vector(); - String[] vv; - v.addElement("anag"); - v.addElement(tag); - v.addElement(word.getText()); - vv = new String[v.size()]; - v.copyInto(vv); - Process p = Runtime.getRuntime().exec(vv); - LineNumberReader fout = - new LineNumberReader(new InputStreamReader(p.getInputStream())); - LineNumberReader ferr = - new LineNumberReader(new InputStreamReader(p.getErrorStream())); - - String l; - v = new Vector(); - while ((l = fout.readLine()) != null) - v.addElement(l); - StringBuffer d = new StringBuffer(); - while ((l = ferr.readLine()) != null) - d.append(l).append("\n"); - l = d.toString(); - if (l.length() > 0) - splat(l); - else { - list.removeAll(); - int i; - vv = new String[v.size()]; - v.copyInto(vv); - for (i = 0; i < vv.length; i++) - list.add(vv[i]); - } + Vector v = new Vector(); + String[] vv; + v.addElement(anag); + v.addElement("-file"); + v.addElement(file); + v.addElement(tag); + v.addElement(word.getText().toLowerCase()); + vv = new String[v.size()]; + v.copyInto(vv); + Process p = Runtime.getRuntime().exec(vv); + listen(p); } catch (IOException e) { - splat(e.toString()); + splat(e.toString()); } } public AnagPanel() { super(); - GridBagLayout gb = new GridBagLayout(); - setLayout(gb); + setLayout(new GridBagLayout()); + GridBagConstraints g = new GridBagConstraints(); Button b; - GridBagConstraints g = new GridBagConstraints(); + sb = null; + g.gridx = g.gridy = GridBagConstraints.RELATIVE; - g.gridwidth = g.gridheight = 1; + g.gridwidth = GridBagConstraints.REMAINDER; g.gridheight = 1; g.weightx = 1; g.weighty = 0; - word = new TextField(40); + word = new TextField(20); g.fill = GridBagConstraints.HORIZONTAL; g.insets = new Insets(8, 8, 8, 8); - add(word); gb.setConstraints(word, g); + add(word, g); - g.fill = GridBagConstraints.NONE; - g.weightx = g.weighty = 0; + list = new java.awt.List(20); + g.fill = GridBagConstraints.BOTH; + g.insets.top = 0; + g.gridwidth = 1; g.gridheight = GridBagConstraints.REMAINDER; + g.weightx = g.weighty = 1; + add(list, g); + + g.fill = GridBagConstraints.BOTH; + g.weightx = 0; g.weighty = 1; g.insets.left = 0; + g.gridheight = 1; g.gridwidth = GridBagConstraints.REMAINDER; + add(new Panel(), g); + + g.fill = GridBagConstraints.HORIZONTAL; + g.weighty = 0; + b = new Button("Anagram"); b.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { getlist("-anagram"); } + public void actionPerformed(ActionEvent e) { getlist("-anagram"); } }); - add(b); gb.setConstraints(b, g); + add(b, g); + b = new Button("Subgram"); b.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { getlist("-subgram"); } + public void actionPerformed(ActionEvent e) { getlist("-subgram"); } }); - add(b); gb.setConstraints(b, g); + add(b, g); + b = new Button("Wildcard"); b.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { getlist("-wildcard"); } + public void actionPerformed(ActionEvent e) { getlist("-wildcard"); } + }); + add(b, g); + + b = new Button("Regular expression"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { getlist("-regexp"); } + }); + add(b, g); + + b = new Button("Perl regexp"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { getlist("-pcre"); } }); - add(b); gb.setConstraints(b, g); + add(b, g); + + b = new Button("Monoalphabetic"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { getlist("-mono"); } + }); + add(b, g); + b = new Button("Trackword"); b.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { getlist("-trackword"); } + public void actionPerformed(ActionEvent e) { getlist("-trackword"); } }); - g.gridwidth = GridBagConstraints.REMAINDER; - add(b); gb.setConstraints(b, g); + add(b, g); - list = new java.awt.List(20); - g.fill = GridBagConstraints.BOTH; - g.insets.left = 8; g.insets.top = 0; - g.gridwidth = 5; - g.weightx = g.weighty = 1; - add(list); gb.setConstraints(list, g); + b = new Button("Run"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { run(); } + }); + add(b, g); + + b = new Button("Help!"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { help(); } + }); + add(b, g); + + b = new Button("Settings..."); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { settings(); } + }); + add(b, g); } -} +}; /*----- Program or applet -------------------------------------------------*/ @@ -177,16 +287,16 @@ public class AnagGUI extends Applet { public static void main(String[] argv) { Frame f = new Frame("Anagram solver"); f.addWindowListener(new WindowAdapter() { - public void windowClosing(WindowEvent e) { System.exit(0); } + public void windowClosing(WindowEvent e) { System.exit(0); } }); AnagPanel p = new AnagPanel(); f.add(p); f.pack(); - f.show(); + f.setVisible(true); } public AnagGUI() { super(); setLayout(new BorderLayout()); } - public void init() { add(new AnagPanel()); } - public void destroy() { removeAll(); } + public void init() { /*add(new AnagPanel());*/ main(null); } + public void destroy() { /*removeAll();*/ } }; /*----- That's all, folks -------------------------------------------------*/