//SimpleDB //Basic client-side read only database //Andy Harris, 06/00 import java.awt.*; import java.applet.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; import Prompt; public class SimpleDB extends Applet { //Components Label[] lbl; TextField[] txt; Label lblRecNum = new Label("1"); Button btnNext = new Button("Next"); Button btnPrev = new Button("Prev"); Button btnSearch = new Button("Search"); Button btnAdd = new Button("Add"); Button btnCopy = new Button("Copy"); Panel pnlData = new Panel(); Panel pnlLabels = new Panel(); Panel pnlControl = new Panel(); //Data Manipulation Variables String[] records = new String[100]; int numFields = 0; int recNum = 0; int recordCount = 0; String dataFile = "pets.dat"; //Custom classes EventHandler evt = new EventHandler(); Prompt p; CopyFrame cf; public void init(){ //make a prompt for input and warnings p = new Prompt((Frame)this.getParent()); //get the datafile parameter from HTML dataFile = getParameter("DataFile"); if (dataFile == null){ p.say("Couldn't find file!!"); dataFile = "pets.dat"; } // end if //Basic Layout String myData = new String(); setLayout(new BorderLayout()); add(pnlData, BorderLayout.CENTER); add(pnlControl, BorderLayout.SOUTH); add(pnlLabels, BorderLayout.WEST); pnlData.setLayout(new GridLayout(0,1)); pnlLabels.setLayout(new GridLayout(0,1)); try { //grab the data URL dataURL = new URL(getDocumentBase(), dataFile); //create a reader to get data InputStream is = dataURL.openStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); String currentLine = in.readLine(); //First line contains field names StringTokenizer st = new StringTokenizer(currentLine, "\t"); //Figure out number of fields in database numFields = st.countTokens(); //create arrays lbl = new Label[numFields]; txt = new TextField[numFields]; //Add the correct number of components for(int counter = 0; counter < numFields; counter++){ lbl[counter] = new Label(st.nextToken()); txt[counter] = new TextField(); //txt[counter].setEditable(false); pnlLabels.add(lbl[counter]); pnlData.add(txt[counter]); } // end for loop //fill up the records array while ( currentLine != null){ records[recNum] = currentLine; currentLine = in.readLine(); recNum++; } // end loop //close up all the streams is.close(); isr.close(); in.close(); //handle any exceptions that might have occurred } catch (MalformedURLException exc){ p.say("Error with URL"); } catch (IOException exc) { p.say("Error getting data"); dataFile = "pets.dat"; } // end try //add the control pad pnlControl.setLayout(new GridLayout(1,0)); pnlControl.add(new Label("Record #")); pnlControl.add(lblRecNum); pnlControl.add(btnPrev); pnlControl.add(btnCopy); pnlControl.add(btnSearch); pnlControl.add(btnAdd); pnlControl.add(btnNext); //register listeners btnPrev.addActionListener(evt); btnNext.addActionListener(evt); btnCopy.addActionListener(evt); btnAdd.addActionListener(evt); btnSearch.addActionListener(evt); //show first record recordCount = recNum - 1; recNum = 1; displayRec(); } // end init private void storeRec(){ //store the current record to the records[] array String currentLine = ""; for(int counter = 0; counter < numFields; counter++){ currentLine += txt[counter].getText(); //don't put a tab after the last field if(counter < numFields -1){ currentLine += "\t"; } // end if } // end for loop records[recNum] = currentLine; } // end storeRec private void displayRec(){ //Display the current record on the form String currentLine = records[recNum]; StringTokenizer st = new StringTokenizer(currentLine, "\t"); for(int counter = 0; counter < numFields; counter++){ txt[counter].setText(st.nextToken()); } // end for loop lblRecNum.setText(String.valueOf(recNum)); } //end displayRec private void search(){ //search for a record String currentLine = ""; p.ask("Search for: "); String searchVal = p.getResponse(); for (int i = 1; i < recordCount; i++){ currentLine = records[i]; if (currentLine.indexOf(searchVal) != -1){ recNum = i; displayRec(); p.say("Record found"); } // end if } // end for } // end search private void copy(){ //Display all records in a form suitable for copying //Use CopyFrame to do all the work cf = new CopyFrame(); } // end copy private class EventHandler extends WindowAdapter implements ActionListener{ //Handle most of the events for the database public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("Prev")){ storeRec(); recNum --; //trap for first record if (recNum < 1){ recNum = recordCount - 1; } // end if displayRec(); } else if (e.getActionCommand().equals("Next")){ storeRec(); recNum++; //trap for last record if (recNum >= recordCount){ recNum = 1; } // end if displayRec(); } else if (e.getActionCommand().equals("Search")){ search(); } else if (e.getActionCommand().equals("Copy")){ copy(); } else if (e.getActionCommand().equals("Add")){ recordCount++; recNum = recordCount - 1; for(int counter = 0; counter < numFields; counter++){ txt[counter].setText(""); } // end for loop lblRecNum.setText(String.valueOf(recNum)); } // end if } // end actionPerformed public void windowClosing(WindowEvent e){ Window theWindow = (Window)e.getSource(); theWindow.setVisible(false); theWindow.dispose(); } // end windowClosing } // end EventHandler class private class CopyFrame extends Frame implements ActionListener{ //class designed to encapsualte displaying data Label instructions = new Label("Use mouse to select and copy data"); TextArea txtData = new TextArea(); Panel pnlSouth = new Panel(); Button btnClose = new Button("Close"); public CopyFrame(){ super("Copy Data"); setLayout(new BorderLayout()); add(instructions, BorderLayout.NORTH); add(txtData, BorderLayout.CENTER); pnlSouth.add(btnClose); add(pnlSouth, BorderLayout.SOUTH); for (int i = 0; i < recordCount; i++){ txtData.append(records[i]); txtData.append("\n"); } // end for loop setSize(300,200); setVisible(true); btnClose.addActionListener(this); addWindowListener(evt); } // end constructor //handle button here public void actionPerformed(ActionEvent e){ setVisible(false); dispose(); } // end actionPerformed } // end copyFrame classDef } // end main class def