import java.awt.*;
import java.applet.*;

public class Lexicon extends Applet {
    protected TextField word_field;    
    protected TextField part_field;  
    protected List my_lexicon;
    protected Choice choose_speechpart;
    protected Label info1;
    protected Label info2;
    protected Label info3;
    protected Button butt_add;
    protected Button butt_edit;
    protected Button butt_delete;
    
    protected boolean word_cond, part_cond, choose_cond;				

    public void init(){ 

         setBackground(Color.white);
     //init ALL Components

	 //text labels
	 info1 = new Label ( "Enter new word"+"   ", Label.CENTER);
	 info2 = new Label ( "Part of Speech: Choose OR Enter   ", Label.CENTER ); 
         info3 = new Label ( "HERE YOU CAN CHANGE LEXICON", Label.CENTER );
        info3.setForeground(Color.red);
        
         //input field for new words and rules
         word_field=new TextField(10);     
         word_field.setText("");
	 part_field=new TextField(10);
         part_field.setText("");

         //possible speechparts list
	 choose_speechpart=new Choice();       
           
         choose_speechpart.addItem("");
	 choose_speechpart.addItem("n");
	 choose_speechpart.addItem("vi");
         choose_speechpart.addItem("vt");
         choose_speechpart.addItem("det");
         choose_speechpart.addItem("adj");
         choose_speechpart.addItem("prep");
         choose_speechpart.select(""); 
         
         //buttons
         butt_add=new Button("Add");
         butt_edit=new Button("Edit");
         butt_delete=new Button("Delete");

         //create list with some lexicon items
        my_lexicon= new List(12,false);
        my_lexicon.addItem( (new LexiconItem("tourists", "n")).toString());
	my_lexicon.addItem( (new LexiconItem("friends", "n")).toString());
	my_lexicon.addItem( (new LexiconItem("men", "n")).toString());
	my_lexicon.addItem( (new LexiconItem("ocean", "n")).toString());
	my_lexicon.addItem( (new LexiconItem("pleasure", "n")).toString());
        my_lexicon.addItem( (new LexiconItem("Egypt", "n")).toString());
        my_lexicon.addItem( (new LexiconItem("fish", "n")).toString());   
        my_lexicon.addItem( (new LexiconItem("fish", "vi")).toString());    
        my_lexicon.addItem( (new LexiconItem("sleep", "vi")).toString());          
        my_lexicon.addItem( (new LexiconItem("study", "vt")).toString()); 
	my_lexicon.addItem( (new LexiconItem("visit", "vt")).toString());
	my_lexicon.addItem( (new LexiconItem("the", "det")).toString());
	my_lexicon.addItem( (new LexiconItem("a", "det")).toString());
	my_lexicon.addItem( (new LexiconItem("an", "det")).toString());
	my_lexicon.addItem( (new LexiconItem("their", "det")).toString());
	my_lexicon.addItem( (new LexiconItem("in", "prep")).toString());
        my_lexicon.addItem( (new LexiconItem("with", "prep")).toString());
        my_lexicon.addItem( (new LexiconItem("big", "adj")).toString());
        my_lexicon.addItem( (new LexiconItem("good", "adj")).toString());

        //ORGANIZE LAYOUT !!!!!!!!!!!!!!!!!!!!!!!!

	GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        setLayout(gbl); //Layout manager for upper and bottom panels       

        gbc.gridx=0;
        gbc.gridy=0;	
        gbc.gridwidth=GridBagConstraints.RELATIVE; 
 //       gbc.weightx=2; 
 //       gbc.weighty=1; 
	gbc.fill = GridBagConstraints.HORIZONTAL;
	gbl.setConstraints(info3,gbc);
	add(info3);        //upper panel

        gbc.gridx=0;
        gbc.gridy=1;	
 //       gbc.weightx=1; 
 //       gbc.weighty=1; 
	gbc.fill = GridBagConstraints.HORIZONTAL;
	gbc.anchor = GridBagConstraints.SOUTH;
        gbc.gridwidth=GridBagConstraints.RELATIVE; 
	gbl.setConstraints(info1,gbc);
	add(info1);        //upper panel

        gbc.gridx=0;
        gbc.gridy=2;	
 //       gbc.weightx=1; 
//        gbc.weighty=1; 
        gbc.gridwidth=GridBagConstraints.RELATIVE; 
	gbc.fill = GridBagConstraints.NONE;
	gbc.anchor = GridBagConstraints.NORTH;
	gbl.setConstraints(word_field,gbc);
	add(word_field);        //upper panel

        gbc.gridx=0;
        gbc.gridy=3;	
 //       gbc.weightx=1; 
 //       gbc.weighty=1; 
        gbc.gridwidth=GridBagConstraints.RELATIVE; 
	gbc.anchor = GridBagConstraints.SOUTH;
	gbc.fill = GridBagConstraints.HORIZONTAL;
	gbl.setConstraints(info2,gbc);
	add(info2);        //upper panel

        Panel ChoicePanel=new Panel();
        gbc.gridx=0;
        gbc.gridy=4;	
//       gbc.weightx=1; 
//        gbc.weighty=1; 
        gbc.gridwidth=GridBagConstraints.RELATIVE; 
        gbc.gridheight=GridBagConstraints.RELATIVE; 
	gbc.anchor = GridBagConstraints.NORTH;
	gbc.fill = GridBagConstraints.HORIZONTAL;
	gbl.setConstraints(ChoicePanel,gbc);
	add(ChoicePanel);        //upper panel
             ChoicePanel.add(choose_speechpart);
             ChoicePanel.add(new Label(""));
             ChoicePanel.add(part_field);

        Panel ButtPanel=new Panel();
        gbc.gridx=0;
        gbc.gridy=5;	
 //       gbc.weightx=1; 
 //       gbc.weighty=1; 
        gbc.gridwidth=GridBagConstraints.RELATIVE; 
        gbc.gridheight=GridBagConstraints.REMAINDER; 
	gbl.setConstraints(ButtPanel,gbc);
        add(ButtPanel);    //bottom panel for buttons 
          ButtPanel.add(butt_add);
          ButtPanel.add(butt_edit);
          ButtPanel.add(butt_delete);

        gbc.gridx=1;
        gbc.gridy=0;	
        gbc.weightx=1; 
        gbc.weighty=1; 
	gbc.fill = GridBagConstraints.BOTH;
        gbc.gridwidth=GridBagConstraints.REMAINDER; 
        gbc.gridheight=GridBagConstraints.REMAINDER; 
	gbl.setConstraints(my_lexicon,gbc);
        add(my_lexicon);     
        
    } 

    //POSSIBLE ACTIONS 

    
    public boolean action(Event evt, Object whatAction){                       
       if( (evt.target == word_field) ||
           (evt.target == butt_add)   ||
           (evt.target == part_field) ){                
               
	   word_cond=wordOK();     //check whether word is entered           
	   part_cond=partOK();             
           choose_cond=chooseOK();
          
          if(part_cond && choose_cond){
                  warn_info3();
                  return true;
          }          
          
          if(!part_cond && !choose_cond && !word_cond){
                  warn_info1();
                  warn_info2();
                  return true;
          } 

          if(!word_cond){
                  warn_info1();
                  return true;
          }
          
          if(!part_cond && !choose_cond){
                  warn_info2();
                  return true;
          } 

          if(word_cond && part_cond && !choose_cond){
                  add_new_item2();
                  set_init_state();
                  return true;
          }          
         
          if( word_cond && choose_cond && !part_cond){
                  add_new_item1();
                  set_init_state();
                  return true;
          }
          
          return false;
       }else if((evt.target == butt_delete) && (my_lexicon.getSelectedItem() != null)){
                my_lexicon.delItem(my_lexicon.getSelectedIndex());
                return true; 
       }else if((evt.target == butt_edit) && (my_lexicon.getSelectedItem() != null)){
                 
                 //restore word and speechpart and fill word_field and choice_list 
                 word_field.setText( (restore_LexItem(my_lexicon.getSelectedItem())).get_word());
                 choose_speechpart.select(
                          (restore_LexItem(my_lexicon.getSelectedItem())
                           ).get_speechpart());
                 my_lexicon.delItem(my_lexicon.getSelectedIndex());      
                 return true; 
       }else return false;
    }

//this method checks the correctness of the lexicon input=word+speechpart
                  
    private boolean wordOK(){
            if( !(word_field.getText().equals("")) ){           
                 info1.setText("Enter new word"+"   ");    
                 return true;
            }else{
                 return false;
            }
    }		
    
    private boolean chooseOK(){
 	     if( !(choose_speechpart.getSelectedItem().equals("")) ){
                   info2.setText( "Part of Speech: Choose OR Enter"+"   ");
                   return true;
             }else return false;
    }    

    private boolean partOK(){
             if(part_field.getText().equals(""))           
                  return false;
             else{
                 info2.setText( "Part of Speech: Choose OR Enter"+"   "); 
                 return true;
             } 
   }                  

   private void warn_info1(){
	     info1.setText("Enter new word"+"!!!");
   }
   
   private void warn_info2(){
             info2.setText("Part of Speech: Choose OR Enter"+"!!!");  
   }

   private void warn_info3(){
             info2.setText("Leave ONE part of speech, not BOTH!");
   }



//return the lexicon input fields to the initial state
  
   private void set_init_state(){
            word_field.setText("");
            choose_speechpart.select("");  
            part_field.setText("");
            word_field.show();
            choose_speechpart.show();           
            part_field.show();
	    info1.setText("Enter new word"+"   ");     
            info2.setText("Part of Speech: Choose OR Enter"+"   ");    
    }







//add new item=word+speechpart to the lexicon

    private void add_new_item1(){           
              my_lexicon.addItem(
                 (new LexiconItem( word_field.getText(),
                                   choose_speechpart.getSelectedItem())
                 ).toString()
                 
              );
    } 

    private void add_new_item2(){           
              my_lexicon.addItem(
                 (new LexiconItem( word_field.getText(),
                                   part_field.getText())
                 ).toString()
                 
              );
    } 

//this function creates LexiconItem object from a Lexicon String
   
    public LexiconItem restore_LexItem(String word_speechpart){
            String word;
            String speechpart;
            
            word_speechpart.trim();
            int blank_index=word_speechpart.indexOf(" ");
            word=word_speechpart.substring(0,blank_index);            
            speechpart=word_speechpart.substring(blank_index, word_speechpart.length());      

            word=word.trim();
            speechpart=speechpart.trim();

            LexiconItem restored = new LexiconItem(word, speechpart);
            return restored;
    }
          


}



      	
