import diamondedge.util.*;
import diamondedge.ado.*;
import diamondedge.vb.*;
import java.awt.*;
import java.awt.event.*;

public class Contacts extends AwtForm
             implements ItemListener, ActionListener
{
  public TextField txtEmail = new TextField();
  public TextField txtName = new TextField();
  public List lstEntries = new List();
  public TextField txtTotalEntries = new TextField();
  public Button cmdClear = new Button();
  public Button cmdAdd = new Button();
  public Button cmdDelete = new Button();
  public AwtLabel lblName = new AwtLabel();
  public AwtLine Line1 = new AwtLine();
  public AwtLabel lblEmail = new AwtLabel();
  public AwtLine Line2 = new AwtLine();
  public AwtLabel lblEntries = new AwtLabel();
  public AwtLabel Label1 = new AwtLabel();

  public Contacts()
  {
  }

  public void initialize()
  {
    if( initialized )
      return;
    try
    {
      setLayout( null );
      setName( "Contacts" );
      setScaleMode( 3 );
      setTitle( "Contacts" );
      //unsup FontUnderline = false;
      //unsup FontStrikethrough = false;
      //unsup LinkTopic = "Form1";
      //unsup PaletteMode = 1;
      setScaleHeight( 305 );
      setScaleWidth( 476 );
      setFormLocation( 98, 115 );
      setSize( 476, 305 );
      setFormSize( 476, 305 );
      setFont( new Font("SansSerif", Font.BOLD, 10) );

      txtName.setName( "txtName" );
      add( txtName );
      //unsup txtName.FontUnderline = false;
      //unsup txtName.FontStrikethrough = false;
      //unsup txtName.TabIndex = 0;
      txtName.setVisible( true );
      txtName.setText( "" );
      txtName.setLocation( 16, 112 );
      txtName.setSize( 169, 25 );
      txtName.setFont( new Font("SansSerif", Font.PLAIN, 10) );

      txtEmail.setName( "txtEmail" );
      add( txtEmail );
      //unsup txtEmail.FontUnderline = false;
      //unsup txtEmail.FontStrikethrough = false;
      //unsup txtEmail.TabIndex = 1;
      txtEmail.setVisible( true );
      txtEmail.setText( "" );
      txtEmail.setLocation( 288, 112 );
      txtEmail.setSize( 169, 25 );
      txtEmail.setFont( new Font("SansSerif", Font.PLAIN, 10) );

      txtTotalEntries.setName( "txtTotalEntries" );
      add( txtTotalEntries );
      //unsup txtTotalEntries.TabIndex = 3;
      txtTotalEntries.setVisible( true );
      txtTotalEntries.setText( "" );
      txtTotalEntries.setLocation( 416, 272 );
      txtTotalEntries.setSize( 41, 19 );

      cmdClear.setName( "cmdClear" );
      add( cmdClear );
      cmdClear.setLabel( "Clear All" );
      //unsup cmdClear.TabIndex = 6;
      cmdClear.setVisible( true );
      cmdClear.setLocation( 192, 264 );
      cmdClear.setSize( 65, 33 );

      cmdDelete.setName( "cmdDelete" );
      add( cmdDelete );
      cmdDelete.setLabel( "Delete" );
      cmdDelete.setEnabled( false );
      //unsup cmdDelete.TabIndex = 5;
      cmdDelete.setVisible( true );
      cmdDelete.setLocation( 104, 264 );
      cmdDelete.setSize( 65, 33 );

      cmdAdd.setName( "cmdAdd" );
      add( cmdAdd );
      cmdAdd.setLabel( "Add" );
      //unsup cmdAdd.Default = true;
      //unsup cmdAdd.TabIndex = 4;
      cmdAdd.setVisible( true );
      cmdAdd.setLocation( 16, 264 );
      cmdAdd.setSize( 65, 33 );

      lstEntries.setName( "lstEntries" );
      add( lstEntries );
      //unsup lstEntries.FontUnderline = false;
      //unsup lstEntries.FontStrikethrough = false;
      lstEntries.setForeground( Color.blue );
      lstEntries.repaint();
      //unsup lstEntries.TabIndex = 2;
      lstEntries.setVisible( true );
      initializeList( lstEntries, null );
      lstEntries.setLocation( 16, 144 );
      lstEntries.setSize( 441, 100 );
      lstEntries.setFont( new Font("Serif", Font.BOLD|Font.ITALIC, 17) );

      Line1.setName( "Line1" );
      add( Line1 );
      Line1.setX1( 16 );
      Line1.setX2( 456 );
      Line1.setY1( 8 );
      Line1.setY2( 8 );
      Line1.setVisible( true );

      Line2.setName( "Line2" );
      add( Line2 );
      Line2.setLineColor( Color.black );
      Line2.setX1( 16 );
      Line2.setX2( 456 );
      Line2.setY1( 72 );
      Line2.setY2( 72 );
      Line2.setVisible( true );

      lblEntries.setName( "lblEntries" );
      add( lblEntries );
      lblEntries.setText( "# of contacts:" );
      //unsup lblEntries.TabIndex = 10;
      lblEntries.setVisible( true );
      lblEntries.setLocation( 312, 272 );
      lblEntries.setSize( 97, 17 );

      lblEmail.setName( "lblEmail" );
      add( lblEmail );
      lblEmail.setText( "Email Address:" );
      //unsup lblEmail.TabIndex = 9;
      lblEmail.setVisible( true );
      lblEmail.setLocation( 288, 88 );
      lblEmail.setSize( 105, 17 );

      lblName.setName( "lblName" );
      add( lblName );
      lblName.setText( "Name:" );
      //unsup lblName.TabIndex = 8;
      lblName.setVisible( true );
      lblName.setLocation( 16, 88 );
      lblName.setSize( 65, 17 );

      Label1.setName( "Label1" );
      add( Label1 );
      Label1.setAlignment( AwtLabel.CENTER );
      Label1.setText( "Contact List" );
      //unsup Label1.FontUnderline = false;
      //unsup Label1.FontStrikethrough = false;
      //unsup Label1.TabIndex = 7;
      Label1.setVisible( true );
      Label1.setLocation( 16, 24 );
      Label1.setSize( 441, 33 );
      Label1.setFont( new Font("SansSerif", Font.BOLD, 26) );
    }
    catch(Exception _e_) { Err.set(_e_,"Contacts"); }

    if( !unloaded )
      enableEvents();
    super.initialize();
    initialized = true;
  }

  // methods

  public void Form_Activate()
  {
    try
    {

      txtName.requestFocus();
      txtTotalEntries.setEditable( !true );
      txtName.setText( "John Doe" );
      txtEmail.setText( "johndoe@anywhere.com" );

    }
    catch(Exception _e_) { Err.set(_e_,"Form_Activate"); }
  }

  // events

  private void lstEntries_Click()
  {
    try
    {

      //Disable "Delete" command button
      if( lstEntries.getSelectedIndex() == -1 )
      {
        cmdDelete.setEnabled( false );
      }

    }
    catch(Exception _e_) { Err.set(_e_,"lstEntries_Click"); }
  }

  private void cmdClear_Click()
  {
    try
    {

      //Clear all entries from list box
      lstEntries.removeAll();

      //Disable "Delete" command button
      cmdDelete.setEnabled( false );

      //Show the number of items in "lstEntries" list box
      txtTotalEntries.setText( String.valueOf( lstEntries.getItemCount() ) );

      //Reset focus to name textbox
      txtName.requestFocus();

    }
    catch(Exception _e_) { Err.set(_e_,"cmdClear_Click"); }
  }

  private void cmdAdd_Click()
  {
    try
    {
      String szEntry = "";


      //Get name and number
      szEntry = txtName.getText() + " --- " + txtEmail.getText();

      //Add name to list box
      lstEntries.add( szEntry );

      //Reset entry fields, number of entries, "Delete" button
      txtEmail.setText( "" );
      txtName.setText( "" );
      txtName.requestFocus();
      txtTotalEntries.setText( String.valueOf( lstEntries.getItemCount() ) );
      cmdDelete.setEnabled( true );

    }
    catch(Exception _e_) { Err.set(_e_,"cmdAdd_Click"); }
  }

  private void cmdDelete_Click()
  {
    try
    {
      int nIndex = 0;


      if( lstEntries.getSelectedIndex() != -1 )
      {
        //Get the index of selected item
        nIndex = lstEntries.getSelectedIndex();

        //Remove the selected item
        lstEntries.remove( nIndex );

        //Show the number of items in "lstEntries" list box
        txtTotalEntries.setText( String.valueOf( lstEntries.getItemCount() ) );

        //If "lstEntries" is empty, disable "Delete" button
        if( lstEntries.getItemCount() == 0 )
        {
          cmdDelete.setEnabled( false );
        }
      }

    }
    catch(Exception _e_) { Err.set(_e_,"cmdDelete_Click"); }
  }

  // implementation of Listener interfaces

  public void itemStateChanged( ItemEvent e )
  {
    if( e.getSource() == lstEntries )
    {
      lstEntries_Click();
    }
  }

  public void actionPerformed( ActionEvent e )
  {
    if( e.getSource() == cmdClear )
    {
      cmdClear_Click();
    }
    else if( e.getSource() == cmdAdd )
    {
      cmdAdd_Click();
    }
    else if( e.getSource() == cmdDelete )
    {
      cmdDelete_Click();
    }
  }

  public void enableEvents()
  {
    lstEntries.addItemListener( this );
    cmdClear.addActionListener( this );
    cmdAdd.addActionListener( this );
    cmdDelete.addActionListener( this );
  }
}