/*
Copyright (c) 1997, Jay F Kominek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL JAY
F KOMINEK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

In other words, this software is under a license identical to that of X11.
*/
import java.applet.*;
import java.awt.*;
import java.util.*;

class Controls extends Panel {
 public Controls() {
  add(new Button("Lookup"));
  add(new Button("Match"));
  add(new Button("Clear"));
 }
}

class InputField extends TextField {
 InputField() {
  super(60);
 }
}

class InfoBox extends TextArea {
 InfoBox() {
  super("",10,60);
  setEditable(false);
  setFont(new Font("Courier",Font.PLAIN,12));
 }

 void Print(String s) {
  insertText(s, getText().length());
 }
}

public class AppletDictLookup extends Applet {
 InputField inputField;
 InfoBox infoBox;
 NetDictionary dictionary;

 public void init() {

  dictionary = new NetDictionary(getParameter("server"));
  dictionary.client("Jay's Java Applet Dict Client v0.99q");

  setBackground(Color.lightGray);
  setLayout(new BorderLayout());

  inputField = new InputField();
  add("North",inputField);

  infoBox = new InfoBox();
  add("Center",infoBox);
  add("South",new Controls());

  resize(600,300);
  show();
 }

 public String lookUp(String word) {
  String results = "";
  Enumeration e;
  Definition current;

  for(e = dictionary.define(word).elements() ; e.hasMoreElements() ; ) {
   current = (Definition)e.nextElement();

   results = results + "From: "+current.getdbname()+"\n";
   results = results + current.getDefinition() + "\n";
  }

  return results;
 }

 public String match(String word) {
  String results = "";
  Enumeration e;
  Definition current;

  results = results + "Possible matches: \n";

  for(e = dictionary.match(word,".","*").elements() ; e.hasMoreElements(); ) {
   current = (Definition)e.nextElement();

   results = results + current.getWord()+"\n";
  }

  results = results + "End of matches\n";

  return results;
 }

 public synchronized boolean handleEvent(Event e) {
  if(e.id == Event.WINDOW_DESTROY) {
   return true;
  } else {
   return super.handleEvent(e);
  }
 }

 public boolean action(Event evt, Object obj) {
  if(evt.target instanceof Button) {
   String label = (String)obj;
   if(label.equals("Lookup")) {
    infoBox.Print(lookUp(inputField.getText()));
   } else if(label.equals("Match")) {
    infoBox.Print(match(inputField.getText()));
   } else if(label.equals("Clear")) {
    infoBox.setText("");
   }
  }
  return true;
 }

 /* Methods overridden from Applet */
 public void destroy() {
  dictionary.close();
 }

 public String getAppletInfo() {
  String results = new String(
   "AppletDictLookup by Jay Kominek <jkominek@xtn.net>\n"+
   "This program is under the GPL\n");
  return results;
 }
}
