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
    {
      jbInit();
    }
    catch(Exception _e_) { Err.set(_e_,"Contacts"); }

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

  private void jbInit() throws Exception
  {
    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( new Dimension(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.setBounds( new Rectangle(16, 112, 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.setBounds( new Rectangle(288, 112, 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.setBounds( new Rectangle(416, 272, 41, 19) );

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

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

    cmdAdd.setName( "cmdAdd" );
    add( cmdAdd );
    cmdAdd.setLabel( "Add" );
    //unsup cmdAdd.Default = true;
    //unsup cmdAdd.TabIndex = 4;
    cmdAdd.setVisible( true );
    cmdAdd.setBounds( new Rectangle(16, 264, 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.setBounds( new Rectangle(16, 144, 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.setBounds( new Rectangle(312, 272, 97, 17) );

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

    lblName.setName( "lblName" );
    add( lblName );
    lblName.setText( "Name:" );
    //unsup lblName.TabIndex = 8;
    lblName.setVisible( true );
    lblName.setBounds( new Rectangle(16, 88, 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.setBounds( new Rectangle(16, 24, 441, 33) );
    Label1.setFont( new Font("SansSerif", Font.BOLD, 26) );
  }

  // 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 );
  }
}