/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Controller;
import Model.Contact;
import Model.ContactModel;
import View.AddView;
import View.ContactView;
import View.EditView;
import View.FileView;
import View.ListView;
import View.OptionView;
import View.SearchView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class ContactController {
private ContactModel m_model;
private FileView m_fView;
private OptionView m_oView;
private AddView m_aView;
private EditView m_eView;
private SearchView m_sView;
private ListView m_lView;
private ContactView m_cView;
public ContactController(ContactModel model, FileView fView) {
m_model = model;
m_fView = fView;
m_oView = new OptionView(m_model);
m_fView.fileButtonClicked(new FileListener());
}
class FileListener implements ActionListener{
public void actionPerformed(ActionEvent e){
m_model.clear();
String inputFile = m_fView.getFileName();//fileName=take text from textfield;
m_model.readContactsFromFile(inputFile);
m_oView.addOptionClicked(new AddOptionListener());
m_oView.searchOptionClicked(new SearchOptionListener());
m_oView.listOptionClicked(new ListOptionListener());
m_oView.setVisible(true);
}
}
class AddOptionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
m_aView = new AddView(m_model);
m_aView.addButtonClicked(new AddButtonListener());
m_aView.setVisible(true);
}
}
class AddButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] values;
values = m_aView.getValues();
m_model.addContact(values[0], values[1], values[2], values[3], values[4]);
m_aView.dispose();
JOptionPane.showMessageDialog(null, "Contact has been added");
}
}
class DeleteOptionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int conIndex;
conIndex = m_cView.getContactIndex();
m_model.deleteContact(conIndex);
m_cView.dispose();
JOptionPane.showMessageDialog(null, "Contact has been deleted");
}
}
class EditOptionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] contactInfo = new String[5];
contactInfo = m_model.getContactInfoArray(m_cView.getContactIndex());
m_eView = new EditView(m_model, contactInfo, m_cView.getContactIndex());
m_eView.editButtonClicked(new EditButtonListener());
m_eView.setVisible(true);
m_cView.dispose();
}
}
class EditButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] values;
values = m_eView.getValues();
m_model.editContact(m_eView.getContactIndex(), values[0], values[1], values[2], values[3], values[4]);
m_eView.dispose();
JOptionPane.showMessageDialog(null, "Contact has been modified");
}
}
class SearchOptionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
m_sView = new SearchView(m_model);
m_sView.searchButtonClicked(new SearchButtonListener());
m_sView.setVisible(true);
}
}
class SearchButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String[] values;
int c;
values = m_sView.getValues();
c = m_model.searchContact(values[0]);
if(c == -1){
JOptionPane.showMessageDialog(null, "Contact Not Found");
}
else{
m_sView.dispose();
m_cView = new ContactView(m_model, m_model.getContactInfo(c), c);
m_cView.editOptionClicked(new EditOptionListener());
m_cView.deleteOptionClicked(new DeleteOptionListener());
m_cView.setVisible(true);
}
}
}
class ListOptionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
m_lView = new ListView(m_model);
m_lView.setVisible(true);
}
}
}