import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.Applet;
import java.io.*;

public class jUIQdiz extends Applet implements ActionListener {

Choice choice; //pop-up list of choices
TextField testo1;
TextArea areatesto;
Label lab1,titolo,sottotitolo,sottotitolo2,SearchType_label;
CheckboxGroup group1;
Checkbox line_begin, line_inside, ClearCheckbox;
Font TitleFont;
Button ClearButton, ExitButton, GoButton;
int trovata = 0, confr;
String riga;

    public jUIQdiz()
    {
          initGui();
          setSize(200,280);
          setVisible(true);
    }

    private void initGui()
    {
    choice = new Choice();
    File dir = new File(".");
    File test;
    String[] children = dir.list(); // Store file/dir list.
    if (children == null)
      { }
    else
      {
        for (int i=0; i<children.length; i++)
        {
          test = new File(children[i]);
          if (test.isDirectory())
          {
            choice.addItem(children[i]); // Show in Choicelist only directories.
          }
        }
      }
//*********


	        setLayout(null);
          titolo = new Label("jUIQdiz by L. Cassioli 2005 - v 3.40");
          sottotitolo=new Label("Choose dictionary:");
          SearchType_label = new Label("Search type:");
          group1 = new CheckboxGroup(); // Build radiobox group for search type.
            line_begin = new Checkbox("Word",group1,true);
            line_inside = new Checkbox("Idiom",group1,false);
          sottotitolo2=new Label(" http://jumpjack.altervista.org",Label.CENTER);
          lab1=new Label("Insert word:");
          testo1=new TextField(20);
          areatesto=new TextArea("",18,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
          ClearButton = new Button("Clear");
          ClearCheckbox = new Checkbox();
          ExitButton = new Button("Exit");
          GoButton = new Button("GO");
					//TitleFont = new Font("Symbian Viking",Font.BOLD,10); // NOT WORKING. WHY???
          //titolo.setFont(TitleFont);


          //******** Set controls positions.
          titolo.setBounds(20,5,190,15);
          sottotitolo.setBounds(10,27,110,15);
          sottotitolo2.setBounds(10,230,190,15);
          SearchType_label.setBounds(10,70,70,17);
          line_begin.setBounds(77,70,60,15);
          line_inside.setBounds(140,70,60,15);
          choice.setBounds(105,26,80,18);
          lab1.setBounds(10,50,70,20);
          testo1.setBounds(80,50,70,20);
          testo1.addActionListener(this);
          areatesto.setBounds(10,90,188,140);
          areatesto.setColumns(10);
          ClearButton.setBounds(20,250,50,20);
          ClearButton.addActionListener(this);
          ClearCheckbox.setBounds(73,253,20,20);
          ExitButton.setBounds(138,250,50,20);
          ExitButton.addActionListener(this);
          GoButton.setBounds(155,50,35,20);
          GoButton.addActionListener(this);


          //******** Add control to frame.
          add(titolo);
          add(sottotitolo);
          add(sottotitolo2);
          add(SearchType_label);
          add(line_begin);
          add(line_inside);
          add(lab1);
          add(testo1);
          add(areatesto);
          add(ClearButton);
          add(ClearCheckbox);
          add(ExitButton);
          add(GoButton);
          add(choice);
   }

   private void updateInfo()
   {
  try {
  String prova,prova2,riga2,nome;
  prova=testo1.getText();
  if (prova.length()<3)
   { areatesto.appendText("Cannot search words shorter\n");
     areatesto.appendText("than 3 characters.\n");
     return;
   }
  prova2=prova.toLowerCase();
  nome=prova2.substring(0,1)+".txt"; // Set file name to first letter of input word.
  if (group1.getSelectedCheckbox() != line_inside)
    { // Look at the beginning of file named with the first letter of input word.
    confr = 0;
     nome = choice.getSelectedItem()+"\\"+nome; // Files ar stored in proper folders.
    //nome ="engita.txt"; // Search inside single text file.
    areatesto.appendText("Searching in  '" + nome +"...'\n");
    }
  else
    { // Look inside whole line of MODI.TXT file.
    confr = 255;
    nome="modi.txt"; // File is stored in main application folder.
    }

  FileReader file = new FileReader(nome);
  BufferedReader fileInput = new BufferedReader(file);
  riga = fileInput.readLine();
  riga2 = riga.toLowerCase();
  prova2 = prova.toLowerCase();
  trovata = 0;
  while (riga2 != null)
  {
  // areatesto.append("."); // Show progress (SLOWS DOWN A LOT!!!)
   if  ((riga2.indexOf(prova2) <= confr) && ( riga2.indexOf(prova2) > -1))
   // "<= 0"  for beginning of line, <= 255 for inline search.
     {
       trovata = 1;
       areatesto.appendText("\nSearch complete.\n== Results for '" + prova +"'  ==\n***" + riga + "\n");
       riga = fileInput.readLine();
       if (riga != null)
         {
           riga2 = riga.toLowerCase();
         }
       else
         {
           riga2 = null;
         }
       while (riga2 != null)
         {
           if  ((riga2.indexOf(prova2) <= confr ) && ( riga2.indexOf(prova2) > -1)) // "== 0"  per inizio riga .
               { areatesto.appendText("***" + riga + "\n");  }
 		       riga = fileInput.readLine();
           if (riga != null)
             {
               riga2 = riga.toLowerCase();
             }
           else
             {
               riga2 = null;
             }
         } //end second while.
      areatesto.appendText("== End of result for '" + prova +"' ==\n"); // Separator at the end of listed words.
      break;
     } //end IF
   riga = fileInput.readLine();
   if (riga != null)
    {
      riga2 = riga.toLowerCase();
    }
   else
    {
     riga2 = null;
    }
  } //end first WHILE
  if (trovata == 0)
    {
    areatesto.appendText("'" + prova + "' not found.\n=====\n");
    }
  trovata = 0; // reset flag.
  } // end TRY
  catch (IOException e)
    {
    //System.err.println("ops, some I/O error happened... ");
    areatesto.appendText("File error. Did you select right folder?\n=====\n");
    }
  finally
    {
      //System.out.println("Search complete.");
    }
 } // end " IF TESTO1 "


   public void actionPerformed(ActionEvent evt) // Detect GoButton or ENTER press.
   {
       if ((evt.getSource().equals(testo1)) || evt.getSource().equals(GoButton))
       {
       		if (ClearCheckbox.getState() == true) // Clear textarea if checkbox is selected.
       			{
       			areatesto.setText("");
       			}
          updateInfo();
       }

       if (evt.getSource().equals(ClearButton)) // Detect ClearButton and clear text area.
       {
           areatesto.setText("");
       }

       if (evt.getSource().equals(ExitButton)) { // Detect ExitButton press.
          System.exit(0);
       }
   }


   public static void main(String[] args)
   {

       Frame f = new Frame("jUIQdiz");
       f.addWindowListener(new WindowAdapter()
       {
       	public void windowClosing(WindowEvent e)
          {System.exit(0);}
       });

          jUIQdiz istanza = new jUIQdiz();
          //super("jUIQdiz");
         /* this.addWindowListener(
             new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                      System.exit(0);
                   }
                }
             );*/
       f.setSize(200,280);
       f.add("Center",istanza);
       f.setVisible(true);

   }

} // program end.
