Project: Calendar-Application
package calendar;
 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Locale; 
 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.border.EtchedBorder; 
import javax.swing.plaf.FontUIResource; 
 
import com.toedter.calendar.JDateChooser; 
 
/**
 *  
 * @author Sorin Dinu 
 * 
 */
 
@SuppressWarnings("serial"
public class ContactPanel extends JPanel implements Comparable<ContactPanel>,  
ActionListener{ 
 private Contact m_contact; 
 private int m_mode; 
 private AddressBookPanel m_parent; 
 public static final int ADD_MODE = 0
 public static final int VIEW_MODE = 1
 public static final int EDIT_MODE = 2
  
 private JLabel m_icon; 
 private JLabel m_name_label; 
 private JLabel m_dob_label; 
 private JLabel m_website_label; 
 private ArrayList<String> m_a_phone_label; 
 private ArrayList<String> m_a_phone_value; 
 private ArrayList<String> m_a_email_label; 
 private ArrayList<String> m_a_email_value; 
 private int m_phone_idx = 0
 private int m_email_idx = 0
 private JButton m_phone_left; 
 private JButton m_phone_right; 
 private JButton m_email_left; 
 private JButton m_email_right; 
 private JLabel m_phone_label; 
 private JLabel m_phone_value; 
 private JLabel m_email_label; 
 private JLabel m_email_value; 
  
 private JTextField m_name_field; 
 private JTextField m_landphone_field; 
 private JTextField m_workphone_field; 
 private JTextField m_mobilephone_field; 
 private JDateChooser m_dob_field; 
 private JTextField m_email_field; 
 private JTextField m_workemail_field; 
 private JTextField m_website_field; 
 private JButton m_add_button; 
 private JButton m_edit_button; 
 private JButton m_delete_button; 
  
  
 public int GetMode() { 
  return m_mode; 
 
  
 public String GetName() { 
  if(m_mode == ContactPanel.ADD_MODE) { 
   return ""
  
  return m_contact.GetName(); 
 
  
 public ContactPanel(AddressBookPanel parent) { 
  setOpaque(false); 
  setFont(new FontUIResource(new Font("Arial", Font.PLAIN, 10))); 
  m_parent = parent; 
  setBorder(new EtchedBorder()); 
  URL iconURL = getClass().getResource("/images/contact2.png"); 
  m_icon = new JLabel(new ImageIcon(iconURL)); 
  m_icon.setPreferredSize(new Dimension(5050)); 
  m_name_field = new JTextField(5); 
  m_dob_field = new JDateChooser(); 
  m_website_field = new JTextField(5); 
  m_landphone_field = new JTextField(5); 
  m_mobilephone_field = new JTextField(5); 
  m_workphone_field = new JTextField(5); 
  m_email_field = new JTextField(5); 
  m_workemail_field = new JTextField(5); 
  m_add_button = new JButton("Add"); 
  m_add_button.setIcon(new ImageIcon(getClass().getResource( 
    "/images/add_20.png")));; 
  m_add_button.addActionListener(this); 
  m_contact = new Contact(); 
  initView(); 
  m_mode = ContactPanel.ADD_MODE; 
  renderAdd(false); 
 
  
 public ContactPanel(Contact c, AddressBookPanel parent) { 
  setOpaque(false); 
  setFont(new FontUIResource(new Font("Arial", Font.PLAIN, 10))); 
  m_parent = parent; 
  setBorder(new EtchedBorder()); 
  m_contact = c; 
  initView(); 
 
  
 public void initView() { 
  setBorder(new EtchedBorder()); 
  setFont(new FontUIResource(new Font("Arial", Font.PLAIN, 10))); 
  Contact c = m_contact; 
  URL iconURL = getClass().getResource("/images/contact2.png"); 
  m_icon = new JLabel(new ImageIcon(iconURL)); 
  m_icon.setPreferredSize(new Dimension(5050)); 
  m_name_label = new JLabel(c.GetName()); 
  m_dob_label = new JLabel(makeDobText(c)); 
  m_website_label = new JLabel(c.GetWebsite()); 
  m_phone_left = new JButton("<"); 
  m_phone_right = new JButton(">"); 
  m_email_left = new JButton("<"); 
  m_email_right = new JButton(">"); 
  m_phone_left.setMargin(new Insets(0000)); 
  m_phone_left.setPreferredSize(new Dimension(40,25)); 
  m_phone_right.setMargin(new Insets(0000)); 
  m_phone_right.setPreferredSize(new Dimension(40,25)); 
  m_email_left.setMargin(new Insets(0000)); 
  m_email_right.setMargin(new Insets(0000)); 
  m_email_left.setPreferredSize(new Dimension(40,25)); 
  m_email_right.setPreferredSize(new Dimension(40,25)); 
  m_a_phone_label = new ArrayList<String>(); 
  m_a_phone_value = new ArrayList<String>(); 
  m_a_phone_label.add(new String(" Phone: ")); 
  m_a_phone_label.add(new String(" Mobile Phone: ")); 
  m_a_phone_label.add(new String(" Work Phone: ")); 
  m_a_phone_value.add(new String(c.GetLandphone())); 
  m_a_phone_value.add(new String(c.GetMobilephone())); 
  m_a_phone_value.add(new String(c.GetWorkphone())); 
  m_a_email_label = new ArrayList<String>(); 
  m_a_email_value = new ArrayList<String>(); 
  m_a_email_label.add(new String(" Email: ")); 
  m_a_email_label.add(new String(" Work Email: ")); 
  m_a_email_value.add(new String(c.GetEmail())); 
  m_a_email_value.add(new String(c.GetWorkemail())); 
  m_phone_label = new JLabel(m_a_phone_label.get(m_phone_idx)); 
  m_phone_value = new JLabel(m_a_phone_value.get(m_phone_idx)); 
  m_email_label = new JLabel(m_a_email_label.get(m_email_idx)); 
  m_email_value = new JLabel(m_a_email_value.get(m_email_idx)); 
  //m_add_button = new JButton("Add"); 
  m_edit_button = new JButton("Edit"); 
  m_delete_button = new JButton("Delete"); 
  m_phone_left.addActionListener(this); 
  m_phone_right.addActionListener(this); 
  m_email_left.addActionListener(this); 
  m_email_right.addActionListener(this); 
   
  //m_edit_button.setBorder(null); 
  m_edit_button.setIcon(new ImageIcon(getClass().getResource( 
    "/images/edit_20.png"))); 
  //m_delete_button.setBorder(null); 
  m_delete_button.setIcon(new ImageIcon(getClass().getResource( 
    "/images/delete_20.png"))); 
  m_edit_button.addActionListener(this); 
  m_delete_button.addActionListener(this); 
   
  m_name_label.setFont(new FontUIResource("Arial", Font.PLAIN, 14)); 
  m_dob_label.setFont(new FontUIResource("Arial", Font.ITALIC, 10)); 
  m_phone_left.setMargin(new Insets(0,0,0,0)); 
  m_phone_right.setMargin(new Insets(0,0,0,0)); 
  m_email_left.setMargin(new Insets(0,0,0,0)); 
  m_email_right.setMargin(new Insets(0,0,0,0)); 
   
  m_name_field = new JTextField(5); 
  m_dob_field = new JDateChooser(); 
  m_dob_field.setPreferredSize(new Dimension(100,30)); 
  m_website_field = new JTextField(5); 
  m_landphone_field = new JTextField(5); 
  m_mobilephone_field = new JTextField(5); 
  m_workphone_field = new JTextField(5); 
  m_email_field = new JTextField(5); 
  m_workemail_field = new JTextField(5); 
   
  m_mode = ContactPanel.VIEW_MODE; 
  renderView(); 
 
  
 private void refreshView() { 
  m_name_label.setText(m_contact.GetName()); 
  m_dob_label.setText(makeDobText(m_contact)); 
  m_a_phone_value.set(0, m_contact.GetLandphone()); 
  m_a_phone_value.set(1, m_contact.GetMobilephone()); 
  m_a_phone_value.set(2, m_contact.GetWorkphone()); 
  m_a_email_value.set(0, m_contact.GetEmail()); 
  m_a_email_value.set(1, m_contact.GetWorkemail()); 
  m_website_label.setText(m_contact.GetWebsite()); 
  renderEmail(); 
  renderPhone(); 
  m_mode = ContactPanel.VIEW_MODE; 
  renderView(); 
 
  
 private void editMode() { 
  m_mode = ContactPanel.EDIT_MODE; 
  renderAdd(true); 
  m_name_field.setText(m_contact.GetName()); 
  m_dob_field.setDate(m_contact.GetDob().getTime()); 
  m_website_field.setText(m_contact.GetWebsite()); 
  m_landphone_field.setText(m_contact.GetLandphone()); 
  m_mobilephone_field.setText(m_contact.GetMobilephone()); 
  m_workphone_field.setText(m_contact.GetWorkphone()); 
  m_email_field.setText(m_contact.GetEmail()); 
  m_workemail_field.setText(m_contact.GetWorkemail()); 
  updateUI(); 
 
  
 private String makeDobText(Contact c) { 
  Calendar dob = c.GetDob(); 
  Calendar now = Calendar.getInstance(); 
  String dob_text = dob.get(Calendar.DAY_OF_MONTH)+" "+ dob. 
    getDisplayName(Calendar.MONTH, Calendar.LONG, new  
      Locale(Data.GetSettings().Get("locale"))) + " "
    dob.get(Calendar.YEAR); 
  int years = now.get(Calendar.YEAR) - dob.get(Calendar.YEAR); 
  now.set(Calendar.YEAR, dob.get(Calendar.YEAR)); 
  if(dob.compareTo(now) > 0) { 
   years--; 
  
  dob_text = years+" years old, born on "+dob_text; 
  return dob_text; 
 
  
 private void renderAdd(boolean isEdit) { 
  removeAll(); 
  setLayout(new GridBagLayout()); 
  setBorder(new EtchedBorder()); 
  setOpaque(false); 
  setFont(new FontUIResource(new Font("Arial", Font.PLAIN, 10))); 
  GridBagConstraints c = new GridBagConstraints(); 
  c.gridx = 0
  c.gridy = 0
  c.gridheight = 3
  c.anchor = GridBagConstraints.CENTER; 
  //add(m_icon, c); 
  c.gridheight = 1
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Name:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_name_field, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Date Of Birth:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_dob_field, c); 
  c.gridx = 1
  c.gridy++; 
   
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Website:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_website_field, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Land Phone:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_landphone_field, c); 
  c.gridx = 1
  c.gridy++; 
   
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Mobile Phone:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_mobilephone_field, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Work Phone:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_workphone_field, c); 
  c.gridx = 1
  c.gridy++; 
   
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Email:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_email_field, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Work Email:"), c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_workemail_field, c); 
  c.gridx=1
  c.gridy++; 
  c.gridwidth = 4
  c.fill = GridBagConstraints.HORIZONTAL; 
  c.anchor = GridBagConstraints.CENTER; 
  if(isEdit) 
   add(m_edit_button, c); 
  else 
   add(m_add_button, c); 
  setFont(new FontUIResource(new Font("Arial", Font.PLAIN, 10))); 
 
  
 private void renderView() { 
  removeAll(); 
  setLayout(new GridBagLayout()); 
  GridBagConstraints c = new GridBagConstraints(); 
  c.gridx = 0
  c.gridy = 0
  c.gridheight = 3
  c.anchor = GridBagConstraints.CENTER; 
  add(m_icon, c); 
  c.gridheight = 1
  c.gridx++; 
  c.gridwidth = 4
  c.anchor = GridBagConstraints.WEST; 
  c.fill = GridBagConstraints.HORIZONTAL; 
  c.weightx = 1
  add(m_name_label, c); 
  c.gridy++; 
  c.gridx = 1
  c.anchor = GridBagConstraints.WEST; 
  add(m_dob_label, c); 
  c.gridy++; 
  c.gridx = 1
  c.gridwidth = 1
  c.fill = GridBagConstraints.NONE; 
  c.weightx = 0
  c.anchor = GridBagConstraints.EAST; 
  add(m_phone_left, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_phone_right, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(m_phone_label, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  c.fill = GridBagConstraints.HORIZONTAL; 
  c.weightx = 1
  add(m_phone_value, c); 
  c.fill = GridBagConstraints.NONE; 
  c.weightx = 0
  c.gridx = 1
  c.gridy++; 
  c.anchor = GridBagConstraints.EAST; 
  add(m_email_left, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  add(m_email_right, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.EAST; 
  add(m_email_label, c); 
  c.gridx++; 
  c.anchor = GridBagConstraints.WEST; 
  c.fill = GridBagConstraints.HORIZONTAL; 
  c.weightx = 1
  add(m_email_value, c); 
  c.fill = GridBagConstraints.NONE; 
  c.weightx = 0
  c.gridx = 1
  c.gridy++; 
  c.gridwidth = 3
  c.anchor = GridBagConstraints.EAST; 
  add(new JLabel("Website: "), c); 
  c.gridx+=3
  c.gridwidth = 1
  c.anchor = GridBagConstraints.WEST; 
  c.fill = GridBagConstraints.HORIZONTAL; 
  c.weightx = 1
  add(m_website_label, c); 
  c.fill = GridBagConstraints.NONE; 
  c.weightx = 0
  c.gridy = 3
  c.gridx = 0
  c.gridwidth = 1
  c.anchor = GridBagConstraints.CENTER; 
  c.fill = GridBagConstraints.HORIZONTAL; 
  add(m_edit_button, c); 
  c.gridy++; 
  add(m_delete_button, c); 
 
  
 private void renderPhone() { 
  m_phone_left.setToolTipText(m_a_phone_label.get((m_phone_idx-1
    m_a_phone_label.size())%m_a_phone_label.size())); 
  m_phone_right.setToolTipText(m_a_phone_label.get((m_phone_idx+1)% 
    m_a_phone_label.size())); 
  m_phone_value.setText(m_a_phone_value.get(m_phone_idx)); 
 
  
 private void renderEmail() { 
  m_email_left.setToolTipText(m_a_email_label.get((m_email_idx-1
    m_a_email_label.size())%m_a_email_label.size())); 
  m_email_right.setToolTipText(m_a_email_label.get((m_email_idx+1)% 
    m_a_email_label.size())); 
  m_email_value.setText(m_a_email_value.get(m_email_idx)); 
 
  
 @Override 
 public int compareTo(ContactPanel o)  { 
  if(GetMode() == ContactPanel.ADD_MODE) { 
   return -1
  
  else if(o.GetMode() == ContactPanel.ADD_MODE) { 
   return 1
  
  else { 
   return GetName().compareTo(o.GetName()); 
  
 
  
 @Override 
 public void actionPerformed(ActionEvent e) { 
  if(e.getSource() == m_phone_left) { 
   m_phone_idx += m_a_phone_value.size()-1
   m_phone_idx %= m_a_phone_value.size(); 
   renderPhone(); 
  
  else if(e.getSource() == m_phone_right) { 
   m_phone_idx++; 
   m_phone_idx %= m_a_phone_value.size(); 
   renderPhone(); 
  
  else if(e.getSource() == m_email_left) { 
   m_email_idx += m_a_email_value.size()-1
   m_email_idx %= m_a_email_value.size(); 
   renderEmail(); 
  
  else if(e.getSource() == m_email_right) { 
   m_email_idx++; 
   m_email_idx %= m_a_email_value.size(); 
   renderEmail(); 
  
  else if(e.getSource() == m_edit_button) { 
   if(m_mode == ContactPanel.VIEW_MODE) { 
    editMode(); 
   else if(m_mode == ContactPanel.EDIT_MODE) { 
    String name = m_name_field.getText(); 
    Calendar dob = Calendar.getInstance(); 
    dob.setTime(m_dob_field.getDate()); 
    String website = m_website_field.getText(); 
    String landphone = m_landphone_field.getText(); 
    String mobilephone = m_mobilephone_field.getText(); 
    String workphone = m_workphone_field.getText(); 
    String email = m_email_field.getText(); 
    String workemail = m_workemail_field.getText(); 
 
    m_contact = Data.EditContact(m_contact.GetId(), name, dob, landphone,  
      mobilephone, workphone, email, workemail, website); 
    refreshView(); 
 
    updateUI(); 
    m_parent.render(); 
    JOptionPane.showMessageDialog(this"Contact edited!""OK!",  
            JOptionPane.INFORMATION_MESSAGE); 
   
  
  else if(e.getSource() == m_add_button) { 
   try { 
    String name = m_name_field.getText(); 
    Calendar dob = Calendar.getInstance(); 
    dob.setTime(m_dob_field.getDate()); 
    String website = m_website_field.getText(); 
    String landphone = m_landphone_field.getText(); 
    String mobilephone = m_mobilephone_field.getText(); 
    String workphone = m_workphone_field.getText(); 
    String email = m_email_field.getText(); 
    String workemail = m_workemail_field.getText(); 
 
    m_contact = Data.AddContact(name, dob, landphone, mobilephone, 
      workphone, email, workemail, website); 
    refreshView(); 
    updateUI(); 
    m_parent.AddEmpty(); 
    JOptionPane.showMessageDialog(this"Contact saved!""OK!",  
            JOptionPane.INFORMATION_MESSAGE); 
   catch (Exception e1) { 
    e1.printStackTrace(); 
    String msg = e1.getMessage(); 
    if(msg == null) { 
     msg = "There's an error in your form. Please " + 
       "complete all the fields."
    
    JOptionPane.showMessageDialog(this, msg, "ERROR!",  
            JOptionPane.ERROR_MESSAGE); 
   
    
  
  else if(e.getSource() == m_delete_button) { 
   try { 
    Data.RemoveContact(m_contact.GetId()); 
    setVisible(false); 
    updateUI(); 
   catch (Exception e1) { 
    e1.printStackTrace(); 
   
  
 
  
 /*@SuppressWarnings({ "unused", "static-access" })
 public static void main(String []args) throws Exception { 
  Data.Init(); 
  ContactPanel cp = new ContactPanel(null); 
  Calendar dob = Data.ParseDate("27/02/1991 0:0:0"); 
  Contact contact = new Contact(Data.AllocateContactId(), "Dinu Sorin",  
    dob, "01792469169", "07863638883", "07864648884",  
    "dinnot21@gmail.com", "sorin@creativeprogramming.co.uk",  
    "www.creativeprograming.co.uk"); 
  ContactPanel cp2 = new ContactPanel(contact, null); 
  JFrame test_frame = new JFrame(); 
  test_frame.add(cp); 
  test_frame.pack(); 
  test_frame.setVisible(true); 
  test_frame.setDefaultCloseOperation(test_frame.EXIT_ON_CLOSE); 
 }*/
 
}