/**
 * Test Stub Midlet.
 * Calls registered web services using client stubs.
 *
 * This sample is designed so that you can quickly test stubs
 * generated for services registered with the J2ME Proxy Server.
 * Simply add an import statement for the generated stub class
 * and modify the callStub() method below.
 */
 
package oracle.wireless.me.sample1;

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

import oracle.wireless.me.core.*;

/* Add import statements for the stubs */

// Example commented out is for xmethods delayed stock quote demo service
// registered with WSDL:
// http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl
//import NetXmethodsServicesStockquoteStockQuoteServiceJ2MEStub;

public class TestStubMidlet extends MIDlet implements CommandListener{

    // call result string
   private String sCallResultString= new String("test stub result");

    /**
	   * Default Constructor
	   */
    public TestStubMidlet () {}
     
    /**
     * Call Stub. Edit this method to test the stub
     * remember to import the stub class
     */    
    private void callStub()
    {
      try {
        // Add your code to test the stub

        // For example: 
        // Use the stub to call xmethods delayed stock quote demo service to get
        // the price of Oracle stock and cache the result for 1 minute

        //NetXmethodsServicesStockquoteStockQuoteServiceJ2MEStub stub =
        //    new NetXmethodsServicesStockquoteStockQuoteServiceJ2MEStub();

        //sCallResultString = new String("Oracle stock price: "
        //    + stub.getQuote(1, false, "ORCL"));
      
      }
      catch (ServiceException e) {
        sCallResultString = new String(e.getMessage() + "\n");
      }
    }


	/**
	 * Start the MIDlet
	 */
	public void startApp() throws MIDletStateChangeException
  {
    if (rootDisplay == null) {
      rootDisplay = Display.getDisplay(this);
    }

	  _showFormMain();
	}

	/**
	 * Called by the framework before the application is unloaded
	 */
	public void destroyApp(boolean unconditional)
  {
    try {
		  //clear everything
		  frmMain = null;
		  frmResult = null;
      // Close ServiceManager
      ServiceManager.getInstance().close();
    }
    catch (Exception e) {
    }
    notifyDestroyed();
	}

	/**
	 * Pause the MIDlet
	 */
	public void pauseApp()
  {
    try {
      // Close ServiceManager
      ServiceManager.getInstance().close();
    }
    catch (Exception e) {
    }
  }


	/**
	 * Handling events
	 */
	public void commandAction(Command command, Displayable display)
  {
	  if(display==frmMain) {
      _handleFormMain(command);
	  }
    else if (display==frmResult) {
      _handleFormResult(command);
	  } 
	}

 	/**
	 * Handling events for Main Form
	 */
    private void _handleFormMain(Command command)
    {
    	if (command==cmdOK) {		
	      // show progress bar
	      _showFormShowProgress();

	      // calling stub
	      Thread t = new Thread() {
          public void run() {
		        callStub();
		        // show the result
		        _disposeFormShowProgress();
            _disposeFormMain();
            _showFormResult();
	        }
        };
	      t.start();
      }
      else if (command==cmdExit) {
		    destroyApp(true);
      }
    }

	/**
	 * Handling event for the Form to enter name
	 */
	private void _handleFormResult(Command command)
  {
		if (command==cmdOK) {
			_disposeFormResult();
      _showFormMain();
		}
    else if (command==cmdExit) {
			destroyApp(true);
		}
	}

    
	/**
	 * Initialize the main form
	 */
	private void _showFormMain()
  {
		if (frmMain==null) frmMain = new Form(APP_TITLE);
		
		// construct items
		sWSName= new StringItem(null, "Call Stub");
            
		// append items to the form
		frmMain.append(sWSName);

    // commands
    frmMain.addCommand(cmdOK);
    frmMain.addCommand(cmdExit);
    frmMain.setCommandListener(this);
	    
    // show it
		rootDisplay.setCurrent(frmMain);
	}

	/**
	 * Dispose the main form
	 */
	private void _disposeFormMain()
  {
		sWSName= null;
		frmMain = null;
	}

	/**
	 * Initialize the form to show results
	 */
	private void _showFormResult()
  {
	  if(frmResult==null) frmResult = new Form("Test Result");

    // construct items
    sCallResult= new StringItem(null, sCallResultString);

    // append items to the form
	  frmResult.append(sCallResult);

    // commands
    frmResult.addCommand(cmdOK);
    frmResult.addCommand(cmdExit);
	  frmResult.setCommandListener(this);
	    
    // show it
	  rootDisplay.setCurrent(frmResult);
	}

	/**
	 * Dispose the form of results
	 */
	private void _disposeFormResult()
  {
		sCallResult= null;
		frmResult = null;
	}

	/**
	 * Initialize the Form to show progress
	 */
	private void _showFormShowProgress()
  {
		if(frmShowProgress==null) frmShowProgress = new Form(null);

	   	// construct items
		Ticker ticker = new Ticker(PROCESSING);
		sitemPleaseWait = new StringItem(null, PLEASE_WAIT);

		// append them
		frmShowProgress.setTicker(ticker);
		frmShowProgress.append(sitemPleaseWait);

		// commands
    //no Commands
        
    // show it
		rootDisplay.setCurrent(frmShowProgress);
	}

	/**
	 * Dispose the Form to show progress
	 */
	private void _disposeFormShowProgress()
  {
    frmShowProgress.setTicker(null);
    frmShowProgress = null;
	}

	// app tile
	private final static String APP_TITLE = "Test J2ME WS Stub";
	// error
	public final static String ERROR     = "error";

	// command labels
	private final static String OK     = "OK";
	private final static String EXIT   = "EXIT";

	// all the commands
	private Command cmdOK        = new Command(OK, Command.OK, 1);
  private Command cmdExit      = new Command(EXIT, Command.OK, 1);

	//all the displayable (screens: forms, lists, lists, textboxes)
	// main form
	private Form frmMain = null;
	private StringItem sWSName= null;

	// Form show result
	private Form frmResult = null;
  private StringItem sCallResult= null;

    // Form: Show Progress
	private Form frmShowProgress = null;
	private final static String PROCESSING = "Calling Web Service...";
  private final static String PLEASE_WAIT = "\n\n       Please Wait";
  private StringItem sitemPleaseWait = null;

	// main display
	private Display rootDisplay = null;
	

}
