package CreaOO6t4a;
//Title: Creative OO 6 :Exceptions
//Copyright: Copyright (c) 2001
//Author: John Fenton
//Company: LMU
//Description:
//Demonstration of Exception handling techniques.
//Brings up a dialog box instead of putting 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;
private Panel pan1;
private Checkbox cb1;
//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();
pan1 = new Panel();
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(pan1,con);
butStart.setBackground(new Color(192,255,192));
pan1.add(butStart);
butCancel = new Button("Cancel");
butCancel.addActionListener(dialogueObj);
butCancel.setBackground(new Color(192,192,224));
pan1.add(butCancel);
butStop = new Button(" Stop ");
butStop.addActionListener(dialogueObj);
butStop.setBackground(new Color(224,192,192));
pan1.add(butStop);
cb1 = new Checkbox("Automatically increment",false);
pan1.add(cb1);
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(pan1);
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 mO)
{
appObj = mO;
}
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");
String mess = "Please press the Start button when ready";
MessageDialogue md = new MessageDialogue(viewObj, mess);
md.show();
}
}
}
class ModelCl
{
String startingNumStr;
BigInteger startingNumBI;
ViewCl viewObj;
ControllerCl dialObj;
Container c;
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!");
String mess = "No data entered!";
MessageDialogue md = new MessageDialogue(viewObj, mess);
md.show();
return;
}
try
{
startingNumBI = new BigInteger(startingNumStr);
}
catch (NumberFormatException nfe)
{
//viewObj.writeMessage("Not a valid number!");
String mess = "Not a valid nunmber";
MessageDialogue md = new MessageDialogue(viewObj, mess);
md.show();
return;
}
int i = startingNumBI.signum();
if ( i < 1 )
{
//viewObj.writeMessage("Must be a positive number greater than 0");
String mess = "Must be a positive number greater than 0";
MessageDialogue md = new MessageDialogue(viewObj, mess);
md.show();
return;
}
}
}
class NoDataException extends Exception
{
public NoDataException()
{
// Nothing special to do here
}
}
class MessageDialogue extends Dialog implements ActionListener
{
Button okBut;
Label messageLab,blank1,blank2;
public MessageDialogue(Frame parent, String message)
{
super(parent, "Error, error :", true); //true = modal
messageLab = new Label(message);
add(messageLab, BorderLayout.NORTH);
blank1 = new Label(" ");
add(blank1,BorderLayout.WEST);
blank2 = new Label(" ");
add(blank2, BorderLayout.EAST);
okBut = new Button("OK");
okBut.addActionListener(this);
add(okBut, BorderLayout.CENTER);
pack();
Point p = parent.getLocationOnScreen();
p.translate(parent.getSize().width/2-120,parent.getSize().height/2-50);
setLocation(p);
//setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
setVisible(false);
}
}
|