/***************************************************************************************/
/* cfw: simple J2ME framework for HTTP connections                                     */
/*-------------------------------------------------------------------------------------*/
/* class files:                                                                        */
/*  c_main.java - main class for setting up application midlet                         */
/*  c_run.java  - thread class for operating the HTTP connection                       */
/*-------------------------------------------------------------------------------------*/
/* This source code is free for any kind of personal or educational use,               */
/* which is absolutely non-profitable.                                                 */
/*                                                                                     */
/* This source code as all derivatives of it may not be used for any                   */
/* commercial purpose. This especially excludes any rights for claiming any            */
/* kind of patents, warranty, applicability for a certain purpose, as                  */
/* finally for earning money on base of the before described inclusion range.          */
/*-------------------------------------------------------------------------------------*/
/* (c) 2004 H. Weghorn, BA-University of Cooperative Education, Germany                */
/***************************************************************************************/

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

// MIDlet application class
public class C_main extends MIDlet implements CommandListener {
  /*************** G L O B A L   S E T T I N G S ***********************************/
  final String urlres = "/url.txt"; // resource file with WEB path
  final String urldef = "http://www.ba-stuttgart.de/~weghorn/j2me.txt"; // default
  final boolean debug = true; // debugging flag for some trace output to STDOUT 
  /*********************************************************************************/

  Display disp;         // display hook
  Form    fo;           // main form with command button
  Thread  my_r;         // reference to C_run
  String  url = urldef; // path to WEB page for being loaded and displayed

  // construction of MIDlet
  public C_main() { /* nothing to be done */}

  // here we go ...
  protected void startApp() {
    // first, figure out target URL
    InputStream urlfile = getClass().getResourceAsStream( urlres );
    if ( urlfile != null ) {
      boolean err = false;
      StringBuffer usb = new StringBuffer();
      try {
        int nextc; byte[] nextb = new byte[1];
        while ( (nextc=urlfile.read()) != -1 ) {
          nextb[0] = (byte) nextc;
          usb.append ( new String(nextb) );
        }
        urlfile.close ();
      }
      catch ( Exception e ) { err = true; }
      if ( debug ) System.out.println ( "URL from resource: " + usb );
      if ( !err && usb.length() > 5 ) url = new String(usb);
    }

    // next, assemble UI screen
    disp = Display.getDisplay(this); 
    fo = new Form ("Accessing");
    fo.append(new StringItem("", url));
    fo.addCommand(new Command("Done", Command.SCREEN, 1));
    fo.setCommandListener(this);

    // last step, create & launch thread for HTTP operation
    my_r = new C_run ( disp, fo, url, debug );
    my_r.start ();
  }

  // actions for making MIDlet sleep
  protected void pauseApp() {/* should be filled with appropriate actions */}

  // termination acctions for MIDlet
  protected void destroyApp(boolean unconditional) {
    disp = null; fo = null; my_r = null; url = null; System.gc ();
    this.notifyDestroyed();
  }

  // method for command listening
  public void commandAction(Command c, Displayable d) { 
    destroyApp(true); // only one action possible
  }
}