package phoneBook

package phoneBook;

import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

class PhoneBook implements ActionListener {

JMenuBar mainMenuBar = new JMenuBar();
JMenu menu = new JMenu();
JMenuItem item;

Toolkit toolKit = Toolkit.getDefaultToolkit();
JFrame mainFrame;
Image img = toolKit.getImage(“C:/Users/User/Desktop//contactsIcon.jpg”);
PhoneBook() {

mainFrame = new JFrame(“Phone Book”);
mainFrame.setSize(680, 400);// 680 is the width ,400 is the height
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setIconImage(img);
options();
mainFrame.show();

}

public void options()

{
mainMenuBar.add(menu);

menu = new JMenu(“Help”);

item = new JMenuItem(“Help”);
menu.add(item);
item.addActionListener(this);

item = new JMenuItem(“About”);
menu.add(item);
item.addActionListener(this);

mainMenuBar.add(menu);

mainFrame.setJMenuBar(mainMenuBar);

JPanel panel1 = new JPanel();
JPanel addContactPanel = new JPanel();
JPanel deleteContactPanel = new JPanel();
JPanel searchContactPanel = new JPanel();
JPanel sortContactsPanel = new JPanel();
JPanel viewContactsanel = new JPanel();

// Add Buttons To Bottom Panel
JButton AddNewContactButton = new JButton(“Add New Contact”);

JButton DeleteContactButton = new JButton(“Delete Contact”);
JButton SearchContactsButton = new JButton(“Search Contacts”);
JButton SortContactsButton = new JButton(“Sort Contacts”);
JButton ViewContactListButton = new JButton(“View Contacts”);

JLabel label = new JLabel(
“Manage Contacts”);

JLabel imageLabelPhoneBook = new JLabel();
imageLabelPhoneBook.setIcon(new javax.swing.ImageIcon(img));

AddNewContactButton.addActionListener(this);
DeleteContactButton.addActionListener(this);
SearchContactsButton.addActionListener(this);
SortContactsButton.addActionListener(this);
ViewContactListButton.addActionListener(this);

JLabel imageLabelAddContact = new JLabel();
Image imageAddContact = toolKit.getImage(“C:/Users/User/Desktop//AddContactIcon.jpg”);
imageLabelAddContact.setIcon(new javax.swing.ImageIcon(imageAddContact));

JLabel imageLabelDeleteContact = new JLabel();
Image imageDeleteContact = toolKit.getImage(“C:/Users/User/Desktop//DeleteContactIcon.jpg”);
imageLabelDeleteContact.setIcon(new javax.swing.ImageIcon(imageDeleteContact));

JLabel imageLabelSearchContact = new JLabel();
Image imageSearchContact = toolKit.getImage(“C:/Users/User/Desktop//SearchContactIcon.jpg”);
imageLabelSearchContact.setIcon(new javax.swing.ImageIcon(imageSearchContact));

JLabel imageLabelSortContact = new JLabel();
Image imageSortContact = toolKit.getImage(“C:/Users/User/Desktop//SortContactIcon.jpg”);
imageLabelSortContact.setIcon(new javax.swing.ImageIcon(imageSortContact));

JLabel imageLabelViewContact = new JLabel();
Image imageViewContact = toolKit.getImage(“C:/Users/User/Desktop//ViewContactIcon.jpg”);
imageLabelViewContact.setIcon(new javax.swing.ImageIcon(imageViewContact));

panel1.add(imageLabelPhoneBook);
panel1.add(label);

addContactPanel.add(imageLabelAddContact);
addContactPanel.add(AddNewContactButton);

deleteContactPanel.add(imageLabelDeleteContact);
deleteContactPanel.add(DeleteContactButton);

searchContactPanel.add(imageLabelSearchContact);
searchContactPanel.add(SearchContactsButton);

sortContactsPanel.add(imageLabelSortContact);
sortContactsPanel.add(SortContactsButton);

viewContactsanel.add(imageLabelViewContact);
viewContactsanel.add(ViewContactListButton);

Container contentPaneMain = mainFrame.getContentPane();
contentPaneMain.setLayout(new GridLayout(1, 10));
contentPaneMain.add(panel1);

Container contentPanelAddContact = mainFrame.getContentPane();
contentPanelAddContact.setLayout(new GridLayout(3, 10));
contentPanelAddContact.add(addContactPanel);

Container contentPanelDeleteContact = mainFrame.getContentPane();
contentPanelDeleteContact.setLayout(new GridLayout(3, 10));
contentPanelDeleteContact.add(deleteContactPanel);

Container contentPanelSearchContact = mainFrame.getContentPane();
contentPanelSearchContact.setLayout(new GridLayout(3, 10));
contentPanelSearchContact.add(searchContactPanel);

Container contentPanelSortContact = mainFrame.getContentPane();
contentPanelSortContact.setLayout(new GridLayout(3, 10));
contentPanelSortContact.add(sortContactsPanel);

Container contentPanelViewContact = mainFrame.getContentPane();
contentPanelViewContact.setLayout(new GridLayout(3, 10));
contentPanelViewContact.add(viewContactsanel);

mainFrame.setResizable(false);

}

public static void main(String args)
{
PhoneBook add = new PhoneBook();

}

OperationHandler opHandler = new OperationHandler();

public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getActionCommand() == “Add New Contact”) {
opHandler.AddNew();

}

else if (actionEvent.getActionCommand() == “Search Contacts”) {
opHandler.SearchContacts();

}

else if (actionEvent.getActionCommand() == “Sort Contacts”) {
opHandler.SortContacts();

}

else if (actionEvent.getActionCommand() == “Delete Contact”) {
opHandler.DeleteContact();

}

else if (actionEvent.getActionCommand() == “View Contacts”) {

opHandler.ViewAllContacts();

}

else if (actionEvent.getActionCommand() == “About”) {
JOptionPane.showMessageDialog(mainFrame,
“About Address Book:Java project, By Group 1 CCBT “,
“About Address Book”, JOptionPane.INFORMATION_MESSAGE);

} else if (actionEvent.getActionCommand() == “Help Contents”) {
try {
opHandler.showHelp();
} catch (IOException e) {
}

}

}

}

class Contacts implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private String nickname;
private String emailId;
private String addressOfContact;
private String phoneNumber;
private String birthday;

public void setDetails(String fname, String lname, String nname, String email, String address, String phone,
String bday) {
firstName = fname;
lastName = lname;
nickname = nname;
emailId = email;
addressOfContact = address;
phoneNumber = phone;
birthday = bday;
}

public String getFName() {
return firstName;
}

public String getLName() {
return lastName;
}

public String getNname() {
return nickname;
}

public String getEMail() {
return emailId;
}

public String getAddress() {
return addressOfContact;
}

public String getPhoneNo() {
return phoneNumber;
}

public String getBday() {
return birthday;
}

}

class OperationHandler implements ListSelectionListener, ActionListener, Runnable {

JFrame newFrame;
JTextField txtFirstName;
JTextField txtFieldLastName;
JTextField txtFieldNickname;
JTextField txtFieldEmail;
JTextField txtFieldAddress;
JTextField txtFieldPhoneNo;
JTextField txtFieldWebpage;
JTextField txtFieldBDay;

Vector v = new Vector(10, 3);
int i = 0, k = 0;

Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

Image img = kit.getImage(“images/icon.JPG”);

FileInputStream fis;
ObjectInputStream ois;

JList list;
DefaultListModel listModel;
ListSelectionModel listSelectionModel;

JRadioButton byFirstName, byLastName;

Thread t;

JTable searchTable;

JTextField txtSearch;

String columnNames = { “First Name”, “Last Name”, “Nickname”, “E-Mail Address”, “Address”, “Phone No.”,
“Birthday” };

Object data = new Object58;

OperationHandler() {

try {
fis = new FileInputStream(“data.txt”);
ois = new ObjectInputStream(fis);
v = (Vector) ois.readObject();
ois.close();
}

catch (Exception e) {

}

}

public void run() {

try {
FileOutputStream fos = new FileOutputStream(“data.txt”);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(v);
oos.flush();
oos.close();

} catch (Exception e) {
JOptionPane.showMessageDialog(newFrame, “Error OpeningData File: Could Not Save Contents.”,
“Error Opening Data File”, JOptionPane.INFORMATION_MESSAGE);
}

}

public void AddNew()
{
newFrame = new JFrame(“Add New”);
newFrame.setSize(220,250);
newFrame.setResizable(false);
newFrame.setIconImage(img);

JLabel lblFirstName = new JLabel(“First Name: “);
JLabel lblLastName = new JLabel(“Last Name: “);
JLabel lblNickname = new JLabel(“Nickname: “);
JLabel lblEMail = new JLabel(“EMail: “);
JLabel lblAddress = new JLabel(“Address: “);
JLabel lblPhoneNo = new JLabel(“Phone No: “);
JLabel lblBDay = new JLabel(“BDay: “);

txtFirstName = new JTextField(10);
txtFieldLastName = new JTextField(10);
txtFieldNickname = new JTextField(10);
txtFieldEmail = new JTextField(10);
txtFieldAddress = new JTextField(10);
txtFieldPhoneNo = new JTextField(10);
txtFieldBDay = new JTextField(10);

JButton BttnSave = new JButton(“Save”);

BttnSave.addActionListener(this);

JPanel centerPane = new JPanel();
JPanel bottomPane = new JPanel();

centerPane.add(lblFirstName);
centerPane.add(txtFirstName);
centerPane.add(lblLastName);
centerPane.add(txtFieldLastName);
centerPane.add(lblNickname);
centerPane.add(txtFieldNickname);
centerPane.add(lblEMail);
centerPane.add(txtFieldEmail);
centerPane.add(lblAddress);
centerPane.add(txtFieldAddress);
centerPane.add(lblPhoneNo);
centerPane.add(txtFieldPhoneNo);
centerPane.add(lblBDay);
centerPane.add(txtFieldBDay);
bottomPane.add(BttnSave);

centerPane.setLayout(new GridLayout(0,2));

newFrame.getContentPane().add(centerPane,BorderLayout.CENTER);

newFrame.getContentPane().add(bottomPane,BorderLayout.SOUTH);
newFrame.setLocation(screenWidth/4, screenHeight/4);
newFrame.show();

}

public void SearchContacts()
{
newFrame = new JFrame(“Search Contacts”);
newFrame.setSize(700,220);
newFrame.setLocation(screenWidth/4, screenHeight/4);
newFrame.setIconImage(img);
newFrame.setResizable(false);

JPanel topPane = new JPanel();
JLabel label1 = new JLabel(“Search Contacts by First”);
topPane.add(label1);

JPanel centerPane = new JPanel();
JLabel label2 = new JLabel(“Search contact:”);
txtSearch = new JTextField(20);
JButton bttnSearch = new JButton(“Search”);
bttnSearch.addActionListener(this);
JButton bttnCancel = new JButton(“Cancel”);
bttnCancel.addActionListener(this);
centerPane.add(label2);
centerPane.add(txtSearch);
centerPane.add(bttnSearch);
centerPane.add(bttnCancel);

searchTable = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(searchTable);
searchTable.setPreferredScrollableViewportSize(new Dimension(500, 90));

newFrame.getContentPane().add(scrollPane, BorderLayout.SOUTH);

newFrame.getContentPane().add(topPane, BorderLayout.NORTH);
newFrame.getContentPane().add(centerPane, BorderLayout.CENTER);
newFrame.show();
}

public void SortContacts() {
newFrame = new JFrame(“Sort Contacts”);
newFrame.setSize(250, 160);
newFrame.setLocation(screenWidth / 4, screenHeight / 4);
newFrame.setIconImage(img);
newFrame.setResizable(false);

byFirstName = new JRadioButton(“By First Name”);
byFirstName.setActionCommand(“By First Name”);
byFirstName.setSelected(true);

byLastName = new JRadioButton(“By Last Name”);
byLastName.setActionCommand(“By Last Name”);

ButtonGroup group = new ButtonGroup();
group.add(byFirstName);
group.add(byLastName);

JPanel topPane = new JPanel();
JLabel label = new JLabel(“Sort Contacts By:”);
topPane.add(label);

JPanel pane = new JPanel(new GridLayout(0, 1));
pane.add(byFirstName);
pane.add(byLastName);

JPanel bottomPane = new JPanel();
JButton sortBttn = new JButton(“Sort Contacts”);
JButton cancelBttn = new JButton(“Cancel”);
bottomPane.add(sortBttn);
bottomPane.add(cancelBttn);
sortBttn.addActionListener(this);
cancelBttn.addActionListener(this);

newFrame.getContentPane().add(topPane, BorderLayout.NORTH);
newFrame.getContentPane().add(pane, BorderLayout.CENTER);
newFrame.getContentPane().add(bottomPane, BorderLayout.SOUTH);

newFrame.show();

}

public void DeleteContact() {
String fname, lname;
newFrame = new JFrame(“Delete Contact”);
newFrame.setSize(300, 300);
newFrame.setLocation(screenWidth / 4, screenHeight / 4);
newFrame.setIconImage(img);

listModel = new DefaultListModel();

Contacts contact = new Contacts();

for (int l = 0; l < v.size(); l++) {
contact = (Contacts) v.elementAt(l);

fname = contact.getFName();
lname = contact.getLName();
listModel.addElement(fname + " " + lname);

}

list = new JList(listModel);

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(this);

JScrollPane listScrollPane = new JScrollPane(list);

JPanel topPane = new JPanel();
JLabel label = new JLabel("Current Contacts:");
topPane.add(label);

JPanel bottomPane = new JPanel();

JButton bttnDelete = new JButton("Delete Selected");
bottomPane.add(bttnDelete);
bttnDelete.addActionListener(this);

JButton bttnCancel = new JButton("Cancel");
bottomPane.add(bttnCancel);
bttnCancel.addActionListener(this);

newFrame.getContentPane().add(topPane, BorderLayout.NORTH);
newFrame.getContentPane().add(listScrollPane, BorderLayout.CENTER);
newFrame.getContentPane().add(bottomPane, BorderLayout.SOUTH);

newFrame.show();

}

public void ViewAllContacts() {

newFrame = new JFrame("All contacts");
newFrame.setSize(600, 300);
newFrame.setIconImage(img);

Contacts con = new Contacts();

String columnNames = { "First Name", "Last Name", "Nickname", "E Mail Address", "Address", "Phone No.", "B'day" };

Object data = new Objectv.size()8;

for (int j = 0; j < v.size(); j++) {

con = (Contacts) v.elementAt(k);

dataj0 = con.getFName();
dataj1 = con.getLName();
dataj2 = con.getNname();
dataj3 = con.getEMail();
dataj4 = con.getAddress();
dataj5 = con.getPhoneNo();
dataj6 = con.getBday();

k++;

}
k = 0;

JTable abtable = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(abtable);
abtable.setPreferredScrollableViewportSize(new Dimension(500, 370));

JPanel pane = new JPanel();
JLabel label = new JLabel("Contacts Currently In The Address Book");
pane.add(label);

newFrame.getContentPane().add(pane, BorderLayout.SOUTH);
newFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);
newFrame.setLocation(screenWidth / 4, screenHeight / 4);
newFrame.show();

}

public void showHelp() throws IOException {

String title = "Help Contents";
String data = "";
FileInputStream helpFile = null;
int line;

newFrame = new JFrame(title);
newFrame.setSize(401, 400);
newFrame.setResizable(false);
newFrame.setLocation(screenWidth / 4, screenHeight / 4);
newFrame.setIconImage(img);

JTextArea textArea = new JTextArea(5, 20);
textArea.setLineWrap(true);
textArea.setEditable(false);

try {
helpFile = new FileInputStream("//storage/storage$/720782/Desktop/help.txt");
} catch (Exception e) {
JOptionPane.showMessageDialog(newFrame, "Sorry ,Currently Help option is not available", "Sorry ,Currently Help option is not available",
JOptionPane.INFORMATION_MESSAGE);
}

do {
line = helpFile.read();
if (line != 1)
data = data + (char) line;
} while (line != -1);

helpFile.close();

textArea.setText(data);

JPanel bottomPane = new JPanel();
JButton button = new JButton("Ok");
bottomPane.add(button);
button.addActionListener(this);

JPanel topPane = new JPanel();
JLabel label = new JLabel(title);
topPane.add(label);

JScrollPane scrollPane = new JScrollPane(textArea);

newFrame.getContentPane().add(topPane, BorderLayout.NORTH);
newFrame.getContentPane().add(scrollPane);

newFrame.getContentPane().add(bottomPane, BorderLayout.SOUTH);

newFrame.show();

}

public void actionPerformed(ActionEvent ae) {

if (ae.getActionCommand() == "Save") {

if (txtFirstName.getText().equals("") && txtFieldLastName.getText().equals("")
&& txtFieldNickname.getText().equals("") && txtFieldEmail.getText().equals("")
&& txtFieldAddress.getText().equals("") && txtFieldPhoneNo.getText().equals("")
&& txtFieldBDay.getText().equals("")) {

JOptionPane.showMessageDialog(newFrame, "Fill contact details to Save contact.",
"Entries Empty", JOptionPane.INFORMATION_MESSAGE);

}

else {
Contacts contact = new Contacts();

contact.setDetails(txtFirstName.getText(), txtFieldLastName.getText(), txtFieldNickname.getText(),
txtFieldEmail.getText(), txtFieldAddress.getText(), txtFieldPhoneNo.getText(),
txtFieldBDay.getText());
v.addElement(contact);
txtFirstName.setText("");
txtFieldLastName.setText("");
txtFieldNickname.setText("");
txtFieldEmail.setText("");
txtFieldAddress.setText("");
txtFieldPhoneNo.setText("");
txtFieldBDay.setText("");
JOptionPane.showMessageDialog(null,"Contact Saved");
}

} else if (ae.getActionCommand() == "Ok") {
newFrame.setVisible(false);

} else if (ae.getActionCommand() == "Delete Selected") {

int index;
try {

index = list.getSelectedIndex();

if (index == -1) {

JOptionPane.showMessageDialog(newFrame, "Select a Contact to delete it.",
"Select a Contact to delete", JOptionPane.INFORMATION_MESSAGE);
}

else {

int n = JOptionPane.showConfirmDialog(newFrame,
"You want to delete the selected Contact?", "you want to delete this contact?",
JOptionPane.YES_NO_OPTION);

if (n == JOptionPane.YES_OPTION) {
listModel.remove(index);
JOptionPane.showMessageDialog(null,"Contact deleted");
v.removeElementAt(index);
saveVector();
newFrame.show();

} else if (n == JOptionPane.NO_OPTION) {

}

}

} catch (Exception e) {

}

} else if (ae.getActionCommand() == "Cancel") {

newFrame.setVisible(false);
} else if (ae.getActionCommand() == "Search") {
String SearchStr;
SearchStr = txtSearch.getText();
boolean flag = false;
Contacts con = new Contacts();
int c = 0;

for (int t = 0; t < 5; t++) {
datat0 = "";
datat1 = "";
datat2 = "";
datat3 = "";
datat4 = "";
datat5 = "";
datat6 = "";
}

for (int t = 0; t < v.size(); t++) {

con = (Contacts) v.elementAt(t);

if (SearchStr.equalsIgnoreCase(con.getFName()) || SearchStr.equalsIgnoreCase(con.getLName())
|| SearchStr.equalsIgnoreCase(con.getFName() + " " + con.getLName())) {
flag = true;

datac0 = con.getFName();
datac1 = con.getLName();
datac2 = con.getNname();
datac3 = con.getEMail();
datac4 = con.getAddress();
datac5 = con.getPhoneNo();
datac6 = con.getBday();
searchTable = new JTable(data, columnNames);
newFrame.setSize(700, 221);
newFrame.setSize(700, 220);

if (c < 5)
c++;
}

}

if (flag) {
JOptionPane.showMessageDialog(newFrame, "Contact found", "Search Result",
JOptionPane.INFORMATION_MESSAGE);
}

else {
JOptionPane.showMessageDialog(newFrame, "Contact not found", "Search Result",
JOptionPane.INFORMATION_MESSAGE);
}

} else if (ae.getActionCommand() == "Sort Contacts") {

if (byFirstName.isSelected()) {
Contacts contact1 = new Contacts();
Contacts contact2 = new Contacts();
Contacts temp = new Contacts();
int l, m;

for (l = 0; l < v.size() – 1; l++) {
for (m = l + 1; m 0) {
temp = (Contacts) v.elementAt(m);

v.setElementAt(v.elementAt(l), m);
v.setElementAt(temp, l);
}

}
}

saveVector();
} else {

Contacts contact1 = new Contacts();
Contacts contact2 = new Contacts();
Contacts temp = new Contacts();
int l, m;

for (l = 0; l < v.size() – 1; l++) {
for (m = l + 1; m 0) {
temp = (Contacts) v.elementAt(m);

v.setElementAt(v.elementAt(l), m);
v.setElementAt(temp, l);
}

}
}

saveVector();
}

newFrame.setVisible(false);
}

}

public void saveVector() {
t = new Thread(this, “Save Vector Thread”);
t.start();

}

public void valueChanged(ListSelectionEvent lse) {

}

}