/***************************************************************************** ******************************CU Organizer************************************ ******************************May 1999************************************* ***********************Brian Morgan and Rhett Dillingham********************** *****************************************************************************/ import java.awt.*; import java.io.*; import java.util.*; import javax.comm.*; // § alt - 789 // ô alt - 147 // Used to handle Contact information in one compact object with // helper functions for retrieval of appropriately sized // data class Contact { public String FirstName,LastName; // First and Last name of present contact public String Address1,Address2; // Two lines of contacts address public String Phone1,Phone2,Phone3; // Contacts phone number public String temp1,temp2; public static int MaxChar = 20; // Maximum number of characters per line // constructor that takes all the values public Contact(String FN, String LN, String Add1, String Add2, String PH1, String PH2, String PH3){ FirstName = FN; LastName = LN; Address1 = Add1; Address2 = Add2; Phone1 = PH1; Phone2 = PH2; Phone3 = PH3; } // empty constructor that fills everything with blank strings public Contact() { FirstName = " "; LastName = " "; Address1 = " "; Address2 = " "; Phone1 = " "; Phone2 = " "; Phone3 = " "; } // get name firstname,lastname public String getNameLF() { temp1 = LastName + "," + FirstName; if (FirstName.length() + LastName.length() + 1 <= MaxChar) return temp1; temp2 = ""; for (int i=0; i<MaxChar; i++) { temp2+=temp1.charAt(i); } return temp2; } // get name firstname lastname public String getNameFL() { temp1 = FirstName + " " + LastName; if (FirstName.length() + LastName.length() + 1 <= MaxChar) return temp1; temp2 = ""; for (int i=0; i<MaxChar; i++) { temp2+=temp1.charAt(i); } return temp2; } // return lastname of size 10 public String getLastName() { temp1 = LastName; if (LastName.length() == 10) { return LastName; }else if (LastName.length() < 10) { while (LastName.length() != 10) { LastName += " "; } return LastName; } else { LastName = ""; for (int i=0; i<10; i++) { LastName+=temp1.charAt(i); } return LastName; } } // get first name of size 10 public String getFirstName() { temp1 = FirstName; if (FirstName.length() == 10) { return FirstName; } else if (FirstName.length() < 10) { while (FirstName.length() != 10) { FirstName += " "; } return FirstName; } else { FirstName = ""; for (int i=0; i<10; i++) { FirstName+=temp1.charAt(i); } return FirstName; } } // get first line of address of size 20 public String getAddress1() { temp1 = Address1; if (Address1.length() == MaxChar) { return Address1; } else if (Address1.length() < MaxChar) { while (Address1.length() != 20){ Address1 += " "; } return Address1; } else { Address1 = ""; for (int i=0; i<MaxChar; i++) { Address1+=temp1.charAt(i); } return Address1; } } // get second line of address of size 20 public String getAddress2() { temp1 = Address2; if (Address2.length() == MaxChar) { return Address2; } else if (Address2.length() < MaxChar) { while (Address2.length() != 20) { Address2 += " "; } return Address2; } else { Address2 = ""; for (int i=0; i<MaxChar; i++) { Address2+=temp1.charAt(i); } return Address2; } } // get area code of size 3 public String getPhone1() { temp1 = Phone1; if (Phone1.length() == 3) { return Phone1; } else if (Phone1.length() < 3) { while (Phone1.length() != 3) { Phone1 += " "; } return Phone1; } else { Phone1 = ""; for (int i=0; i<3; i++) { Phone1+=temp1.charAt(i); } return Phone1; } } // get first 3 numbers public String getPhone2() { temp1 = Phone2; if (Phone2.length() == 3) { return Phone2; } else if (Phone2.length() < 3) { while (Phone2.length() != 3) { Phone2 += " "; } return Phone2; } else { Phone2 = ""; for (int i=0; i<3; i++) { Phone2+=temp1.charAt(i); } return Phone2; } } // get last 4 numbers public String getPhone3() { temp1 = Phone3; if (Phone3.length() == 4) { return Phone3; } else if (Phone3.length() < 4) { while (Phone3.length() != 4) { Phone3 += " "; } return Phone3; } else { Phone3 = ""; for (int i=0; i<4; i++) { Phone3+=temp1.charAt(i); } return Phone3; } } } // object used to store memo information // with methods to return appropriately sized Strings class Memo { public String Date1,Date2,Date3; // Date public String Memo1,Memo2,Memo3; // Memo public String temp1,temp2; public static int MaxChar = 20; // constructor that takes all variables public Memo (String D1, String D2, String D3, String M1, String M2, String M3) { Date1 = D1; Date2 = D2; Date3 = D3; Memo1 = M1; Memo2 = M2; Memo3 = M3; } // empty constructor public Memo() { Date1 = " "; Date2 = " "; Date3 = " "; Memo1 = " "; Memo2 = " "; Memo3 = " "; } // return the month of size 2 public String getDate1() { temp1 = Date1; if (Date1.length() == 2) { return Date1; } else if (Date1.length() < 2) { while (Date1.length() != 2) { Date1 = "0" + Date1; } return Date1; } else { Date1 = ""; for (int i=0; i<2; i++) { Date1+=temp1.charAt(i); } return Date1; } } // return the day of size 2 public String getDate2() { temp1 = Date2; if (Date2.length() <= 2) { while (Date2.length() != 2) { Date2 = "0" + Date2; } return Date2; } else { Date2 = ""; for (int i=0; i<2; i++) { Date2+=temp1.charAt(i); } return Date2; } } // return year of size 4 public String getDate3() { temp1 = Date3; if (Date3.length() <= 4) { while (Date3.length() != 4) { Date3 += " "; } return Date3; } else { Date3 = ""; for (int i=0; i<4; i++) { Date3+=temp1.charAt(i); } return Date3; } } // get first line of memo of size 20 public String getMemo1() { temp1 = Memo1; if (Memo1.length() <=MaxChar) { while (Memo1.length() != 20) { Memo1 += " "; } return Memo1; } else { Memo1 = ""; for (int i=0; i<MaxChar; i++) { Memo1+=temp1.charAt(i); } Memo2 = temp1.substring(MaxChar,temp1.length()) + " " + Memo2; return Memo1; } } // get second line of memo of size 20 public String getMemo2() { temp1 = Memo2; if (Memo2.length() <=MaxChar) { while (Memo2.length() != 20) { Memo2 += " "; } return Memo2; } else { Memo2 = ""; for (int i=0; i<MaxChar; i++) { Memo2+=temp1.charAt(i); } Memo3 = temp1.substring(MaxChar,temp1.length()) + " " + Memo3; return Memo2; } } // get third line of memo of size 20 public String getMemo3() { temp1 = Memo3; if (Memo2.length() <= MaxChar) { while (Memo3.length() != 20) { Memo3 += " "; } return Memo3; } else { Memo3 = ""; for (int i=0; i<MaxChar; i++) { Memo3+=temp1.charAt(i); } return Memo3; } } } // graphical user interface and i/o methods public class Frametest extends Frame implements Runnable, SerialPortEventListener { private GridBagLayout gbLayout; // layout information private GridBagConstraints gbConstraints; // grid constraints // Text fields of the graphical user interface that contains the memo private TextField MemoTField1,MemoTField2,MemoTField3; // Text fields of the graphical user interface that contains the Contact's address private TextField AddressTArea1,AddressTArea2; // Text fields of the graphical user interface that contains the Contact's Name // and phone number private TextField NameTFieldF,NameTFieldL,PhoneTField1,PhoneTField2,PhoneTField3; // Text fields of the graphical user interface that contains the Memo's date private TextField DateTField1,DateTField2,DateTField3; // Buttons that allow actions of // - Adding contacts // - Deleting contacts // - Adding Memos // - Deleting Memos // - Sending Data // - Receiving Data private Button AddContact,DelContact,AddMemo,DelMemo,SendData,RecvData; // Buttons that allow navigation of Memos and Contacts private Button NextContact,NextMemo,PrevContact,PrevMemo; // Labels for the above text fields private Label NameLabelF,NameLabelL,AddressLabel,PhoneLabel,DateLabel,MemoLabel; // Menu information that allows saving and closing of the program private MenuItem Load,save,saveas,close; // string that contains the current data file private String DataFile; // object that allows reading in information from files private Reader409 myinput; // contains the total number of contacts and the contact that is presently // being displayed private int ContactCount=0,ContactCountb=0; // contains the total number of memos and the memo that is presently // being displayed private int MemoCount=0,MemoCountb=0; // determines whether we are loading contacts or memos private int mode = 0; // output and input streams for file communication private DataOutputStream output; private DataInputStream input; // determine whether a new Contact or Memo has been added private boolean MemoAdd=false, ContactAdd=false; // determines the state of receiving information private boolean ReceiveValid = false; // arrays that contain the contacts and memos private Contact myContact[] = new Contact[20]; private Memo myMemo[] = new Memo[20]; // list of the available ports static Enumeration portList; // comm port information static CommPortIdentifier portId; // serial port information static SerialPort serialPort; // input and output stream for serial communication static OutputStream outputStream; static InputStream inputStream; // thread that allows the program to continually monitor the serial // port for incoming data Thread readThread; // contains the byte information about the data received through the // serial connection public byte[] readBuffer = new byte[20]; // the number of ones that need to be seen on the serial port before the // transfer is assumed complete public int OneCount = 3; // constructor that sets the graphical user interface up and the // serial connection public Frametest( String s ) { // can the constructor of the Frame class to put a title on the // application super( s ); // obtain a litst of available communication ports portList = CommPortIdentifier.getPortIdentifiers(); // try all the available ports until we find the one that we are looking for while (portList.hasMoreElements()) { // get next port on the list portId = (CommPortIdentifier) portList.nextElement(); // we are only looking at the serial ports if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { // we want to communicate on COM port 2 if (portId.getName().equals("COM2")) { // try to establish control of the port try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000); } catch (PortInUseException ex) {} // now try to open both an input and output stream // for serial communcation try { outputStream = serialPort.getOutputStream(); inputStream = serialPort.getInputStream(); } catch (IOException ex) {} // try to add a listener which will "interrupt" // for certain events on the com port try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {} // enable one of events serialPort.notifyOnDataAvailable(true); // try to set-up the com port with the // following parameters try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException ex) {} // create and start thread that will handle // receiving information over the comm port readThread = new Thread(this); readThread.start(); } } } // create a menu bar and add a file option // that will allow the user to load and save files as well as // close the application MenuBar bar = new MenuBar(); Menu fileMenu = new Menu ("File"); Load = new MenuItem ("Load File"); save = new MenuItem ("Save"); saveas = new MenuItem ("Save as"); close = new MenuItem ("Close"); fileMenu.add(Load); fileMenu.add(save); fileMenu.add(saveas); fileMenu.add(close); bar.add(fileMenu); setMenuBar(bar); // now it is time to set up the user interface // create a grid interface and insert it with constraints gbLayout = new GridBagLayout(); setLayout( gbLayout); gbConstraints = new GridBagConstraints(); // initialize text fields, Buttons, labels MemoTField1 = new TextField( "", 20 ); MemoTField2 = new TextField( "", 20 ); MemoTField3 = new TextField( "", 20 ); AddressTArea1 = new TextField( "", 20 ); AddressTArea2 = new TextField( "", 20 ); NextContact = new Button("Next Contact"); PrevContact = new Button("Previous Contact"); NextMemo = new Button ("Next Memo"); PrevMemo = new Button ("Previous Memo"); AddContact = new Button( "Add Contact" ); DelContact = new Button( "Delete Contact" ); AddMemo = new Button( "Add Memo" ); DelMemo = new Button( "Delete Memo" ); SendData = new Button( "Send Data" ); RecvData = new Button( "Receive Data" ); NameLabelL = new Label ("Last Name:"); NameLabelF = new Label ("First Name:"); AddressLabel = new Label ("Address:"); PhoneLabel = new Label ("Phone Number:"); NameTFieldL = new TextField( 10 ); NameTFieldF = new TextField( 10 ); // do not allow to name to be edited // this is the information used to sort data NameTFieldL.setEditable(false); NameTFieldF.setEditable(false); PhoneTField1 = new TextField( "xxx",3 ); PhoneTField2 = new TextField( "xxx",3 ); PhoneTField3 = new TextField( "xxxx",4 ); DateLabel = new Label ("Date:"); MemoLabel = new Label ("Memo:"); DateTField1 = new TextField( "xx",2 ); DateTField2 = new TextField( "xx",2 ); DateTField3 = new TextField( "xxxx",4 ); // Also do not allow the date of Memos to // be edited, because it is used for sorting DateTField1.setEditable(false); DateTField2.setEditable(false); DateTField3.setEditable(false); // inser the elements defined above into the grid gbConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent( PrevContact, gbLayout, gbConstraints, 0, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent( NextContact, gbLayout, gbConstraints, 1, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( NameLabelF, gbLayout, gbConstraints, 2, 0, 2, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( NameLabelL, gbLayout, gbConstraints, 2, 2, 2, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( NameTFieldF, gbLayout, gbConstraints, 3, 0, 2, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( NameTFieldL, gbLayout, gbConstraints, 3, 2, 2, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( AddressLabel, gbLayout, gbConstraints, 4, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( AddressTArea1, gbLayout, gbConstraints, 5, 0, 4, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( AddressTArea2, gbLayout, gbConstraints, 6, 0, 4, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( PhoneLabel, gbLayout, gbConstraints, 7, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( PhoneTField1, gbLayout, gbConstraints, 8, 0, 1, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( PhoneTField2, gbLayout, gbConstraints, 8, 1, 1, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( PhoneTField3, gbLayout, gbConstraints, 8, 2, 1, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( AddContact, gbLayout, gbConstraints, 9, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( DelContact, gbLayout, gbConstraints, 10, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( SendData, gbLayout, gbConstraints, 11, 0, 3, 1 ); gbConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent( PrevMemo, gbLayout, gbConstraints, 0, 5, 3, 1 ); gbConstraints.fill = GridBagConstraints.HORIZONTAL; addComponent( NextMemo, gbLayout, gbConstraints, 1, 5, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( DateLabel, gbLayout, gbConstraints, 2, 5, 3, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( DateTField1, gbLayout, gbConstraints, 3, 5, 1, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( DateTField2, gbLayout, gbConstraints, 3, 6, 1, 1 ); gbConstraints.fill = GridBagConstraints.VERTICAL; addComponent( DateTField3, gbLayout, gbConstraints, 3, 7, 1, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( MemoLabel, gbLayout, gbConstraints, 4, 5, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( MemoTField1, gbLayout, gbConstraints, 5, 5, 4, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( MemoTField2, gbLayout, gbConstraints, 6, 5, 4, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( MemoTField3, gbLayout, gbConstraints, 7, 5, 4, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( AddMemo, gbLayout, gbConstraints, 9, 5, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( DelMemo, gbLayout, gbConstraints, 10, 5, 3, 1 ); gbConstraints.fill = GridBagConstraints.BOTH; addComponent( RecvData, gbLayout, gbConstraints, 11, 5, 3, 1 ); resize( 400, 400 ); // define size show(); // display frame } // method to handle different events that happen with respect to the // GUI public boolean handleEvent( Event e ) { // if the event is an action then dispatch a method to handle it if ( e.id == Event.ACTION_EVENT ) { action( e, e.arg ); return true; } // if the event is to destroy the window then close the program // with x else if ( e.id == Event.WINDOW_DESTROY ) { removeFrame( this ); System.exit( 0 ); // terminate program return true; } // else let the super handle it return super.handleEvent( e ); } // method to remove the current frame public void removeFrame( Frame w ) { w.hide(); // hide frame w.dispose(); // free resources } // methods to handle actions public boolean action( Event e, Object o ) { // make sure that these fields are not editable NameTFieldL.setEditable(false); NameTFieldF.setEditable(false); DateTField1.setEditable(false); DateTField2.setEditable(false); DateTField3.setEditable(false); // if one of the actions was a Button push if (e.target instanceof Button) { // see if the button was a navigation of the Contacts if ((e.arg.equals (PrevContact.getLabel())) || (e.arg.equals (NextContact.getLabel()))) { //Save Current Contact information myContact[ContactCountb].LastName = NameTFieldL.getText(); myContact[ContactCountb].FirstName = NameTFieldF.getText(); myContact[ContactCountb].Address1 = AddressTArea1.getText(); myContact[ContactCountb].Address2 = AddressTArea2.getText(); myContact[ContactCountb].Phone1 = PhoneTField1.getText(); myContact[ContactCountb].Phone2 = PhoneTField2.getText(); myContact[ContactCountb].Phone3 = PhoneTField3.getText(); // if a contact has just been added, then resort the data if(ContactAdd){ SortContact(); ContactAdd = false; } // if the button was to move to the previous and we are not // at the first contact, then decrement the current contact // counter if (e.arg.equals( PrevContact.getLabel())) { if (ContactCountb != 0) { ContactCountb --; } } // else the button must have been to move to the next contact // if we are not at the last contact then move to the next // contact else if (e.arg.equals (NextContact.getLabel())) { if (ContactCountb != ContactCount - 1) { ContactCountb ++; } } // write the new data to the display NameTFieldL.setText(myContact[ContactCountb].getLastName()); NameTFieldF.setText(myContact[ContactCountb].getFirstName()); AddressTArea1.setText(myContact[ContactCountb].getAddress1()); AddressTArea2.setText(myContact[ContactCountb].getAddress2()); PhoneTField1.setText(myContact[ContactCountb].getPhone1()); PhoneTField2.setText(myContact[ContactCountb].getPhone2()); PhoneTField3.setText(myContact[ContactCountb].getPhone3()); } // if the user requested to move through the memos else if ((e.arg.equals (PrevMemo.getLabel())) || (e.arg.equals (NextMemo.getLabel()))) { // Save Current Memo information myMemo[MemoCountb].Date1 = DateTField1.getText(); myMemo[MemoCountb].Date2 = DateTField2.getText(); myMemo[MemoCountb].Date3 = DateTField3.getText(); myMemo[MemoCountb].Memo1 = MemoTField1.getText(); myMemo[MemoCountb].Memo2 = MemoTField2.getText(); myMemo[MemoCountb].Memo3 = MemoTField3.getText(); // if a memo has just been added then the memos need // to be resorted if(MemoAdd){ SortMemo(); MemoAdd = false; } // if previous button was pushed and we are not at the first // memo then move to the previous memo if (e.arg.equals( PrevMemo.getLabel())) { if (MemoCountb != 0) { MemoCountb --; } } // if next button was pushed and we are not at the last memo // them move to the next memo else if (e.arg.equals (NextMemo.getLabel())) { if (MemoCountb != MemoCount - 1) { MemoCountb ++; } } // write new memo to the display DateTField1.setText(myMemo[MemoCountb].getDate1()); DateTField2.setText(myMemo[MemoCountb].getDate2()); DateTField3.setText(myMemo[MemoCountb].getDate3()); MemoTField1.setText(myMemo[MemoCountb].getMemo1()); MemoTField2.setText(myMemo[MemoCountb].getMemo2()); MemoTField3.setText(myMemo[MemoCountb].getMemo3()); } // if the action is to add a contact else if (e.arg.equals (AddContact.getLabel())) { // Save Current Contact myContact[ContactCountb].Address1 = AddressTArea1.getText(); myContact[ContactCountb].Address2 = AddressTArea2.getText(); myContact[ContactCountb].Phone1 = PhoneTField1.getText(); myContact[ContactCountb].Phone2 = PhoneTField2.getText(); myContact[ContactCountb].Phone3 = PhoneTField3.getText(); if (ContactCount < 20 ) { // set add to true ContactAdd = true; NameTFieldL.setEditable(true); NameTFieldF.setEditable(true); NameTFieldL.setText(""); NameTFieldF.setText(""); AddressTArea1.setText(""); AddressTArea2.setText(""); PhoneTField1.setText("xxx"); PhoneTField2.setText("xxx"); PhoneTField3.setText("xxxx"); ContactCountb = ContactCount; ContactCount++; myContact[ContactCountb] = new Contact(); } } // if action is to delete the current contact else if (e.arg.equals (DelContact.getLabel())) { // if a contact exists then delete it if (ContactCount != 0) { for (int i=ContactCountb; i<ContactCount - 1; i++){ myContact[i] = myContact[i+1]; } if (ContactCountb == ContactCount-1) { ContactCountb --; } ContactCount--; // display information NameTFieldL.setText(myContact[ContactCountb].getLastName()); NameTFieldF.setText(myContact[ContactCountb].getFirstName()); AddressTArea1.setText(myContact[ContactCountb].getAddress1()); AddressTArea2.setText(myContact[ContactCountb].getAddress2()); PhoneTField1.setText(myContact[ContactCountb].getPhone1()); PhoneTField2.setText(myContact[ContactCountb].getPhone2()); PhoneTField3.setText(myContact[ContactCountb].getPhone3()); } } // if the event was to add a new memo else if (e.arg.equals (AddMemo.getLabel())) { // Save Current Memo myMemo[MemoCountb].Memo1 = MemoTField1.getText(); myMemo[MemoCountb].Memo2 = MemoTField2.getText(); myMemo[MemoCountb].Memo3 = MemoTField3.getText(); // make sure that there is room to add if (MemoCount < 20 ) { MemoAdd = true; DateTField1.setEditable(true); DateTField2.setEditable(true); DateTField3.setEditable(true); DateTField1.setText("xx"); DateTField2.setText("xx"); DateTField3.setText("xxxx"); MemoTField1.setText(""); MemoTField2.setText(""); MemoTField3.setText(""); MemoCountb = MemoCount; MemoCount++; myMemo[MemoCountb] = new Memo(); } } // if action is to delete current memo else if (e.arg.equals (DelMemo.getLabel())) { // if there are Memos then delete this one if (MemoCount !=0 ){ for (int i=MemoCountb; i<MemoCount - 1; i++){ myMemo[i] = myMemo[i+1]; } if (MemoCountb == MemoCountb-1) { MemoCountb--; } MemoCount--; DateTField1.setText(myMemo[MemoCountb].getDate1()); DateTField2.setText(myMemo[MemoCountb].getDate2()); DateTField3.setText(myMemo[MemoCountb].getDate3()); MemoTField1.setText(myMemo[MemoCountb].getMemo1()); MemoTField2.setText(myMemo[MemoCountb].getMemo2()); MemoTField3.setText(myMemo[MemoCountb].getMemo3()); } } // if action is to send the current data else if (e.arg.equals (SendData.getLabel())) { try { // send init code outputStream.write(1); outputStream.write(4); outputStream.write(5); // loop through the contacts and send them for(int i=0; i<ContactCount; i++){ printinfo(myContact[i].getFirstName()); System.out.println(myContact[i].getFirstName()); // print field divider outputStream.write(2); printinfo(myContact[i].getLastName()); System.out.println(myContact[i].getLastName()); outputStream.write(2); printinfo(myContact[i].getAddress1()); System.out.println(myContact[i].getAddress1()); outputStream.write(2); printinfo(myContact[i].getAddress2()); System.out.println(myContact[i].getAddress2()); outputStream.write(2); printinfo(myContact[i].getPhone1()); System.out.println(myContact[i].getPhone1()); printinfo(myContact[i].getPhone2()); System.out.println(myContact[i].getPhone2()); printinfo(myContact[i].getPhone3()); System.out.println(myContact[i].getPhone3()); outputStream.write(2); // deliniate the end of the current contact outputStream.write(3); } // divider between contacts and memos outputStream.write(1); // loop through all of the memos and send them for(int i=0; i<MemoCount; i++){ printinfo(myMemo[i].getDate1()); System.out.println(myMemo[i].getDate1()); printinfo(myMemo[i].getDate2()); System.out.println(myMemo[i].getDate2()); printinfo(myMemo[i].getDate3()); System.out.println(myMemo[i].getDate3()); outputStream.write(2); printinfo(myMemo[i].getMemo1()); System.out.println(myMemo[i].getMemo1()); outputStream.write(2); printinfo(myMemo[i].getMemo2()); System.out.println(myMemo[i].getMemo2()); outputStream.write(2); printinfo(myMemo[i].getMemo3()); System.out.println(myMemo[i].getMemo3()); // field divider outputStream.write(2); // end of memo divider outputStream.write(3); } // end of transmission 1 outputStream.write(1); } catch (IOException ex) {} } // if the event is to receive data else if (e.arg.equals (RecvData.getLabel())) { // toggle receive state ReceiveValid = !ReceiveValid; // display appropriate state if(ReceiveValid) { System.out.println("Receiving Data!!"); // open receive file to hold to data for later // processing try{ output = new DataOutputStream (new FileOutputStream ("Download.dat")); } catch (FileNotFoundException e2) {} } else { System.out.println("Done Receiving Data!!"); } } } // if a menu item has been selected else if (e.target instanceof MenuItem) { // if load was selected if (e.arg.equals( Load.getLabel())) { // get file name from dialog window FileDialog fd = new FileDialog(new Frame(),"Select Organizer"); fd.move(100,100); fd.show(); DataFile = fd.getFile(); try { myinput = new Reader409(DataFile); } catch (FileNotFoundException ex) { System.out.println("File Not Found!"); System.exit(0); } int Count =0; // continue to loop until all things have been loaded while (true){ try { // get the first line and determine whether // we are currently loading contacts or memos String temp = myinput.readLine(); int locate = temp.indexOf((int) '§'); if (locate == 0){ System.out.println(temp); if (temp.equals("§Contact§")){ mode = 0; ContactCount = 0; ContactCountb = 0; }else{ mode = 1; MemoCount = 0; MemoCountb = 0; } temp = myinput.readLine(); } // if contact mode, read in contacts if (mode == 0 ){ // create a new contact myContact[ContactCount] = new Contact(); // use delineator to get fields int a = temp.indexOf((int) 'ô'); if (a == -1) break; // get last name myContact[ContactCount].LastName = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].LastName); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get first name myContact[ContactCount].FirstName = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].FirstName); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get first line of the contact's address myContact[ContactCount].Address1 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].Address1); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get second line of the contact's address myContact[ContactCount].Address2 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].Address2); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get area code myContact[ContactCount].Phone1 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].Phone1); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get first 3 numbers of phone number myContact[ContactCount].Phone2 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].Phone2); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get last 4 numbers of the phone number myContact[ContactCount].Phone3 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myContact[ContactCount].Phone3); // increment the number of contacts ContactCount++; } // deal with memo mode else { // create a new memo myMemo[MemoCount] = new Memo(); int a = temp.indexOf((int) 'ô'); if (a == -1) break; // get the month myMemo[MemoCount].Date1 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myMemo[MemoCount].Date1); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get the day myMemo[MemoCount].Date2 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myMemo[MemoCount].Date2); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get the year myMemo[MemoCount].Date3 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myMemo[MemoCount].Date3); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get the first line of the memo myMemo[MemoCount].Memo1 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myMemo[MemoCount].Memo1); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get the second line of the memo myMemo[MemoCount].Memo2 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myMemo[MemoCount].Memo2); a = temp.indexOf((int) 'ô'); if (a == -1) break; // get the third line of the memo myMemo[MemoCount].Memo3 = temp.substring(0,a); temp = temp.substring(a+1,temp.length()); System.out.println(myMemo[MemoCount].Memo3); // increment the number of memos MemoCount++; } } catch (EOFException ex){ //System.out.println("End of File"); try { myinput.close(); } catch (IOException ex1) { System.out.println("IO ERROR!"); System.exit(0); } break; } catch (IOException ex) { System.out.println("IO ERROR!"); System.exit(0); } } // sort contacts SortContact(); // sort memos SortMemo(); // update GUI NameTFieldL.setText(myContact[ContactCountb].getLastName()); NameTFieldF.setText(myContact[ContactCountb].getFirstName()); AddressTArea1.setText(myContact[ContactCountb].getAddress1()); AddressTArea2.setText(myContact[ContactCountb].getAddress2()); PhoneTField1.setText(myContact[ContactCountb].getPhone1()); PhoneTField2.setText(myContact[ContactCountb].getPhone2()); PhoneTField3.setText(myContact[ContactCountb].getPhone3()); DateTField1.setText(myMemo[MemoCountb].getDate1()); DateTField2.setText(myMemo[MemoCountb].getDate2()); DateTField3.setText(myMemo[MemoCountb].getDate3()); MemoTField1.setText(myMemo[MemoCountb].getMemo1()); MemoTField2.setText(myMemo[MemoCountb].getMemo2()); MemoTField3.setText(myMemo[MemoCountb].getMemo3()); } // if save or save as else if ( (e.arg.equals( save.getLabel())) || (e.arg.equals( saveas.getLabel())) ) { // if save as get new file name if (e.arg.equals( saveas.getLabel())) { FileDialog fd = new FileDialog(new Frame(),"Save Organizer as ..."); fd.move(100,100); fd.show(); DataFile = fd.getFile(); System.out.println(DataFile); } try{ output = new DataOutputStream ( new FileOutputStream (DataFile)); } catch (IOException e1) { System.err.println( "File not opened properly\n" + e1.toString()); System.exit(1); } // save the data to the file DataFile try { String s = "§Contact§\n"; printstring(s); for (int i=0; i < ContactCount; i++) { printstring(myContact[i].getLastName() + "ô"); printstring(myContact[i].getFirstName() + "ô"); printstring(myContact[i].getAddress1() + "ô"); printstring(myContact[i].getAddress2() + "ô"); printstring(myContact[i].getPhone1() + "ô"); printstring(myContact[i].getPhone2() + "ô"); printstring(myContact[i].getPhone3() + "ô\n"); } printstring("§Memo§\n"); for (int i=0; i < MemoCount; i++) { printstring(myMemo[i].getDate1() + "ô"); printstring(myMemo[i].getDate2() + "ô"); printstring(myMemo[i].getDate3() + "ô"); printstring(myMemo[i].getMemo1() + "ô"); printstring(myMemo[i].getMemo2() + "ô"); printstring(myMemo[i].getMemo3() + "ô\n"); } output.flush(); output.close(); } catch (IOException e1) { System.err.println("Error during write to file\n" + e1.toString()); System.exit(1); } } // else it must be close else { removeFrame( this ); System.exit( 0 ); // terminate program return true; } } return true; } // addComponent is programmer defined private void addComponent( Component c, GridBagLayout g, GridBagConstraints gc, int row, int column, int width, int height ) { // set gridx and gridy gc.gridx = column; gc.gridy = row; // set gridwidth and gridheight gc.gridwidth = width; gc.gridheight = height; g.setConstraints( c, gc ); // set constraints add( c ); // add component to applet } // sort the Memos by date using bubble sort private void SortMemo (){ Memo temp = new Memo(); for (int pass = 1; pass < MemoCount; pass++) { for (int i=0; i < MemoCount-1; i++) { String a = myMemo[i].getDate3() + myMemo[i].getDate1() + myMemo[i].getDate2(); String b = myMemo[i+1].getDate3() + myMemo[i+1].getDate1() + myMemo[i+1].getDate2(); //System.out.println(a + " --> " + b + "compare: " + b.compareTo(a)); if (b.compareTo(a) < 0) { temp = new Memo(); temp = myMemo[i]; myMemo[i] = new Memo(); myMemo[i] = myMemo[i+1]; myMemo[i+1] = new Memo(); myMemo[i+1] = temp; } } } } // sort the contacts by last name using bubble sort private void SortContact (){ Contact temp = new Contact(); for (int pass = 1; pass < ContactCount; pass++) { for (int i=0; i < ContactCount-1; i++) { String a = myContact[i].getLastName(); String b = myContact[i+1].getLastName(); //System.out.println(a + " --> " + b + "compare: " + b.compareTo(a)); if (b.compareTo(a) < 0) { temp = new Contact(); temp = myContact[i]; myContact[i] = new Contact(); myContact[i] = myContact[i+1]; myContact[i+1] = new Contact(); myContact[i+1] = temp; } } } } // method to write data to the currently open comm port // output stream private void printinfo (String s) { char a; String s1=""; try { for (int i=0; i<s.length(); i++) { s1 =""; a = s.charAt(i); s1 += a; outputStream.write(s1.getBytes()); } } catch (IOException ex) {} } // method to print to the current open file output stream private void printstring (String s) { char temp; try { for (int i=0; i < s.length(); i++) { temp = s.charAt(i); output.write(temp); } } catch (IOException e1) { System.err.println("Error during write to file\n" + e1.toString()); System.exit(1); } } // run method to allow this class to run as a thread public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) {} } // serial event handler public void serialEvent(SerialPortEvent event){ switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: // hold currently received byte readBuffer = new byte[1]; // while we are in receive mode while (ReceiveValid) { try { // receive a character and write it to Download.dat int numBytes = inputStream.read(readBuffer); output.writeByte(readBuffer[0]); System.out.print(new String(readBuffer)); } catch (IOException e) {} // count the number of 1's if (readBuffer[0] == 1) { OneCount -=1; // once all the information has been received, // process it if(OneCount == 0) { // we have finished receiving, print message // and set variable System.out.println("Done Receiving Data!!"); ReceiveValid = false; OneCount = 3; try { // open Download.dat for input input = new DataInputStream(new FileInputStream ("Download.dat")); // get initialization characters while (input.readByte() !=1) {} while (input.readByte() !=4) {} while (input.readByte() !=5) {} // clear variables, because we are getting new stuff readBuffer = new byte[20]; ContactCount = 0; ContactCountb = 0; MemoCount = 0; MemoCountb = 0; while (true) { byte temp = input.readByte(); // a 1 denotes the end of contacts if (temp == 1) { break; } else { // create a new contact myContact[ContactCount] = new Contact(); // store the first character since it was not a 1 readBuffer[0] = temp; // get the remaining 9 characters in the first name for(int i=1; i<10; i++) { readBuffer[i] = input.readByte(); } // store the first name myContact[ContactCount].FirstName = new String(readBuffer); System.out.println(myContact[ContactCount].getFirstName()); input.readByte(); // get 2 and throw it out // read ten characters in the last name for(int i=0; i<10; i++) { readBuffer[i] = input.readByte(); } // store the last name myContact[ContactCount].LastName = new String(readBuffer); System.out.println(myContact[ContactCount].getLastName()); input.readByte(); // get 2 and throw away // read 20 characters for the first line of the address for(int i=0; i<20; i++) { readBuffer[i] = input.readByte(); } // store Address1 myContact[ContactCount].Address1 = new String(readBuffer); System.out.println(myContact[ContactCount].getAddress1()); input.readByte(); // get 2 and throw away // read 20 characters for the second line of the address for(int i=0; i<20; i++) { readBuffer[i] = input.readByte(); } // store address 2 myContact[ContactCount].Address2 = new String(readBuffer); System.out.println(myContact[ContactCount].getAddress2()); input.readByte(); // get 2 and throw away // read 3 characters for the area code for(int i=0; i<3; i++) { readBuffer[i] = input.readByte(); } // store the area code myContact[ContactCount].Phone1 = new String(readBuffer); System.out.print(myContact[ContactCount].getPhone1()); // read the first three digits of the phone number for(int i=0; i<3; i++) { readBuffer[i] = input.readByte(); } // store the first 3 digits of the phone number myContact[ContactCount].Phone2 = new String(readBuffer); System.out.print(myContact[ContactCount].getPhone2()); // get the last 4 numbers of the phone number for(int i=0; i<4; i++) { readBuffer[i] = input.readByte(); } // store the last 4 numbers of the phone number myContact[ContactCount].Phone3 = new String(readBuffer); System.out.println(myContact[ContactCount].getPhone3()); input.readByte(); // get 2 and throw away input.readByte(); // get 3 and throw away // increment the number of contacts ContactCount++; } } // now handle the memos while (true) { byte temp = input.readByte(); // if it is a 1 we are done if (temp == 1) { break; } else { // create a new memo myMemo[MemoCount] = new Memo(); // store the character, because it was not a 1 (byte) readBuffer[0] = temp; // get the remaining character of the month for (int i=1; i<2; i++) { readBuffer[i] = input.readByte(); } // store the month myMemo[MemoCount].Date1 = new String(readBuffer); System.out.println(myMemo[MemoCount].getDate1()); // get the 2 characters (bytes) in the day for (int i=0; i<2; i++) { readBuffer[i] = input.readByte(); } // store day myMemo[MemoCount].Date2 = new String(readBuffer); System.out.println(myMemo[MemoCount].getDate2()); // get 4 characters in year for (int i=0; i<4; i++) { readBuffer[i] = input.readByte(); } // store year myMemo[MemoCount].Date3 = new String(readBuffer); System.out.println(myMemo[MemoCount].getDate3()); input.readByte(); // get 2 divider // read first line of memo for (int i=0; i<20; i++) { readBuffer[i] = input.readByte(); } // store first line of memo myMemo[MemoCount].Memo1 = new String(readBuffer); System.out.println(myMemo[MemoCount].getMemo1()); input.readByte(); // 2 // get second line of memo for (int i=0; i<20; i++) { readBuffer[i] = input.readByte(); } // store second line of memo myMemo[MemoCount].Memo2 = new String(readBuffer); System.out.println(myMemo[MemoCount].getMemo2()); input.readByte(); // 2 // get third line of memo for (int i=0; i<20; i++) { readBuffer[i] = input.readByte(); } // store third line of memo myMemo[MemoCount].Memo3 = new String(readBuffer); System.out.println(myMemo[MemoCount].getMemo3()); input.readByte(); // 2 input.readByte(); // 3 // increment the number if Memos MemoCount++; } } } catch (IOException e) {} // sort and update display SortContact(); SortMemo(); NameTFieldL.setText(myContact[ContactCountb].getLastName()); NameTFieldF.setText(myContact[ContactCountb].getFirstName()); AddressTArea1.setText(myContact[ContactCountb].getAddress1()); AddressTArea2.setText(myContact[ContactCountb].getAddress2()); PhoneTField1.setText(myContact[ContactCountb].getPhone1()); PhoneTField2.setText(myContact[ContactCountb].getPhone2()); PhoneTField3.setText(myContact[ContactCountb].getPhone3()); DateTField1.setText(myMemo[MemoCountb].getDate1()); DateTField2.setText(myMemo[MemoCountb].getDate2()); DateTField3.setText(myMemo[MemoCountb].getDate3()); MemoTField1.setText(myMemo[MemoCountb].getMemo1()); MemoTField2.setText(myMemo[MemoCountb].getMemo2()); MemoTField3.setText(myMemo[MemoCountb].getMemo3()); break; } } } } } // create a new instance of this class public static void main( String args[] ) { Frametest myself; myself = new Frametest("CU Organizer"); } }