///////////////////////////////////////////////////////////////////////
//
// RandomAddin.java : Provide a Java interface to the Simple add-in.
//
///////////////////////////////////////////////////////////////////////
//
// Copyright © 1997-99 by the IBM Corporation. All Rights Reserved.
//
// Author:          Angel L. Diaz     aldiaz@us.ibm.com
//
// IBM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IBM SHALL NOT BE LIABLE FOR
// ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING
// THIS SOFTWARE OR ITS DERIVATIVES.
//
///////////////////////////////////////////////////////////////////////

import java.awt.*;
import java.applet.Applet;

import ibm.techexplorer.techexplorer;
import ibm.techexplorer.control.techexplorerControl;
import ibm.techexplorer.axtchexp.AxTchExpRaw;

public class RandomAddin extends java.applet.Applet {
   
  Label              randomAddinLabel   = null;
  Button             addInInitialize    = null;
  Button             addInShutdown      = null;
  Button             addInEvaluate      = null;
  
  Panel              randomControlPanel = null;
  Panel              buttonPanel        = null;

  /*
   * techexplorer
   */

  private techexplorer techexplorer = null;

  public void setPlugin( techexplorer obj )
  {
     if ( obj instanceof techexplorer )
        techexplorer = obj;
  }

  public void setControl( AxTchExpRaw obj )
  {
     if ( obj instanceof AxTchExpRaw )
         techexplorer = new techexplorerControl( obj );
  }

  public void init() {
     randomAddinLabel = new Label( "Random 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);

     randomControlPanel = new Panel();
	 randomControlPanel.setLayout(new GridLayout(2,1) );
	 randomControlPanel.add(randomAddinLabel);
	 randomControlPanel.add(buttonPanel);
	 
     setLayout(new GridLayout(1, 1));
	 add(randomControlPanel);
     
     System.out.println( "init" );
   }

  public void start() {     
     System.out.println( "start" );
  }

  public void stop() {
  }

  public boolean action(Event evt, Object arg) {
    if ( evt.target == addInInitialize ){
	   int nRval = techexplorer.addInInitialize( "application/x-random" );
	   switch( nRval ) {
	      case techexplorer.ADDIN_SUCCESS:
	         randomAddinLabel.setText( "Random add-in Control Panel : Loaded" );
		  break;
		  case techexplorer.ADDIN_ERROR_AVAILABLE:
	         randomAddinLabel.setText( "Random add-in Control Panel : Random add-in not available" );
		  break;
		  case techexplorer.ADDIN_ERROR_LOAD:
	         randomAddinLabel.setText( "Random add-in Control Panel : can not load Random add-in dll" );
		  break;
		  default:
	    	 randomAddinLabel.setText( "Random add-in Control Panel : Not Loaded" );
		  break;
	   }
	   return true;
	} else
    if ( evt.target == addInShutdown ){
	  techexplorer.addInShutdown( "application/x-random" );
	  randomAddinLabel.setText( "Random add-in Control Panel : Not Loaded" );
	  return true;
	}
	else
    if ( evt.target == addInEvaluate )
    {
       String sRandomNumber = techexplorer.addInBlockingEvaluate( "application/x-random", "" );
       
       if ( sRandomNumber != null )
       {
          techexplorer.reloadFromTeXString( "\\pagecolor{white}\\Large\\color{black}Random Number: " + sRandomNumber ); 
       }
        
	   return true;
	}
	else
       return false;
  }

}
