///////////////////////////////////////////////////////////////////////
//
// SimpleAddin.java : Provide a Java interface to the Simple add-in.
//
///////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004 Integre Technical Publishing.  All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////

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

import integre.techexplorer.techexplorer;
import integre.techexplorer.control.techexplorerControl;
import integre.techexplorer.axtchexp.AxTchExpRaw;

public class SimpleAddin extends java.applet.Applet {
   
  Label              simpleAddinLabel   = null;
  Button             addInInitialize    = null;
  Button             addInShutdown      = null;
  Button             addInEvaluate      = null;
  TextField          evalTextField		= null;

  Panel              simpleControlPanel = null;
  Panel              buttonPanel        = null;

  /*
   * techexplorer
   */

  private techexplorer te = null;

  public void setPlugin( techexplorer obj )
  {
     if ( obj instanceof techexplorer )
        te = obj;
  }

  public void setControl( AxTchExpRaw obj )
  {
     if ( obj instanceof AxTchExpRaw )
         te = new techexplorerControl( obj );
  }


  public void init() {
     simpleAddinLabel = new Label( "Simple add-in Control Panel : Not Loaded" );

     addInInitialize  = new Button( "Initialize" );
	 addInShutdown    = new Button( "Shutdown" );
	 addInEvaluate    = new Button( "Evaluate" );
	 
	 buttonPanel = new Panel();
	 buttonPanel.setLayout(new GridLayout(1, 3));
	 buttonPanel.add(addInInitialize);
	 buttonPanel.add(addInShutdown);
	 buttonPanel.add(addInEvaluate);

     evalTextField = new TextField();

     simpleControlPanel = new Panel();
	 simpleControlPanel.setLayout(new GridLayout(3,1) );
	 simpleControlPanel.add(simpleAddinLabel);
	 simpleControlPanel.add(evalTextField);
	 simpleControlPanel.add(buttonPanel);
	 
     setLayout(new GridLayout(1, 1));
	 add(simpleControlPanel);
   }

  public void start() {      
     if ( te == null ) System.out.println("SimpleAddin: start(): null techexplorer");
  }

  public void stop() {
  }

  public void destroy() {
	// clean up non-java object
    te.gc();
  }

  public boolean action(Event evt, Object arg) {
    if ( evt.target == addInInitialize ){
	   int nRval = te.addInInitialize( "application/x-simple-string" );
	   switch( nRval ) {
	      case techexplorer.ADDIN_SUCCESS:
	         simpleAddinLabel.setText( "Simple add-in Control Panel : Loaded" );
		  break;
		  case techexplorer.ADDIN_ERROR_AVAILABLE:
	         simpleAddinLabel.setText( "Simple add-in Control Panel : Simple add-in not available" );
		  break;
		  case techexplorer.ADDIN_ERROR_LOAD:
	         simpleAddinLabel.setText( "Simple add-in Control Panel : can not load Simple add-in dll" );
		  break;
		  default:
	    	 simpleAddinLabel.setText( "Simple add-in Control Panel : Not Loaded" );
		  break;
	   }
	   return true;
	} else
    if ( evt.target == addInShutdown ){
	  te.addInShutdown( "application/x-simple-string" );
	  simpleAddinLabel.setText( "Simple add-in Control Panel : Not Loaded" );
	  return true;
	}
	else
    if ( evt.target == addInEvaluate ){
	   if ( te.addInEvaluate( "application/x-simple-string", evalTextField.getText() ) != techexplorer.ADDIN_SUCCESS )
	      simpleAddinLabel.setText( "Simple add-in Control Panel : evaluate failed" );   
	   return true;
	}
	else
       return false;
  }

}
