FileDocCategorySizeDatePackage
creaoo6t4.javaAPI DocExample6582Sat Mar 31 10:41:54 BST 2001CreaOO6t4

creaoo6t4.java

package CreaOO6t4;
//Title:      Creative OO 6 :Exceptions
//Copyright:  Copyright (c) 2001
//Author:     John Fenton
//Company:    LMU
//Description:
//Demonstration of Exception handling techniques.
//Better to bring up a dialog box than put error messages into textfield
//No "works" yet - only inputs starting number and checks validity

import java.awt.*;
import java.awt.event.*;
import java.math.*;
//import javax.swing.*;//if using the Swing classes

public class ThreeX
{
	public ThreeX()
	{
		ViewCl presentationObj = new ViewCl();
	}

	public static void main (String[] args)
	{
		ThreeX threeXObj = new ThreeX();
	}
}
		
class ViewCl extends Frame  //or JFrame if using Swing
{
  private ControllerCl dialogueObj;
  private ModelCl applicationObj;
  private Label lab1,lab2,lab3,lab4;
  private GridBagLayout m;
  private GridBagConstraints con;
  protected Container c;
  protected TextField tf1;
  protected TextField tf2;
  protected TextField tf3;
  protected TextField tf4;
  protected Button butStart;
  protected Button butCancel;
  protected Button butStop;

  public ViewCl()
  {
    dialogueObj = new ControllerCl(this);
    applicationObj = new ModelCl(this);
    dialogueObj.getAppObj(applicationObj);
    applicationObj.getDialObj(dialogueObj);
		setSize(700,500);
		setTitle("Three X Plus One Application");
		drawInterface();
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing (WindowEvent we)
			{
				System.exit(0);
			}
		});
		setVisible(true);
    tf1.setCaretPosition(0);
  }

	public void drawInterface()
	{
      m = new GridBagLayout();
      lab1 = new Label("Enter number to try:");
      con = new GridBagConstraints();
      con.gridx = 0;
      con.gridy = 0;
      con.gridwidth = GridBagConstraints.REMAINDER;
      con.ipady = 20;
      con.anchor = GridBagConstraints.WEST;
      m.setConstraints(lab1,con);
      tf1 = new TextField(50);
      tf1.setEditable(true);
      tf1.addActionListener(dialogueObj);
      con.gridx = 0;
      con.gridy = 1;
      con.ipady = 5;
      con.weightx = 0.5;
      con.fill = GridBagConstraints.HORIZONTAL;
      m.setConstraints(tf1,con);
      butStart = new Button("Start");
      butStart.addActionListener(dialogueObj);
      con.gridx = 1;
      con.gridy = 2;
      con.gridwidth = 1;
      con.fill = GridBagConstraints.NONE;
      con.anchor = GridBagConstraints.WEST;
      con.insets = new Insets(10,10,10,10);
      m.setConstraints(butStart,con);
      butStart.setBackground(new Color(-4136756));
      butCancel = new Button("Cancel");
      butCancel.addActionListener(dialogueObj);
      con.gridx = 2;
      con.gridy = 2;
      con.anchor = GridBagConstraints.CENTER;
      con.weightx = 0.5;
      m.setConstraints(butCancel,con);
      butCancel.setBackground(new Color(12632288));
      butStop = new Button("Stop");
      butStop.addActionListener(dialogueObj);
      con.gridx = 3;
      con.gridy = 2;
      con.anchor = GridBagConstraints.EAST;
      m.setConstraints(butStop,con);
      butStop.setBackground(new Color(-2047796));
      con.insets = new Insets(0,0,0,0);
      lab2 = new Label("Largest number generated so far:");
      con.gridx = 0;
      con.gridy = 3;
      con.ipady = 20;
      con.gridwidth = GridBagConstraints.REMAINDER;
      con.anchor = GridBagConstraints.WEST;
      m.setConstraints(lab2,con);
      tf2 = new TextField(50);
      tf2.setEditable(false);
      con.gridx = 0;
      con.gridy = 4;
      con.ipady = 5;
      con.fill = GridBagConstraints.HORIZONTAL;
      m.setConstraints(tf2,con);
      lab3 = new Label("Smallest number generated so far: ");
      con.gridx = 0;
      con.gridy = 5;
      con.ipady = 20;
      con.gridwidth = GridBagConstraints.REMAINDER;
      con.anchor = GridBagConstraints.WEST;
      m.setConstraints(lab3,con);
      tf3 = new TextField(50);
      tf3.setEditable(false);
      con.gridx = 0;
      con.gridy = 6;
      con.ipady = 5;
      m.setConstraints(tf3,con);
      lab4 = new Label("Total number of iterations: ");
      con.gridx = 0;
      con.gridy = 7;
      con.ipady = 20;
      con.gridwidth = GridBagConstraints.REMAINDER;
      con.anchor = GridBagConstraints.WEST;
      m.setConstraints(lab4,con);
      tf4 = new TextField(50);
      tf4.setEditable(false);
      con.gridx = 0;
      con.gridy = 8;
      con.ipady = 5;
      m.setConstraints(tf4,con);
      setLayout(m);
      add(lab1);
      add(tf1);
      add(butStart);
      add(butCancel);
      add(butStop);
      add(lab2);
      add(tf2);
      add(lab3);
      add(tf3);
      add(lab4);
      add(tf4);
      //validate();
	}

  public void writeMessage(String mess)
  {
    tf4.setText(mess);
  }

  public String getData()
  {
    String s = tf1.getText();
    return s;
  }
}

class ControllerCl implements ActionListener
{
  ViewCl viewObj;
  ModelCl appObj;

  public ControllerCl(ViewCl vO)
  {
    viewObj = vO;
  }

  public void getAppObj(ModelCl oModel)
  {
    appObj = oModel;
  }

  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getSource() == viewObj.butStart)
    {
       viewObj.writeMessage("Running");
       appObj.getAndValidateData();
    }
    if(ae.getSource() == viewObj.tf1)
    {
       viewObj.writeMessage("Please press Start button when ready");
    }
  }
}

class ModelCl
{
  String startingNumStr;
  BigInteger startingNumBI;
  ViewCl viewObj;
  ControllerCl dialObj;

  public ModelCl(ViewCl vO)
  {
    viewObj = vO;
  }

  public void getDialObj(ControllerCl cO)
  {
    dialObj = cO;
  }

  public void getAndValidateData( )
  {
    try
    {
       startingNumStr = viewObj.getData( );
       if (startingNumStr.equals(""))
       {
          throw new NoDataException( );
       }
    }
    catch (NoDataException nde)
    {
        viewObj.writeMessage("No data entered!");
        return;
    }

    try
    {
       startingNumBI = new BigInteger(startingNumStr);
    }
    catch (NumberFormatException nfe)
    {
       viewObj.writeMessage("Not a valid number!");
       return;
    }

    int i = startingNumBI.signum();
    if ( i < 1 )
    {
        viewObj.writeMessage("Must be a positive number greater than 0");
        return;
    }
  }
}

class NoDataException extends Exception
{
   public NoDataException()
   {
      // Nothing special to do here
   }
}