Add a settings panel (currently only allows the wordlist to be
authormdw <mdw>
Wed, 7 Feb 2001 09:10:04 +0000 (09:10 +0000)
committermdw <mdw>
Wed, 7 Feb 2001 09:10:04 +0000 (09:10 +0000)
changed).  Move the buttons down the right-hand side of the list.  Add a
`Run' button which passes arguments through directly.

AnagGUI.java

index 01f91ee..ccae591 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-java-*-
  *
- * $Id: AnagGUI.java,v 1.1 2001/02/04 19:53:07 mdw Exp $
+ * $Id: AnagGUI.java,v 1.2 2001/02/07 09:10:04 mdw Exp $
  *
  * Front-end GUI
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: AnagGUI.java,v $
+ * Revision 1.2  2001/02/07 09:10:04  mdw
+ * Add a settings panel (currently only allows the wordlist to be
+ * changed).  Move the buttons down the right-hand side of the list.  Add a
+ * `Run' button which passes arguments through directly.
+ *
  * Revision 1.1  2001/02/04 19:53:07  mdw
  * Simple GUI front-end in Java.
  *
@@ -47,129 +52,216 @@ 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();    
   }
-}
+};
 
 class AnagPanel extends Panel {
   TextField word;
   java.awt.List list;
+  String file;
+  Settings sb;
 
-  void splat(String gripe) {
-    new Whinge(gripe);
+  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.show();
+    }
+  };
+
+  void splat(String gripe) { new Whinge(gripe); }
+  void settings() { if (sb != null) sb.toFront(); else sb = new Settings(); }
+
+  void listen(Process p) throws IOException {
+    LineNumberReader fout =
+      new LineNumberReader(new InputStreamReader(p.getInputStream()));
+    LineNumberReader ferr =
+      new LineNumberReader(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 -file ")
+       .append(file)
+       .append(" ")
+       .append(word.getText());
+      Process p = Runtime.getRuntime().exec(b.toString());
+      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());
+      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();
+    file = "/usr/dict/words";
+    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); gb.setConstraints(b, g);
+    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("Settings...");
+    b.addActionListener(new ActionListener() {
+      public void actionPerformed(ActionEvent e) { settings(); }
+    });
+    add(b, g);
   }
-}
+};
 
 /*----- Program or applet -------------------------------------------------*/