Skip to main content

skip to main content

developerWorks  >  Java technology  >

Revisiting Java technology on the client

Make use of the full spectrum of the Java platform to build compelling systems

developerWorks


Listing 4. The Java code for the weblet class


import org.w3c.dom.*;
import org.w3c.dom.html.*;
import org.w3c.dom.events.*;
import com.ibm.weblet.*;
import java.net.*;

public class ConversationDemo implements Weblet
{
  private WebletContext m_engine;
  private HTMLDocument doc;
  private HTMLElement HeadingSection;
  private HTMLElement FormSection;
  private HTMLElement ControlsSection;
  private HTMLFormElement PersonalInformationForm;
  private HTMLFormElement AddressInformationForm;
  private ControlsEventListener LocalEventHandler = new ControlsEventListener();
  private int ProcessingStep = 0;
  private HTMLButtonElement RetreatButton;
  private HTMLButtonElement ContinueButton;
  private Node BreakingSpace;
  

  /** Creates new ConversationDemo */
  public ConversationDemo()
  {
  }

  /**
   * Called when the Weblet is first loaded into memory.
   */
  public void init(WebletContext engine)

  {
    try
    {
      Element GenericElement;
      m_engine = engine;
      doc = m_engine.getOwnerDocument();
      BreakingSpace = doc.createTextNode(" ");
      GenericElement = doc.getElementById("HeadingSection");
      if(GenericElement instanceof HTMLElement)
        HeadingSection = (HTMLElement)GenericElement;
      else
        throw(new NotImplementedException());
      GenericElement = doc.getElementById("FormSection");
      if(GenericElement instanceof HTMLElement)
        FormSection = (HTMLElement)GenericElement;
      else
        throw(new NotImplementedException());
      GenericElement = doc.getElementById("ControlsSection");
      if(GenericElement instanceof HTMLElement)
        ControlsSection = (HTMLElement)GenericElement;
      else
        throw(new NotImplementedException());
      GenericElement = doc.getElementById("PersonalInformationForm");
      if(GenericElement instanceof HTMLFormElement)
        PersonalInformationForm = (HTMLFormElement)GenericElement;
      else
        throw(new NotImplementedException());
      GenericElement = doc.getElementById("AddressInformationForm");
      if(GenericElement instanceof HTMLFormElement)
        AddressInformationForm = (HTMLFormElement)GenericElement;
      else
        throw(new NotImplementedException());
      GenericElement = doc.getElementById("ContinueButton");
      if(GenericElement instanceof HTMLButtonElement)
        ContinueButton = (HTMLButtonElement)GenericElement;
      else
        throw(new NotImplementedException());
      GenericElement = doc.getElementById("RetreatButton");
      if(GenericElement instanceof HTMLButtonElement)
        RetreatButton = (HTMLButtonElement)GenericElement;
      else
        throw(new NotImplementedException());

    } catch (ComException ComEx)
    {
      String exName = ComEx.getClass().getName();
      m_engine.showDialog("Fatal exception in Weblets Plugin in init()", exName);
    } catch (NotImplementedException NotImpEx)
    {
      Transcript.println("Source HTML page is missing required elements in init()");
      Transcript.printStackTrace(NotImpEx);
      Transcript.showTranscript(true);
    } catch (DOMException DOMEx)
    {
      Transcript.println("Failure accessing DOM tree in init()");
      Transcript.printStackTrace(DOMEx);
      Transcript.showTranscript(true);
    } catch (org.w3c.dom.events.EventException EventEx)
    {
      Transcript.println("Failure accessing DOM events in init()");
      Transcript.printStackTrace(EventEx);
      Transcript.showTranscript(true);
    }
    catch(Exception e)
    {
      Transcript.println("Other Exception in init()");
      Transcript.printStackTrace(e);
      Transcript.showTranscript(true);
    }
  }

  /**
   * Called after the Weblet is setup and ready to go.  This is called each time
   * the browser refreshes the page or returns to the page.
   */
  public void start()
  {

// Note: there are four exception types that any DOM or weblet call could raise.
//  these are caught by this try/catch block.  The ComException and NotImplementedException
//  are both Runtime Exceptions, so they could have been ignored, although that is not
//  good practice.
    try
    {
      ControlsSection.removeChild(RetreatButton);
      FormSection.removeChild(PersonalInformationForm);
      FormSection.removeChild(AddressInformationForm);

      addTextIntroduction();

      ((EventTarget)ContinueButton).addEventListener("click", LocalEventHandler, false);
      ((EventTarget)RetreatButton).addEventListener("click", LocalEventHandler, false);

    } catch (ComException ComEx)
    {
      String exName = ComEx.getClass().getName();
      m_engine.showDialog("Fatal exception in Weblets Plugin in start()", exName);
    } catch (NotImplementedException NotImpEx)
    {
      Transcript.println("Method not implemented in this version of DirectDOM(tm) in start()");
      Transcript.printStackTrace(NotImpEx);
      Transcript.showTranscript(true);
    } catch (DOMException DOMEx)
    {
      Transcript.println("Failure accessing DOM tree in start()");
      Transcript.printStackTrace(DOMEx);
      Transcript.showTranscript(true);
    } catch (org.w3c.dom.events.EventException EventEx)
    {
      Transcript.println("Failure accessing DOM events in start()");
      Transcript.printStackTrace(EventEx);
      Transcript.showTranscript(true);
    }
  }

  /**
   * Called just before the browser leaves the page the Weblet is on.
   */
  public void stop()
  {
    ((EventTarget)RetreatButton).removeEventListener("click", LocalEventHandler, false);
    ((EventTarget)ContinueButton).removeEventListener("click", LocalEventHandler, false);
  }

  /**
   * Called just before the Weblet is removed from memory.
   */
  public void destroy()
  {

  }

  protected void addTextIntroduction()
  {
    StringBuffer IntroParagraphText = new StringBuffer();
    IntroParagraphText.append("This evening we will be working through");
    IntroParagraphText.append(" a demonstration weblet together.  ");
    IntroParagraphText.append("This weblet works with a servlet on the");
    IntroParagraphText.append(" server to provide a more responsive ");
    IntroParagraphText.append("user interface, and to permit data to be");
    IntroParagraphText.append(" transferred without the usual ");
    IntroParagraphText.append("limitations of browser-based communications.");

    try
    {
      Element TitleParagraph = doc.createElement("P");
      TitleParagraph.appendChild(
         doc.createTextNode("Welcome to the Weblets Conversation Demonstration."));

      Element HeadingElement = doc.createElement("H1");
      HeadingElement.appendChild(TitleParagraph);
      HeadingElement.appendChild(doc.createElement("/P"));

      Element IntroParagraph = doc.createElement("P");
      IntroParagraph.appendChild(doc.createTextNode(IntroParagraphText.toString()));

      Element CenterElement = doc.createElement("Center");
      CenterElement.appendChild(HeadingElement);
      CenterElement.appendChild(doc.createElement("/H1"));

      HeadingSection.appendChild(CenterElement);
      HeadingSection.appendChild(doc.createElement("/Center"));
      HeadingSection.appendChild(IntroParagraph);
      HeadingSection.appendChild(doc.createElement("/P"));
    } catch (DOMException DOMEx)
    {
      Transcript.println("Failure accessing DOM tree in start()");
      Transcript.printStackTrace(DOMEx);
      Transcript.showTranscript(true);
    } catch (org.w3c.dom.events.EventException EventEx)
    {
      Transcript.println("Failure accessing DOM events in start()");
      Transcript.printStackTrace(EventEx);
      Transcript.showTranscript(true);
    }
  }

  private boolean validatePersonalForm()
  {
    return true;
  }

  private boolean validateAddressForm()
  {
    return true;
  }

  private class ControlsEventListener implements EventListener
  {
    public void handleEvent(Event ControlEvent)
    {
     boolean GoForward;
      try
      {
        GoForward = (((HTMLButtonElement)
          (ControlEvent.getTarget())).getName().equalsIgnoreCase("ContinueButton"));
        switch(ProcessingStep)
        {
          case 0:
            if(GoForward == true)
            {
              FormSection.appendChild(PersonalInformationForm);
              ControlsSection.appendChild(RetreatButton);
              ControlsSection.appendChild(BreakingSpace);
              ControlsSection.appendChild(ContinueButton);
              ContinueButton.setValue("Next");
              RetreatButton.setValue("Previous");
              ProcessingStep = 1;
            }
            else
            {
            }
            break;
          case 1:
            if(GoForward)
            {
              if(validatePersonalForm());
              {
                FormSection.removeChild(PersonalInformationForm);
                FormSection.appendChild(AddressInformationForm);
                ContinueButton.setValue("Submit");
                ProcessingStep = 2;
              }
            }
            else
            {
              FormSection.removeChild(PersonalInformationForm);
              ControlsSection.removeChild(RetreatButton);
              ContinueButton.setValue("Continue");
              ProcessingStep = 0;
            }
            break;
          case 2:
            if(GoForward)
            {
              if(validateAddressForm())
              {
               // add code here to submit the form data.
               // a thought: have another from with nothing but hidden
               // fields for the required data and a have the browser
               // submit that form.
              }
            }
            else
            {
              FormSection.removeChild(AddressInformationForm);
              FormSection.appendChild(PersonalInformationForm);
              ControlsSection.appendChild(RetreatButton);
              ControlsSection.appendChild(BreakingSpace);
              ControlsSection.appendChild(ContinueButton);
              RetreatButton.setValue("Previous");
              ContinueButton.setValue("Next");
              ProcessingStep = 1;
            }
            break;
        }
      Runtime.getRuntime().gc();
      } catch (ComException ComEx)
      {
        String exName = ComEx.getClass().getName();
        m_engine.showDialog("Fatal exception in Weblets Plugin in handleEvent()", exName);
      } catch (NotImplementedException NotImpEx)
      {
        Transcript.println("Method not implemented in this version" +
                           " of DirectDOM(tm) in handleEvent()");
        Transcript.printStackTrace(NotImpEx);
        Transcript.showTranscript(true);
      } catch (DOMException DOMEx)
      {
        Transcript.println("Failure accessing DOM tree in handleEvent()");
        Transcript.printStackTrace(DOMEx);
        Transcript.showTranscript(true);
      } catch (org.w3c.dom.events.EventException EventEx)
      {
        Transcript.println("Failure accessing DOM events in handleEvent()");
        Transcript.printStackTrace(EventEx);
        Transcript.showTranscript(true);
      }
      catch(Exception e)
      {
        Transcript.println("Other Exception in handleEvent()");
        Transcript.printStackTrace(e);
        Transcript.showTranscript(true);
      }
    }
  }


}


Return to article