Advertisement
Sothian

Untitled

Nov 26th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 38.89 KB | None | 0 0
  1. package frontend;
  2.  
  3. import backend.*;
  4. import inputcheckers.*;
  5. import people.*;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import javax.swing.JFrame;
  10. import javax.swing.JPanel;
  11. import javax.swing.JPasswordField;
  12. import java.awt.Color;
  13. import java.awt.Component;
  14. import java.awt.Cursor;
  15. import java.awt.Desktop;
  16. import java.awt.EventQueue;
  17. import javax.swing.border.LineBorder;
  18. import javax.swing.event.DocumentEvent;
  19. import javax.swing.event.DocumentListener;
  20. import javax.swing.table.TableRowSorter;
  21. import javax.swing.text.JTextComponent;
  22. import java.awt.GridLayout;
  23. import java.awt.Image;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.FocusEvent;
  27. import java.awt.event.FocusListener;
  28. import java.awt.event.MouseAdapter;
  29. import java.awt.event.MouseEvent;
  30. import java.awt.image.BufferedImage;
  31. import java.io.File;
  32. import java.io.FileNotFoundException;
  33. import java.io.IOException;
  34. import java.net.URI;
  35. import java.net.URISyntaxException;
  36. import javax.swing.JTextField;
  37. import javax.swing.JTextPane;
  38. import javax.swing.RowFilter;
  39. import javax.swing.SwingConstants;
  40. import javax.swing.JLabel;
  41. import javax.swing.JOptionPane;
  42. import javax.imageio.ImageIO;
  43. import javax.swing.ImageIcon;
  44. import javax.swing.JButton;
  45. import javax.swing.JComboBox;
  46. import javax.swing.JFileChooser;
  47. import javax.swing.JFormattedTextField;
  48. import java.awt.Font;
  49. import javax.swing.JSeparator;
  50. import javax.swing.JTable;
  51. import javax.swing.JScrollPane;
  52.  
  53. public class FrontEnd {
  54.     // Entire frame
  55.     public JFrame frame;
  56.  
  57.     // Panels
  58.     private JPanel containerPanel;
  59.     private JPanel loginInspectorPanel;
  60.     private JPanel registerInspectorPanel;
  61.     private JPanel addNewContactPanel;
  62.     private JPanel leftSideMenu;
  63.     private JPanel contactsPanel = new JPanel();
  64.     private JPanel loginOrRegisterPanel;
  65.    
  66.     // Components on loginInspectorPanel
  67.     private JFormattedTextField loginInspectorUsername;
  68.     private JPasswordField loginInspectorPassword;;
  69.    
  70.     // Components on registerInspectorPanel
  71.     private JTextField registryInspectorFirstName;
  72.     private JTextField registryInspectorLastName;
  73.     private JTextField registryInspectorUsername;
  74.     private JPasswordField registryInspectorPassword;
  75.     private JPasswordField registryInspectorConfirmPassword;
  76.     private JButton registryInspectorRegisterButton;
  77.     private JLabel registryInspectorRegisterIcon;
  78.     private JButton registryInspectorReturnButton;
  79.  
  80.     // Components on addNewContactPanel
  81.     private JTextField addContactFirstName;;
  82.     private JTextField addContactLastName;;
  83.     private JTextField addContactPhoneNumber;;
  84.     private JTextPane addContactCommentPane;;
  85.     private JLabel newContactLabelContactProfileArea;
  86.     private JScrollPane commentScrollPane;
  87.  
  88.     // Components on tablePanel to access
  89.     private JTextField filterText;
  90.    
  91.     // List with inspectors in system
  92.     private ArrayList<Inspector> inspectorsList;
  93.     // List with contacts of inspectors
  94.     private ArrayList<PersonInContacts> personICList;
  95.     // Access to files since we do not have database yet
  96.     private FileManager fileManager = new FileManager();
  97.  
  98.     // Images paths
  99.     private String appLogo = "resources/images/logoImage.png";
  100.     private String contactsAvatars = "resources/images/avatars";
  101.    
  102.     // Info about current session
  103.     private Inspector loggedInspector;
  104.     private int contactIndex;
  105.     private BufferedImage contactAvatar;
  106.  
  107.     // Viewing and editing contacts
  108.     private ContactsPane contactsPaneModel = null;
  109.     private TableRowSorter<ContactsPane> sorter;
  110.     private JTable contactsTable = new JTable(contactsPaneModel);
  111.     private JScrollPane contactScrollPane = new JScrollPane(contactsTable);
  112.     private ArrayList<PersonInContacts> filteredPersonICList;
  113.     private JLabel loggedInspectorNameLabel;
  114.     private boolean editContactPressed = false;
  115.     private File pp;
  116.  
  117.     // Constructor
  118.     public FrontEnd() {
  119.         try {
  120.             fileManager.makeInspectorsFile();
  121.             fileManager.makePeopleICFile();
  122.         } catch (FileNotFoundException e) {
  123.             e.printStackTrace();
  124.         }
  125.         inspectorsList = fileManager.getInspectorList();
  126.         initialize();      
  127.     }
  128.  
  129.     private void initialize() {
  130.         frame = new JFrame();
  131.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  132.         frame.setTitle("ITD Helper System");
  133.         frame.setBounds(0, 0, 1200, 700);
  134.         frame.getContentPane().setLayout(null);
  135.         frame.setLocationRelativeTo(null);
  136.         containerPanel = new JPanel();
  137.         containerPanel.setBounds(0, 0, 1200, 678);
  138.         containerPanel.setLayout(null);
  139.         containerPanel.setVisible(false);
  140.         frame.getContentPane().add(containerPanel);
  141.        
  142.         generateLoginPanel();
  143.         registerInspectorPanel();
  144.         generateNewContactToAdd();
  145.         generateLeftSideMenuPanel();
  146.         generateLoginOrRegisterPanel();
  147.        
  148.         //Setting which panel is visible at the beginning
  149.         loginOrRegisterPanel.setVisible(true);
  150.         loginInspectorPanel.setVisible(false);
  151.         registerInspectorPanel.setVisible(false);
  152.         contactsPanel.setVisible(false);
  153.         addNewContactPanel.setVisible(false);
  154.     }
  155.  
  156.     private JPanel generateLoginOrRegisterPanel() {
  157.         // Make main choose panel
  158.         loginOrRegisterPanel = new JPanel();
  159.         loginOrRegisterPanel.setBounds(0, 0, 1200, 678);
  160.         loginOrRegisterPanel.setLayout(null);
  161.         frame.getContentPane().add(loginOrRegisterPanel);
  162.        
  163.         // Button fields on loginOrRegister panel
  164.         JButton loginOrRegisterLOG = new JButton("Login");
  165.         loginOrRegisterLOG.setBounds(432, 466, 161, 29);
  166.         loginOrRegisterPanel.add(loginOrRegisterLOG);
  167.         loginOrRegisterLOG.addActionListener(new ActionListener() {
  168.             public void actionPerformed(ActionEvent e) {
  169.                 loginInspectorPanel.setVisible(true);
  170.                 loginOrRegisterPanel.setVisible(false);
  171.             }
  172.         });
  173.         JButton loginOrRegisterREG = new JButton("Register");
  174.         loginOrRegisterREG.setBounds(732, 466, 161, 29);
  175.         loginOrRegisterPanel.add(loginOrRegisterREG);
  176.         loginOrRegisterREG.addActionListener(new ActionListener() {
  177.             public void actionPerformed(ActionEvent e) {
  178.                 registerInspectorPanel.setVisible(true);
  179.                 loginOrRegisterPanel.setVisible(false);
  180.             }
  181.         });
  182.         //Icon on loginOrRegisterPanel
  183.         JLabel loginOrRegisterPanelImage = new JLabel("");
  184.         loginOrRegisterPanelImage.setBounds(500, 100, 300, 300);
  185.         loginOrRegisterPanel.add(loginOrRegisterPanelImage);
  186.         ImageIcon imageIcon = new ImageIcon(appLogo);
  187.         Image image = imageIcon.getImage();
  188.         Image newimg = image.getScaledInstance(300, 300,  java.awt.Image.SCALE_SMOOTH);
  189.         imageIcon = new ImageIcon(newimg);
  190.         loginOrRegisterPanelImage.setIcon(imageIcon);
  191.        
  192.         return loginOrRegisterPanel;
  193.     }
  194.    
  195.     // Register section
  196.     private JPanel registerInspectorPanel() {
  197.         // Make main registry panel
  198.         registerInspectorPanel = new JPanel();
  199.         registerInspectorPanel.setBounds(0, 0, 1200, 678);
  200.         registerInspectorPanel.setLayout(null);
  201.         frame.getContentPane().add(registerInspectorPanel);
  202.  
  203.         // Labels on registry panel
  204.         JLabel registryLabelFirstName = new JLabel("First Name");
  205.         registryLabelFirstName.setBounds(474, 231, 107, 16);
  206.         registerInspectorPanel.add(registryLabelFirstName);
  207.         JLabel registryLabelLastName = new JLabel("Last Name");
  208.         registryLabelLastName.setBounds(474, 273, 107, 16);
  209.         registerInspectorPanel.add(registryLabelLastName);
  210.         JLabel registryLabelUsername = new JLabel("Username");
  211.         registryLabelUsername.setBounds(474, 313, 107, 16);
  212.         registerInspectorPanel.add(registryLabelUsername);
  213.         JLabel registryLabelPassword = new JLabel("Password");
  214.         registryLabelPassword.setBounds(474, 363, 107, 16);
  215.         registerInspectorPanel.add(registryLabelPassword);
  216.         JLabel registryLabelRegister = new JLabel("Registered new Inspector");
  217.         registryLabelRegister.setFont(new Font("Helvetica", Font.PLAIN, 18));
  218.         registryLabelRegister.setBounds(519, 190, 175, 16);
  219.         registerInspectorPanel.add(registryLabelRegister);
  220.         JLabel registryLabelConfirmPassword = new JLabel("Confirm Password");
  221.         registryLabelConfirmPassword.setBounds(474, 408, 161, 16);
  222.         registerInspectorPanel.add(registryLabelConfirmPassword);
  223.         registryInspectorRegisterIcon = new JLabel("");
  224.         registryInspectorRegisterIcon.setBounds(519, 41, 186, 122);
  225.         registerInspectorPanel.add(registryInspectorRegisterIcon);
  226.        
  227.         // Text fields on registry panel
  228.         registryInspectorFirstName = new JTextField();
  229.         registryInspectorFirstName.setBounds(636, 226, 130, 26);
  230.         registerInspectorPanel.add(registryInspectorFirstName);
  231.         registryInspectorFirstName.setColumns(10);
  232.         registryInspectorLastName = new JTextField();
  233.         registryInspectorLastName.setColumns(10);
  234.         registryInspectorLastName.setBounds(636, 268, 130, 26);
  235.         registerInspectorPanel.add(registryInspectorLastName);
  236.         registryInspectorUsername = new JTextField();
  237.         registryInspectorUsername.setColumns(10);
  238.         registryInspectorUsername.setBounds(636, 308, 130, 26);
  239.         registerInspectorPanel.add(registryInspectorUsername);
  240.  
  241.         // Password fields on registry panel
  242.         registryInspectorPassword = new JPasswordField();
  243.         registryInspectorPassword.setBounds(636, 403, 130, 26);
  244.         registerInspectorPanel.add(registryInspectorPassword);
  245.         registerInspectorPanel.setVisible(false);
  246.         registryInspectorConfirmPassword = new JPasswordField();
  247.         registryInspectorConfirmPassword.setBounds(636, 358, 130, 26);
  248.         registerInspectorPanel.add(registryInspectorConfirmPassword);
  249.        
  250.         // Buttons on registry panel
  251.         JButton registryInspectorReturnButton = new JButton("Return");
  252.         registryInspectorReturnButton.setBounds(432, 466, 161, 29);
  253.         registerInspectorPanel.add(registryInspectorReturnButton);
  254.         registryInspectorReturnButton.addActionListener(new ActionListener() {
  255.             public void actionPerformed(ActionEvent e) {
  256.                 loginOrRegisterPanel.setVisible(true);
  257.                 registerInspectorPanel.setVisible(false);
  258.             }
  259.         });
  260.         registryInspectorRegisterButton = new JButton("Register and Login");
  261.         registryInspectorRegisterButton.setBounds(605, 466, 161, 29);
  262.         registerInspectorPanel.add(registryInspectorRegisterButton);
  263.         registryInspectorRegisterButton.addActionListener(new ActionListener() {
  264.             public void actionPerformed(ActionEvent e) {
  265.                 if(!registryInspectorPassword.getText().equals(registryInspectorConfirmPassword.getText())) {
  266.                     JOptionPane.showMessageDialog(frame, "Passwords must match!", "Warning", JOptionPane.WARNING_MESSAGE);
  267.                 }
  268.                 else{
  269.                     Inspector addInspectorToCheck = setRegistryFields();
  270.                     checkInspectorFromRegisterExist(addInspectorToCheck);
  271.                 }
  272.             }
  273.         });
  274.        
  275.         //Logo settings
  276.         ImageIcon imageIcon = new ImageIcon(appLogo);
  277.         Image image = imageIcon.getImage();
  278.         Image newimg = image.getScaledInstance(120, 120,  java.awt.Image.SCALE_SMOOTH);
  279.         imageIcon = new ImageIcon(newimg);
  280.         registryInspectorRegisterIcon.setIcon(imageIcon);
  281.         JSeparator separator = new JSeparator();
  282.         separator.setBounds(455, 207, 337, 12);
  283.         registerInspectorPanel.add(separator);
  284.  
  285.         return registerInspectorPanel;
  286.     }
  287.    
  288.     private Inspector setRegistryFields(){
  289.         Inspector addInspector = new Inspector();
  290.         addInspector.setInspectorPassword(registryInspectorConfirmPassword.getText());
  291.         addInspector.setInspectorUsername(registryInspectorUsername.getText());
  292.         addInspector.setFirstName(registryInspectorFirstName.getText());
  293.         addInspector.setLastName(registryInspectorLastName.getText());
  294.         addInspector.setInspectorID(addInspector.hashCode());
  295.         return addInspector;
  296.     }
  297.    
  298.     private boolean checkInspectorFromRegisterExist(Inspector addInspector){
  299.         boolean inspectorExists = false;
  300.         int addID = addInspector.getInspectorID();
  301.         for(Inspector inspector: inspectorsList){
  302.             if(inspector.getInspectorID() == addID) {  
  303.                 inspectorExists = true;
  304.                 addInspector = inspector;
  305.                 break;
  306.             }
  307.         }
  308.         if(!inspectorExists) {
  309.             filteredPersonICList = new ArrayList<PersonInContacts>();
  310.             contactsPaneModel = new ContactsPane(filteredPersonICList);
  311.             JOptionPane.showMessageDialog(frame, "Successfully registered as " + addInspector.getFirstName() + " " + addInspector.getLastName(), "Login Successful", JOptionPane.INFORMATION_MESSAGE);
  312.             loggedInspectorNameLabel.setText("Logged as " + addInspector.getFirstName() + " " + addInspector.getLastName());
  313.             inspectorsList.add(addInspector);
  314.             loggedInspector = addInspector;
  315.             personICList = fileManager.getPersonICList();
  316.             storeImages(personICList);
  317.             try {
  318.                 fileManager.addInspectorToFile(inspectorsList);
  319.             } catch (FileNotFoundException e) {
  320.                 e.printStackTrace();
  321.             }
  322.             for(PersonInContacts contact: personICList) {
  323.                 if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
  324.                     filteredPersonICList.add(contact);
  325.                 }
  326.             }
  327.             for(PersonInContacts contact: filteredPersonICList) {
  328.                 if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
  329.                     personICList.remove(contact);
  330.                 }
  331.             }
  332.             generateContactMainPanel();
  333.             clearFields();
  334.             contactsPanel.setVisible(false);
  335.             loginInspectorPanel.setVisible(false);
  336.             registerInspectorPanel.setVisible(false);
  337.             containerPanel.setVisible(true);
  338.             addNewContactPanel.setVisible(true);
  339.         }
  340.         if(inspectorExists) {
  341.             int reply = JOptionPane.showConfirmDialog(null, addInspector.getFirstName() + " " + addInspector.getLastName() + " already registered. Login in as this inspector?" , "Registration", JOptionPane.YES_NO_OPTION);
  342.             if (reply == JOptionPane.YES_OPTION) {
  343.                 filteredPersonICList=new ArrayList<PersonInContacts>();
  344.                 contactsPaneModel = new ContactsPane(filteredPersonICList);
  345.                 JOptionPane.showMessageDialog(frame, "Successfully logged as " + addInspector.getFirstName() + " " + addInspector.getLastName(), "Login Successful", JOptionPane.INFORMATION_MESSAGE);
  346.                 loggedInspectorNameLabel.setText("Logged as "+addInspector.getFirstName()+" "+addInspector.getLastName());
  347.                 loggedInspector = addInspector;
  348.                 personICList = fileManager.getPersonICList();
  349.                 storeImages(personICList);
  350.                 for(PersonInContacts contact: personICList) {
  351.                     if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
  352.                         filteredPersonICList.add(contact);
  353.                     }
  354.                 }
  355.                 for(PersonInContacts contact: filteredPersonICList) {
  356.                     if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
  357.                         personICList.remove(contact);
  358.                     }
  359.                 }
  360.                 generateContactMainPanel();
  361.                 clearFields();
  362.                 contactsPanel.setVisible(false);
  363.                 loginInspectorPanel.setVisible(false);
  364.                 registerInspectorPanel.setVisible(false);
  365.                 containerPanel.setVisible(true);
  366.                 addNewContactPanel.setVisible(true);   
  367.             }              
  368.         }
  369.         return inspectorExists;
  370.     }
  371.    
  372.     // Login section
  373.     private JPanel generateLoginPanel(){
  374.         // Make main login panel
  375.         loginInspectorPanel = new JPanel();
  376.         loginInspectorPanel.setBounds(0, 0, 1200, 678);
  377.         frame.getContentPane().add(loginInspectorPanel);
  378.         loginInspectorPanel.setLayout(null);
  379.  
  380.         // Labels on login panel
  381.         JLabel registryInspectorLoginIcon = new JLabel("");
  382.         registryInspectorLoginIcon.setBounds(517, 113, 159, 158);
  383.         loginInspectorPanel.add(registryInspectorLoginIcon);
  384.        
  385.         // Text fields on login panel
  386.         loginInspectorUsername = new JFormattedTextField();
  387.         loginInspectorUsername.setBounds(517, 330, 159, 25);
  388.         loginInspectorUsername.setText("Enter Username");
  389.         loginInspectorPanel.add(loginInspectorUsername);
  390.         loginInspectorUsername.addMouseListener(new MouseAdapter(){
  391.             @Override
  392.             public void mouseClicked(MouseEvent e){
  393.                 loginInspectorUsername.setText("");
  394.             }
  395.         });
  396.        
  397.         // Password fields on login panel
  398.         loginInspectorPassword = new JPasswordField();
  399.         loginInspectorPassword.setText("Enter Password");
  400.         loginInspectorPassword.setBounds(517, 391, 159, 25);
  401.         loginInspectorPanel.add(loginInspectorPassword);
  402.         loginInspectorPassword.addMouseListener(new MouseAdapter(){
  403.             @Override
  404.             public void mouseClicked(MouseEvent e){
  405.                 loginInspectorPassword.setText("");
  406.             }
  407.         });
  408.        
  409.         // Button fields on login panel
  410.         JButton loginInspectorButton = new JButton("Login");
  411.         loginInspectorButton.setBounds(517, 465, 159, 25);
  412.         loginInspectorPanel.add(loginInspectorButton); 
  413.         loginInspectorButton.addActionListener(new ActionListener() {
  414.             public void actionPerformed(ActionEvent e) {
  415.                 Inspector addInspectorToCheck = setInspectorFields();
  416.                 checkInspectorFromLoginExist(addInspectorToCheck);
  417.                 }
  418.         });
  419.         JButton loginPanelReturnButton = new JButton("Return");
  420.         loginPanelReturnButton.setBounds(717, 465, 159, 25);
  421.         loginInspectorPanel.add(loginPanelReturnButton);
  422.         loginPanelReturnButton.addActionListener(new ActionListener() {
  423.             public void actionPerformed(ActionEvent e) {
  424.                 loginInspectorPanel.setVisible(false);
  425.                 loginOrRegisterPanel.setVisible(true);
  426.             }
  427.         });
  428.  
  429.         //Logo settings
  430.         ImageIcon imageIcon = new ImageIcon(appLogo);
  431.         Image image = imageIcon.getImage();
  432.         Image newimg = image.getScaledInstance(120, 120,  java.awt.Image.SCALE_SMOOTH);
  433.         imageIcon = new ImageIcon(newimg);
  434.         registryInspectorLoginIcon.setIcon(imageIcon);
  435.         loginInspectorPanel.setVisible(false);
  436.  
  437.         return loginInspectorPanel;
  438.     }
  439.    
  440.     private Inspector setInspectorFields() {
  441.         Inspector inspector = new Inspector();
  442.         inspector.setInspectorPassword(loginInspectorPassword.getText());
  443.         inspector.setInspectorUsername(loginInspectorUsername.getText());          
  444.         inspector.setInspectorID(inspector.hashCode());
  445.         return inspector;
  446.     }
  447.  
  448.     private boolean checkInspectorFromLoginExist(Inspector addInspector) {
  449.         boolean inspectorExists = false;
  450.         int addID = addInspector.getInspectorID();
  451.         for(Inspector inspector: inspectorsList) {
  452.             if(inspector.getInspectorID()==addID) {
  453.                 inspectorExists = true;
  454.                 addInspector = inspector;
  455.                 break;
  456.             }
  457.         }
  458.         if(!inspectorExists) {
  459.             int reply = JOptionPane.showConfirmDialog(null, "User not registered. Registry?", "Registration", JOptionPane.YES_NO_OPTION);
  460.             if(reply == JOptionPane.YES_OPTION) {
  461.                 loginInspectorPanel.setVisible(false);
  462.                 registerInspectorPanel.setVisible(true);
  463.                 Component[] components = registerInspectorPanel.getComponents();
  464.                 for(Component c: components){
  465.                     if (c instanceof JTextField) {
  466.                         JTextComponent specificObject = (JTextComponent) c;
  467.                         specificObject.setText("");
  468.                     }
  469.                 }
  470.             }          
  471.         }
  472.         if(inspectorExists) {
  473.             filteredPersonICList = new ArrayList<PersonInContacts>();
  474.             contactsPaneModel = new ContactsPane(filteredPersonICList);
  475.             JOptionPane.showMessageDialog(frame, "Congratulations! You are logged as " + addInspector.getFirstName() + " " + addInspector.getLastName(), "Login Successful", JOptionPane.INFORMATION_MESSAGE);
  476.             loggedInspectorNameLabel.setText("Logged as " + addInspector.getFirstName() + " " + addInspector.getLastName());
  477.             loggedInspector = addInspector;
  478.             personICList = fileManager.getPersonICList();
  479.             storeImages(personICList);
  480.             for(PersonInContacts person: personICList) {
  481.                 if(person.getPersonsICInspectorID() == loggedInspector.getInspectorID()){
  482.                     filteredPersonICList.add(person);
  483.                 }
  484.             }
  485.             for(PersonInContacts person: filteredPersonICList) {
  486.                 if(person.getPersonsICInspectorID() == loggedInspector.getInspectorID()){
  487.                     personICList.remove(person);
  488.                 }
  489.             }
  490.             generateContactMainPanel();
  491.             clearFields();
  492.             contactsPanel.setVisible(false);
  493.             loginInspectorPanel.setVisible(false);
  494.             containerPanel.setVisible(true);
  495.             addNewContactPanel.setVisible(true);           
  496.         }
  497.        
  498.         return inspectorExists;
  499.     }
  500.      
  501.     // Generating new Contact
  502.     private JPanel generateNewContactToAdd() {
  503.         // Make main new contact Panel
  504.         addNewContactPanel = new JPanel();
  505.         addNewContactPanel.setBackground(new Color(204, 255, 255));
  506.         addNewContactPanel.setBounds(212, 0, 988, 678);
  507.  
  508.         // Labels on contact adding panel
  509.         JLabel newContactLabelFirstName = new JLabel("First Name");
  510.         addNewContactPanel.add(newContactLabelFirstName, "cell 0 0,growx,aligny center");
  511.         JLabel newContactLabelLirstName = new JLabel("Last Name");
  512.         addNewContactPanel.add(newContactLabelLirstName, "cell 0 4,growx,aligny top");
  513.         JLabel newContactLabelPhoneNumber = new JLabel("Phone Number");
  514.         addNewContactPanel.add(newContactLabelPhoneNumber, "cell 0 12,growx,aligny center");
  515.         JLabel newContactLabelContacts = new JLabel("Comments");
  516.         addNewContactPanel.add(newContactLabelContacts, "cell 0 22,growx,aligny top");
  517.         newContactLabelContactProfileArea = new JLabel();
  518.         addNewContactPanel.add(newContactLabelContactProfileArea, "cell 10 22,grow");
  519.        
  520.         // Text fields on contact adding panel
  521.         addContactFirstName = new JTextField();
  522.         addNewContactPanel.add(addContactFirstName, "cell 4 0,alignx left,aligny top");
  523.         addContactFirstName.setColumns(10);
  524.         addContactLastName = new JTextField();
  525.         addContactLastName.setColumns(10);
  526.         addNewContactPanel.add(addContactLastName, "cell 4 2 1 3,alignx left,aligny top");
  527.         addContactPhoneNumber = new JTextField();
  528.         addContactPhoneNumber.setInputVerifier(new VerifyPhoneNumber());
  529.         addContactPhoneNumber.setColumns(10);
  530.         addNewContactPanel.add(addContactPhoneNumber, "cell 4 12,alignx left,aligny top");
  531.         addContactCommentPane = new JTextPane();
  532.         commentScrollPane = new JScrollPane(addContactCommentPane);
  533.         addNewContactPanel.add(commentScrollPane, "cell 4 22,grow");
  534.        
  535.         // Buttons on contact adding panel
  536.         JButton newContactUploadContactButton = new JButton("Upload");
  537.         addNewContactPanel.add(newContactUploadContactButton, "cell 8 22,alignx left,aligny center");
  538.         newContactUploadContactButton.addActionListener(new ActionListener() {
  539.             public void actionPerformed(ActionEvent e) {
  540.                 JFileChooser chooseFile = new JFileChooser(contactsAvatars);
  541.                 chooseFile.showOpenDialog(null);
  542.                 try {
  543.                     contactAvatar = null;
  544.                     pp = chooseFile.getSelectedFile();
  545.                     contactAvatar = ImageIO.read(pp);
  546.                     Image finalImageLook = contactAvatar.getScaledInstance(120, 120,Image.SCALE_SMOOTH);//This scales the image
  547.                     newContactLabelContactProfileArea.setIcon(new ImageIcon(finalImageLook));
  548.                 } catch (IOException io) {
  549.                     io.printStackTrace();
  550.                 }
  551.             }
  552.         });
  553.         JButton newContactConfirmButton = new JButton("Confirm");
  554.         addNewContactPanel.add(newContactConfirmButton, "cell 8 26 3 1,alignx left,aligny top");
  555.         newContactConfirmButton.addActionListener(new ActionListener() {
  556.             public void actionPerformed(ActionEvent e) {   
  557.                     PersonInContacts addPersonIC = setNewContactFields();
  558.                     checkNewContact(addPersonIC);                  
  559.                 }          
  560.         });
  561.        
  562.         // Separators on contact adding for some visual organization
  563.         JSeparator newContactSeparator = new JSeparator();
  564.         newContactSeparator.setForeground(Color.LIGHT_GRAY);
  565.         addNewContactPanel.add(newContactSeparator, "cell 0 24 7 1,growx,aligny top");
  566.         JSeparator newContactSeparator1 = new JSeparator();
  567.         newContactSeparator1.setOrientation(SwingConstants.VERTICAL);
  568.         newContactSeparator1.setForeground(Color.LIGHT_GRAY);
  569.         addNewContactPanel.add(newContactSeparator1, "cell 7 0 1 27,grow");
  570.         JSeparator newContactSeparator2 = new JSeparator();
  571.         newContactSeparator2.setForeground(Color.LIGHT_GRAY);
  572.         addNewContactPanel.add(newContactSeparator2, "cell 8 24 3 1,growx,aligny top");
  573.  
  574.         containerPanel.add(addNewContactPanel);
  575.         return addNewContactPanel;
  576.     }
  577.    
  578.     private PersonInContacts setNewContactFields() {
  579.         PersonInContacts addPersonIC = new PersonInContacts();
  580.         addPersonIC.setPersonsICFirstName(addContactFirstName.getText());
  581.         addPersonIC.setPersonsICLastName(addContactLastName.getText());
  582.         addPersonIC.setPersonsICComments(addContactCommentPane.getText().replaceAll("\n|\t|,", " "));
  583.         addPersonIC.setPersonsICPhoneNumber(addContactPhoneNumber.getText());
  584.         addPersonIC.setPersonsICInspectorID(loggedInspector.getInspectorID());
  585.         addPersonIC.setPersonsICInspectorUsername(loggedInspector.getInspectorUsername());
  586.         addPersonIC.setPersonsICID(addPersonIC.hashCode());
  587.         return addPersonIC;
  588.     }
  589.    
  590.     private void checkNewContact(PersonInContacts addPersonIC){
  591.         PersonInContacts replaceExistingContact = null;
  592.         int addID = addPersonIC.getPersonsICID();
  593.         boolean contactToAddExists = false;
  594.         contactIndex = 0;
  595.         for(PersonInContacts person: filteredPersonICList) {
  596.             if(person.getPersonsICID() == addID) { 
  597.                 contactToAddExists = true;
  598.                 contactIndex = filteredPersonICList.indexOf(addPersonIC);
  599.                 replaceExistingContact = filteredPersonICList.get(contactIndex);
  600.                 break;
  601.             }
  602.         }
  603.         if(!contactToAddExists && editContactPressed) {
  604.             JOptionPane.showMessageDialog(frame, "You cant change First Name or Last Name of existing contact.", "Warning - trying to create new patient", JOptionPane.WARNING_MESSAGE);
  605.         }
  606.         else if(contactToAddExists && editContactPressed) {
  607.             personICList.removeAll(filteredPersonICList);
  608.             filteredPersonICList.set(contactIndex, addPersonIC);
  609.             contactsPaneModel.setList(filteredPersonICList);
  610.             storeImages(addPersonIC);          
  611.             try {
  612.                 personICList.addAll(filteredPersonICList);
  613.                 fileManager.addPeopleICToFile(personICList);
  614.             } catch (FileNotFoundException e) {
  615.                 e.printStackTrace();
  616.             }
  617.             JOptionPane.showMessageDialog(frame, "Contact has been edited");
  618.             contactsPanel.setVisible(true);
  619.             addNewContactPanel.setVisible(false);
  620.         }
  621.         else if(!contactToAddExists && !editContactPressed) {          
  622.             storeImages(addPersonIC);
  623.             personICList.removeAll(filteredPersonICList);
  624.             filteredPersonICList.add(addPersonIC);
  625.             contactsPaneModel.setList(filteredPersonICList);           
  626.             try {
  627.                 personICList.addAll(filteredPersonICList);
  628.                 fileManager.addPeopleICToFile(personICList);
  629.             } catch (FileNotFoundException e) {
  630.                 e.printStackTrace();
  631.             }
  632.             JOptionPane.showMessageDialog(frame, "Contact has been added");
  633.             contactsPanel.setVisible(true);
  634.             addNewContactPanel.setVisible(false);
  635.         }
  636.         else {
  637.             int reply = JOptionPane.showConfirmDialog(frame, "Contact already in the system! Edit?", "Warning", JOptionPane.YES_NO_OPTION);
  638.             if (reply == JOptionPane.YES_OPTION) {
  639.                 editContactPressed = true;
  640.                 setEditText(replaceExistingContact);
  641.             }
  642.         }
  643.     }
  644.  
  645.     private JPanel generateContactMainPanel(){
  646.         // Make main panel
  647.         contactsPanel.setBackground(new Color(153, 204, 255));
  648.         contactsPanel.setBounds(212, 0, 988, 678);
  649.         contactsPanel.setLayout(null);
  650.        
  651.         contactsTable.setModel(contactsPaneModel);
  652.         contactsTable.setFillsViewportHeight(true);
  653.         contactsTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
  654.         sorter = new TableRowSorter<ContactsPane> (contactsPaneModel);
  655.        
  656.         int columncount = contactsPaneModel.getColumnCount();
  657.         for(int i=0; i < columncount; i++) {
  658.             sorter.setSortable(i, false);
  659.         }          
  660.         contactsTable.setRowSorter(sorter);
  661.                    
  662.         // Search among contacts in table
  663.         filterText = new JTextField("Type to search among contacts");
  664.         filterText.setSize(520, 18);
  665.         filterText.setLocation(220, 28);
  666.         contactsPanel.add(filterText);
  667.         filterText.setColumns(10);
  668.         filterText.addFocusListener(new FocusListener() {
  669.             @Override
  670.             public void focusGained(FocusEvent e) {
  671.                 filterText.setText("");
  672.             }
  673.             @Override
  674.             public void focusLost(FocusEvent e) {
  675.             }
  676.         });
  677.         filterText.getDocument().addDocumentListener(new DocumentListener() {
  678.             RowFilter<ContactsPane, Object> rf = null;
  679.             String text = filterText.getText();
  680.             public void changedUpdate(DocumentEvent e) {               
  681.                 if(text.length() == 0) {
  682.                     sorter.setRowFilter(null);
  683.                 }
  684.                 else {
  685.                     try {
  686.                         rf = RowFilter.regexFilter("(?i)" + filterText.getText());
  687.                         sorter.setRowFilter(rf);
  688.                     } catch (java.util.regex.PatternSyntaxException p) {
  689.                         return;
  690.                     }                  
  691.                 }          
  692.             }
  693.             @Override
  694.             public void insertUpdate(DocumentEvent e) {
  695.                 if(text.length() == 0) {
  696.                     sorter.setRowFilter(null);
  697.                 }
  698.                 else {
  699.                     try {
  700.                         rf = RowFilter.regexFilter("(?i)" + filterText.getText());
  701.                         sorter.setRowFilter(rf);
  702.                     } catch (java.util.regex.PatternSyntaxException p) {
  703.                         return;
  704.                     }          
  705.                 }              
  706.             }
  707.             @Override
  708.             public void removeUpdate(DocumentEvent e) {
  709.                 if (text.length() == 0) {
  710.                     sorter.setRowFilter(null);
  711.                 }
  712.                 else {
  713.                     try {
  714.                         rf = RowFilter.regexFilter("(?i)" + filterText.getText());
  715.                         sorter.setRowFilter(rf);
  716.                     } catch (java.util.regex.PatternSyntaxException p) {
  717.                         return;
  718.                     }          
  719.                 }              
  720.             }
  721.         });  
  722.         contactScrollPane.setLocation(6, 154);
  723.         contactScrollPane.setSize(968, 505);
  724.         contactsPanel.add(contactScrollPane);
  725.  
  726.         // Buttons on ContactMainPanel to manage contacts in files
  727.         JButton contactPanelExportContactsButton = new JButton("Export selected contacts");
  728.         contactPanelExportContactsButton.setBounds(795, 123, 179, 19);
  729.         contactsPanel.add(contactPanelExportContactsButton);
  730.         contactPanelExportContactsButton.addActionListener(new ActionListener() {
  731.             public void actionPerformed(ActionEvent e) {
  732.                 editContactPressed = false;
  733.                 int[] selection = contactsTable.getSelectedRows();
  734.                 ArrayList<PersonInContacts> exportContactList = new ArrayList<PersonInContacts>();
  735.                 for (int i = 0; i < selection.length; i++) {
  736.                     selection[i] = contactsTable.convertRowIndexToModel(selection[i]);
  737.                     exportContactList.add(filteredPersonICList.get(contactsTable.convertRowIndexToModel(selection[i])));
  738.                 }
  739.                 JFileChooser fc = new JFileChooser("./databases");
  740.                 int returnVal = fc.showSaveDialog(frame);
  741.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  742.                     File exportfile = new File(fc.getSelectedFile() + ".csv");         
  743.                     try {
  744.                         fileManager.exportContacts(exportContactList, exportfile);
  745.                     } catch (IOException e1) {
  746.                         e1.printStackTrace();
  747.                     }
  748.                     JOptionPane.showMessageDialog(frame, "File has been exported", "Confirmed", JOptionPane.INFORMATION_MESSAGE);
  749.                 }
  750.             }
  751.         });
  752.         JButton contactPanelEditContactButton = new JButton("Edit selected contact");
  753.         contactPanelEditContactButton.setBounds(314, 115, 179, 24);
  754.         contactsPanel.add(contactPanelEditContactButton);  
  755.         contactPanelEditContactButton.addActionListener(new ActionListener() {
  756.             public void actionPerformed(ActionEvent e) {                       
  757.                 if(contactsTable.getSelectedRowCount() > 1) {
  758.                     JOptionPane.showMessageDialog(frame, "Only one at a time", "Warning", JOptionPane.WARNING_MESSAGE);
  759.                 }
  760.                 else {
  761.                     editContactPressed = true;
  762.                     int selectedRow = contactsTable.getSelectedRow();
  763.                     PersonInContacts editContact = filteredPersonICList.get(contactsTable.convertRowIndexToModel(selectedRow)); //This is definitely the selected patient, I have checked via debug mode           
  764.                     setEditText(editContact);
  765.                 }          
  766.             }
  767.         });
  768.         JButton contactPanelDeleteContacttButton = new JButton("Delete selected contact");
  769.         contactPanelDeleteContacttButton.setBounds(26, 115, 200, 24);
  770.         contactsPanel.add(contactPanelDeleteContacttButton);   
  771.         contactPanelDeleteContacttButton.addActionListener(new ActionListener() {
  772.             public void actionPerformed(ActionEvent e) {
  773.                 if(contactsTable.getSelectedRowCount() > 1) {
  774.                     JOptionPane.showMessageDialog(frame, "Only one at a time", "Warning", JOptionPane.WARNING_MESSAGE);
  775.                 }
  776.                 else {
  777.                     int modelRow = contactsTable.convertRowIndexToModel(contactsTable.getSelectedRow());
  778.                     PersonInContacts deleteContact = filteredPersonICList.get(modelRow);
  779.                     int reply = JOptionPane.showConfirmDialog(null, "Delete "+deleteContact.getPersonsICFirstName()+" "+deleteContact.getPersonsICLastName()+ " ?", "Confirm delete", JOptionPane.YES_NO_OPTION);
  780.                     if(reply == JOptionPane.YES_OPTION) {
  781.                         personICList.removeAll(filteredPersonICList);
  782.                         filteredPersonICList.remove(deleteContact);
  783.                         contactsPaneModel.setList(filteredPersonICList);
  784.                         String imagepath = "resources/images/"+String.valueOf(deleteContact.getPersonsICInspectorUsername().replaceAll("\\s+","")+"/"+deleteContact.getPersonsICFirstName().toLowerCase()+"_"+deleteContact.getPersonsICLastName().toLowerCase());
  785.                         File imagefolder = new File(imagepath);
  786.                         deleteImages(imagefolder);                 
  787.                         try {
  788.                             personICList.addAll(filteredPersonICList);
  789.                             fileManager.addPeopleICToFile(personICList);
  790.                         } catch (FileNotFoundException e1) {
  791.                             e1.printStackTrace();
  792.                         }
  793.                     }
  794.                 }
  795.             }
  796.         });
  797.        
  798.         containerPanel.add(contactsPanel);
  799.         contactsPanel.setVisible(true);
  800.         return contactsPanel;
  801.     }
  802.  
  803.     public void deleteImages(File file) {
  804.         if(file.isDirectory()) {
  805.             File[] files = file.listFiles();
  806.             for(int i=0;i<files.length;i++) {
  807.                 deleteImages(files[i]);
  808.             }
  809.             file.delete();
  810.         }
  811.         else {
  812.             file.delete();
  813.         }
  814.     }
  815.    
  816.     // Left side menu while managing contacts
  817.     private JPanel generateLeftSideMenuPanel() {
  818.         // Make left side panel
  819.         leftSideMenu = new JPanel(new GridLayout(20,0));
  820.         leftSideMenu.setBorder(new LineBorder(new Color(0, 0, 0)));
  821.         leftSideMenu.setBounds(0, 0, 213, 678);
  822.         leftSideMenu.setBackground(new Color(153, 204, 204));
  823.         loggedInspectorNameLabel = new JLabel("");
  824.         leftSideMenu.add(loggedInspectorNameLabel);
  825.         containerPanel.add(leftSideMenu);
  826.        
  827.         //Left side panel buttons
  828.         JButton leftSideMenuAddContactButton = new JButton("Add new contact");
  829.         leftSideMenu.add(leftSideMenuAddContactButton);
  830.         leftSideMenuAddContactButton.addActionListener(new ActionListener() {
  831.             public void actionPerformed(ActionEvent e) {
  832.                 editContactPressed = false;
  833.                 contactsPanel.setVisible(false);
  834.                 addNewContactPanel.setVisible(true);
  835.                 clearFields();
  836.             }
  837.         });    
  838.         JButton leftSideMenuViewContactButton = new JButton("View contact");
  839.         leftSideMenu.add(leftSideMenuViewContactButton);
  840.         leftSideMenuViewContactButton.addActionListener(new ActionListener() {
  841.             public void actionPerformed(ActionEvent e) {
  842.                 editContactPressed = false;
  843.                 contactsPanel.setVisible(true);
  844.                 addNewContactPanel.setVisible(false);
  845.             }  
  846.         });
  847.         JButton leftSideMenuImportContactsButton = new JButton("Import Contacts (.csv)");
  848.         leftSideMenu.add(leftSideMenuImportContactsButton);
  849.         leftSideMenuImportContactsButton.addActionListener(new ActionListener() {
  850.             public void actionPerformed(ActionEvent e) {
  851.                 JFileChooser fc = new JFileChooser("./databases");
  852.                 int returnVal = fc.showOpenDialog(frame);
  853.                 if (returnVal == JFileChooser.APPROVE_OPTION) {
  854.                     File importFile = fc.getSelectedFile();
  855.                     ArrayList<PersonInContacts> importContactList = fileManager.getPersonICList(importFile);
  856.                     ArrayList<PersonInContacts> checkedContactList = new ArrayList<PersonInContacts>();
  857.                     boolean isUnique = true;
  858.                     for(PersonInContacts impoertedContact: importContactList){                                                                         
  859.                         for(PersonInContacts contact: filteredPersonICList){
  860.                             if (impoertedContact.getPersonsICID() == contact.getPersonsICID()){
  861.                                 isUnique = false;
  862.                                 break;
  863.                             }
  864.                             else{
  865.                                 isUnique = true;
  866.                             }
  867.                         }
  868.                         if(isUnique) {
  869.                             checkedContactList.add(impoertedContact);
  870.                         }
  871.                     }
  872.                     personICList.removeAll(filteredPersonICList);
  873.                     filteredPersonICList.addAll(checkedContactList);
  874.                     contactsPaneModel.setList(filteredPersonICList);
  875.                     personICList.addAll(filteredPersonICList);
  876.                     try {
  877.                         fileManager.addPeopleICToFile(personICList);
  878.                     } catch (FileNotFoundException e1) {
  879.                         e1.printStackTrace();
  880.                     }  
  881.                 }
  882.             }  
  883.         });
  884.         JButton leftSideMenuLogoutButton = new JButton("Logout");
  885.         leftSideMenu.add(leftSideMenuLogoutButton);
  886.         leftSideMenu.setVisible(true);
  887.         leftSideMenuLogoutButton.addActionListener(new ActionListener() {
  888.             public void actionPerformed(ActionEvent event) {
  889.                 editContactPressed = false;
  890.                 int reply = JOptionPane.showConfirmDialog(frame, "Logout?", null, JOptionPane.YES_NO_OPTION);
  891.                 switch(reply) {
  892.                 case JOptionPane.YES_OPTION:
  893.                     loggedInspector = null;
  894.                     containerPanel.setVisible(false);
  895.                     loginOrRegisterPanel.setVisible(true);
  896.                     Component[] registerEntries = registerInspectorPanel.getComponents();
  897.                     for(Component component: registerEntries) {
  898.                         if (component instanceof JTextField) {
  899.                             JTextComponent specificObject = (JTextComponent) component;
  900.                             specificObject.setText("");            
  901.                         }
  902.                     }
  903.                     Component[] components = addNewContactPanel.getComponents();
  904.                     for (Component component: components) {
  905.                         if (component instanceof JTextField) {
  906.                             JTextComponent specificObject = (JTextComponent) component;
  907.                             specificObject.setText("");            
  908.                         }
  909.                         else if(component.equals(newContactLabelContactProfileArea)){
  910.                             JLabel specificObject = (JLabel) component;
  911.                             specificObject.setIcon(null);
  912.                         }
  913.                     }
  914.                     break;
  915.                 default:
  916.                     containerPanel.setVisible(true);
  917.                     loginInspectorPanel.setVisible(false);
  918.                     break;
  919.                 }
  920.             }
  921.         });
  922.  
  923.         return leftSideMenu;
  924.     }
  925.  
  926.     // Used in many Panels
  927.     private void clearFields() {
  928.         addContactCommentPane.setText("");
  929.         Component[] components = addNewContactPanel.getComponents();
  930.         for (Component component: components) {
  931.             if (component instanceof JTextField) {
  932.                 JTextComponent specificObject = (JTextComponent) component;
  933.                 specificObject.setText("");            
  934.             }if(component.equals(newContactLabelContactProfileArea)){
  935.                 JLabel specificObject = (JLabel) component;
  936.                 specificObject.setIcon(null);
  937.             }
  938.         }
  939.     }
  940.    
  941.     private void storeImages(ArrayList<PersonInContacts> personICList){
  942.         for(PersonInContacts person: personICList){
  943.             String imagepath = "resources/images/" + String.valueOf(person.getPersonsICInspectorUsername().replaceAll("\\s+","") + "/" + person.getPersonsICFirstName().toLowerCase() + "_" + person.getPersonsICLastName().toLowerCase());
  944.             File folderForContactImages = new File(imagepath);
  945.             folderForContactImages.mkdirs();
  946.         }
  947.     }
  948.     //Overload storeImages
  949.     private void storeImages(PersonInContacts contact) {
  950.         String imagepath = "resources/images/" + String.valueOf(contact.getPersonsICInspectorUsername().replaceAll("\\s+","") + "/" + contact.getPersonsICFirstName().toLowerCase() + "_" + contact.getPersonsICLastName().toLowerCase());
  951.         File folderForContactImages = new File(imagepath);
  952.         folderForContactImages.mkdirs();
  953.         try {
  954.             if(contactAvatar != null) {
  955.                 BufferedImage bufferContactImage = contactAvatar;
  956.                 File contactAvatarFile = new File(imagepath + "/contactAvatar.png");
  957.                 ImageIO.write(bufferContactImage, "png", contactAvatarFile);
  958.             }
  959.         } catch (IOException e) {
  960.             e.printStackTrace();
  961.         }
  962.     }
  963.    
  964.     private void setEditText(PersonInContacts editContact) {
  965.         editContactPressed = true;
  966.         addContactFirstName.setText(editContact.getPersonsICFirstName());
  967.         addContactLastName.setText(editContact.getPersonsICLastName());
  968.         addContactPhoneNumber.setText(editContact.getPersonsICPhoneNumber());
  969.         addContactCommentPane.setText(editContact.getPersonsICComments());
  970.         String imagepath = "resources/images/" + String.valueOf(editContact.getPersonsICInspectorUsername().replaceAll("\\s+","") + "/" + editContact.getPersonsICFirstName().toLowerCase() + "_" + editContact.getPersonsICLastName().toLowerCase());
  971.         File fileOnContactProfile = new File(imagepath + "/profilephoto.png");
  972.         try {
  973.             if(fileOnContactProfile.exists()) {
  974.                 BufferedImage profilePhoto = ImageIO.read(fileOnContactProfile);
  975.                 Image finalImageLook = profilePhoto.getScaledInstance(120, 120,Image.SCALE_SMOOTH);
  976.                 newContactLabelContactProfileArea.setIcon(new ImageIcon(finalImageLook));
  977.             }
  978.         } catch (IOException e) {
  979.             e.printStackTrace();
  980.         }
  981.         addNewContactPanel.setVisible(true);
  982.         contactsPanel.setVisible(false);
  983.     }
  984. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement