/* * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED * Use of this software is authorized pursuant to the terms * of the license found at * http://developer.java.sun.com/berkeley_license.html. */ package divelog; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; import divelog.UIWestPanel; public class DiveHandler extends JPanel {//Opens DiveHandler class private ObjectOutputStream output; private ObjectInputStream input; private UIWestPanel ui; private DiveRecord Dr; protected JButton enterButton; protected JButton viewButton; private static int callIndex; private static boolean calledAlready = false; private static Vector _cacheList = new Vector(); private static int objectCount ; public DiveHandler() {//Opens constructor //Initialize and add graphic user interface ui = new UIWestPanel(); add("Center",ui); try { //open try output = new ObjectOutputStream ( new FileOutputStream ("diveLogs.dat", true)); } catch (IOException ioex) {//open catch JOptionPane.showMessageDialog(null, "Error during reading file", null, JOptionPane.ERROR_MESSAGE); }//close catch try{ input = new ObjectInputStream( new FileInputStream("diveLogs.dat")); } catch (IOException ioex) {//open catch JOptionPane.showMessageDialog(null, "Error during reading file", null, JOptionPane.ERROR_MESSAGE); }//close catch enterButton = ui.getEnter(); enterButton.setText("Enter"); enterButton.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent ae) { //Open method ui.getAirIntake(); ui.getCount(); addDive(); }//Close method } ); viewButton = ui.getView(); viewButton.setText("View"); viewButton.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent ae) { //Open method viewDive(); }//Close method } ); } public void addDive() { //Open addDive Vector values = ui.getUIFields(); //Gets each item and assigns it to a //variable of the proper type. String t = (String)values.get(0); System.out.println(t); String d = (String)values.get(1); System.out.println(d); //Converting strings to ints int dth = Integer.parseInt((String)values.get(2)); System.out.println(dth); int s = Integer.parseInt((String)values.get(3)); System.out.println(s); int e = Integer.parseInt((String)values.get(4)); System.out.println(e); int u = Integer.parseInt((String)values.get(5)); System.out.println(u); int bt = Integer.parseInt((String)values.get(6)); System.out.println(bt); int c = Integer.parseInt((String)values.get(7)); System.out.println(c); int v = Integer.parseInt((String)values.get(8)); System.out.println(v); String cts = (String)values.get(9); System.out.println(cts); //Calls the setDive method of the Dive Record //to create the dive object, passing the //assigned items from above. try { //open try Dr = new DiveRecord(); Dr.setDive(t, d, dth, s, e, u, bt, c, v, cts); output.writeObject (Dr); output.flush(); objectCount ++; if(input != null) { input.close(); input = null; } } //close try catch (NumberFormatException nef) { JOptionPane.showMessageDialog(this, "You need to fill out all fields", "Invalid fields", JOptionPane.ERROR_MESSAGE); }//close catch catch (FileNotFoundException fnf) { JOptionPane.showMessageDialog(this, "File not found", null, JOptionPane.ERROR_MESSAGE); }//close catch catch (IOException io) {//open catch JOptionPane.showMessageDialog(this, "Error closing file", null, JOptionPane.ERROR_MESSAGE); closeFile(); }//close catch ui.clearFields(); }//Close method public void viewDive() { try { DiveRecord Dr; if(input == null) { try{ input = new ObjectInputStream(new FileInputStream("diveLogs.dat")); }catch (IOException ioex){ JOptionPane.showMessageDialog(null, "Error during reading file", null, JOptionPane.ERROR_MESSAGE); } } Dr = (DiveRecord) input.readObject(); String svalues[] = { Dr.getType(), Dr.getDte(), String.valueOf(Dr.getDepth()), String.valueOf(Dr.getStart()), String.valueOf(Dr.getEnd()), String.valueOf(Dr.getUsed()), String.valueOf(Dr.getTime()), String.valueOf(Dr.getCount()), String.valueOf(Dr.getVis()), Dr.getComments()}; ui.setFields(svalues); return; }//close try catch (EOFException eofex){ JOptionPane.showMessageDialog (null,"No more records in file",null, JOptionPane.ERROR_MESSAGE); }//close catch catch (ClassNotFoundException cnfex){ JOptionPane.showMessageDialog(null,"Unable to open object", null, JOptionPane.ERROR_MESSAGE); }//close catch catch (IOException ioex) { JOptionPane.showMessageDialog(null,"Error during reading file", null, JOptionPane.ERROR_MESSAGE); }//close catch } public void closeFile() { try { input.close(); } catch (IOException ioe) { JOptionPane.showMessageDialog(this, "Problem closing file", "Error", JOptionPane.ERROR_MESSAGE); } } }// closes DiveHandler class