Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package frontend;
- import backend.*;
- import inputcheckers.*;
- import people.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Cursor;
- import java.awt.Desktop;
- import java.awt.EventQueue;
- import javax.swing.border.LineBorder;
- import javax.swing.event.DocumentEvent;
- import javax.swing.event.DocumentListener;
- import javax.swing.table.TableRowSorter;
- import javax.swing.text.JTextComponent;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import javax.swing.JTextField;
- import javax.swing.JTextPane;
- import javax.swing.RowFilter;
- import javax.swing.SwingConstants;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFileChooser;
- import javax.swing.JFormattedTextField;
- import java.awt.Font;
- import javax.swing.JSeparator;
- import javax.swing.JTable;
- import javax.swing.JScrollPane;
- public class FrontEnd {
- // Entire frame
- public JFrame frame;
- // Panels
- private JPanel containerPanel;
- private JPanel loginInspectorPanel;
- private JPanel registerInspectorPanel;
- private JPanel addNewContactPanel;
- private JPanel leftSideMenu;
- private JPanel contactsPanel = new JPanel();
- private JPanel loginOrRegisterPanel;
- // Components on loginInspectorPanel
- private JFormattedTextField loginInspectorUsername;
- private JPasswordField loginInspectorPassword;;
- // Components on registerInspectorPanel
- private JTextField registryInspectorFirstName;
- private JTextField registryInspectorLastName;
- private JTextField registryInspectorUsername;
- private JPasswordField registryInspectorPassword;
- private JPasswordField registryInspectorConfirmPassword;
- private JButton registryInspectorRegisterButton;
- private JLabel registryInspectorRegisterIcon;
- private JButton registryInspectorReturnButton;
- // Components on addNewContactPanel
- private JTextField addContactFirstName;;
- private JTextField addContactLastName;;
- private JTextField addContactPhoneNumber;;
- private JTextPane addContactCommentPane;;
- private JLabel newContactLabelContactProfileArea;
- private JScrollPane commentScrollPane;
- // Components on tablePanel to access
- private JTextField filterText;
- // List with inspectors in system
- private ArrayList<Inspector> inspectorsList;
- // List with contacts of inspectors
- private ArrayList<PersonInContacts> personICList;
- // Access to files since we do not have database yet
- private FileManager fileManager = new FileManager();
- // Images paths
- private String appLogo = "resources/images/logoImage.png";
- private String contactsAvatars = "resources/images/avatars";
- // Info about current session
- private Inspector loggedInspector;
- private int contactIndex;
- private BufferedImage contactAvatar;
- // Viewing and editing contacts
- private ContactsPane contactsPaneModel = null;
- private TableRowSorter<ContactsPane> sorter;
- private JTable contactsTable = new JTable(contactsPaneModel);
- private JScrollPane contactScrollPane = new JScrollPane(contactsTable);
- private ArrayList<PersonInContacts> filteredPersonICList;
- private JLabel loggedInspectorNameLabel;
- private boolean editContactPressed = false;
- private File pp;
- // Constructor
- public FrontEnd() {
- try {
- fileManager.makeInspectorsFile();
- fileManager.makePeopleICFile();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- inspectorsList = fileManager.getInspectorList();
- initialize();
- }
- private void initialize() {
- frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setTitle("ITD Helper System");
- frame.setBounds(0, 0, 1200, 700);
- frame.getContentPane().setLayout(null);
- frame.setLocationRelativeTo(null);
- containerPanel = new JPanel();
- containerPanel.setBounds(0, 0, 1200, 678);
- containerPanel.setLayout(null);
- containerPanel.setVisible(false);
- frame.getContentPane().add(containerPanel);
- generateLoginPanel();
- registerInspectorPanel();
- generateNewContactToAdd();
- generateLeftSideMenuPanel();
- generateLoginOrRegisterPanel();
- //Setting which panel is visible at the beginning
- loginOrRegisterPanel.setVisible(true);
- loginInspectorPanel.setVisible(false);
- registerInspectorPanel.setVisible(false);
- contactsPanel.setVisible(false);
- addNewContactPanel.setVisible(false);
- }
- private JPanel generateLoginOrRegisterPanel() {
- // Make main choose panel
- loginOrRegisterPanel = new JPanel();
- loginOrRegisterPanel.setBounds(0, 0, 1200, 678);
- loginOrRegisterPanel.setLayout(null);
- frame.getContentPane().add(loginOrRegisterPanel);
- // Button fields on loginOrRegister panel
- JButton loginOrRegisterLOG = new JButton("Login");
- loginOrRegisterLOG.setBounds(432, 466, 161, 29);
- loginOrRegisterPanel.add(loginOrRegisterLOG);
- loginOrRegisterLOG.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- loginInspectorPanel.setVisible(true);
- loginOrRegisterPanel.setVisible(false);
- }
- });
- JButton loginOrRegisterREG = new JButton("Register");
- loginOrRegisterREG.setBounds(732, 466, 161, 29);
- loginOrRegisterPanel.add(loginOrRegisterREG);
- loginOrRegisterREG.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- registerInspectorPanel.setVisible(true);
- loginOrRegisterPanel.setVisible(false);
- }
- });
- //Icon on loginOrRegisterPanel
- JLabel loginOrRegisterPanelImage = new JLabel("");
- loginOrRegisterPanelImage.setBounds(500, 100, 300, 300);
- loginOrRegisterPanel.add(loginOrRegisterPanelImage);
- ImageIcon imageIcon = new ImageIcon(appLogo);
- Image image = imageIcon.getImage();
- Image newimg = image.getScaledInstance(300, 300, java.awt.Image.SCALE_SMOOTH);
- imageIcon = new ImageIcon(newimg);
- loginOrRegisterPanelImage.setIcon(imageIcon);
- return loginOrRegisterPanel;
- }
- // Register section
- private JPanel registerInspectorPanel() {
- // Make main registry panel
- registerInspectorPanel = new JPanel();
- registerInspectorPanel.setBounds(0, 0, 1200, 678);
- registerInspectorPanel.setLayout(null);
- frame.getContentPane().add(registerInspectorPanel);
- // Labels on registry panel
- JLabel registryLabelFirstName = new JLabel("First Name");
- registryLabelFirstName.setBounds(474, 231, 107, 16);
- registerInspectorPanel.add(registryLabelFirstName);
- JLabel registryLabelLastName = new JLabel("Last Name");
- registryLabelLastName.setBounds(474, 273, 107, 16);
- registerInspectorPanel.add(registryLabelLastName);
- JLabel registryLabelUsername = new JLabel("Username");
- registryLabelUsername.setBounds(474, 313, 107, 16);
- registerInspectorPanel.add(registryLabelUsername);
- JLabel registryLabelPassword = new JLabel("Password");
- registryLabelPassword.setBounds(474, 363, 107, 16);
- registerInspectorPanel.add(registryLabelPassword);
- JLabel registryLabelRegister = new JLabel("Registered new Inspector");
- registryLabelRegister.setFont(new Font("Helvetica", Font.PLAIN, 18));
- registryLabelRegister.setBounds(519, 190, 175, 16);
- registerInspectorPanel.add(registryLabelRegister);
- JLabel registryLabelConfirmPassword = new JLabel("Confirm Password");
- registryLabelConfirmPassword.setBounds(474, 408, 161, 16);
- registerInspectorPanel.add(registryLabelConfirmPassword);
- registryInspectorRegisterIcon = new JLabel("");
- registryInspectorRegisterIcon.setBounds(519, 41, 186, 122);
- registerInspectorPanel.add(registryInspectorRegisterIcon);
- // Text fields on registry panel
- registryInspectorFirstName = new JTextField();
- registryInspectorFirstName.setBounds(636, 226, 130, 26);
- registerInspectorPanel.add(registryInspectorFirstName);
- registryInspectorFirstName.setColumns(10);
- registryInspectorLastName = new JTextField();
- registryInspectorLastName.setColumns(10);
- registryInspectorLastName.setBounds(636, 268, 130, 26);
- registerInspectorPanel.add(registryInspectorLastName);
- registryInspectorUsername = new JTextField();
- registryInspectorUsername.setColumns(10);
- registryInspectorUsername.setBounds(636, 308, 130, 26);
- registerInspectorPanel.add(registryInspectorUsername);
- // Password fields on registry panel
- registryInspectorPassword = new JPasswordField();
- registryInspectorPassword.setBounds(636, 403, 130, 26);
- registerInspectorPanel.add(registryInspectorPassword);
- registerInspectorPanel.setVisible(false);
- registryInspectorConfirmPassword = new JPasswordField();
- registryInspectorConfirmPassword.setBounds(636, 358, 130, 26);
- registerInspectorPanel.add(registryInspectorConfirmPassword);
- // Buttons on registry panel
- JButton registryInspectorReturnButton = new JButton("Return");
- registryInspectorReturnButton.setBounds(432, 466, 161, 29);
- registerInspectorPanel.add(registryInspectorReturnButton);
- registryInspectorReturnButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- loginOrRegisterPanel.setVisible(true);
- registerInspectorPanel.setVisible(false);
- }
- });
- registryInspectorRegisterButton = new JButton("Register and Login");
- registryInspectorRegisterButton.setBounds(605, 466, 161, 29);
- registerInspectorPanel.add(registryInspectorRegisterButton);
- registryInspectorRegisterButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(!registryInspectorPassword.getText().equals(registryInspectorConfirmPassword.getText())) {
- JOptionPane.showMessageDialog(frame, "Passwords must match!", "Warning", JOptionPane.WARNING_MESSAGE);
- }
- else{
- Inspector addInspectorToCheck = setRegistryFields();
- checkInspectorFromRegisterExist(addInspectorToCheck);
- }
- }
- });
- //Logo settings
- ImageIcon imageIcon = new ImageIcon(appLogo);
- Image image = imageIcon.getImage();
- Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH);
- imageIcon = new ImageIcon(newimg);
- registryInspectorRegisterIcon.setIcon(imageIcon);
- JSeparator separator = new JSeparator();
- separator.setBounds(455, 207, 337, 12);
- registerInspectorPanel.add(separator);
- return registerInspectorPanel;
- }
- private Inspector setRegistryFields(){
- Inspector addInspector = new Inspector();
- addInspector.setInspectorPassword(registryInspectorConfirmPassword.getText());
- addInspector.setInspectorUsername(registryInspectorUsername.getText());
- addInspector.setFirstName(registryInspectorFirstName.getText());
- addInspector.setLastName(registryInspectorLastName.getText());
- addInspector.setInspectorID(addInspector.hashCode());
- return addInspector;
- }
- private boolean checkInspectorFromRegisterExist(Inspector addInspector){
- boolean inspectorExists = false;
- int addID = addInspector.getInspectorID();
- for(Inspector inspector: inspectorsList){
- if(inspector.getInspectorID() == addID) {
- inspectorExists = true;
- addInspector = inspector;
- break;
- }
- }
- if(!inspectorExists) {
- filteredPersonICList = new ArrayList<PersonInContacts>();
- contactsPaneModel = new ContactsPane(filteredPersonICList);
- JOptionPane.showMessageDialog(frame, "Successfully registered as " + addInspector.getFirstName() + " " + addInspector.getLastName(), "Login Successful", JOptionPane.INFORMATION_MESSAGE);
- loggedInspectorNameLabel.setText("Logged as " + addInspector.getFirstName() + " " + addInspector.getLastName());
- inspectorsList.add(addInspector);
- loggedInspector = addInspector;
- personICList = fileManager.getPersonICList();
- storeImages(personICList);
- try {
- fileManager.addInspectorToFile(inspectorsList);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- for(PersonInContacts contact: personICList) {
- if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
- filteredPersonICList.add(contact);
- }
- }
- for(PersonInContacts contact: filteredPersonICList) {
- if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
- personICList.remove(contact);
- }
- }
- generateContactMainPanel();
- clearFields();
- contactsPanel.setVisible(false);
- loginInspectorPanel.setVisible(false);
- registerInspectorPanel.setVisible(false);
- containerPanel.setVisible(true);
- addNewContactPanel.setVisible(true);
- }
- if(inspectorExists) {
- int reply = JOptionPane.showConfirmDialog(null, addInspector.getFirstName() + " " + addInspector.getLastName() + " already registered. Login in as this inspector?" , "Registration", JOptionPane.YES_NO_OPTION);
- if (reply == JOptionPane.YES_OPTION) {
- filteredPersonICList=new ArrayList<PersonInContacts>();
- contactsPaneModel = new ContactsPane(filteredPersonICList);
- JOptionPane.showMessageDialog(frame, "Successfully logged as " + addInspector.getFirstName() + " " + addInspector.getLastName(), "Login Successful", JOptionPane.INFORMATION_MESSAGE);
- loggedInspectorNameLabel.setText("Logged as "+addInspector.getFirstName()+" "+addInspector.getLastName());
- loggedInspector = addInspector;
- personICList = fileManager.getPersonICList();
- storeImages(personICList);
- for(PersonInContacts contact: personICList) {
- if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
- filteredPersonICList.add(contact);
- }
- }
- for(PersonInContacts contact: filteredPersonICList) {
- if(contact.getPersonsICInspectorID() == loggedInspector.getInspectorID()) {
- personICList.remove(contact);
- }
- }
- generateContactMainPanel();
- clearFields();
- contactsPanel.setVisible(false);
- loginInspectorPanel.setVisible(false);
- registerInspectorPanel.setVisible(false);
- containerPanel.setVisible(true);
- addNewContactPanel.setVisible(true);
- }
- }
- return inspectorExists;
- }
- // Login section
- private JPanel generateLoginPanel(){
- // Make main login panel
- loginInspectorPanel = new JPanel();
- loginInspectorPanel.setBounds(0, 0, 1200, 678);
- frame.getContentPane().add(loginInspectorPanel);
- loginInspectorPanel.setLayout(null);
- // Labels on login panel
- JLabel registryInspectorLoginIcon = new JLabel("");
- registryInspectorLoginIcon.setBounds(517, 113, 159, 158);
- loginInspectorPanel.add(registryInspectorLoginIcon);
- // Text fields on login panel
- loginInspectorUsername = new JFormattedTextField();
- loginInspectorUsername.setBounds(517, 330, 159, 25);
- loginInspectorUsername.setText("Enter Username");
- loginInspectorPanel.add(loginInspectorUsername);
- loginInspectorUsername.addMouseListener(new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e){
- loginInspectorUsername.setText("");
- }
- });
- // Password fields on login panel
- loginInspectorPassword = new JPasswordField();
- loginInspectorPassword.setText("Enter Password");
- loginInspectorPassword.setBounds(517, 391, 159, 25);
- loginInspectorPanel.add(loginInspectorPassword);
- loginInspectorPassword.addMouseListener(new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e){
- loginInspectorPassword.setText("");
- }
- });
- // Button fields on login panel
- JButton loginInspectorButton = new JButton("Login");
- loginInspectorButton.setBounds(517, 465, 159, 25);
- loginInspectorPanel.add(loginInspectorButton);
- loginInspectorButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Inspector addInspectorToCheck = setInspectorFields();
- checkInspectorFromLoginExist(addInspectorToCheck);
- }
- });
- JButton loginPanelReturnButton = new JButton("Return");
- loginPanelReturnButton.setBounds(717, 465, 159, 25);
- loginInspectorPanel.add(loginPanelReturnButton);
- loginPanelReturnButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- loginInspectorPanel.setVisible(false);
- loginOrRegisterPanel.setVisible(true);
- }
- });
- //Logo settings
- ImageIcon imageIcon = new ImageIcon(appLogo);
- Image image = imageIcon.getImage();
- Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH);
- imageIcon = new ImageIcon(newimg);
- registryInspectorLoginIcon.setIcon(imageIcon);
- loginInspectorPanel.setVisible(false);
- return loginInspectorPanel;
- }
- private Inspector setInspectorFields() {
- Inspector inspector = new Inspector();
- inspector.setInspectorPassword(loginInspectorPassword.getText());
- inspector.setInspectorUsername(loginInspectorUsername.getText());
- inspector.setInspectorID(inspector.hashCode());
- return inspector;
- }
- private boolean checkInspectorFromLoginExist(Inspector addInspector) {
- boolean inspectorExists = false;
- int addID = addInspector.getInspectorID();
- for(Inspector inspector: inspectorsList) {
- if(inspector.getInspectorID()==addID) {
- inspectorExists = true;
- addInspector = inspector;
- break;
- }
- }
- if(!inspectorExists) {
- int reply = JOptionPane.showConfirmDialog(null, "User not registered. Registry?", "Registration", JOptionPane.YES_NO_OPTION);
- if(reply == JOptionPane.YES_OPTION) {
- loginInspectorPanel.setVisible(false);
- registerInspectorPanel.setVisible(true);
- Component[] components = registerInspectorPanel.getComponents();
- for(Component c: components){
- if (c instanceof JTextField) {
- JTextComponent specificObject = (JTextComponent) c;
- specificObject.setText("");
- }
- }
- }
- }
- if(inspectorExists) {
- filteredPersonICList = new ArrayList<PersonInContacts>();
- contactsPaneModel = new ContactsPane(filteredPersonICList);
- JOptionPane.showMessageDialog(frame, "Congratulations! You are logged as " + addInspector.getFirstName() + " " + addInspector.getLastName(), "Login Successful", JOptionPane.INFORMATION_MESSAGE);
- loggedInspectorNameLabel.setText("Logged as " + addInspector.getFirstName() + " " + addInspector.getLastName());
- loggedInspector = addInspector;
- personICList = fileManager.getPersonICList();
- storeImages(personICList);
- for(PersonInContacts person: personICList) {
- if(person.getPersonsICInspectorID() == loggedInspector.getInspectorID()){
- filteredPersonICList.add(person);
- }
- }
- for(PersonInContacts person: filteredPersonICList) {
- if(person.getPersonsICInspectorID() == loggedInspector.getInspectorID()){
- personICList.remove(person);
- }
- }
- generateContactMainPanel();
- clearFields();
- contactsPanel.setVisible(false);
- loginInspectorPanel.setVisible(false);
- containerPanel.setVisible(true);
- addNewContactPanel.setVisible(true);
- }
- return inspectorExists;
- }
- // Generating new Contact
- private JPanel generateNewContactToAdd() {
- // Make main new contact Panel
- addNewContactPanel = new JPanel();
- addNewContactPanel.setBackground(new Color(204, 255, 255));
- addNewContactPanel.setBounds(212, 0, 988, 678);
- // Labels on contact adding panel
- JLabel newContactLabelFirstName = new JLabel("First Name");
- addNewContactPanel.add(newContactLabelFirstName, "cell 0 0,growx,aligny center");
- JLabel newContactLabelLirstName = new JLabel("Last Name");
- addNewContactPanel.add(newContactLabelLirstName, "cell 0 4,growx,aligny top");
- JLabel newContactLabelPhoneNumber = new JLabel("Phone Number");
- addNewContactPanel.add(newContactLabelPhoneNumber, "cell 0 12,growx,aligny center");
- JLabel newContactLabelContacts = new JLabel("Comments");
- addNewContactPanel.add(newContactLabelContacts, "cell 0 22,growx,aligny top");
- newContactLabelContactProfileArea = new JLabel();
- addNewContactPanel.add(newContactLabelContactProfileArea, "cell 10 22,grow");
- // Text fields on contact adding panel
- addContactFirstName = new JTextField();
- addNewContactPanel.add(addContactFirstName, "cell 4 0,alignx left,aligny top");
- addContactFirstName.setColumns(10);
- addContactLastName = new JTextField();
- addContactLastName.setColumns(10);
- addNewContactPanel.add(addContactLastName, "cell 4 2 1 3,alignx left,aligny top");
- addContactPhoneNumber = new JTextField();
- addContactPhoneNumber.setInputVerifier(new VerifyPhoneNumber());
- addContactPhoneNumber.setColumns(10);
- addNewContactPanel.add(addContactPhoneNumber, "cell 4 12,alignx left,aligny top");
- addContactCommentPane = new JTextPane();
- commentScrollPane = new JScrollPane(addContactCommentPane);
- addNewContactPanel.add(commentScrollPane, "cell 4 22,grow");
- // Buttons on contact adding panel
- JButton newContactUploadContactButton = new JButton("Upload");
- addNewContactPanel.add(newContactUploadContactButton, "cell 8 22,alignx left,aligny center");
- newContactUploadContactButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooseFile = new JFileChooser(contactsAvatars);
- chooseFile.showOpenDialog(null);
- try {
- contactAvatar = null;
- pp = chooseFile.getSelectedFile();
- contactAvatar = ImageIO.read(pp);
- Image finalImageLook = contactAvatar.getScaledInstance(120, 120,Image.SCALE_SMOOTH);//This scales the image
- newContactLabelContactProfileArea.setIcon(new ImageIcon(finalImageLook));
- } catch (IOException io) {
- io.printStackTrace();
- }
- }
- });
- JButton newContactConfirmButton = new JButton("Confirm");
- addNewContactPanel.add(newContactConfirmButton, "cell 8 26 3 1,alignx left,aligny top");
- newContactConfirmButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- PersonInContacts addPersonIC = setNewContactFields();
- checkNewContact(addPersonIC);
- }
- });
- // Separators on contact adding for some visual organization
- JSeparator newContactSeparator = new JSeparator();
- newContactSeparator.setForeground(Color.LIGHT_GRAY);
- addNewContactPanel.add(newContactSeparator, "cell 0 24 7 1,growx,aligny top");
- JSeparator newContactSeparator1 = new JSeparator();
- newContactSeparator1.setOrientation(SwingConstants.VERTICAL);
- newContactSeparator1.setForeground(Color.LIGHT_GRAY);
- addNewContactPanel.add(newContactSeparator1, "cell 7 0 1 27,grow");
- JSeparator newContactSeparator2 = new JSeparator();
- newContactSeparator2.setForeground(Color.LIGHT_GRAY);
- addNewContactPanel.add(newContactSeparator2, "cell 8 24 3 1,growx,aligny top");
- containerPanel.add(addNewContactPanel);
- return addNewContactPanel;
- }
- private PersonInContacts setNewContactFields() {
- PersonInContacts addPersonIC = new PersonInContacts();
- addPersonIC.setPersonsICFirstName(addContactFirstName.getText());
- addPersonIC.setPersonsICLastName(addContactLastName.getText());
- addPersonIC.setPersonsICComments(addContactCommentPane.getText().replaceAll("\n|\t|,", " "));
- addPersonIC.setPersonsICPhoneNumber(addContactPhoneNumber.getText());
- addPersonIC.setPersonsICInspectorID(loggedInspector.getInspectorID());
- addPersonIC.setPersonsICInspectorUsername(loggedInspector.getInspectorUsername());
- addPersonIC.setPersonsICID(addPersonIC.hashCode());
- return addPersonIC;
- }
- private void checkNewContact(PersonInContacts addPersonIC){
- PersonInContacts replaceExistingContact = null;
- int addID = addPersonIC.getPersonsICID();
- boolean contactToAddExists = false;
- contactIndex = 0;
- for(PersonInContacts person: filteredPersonICList) {
- if(person.getPersonsICID() == addID) {
- contactToAddExists = true;
- contactIndex = filteredPersonICList.indexOf(addPersonIC);
- replaceExistingContact = filteredPersonICList.get(contactIndex);
- break;
- }
- }
- if(!contactToAddExists && editContactPressed) {
- JOptionPane.showMessageDialog(frame, "You cant change First Name or Last Name of existing contact.", "Warning - trying to create new patient", JOptionPane.WARNING_MESSAGE);
- }
- else if(contactToAddExists && editContactPressed) {
- personICList.removeAll(filteredPersonICList);
- filteredPersonICList.set(contactIndex, addPersonIC);
- contactsPaneModel.setList(filteredPersonICList);
- storeImages(addPersonIC);
- try {
- personICList.addAll(filteredPersonICList);
- fileManager.addPeopleICToFile(personICList);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- JOptionPane.showMessageDialog(frame, "Contact has been edited");
- contactsPanel.setVisible(true);
- addNewContactPanel.setVisible(false);
- }
- else if(!contactToAddExists && !editContactPressed) {
- storeImages(addPersonIC);
- personICList.removeAll(filteredPersonICList);
- filteredPersonICList.add(addPersonIC);
- contactsPaneModel.setList(filteredPersonICList);
- try {
- personICList.addAll(filteredPersonICList);
- fileManager.addPeopleICToFile(personICList);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- JOptionPane.showMessageDialog(frame, "Contact has been added");
- contactsPanel.setVisible(true);
- addNewContactPanel.setVisible(false);
- }
- else {
- int reply = JOptionPane.showConfirmDialog(frame, "Contact already in the system! Edit?", "Warning", JOptionPane.YES_NO_OPTION);
- if (reply == JOptionPane.YES_OPTION) {
- editContactPressed = true;
- setEditText(replaceExistingContact);
- }
- }
- }
- private JPanel generateContactMainPanel(){
- // Make main panel
- contactsPanel.setBackground(new Color(153, 204, 255));
- contactsPanel.setBounds(212, 0, 988, 678);
- contactsPanel.setLayout(null);
- contactsTable.setModel(contactsPaneModel);
- contactsTable.setFillsViewportHeight(true);
- contactsTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
- sorter = new TableRowSorter<ContactsPane> (contactsPaneModel);
- int columncount = contactsPaneModel.getColumnCount();
- for(int i=0; i < columncount; i++) {
- sorter.setSortable(i, false);
- }
- contactsTable.setRowSorter(sorter);
- // Search among contacts in table
- filterText = new JTextField("Type to search among contacts");
- filterText.setSize(520, 18);
- filterText.setLocation(220, 28);
- contactsPanel.add(filterText);
- filterText.setColumns(10);
- filterText.addFocusListener(new FocusListener() {
- @Override
- public void focusGained(FocusEvent e) {
- filterText.setText("");
- }
- @Override
- public void focusLost(FocusEvent e) {
- }
- });
- filterText.getDocument().addDocumentListener(new DocumentListener() {
- RowFilter<ContactsPane, Object> rf = null;
- String text = filterText.getText();
- public void changedUpdate(DocumentEvent e) {
- if(text.length() == 0) {
- sorter.setRowFilter(null);
- }
- else {
- try {
- rf = RowFilter.regexFilter("(?i)" + filterText.getText());
- sorter.setRowFilter(rf);
- } catch (java.util.regex.PatternSyntaxException p) {
- return;
- }
- }
- }
- @Override
- public void insertUpdate(DocumentEvent e) {
- if(text.length() == 0) {
- sorter.setRowFilter(null);
- }
- else {
- try {
- rf = RowFilter.regexFilter("(?i)" + filterText.getText());
- sorter.setRowFilter(rf);
- } catch (java.util.regex.PatternSyntaxException p) {
- return;
- }
- }
- }
- @Override
- public void removeUpdate(DocumentEvent e) {
- if (text.length() == 0) {
- sorter.setRowFilter(null);
- }
- else {
- try {
- rf = RowFilter.regexFilter("(?i)" + filterText.getText());
- sorter.setRowFilter(rf);
- } catch (java.util.regex.PatternSyntaxException p) {
- return;
- }
- }
- }
- });
- contactScrollPane.setLocation(6, 154);
- contactScrollPane.setSize(968, 505);
- contactsPanel.add(contactScrollPane);
- // Buttons on ContactMainPanel to manage contacts in files
- JButton contactPanelExportContactsButton = new JButton("Export selected contacts");
- contactPanelExportContactsButton.setBounds(795, 123, 179, 19);
- contactsPanel.add(contactPanelExportContactsButton);
- contactPanelExportContactsButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- editContactPressed = false;
- int[] selection = contactsTable.getSelectedRows();
- ArrayList<PersonInContacts> exportContactList = new ArrayList<PersonInContacts>();
- for (int i = 0; i < selection.length; i++) {
- selection[i] = contactsTable.convertRowIndexToModel(selection[i]);
- exportContactList.add(filteredPersonICList.get(contactsTable.convertRowIndexToModel(selection[i])));
- }
- JFileChooser fc = new JFileChooser("./databases");
- int returnVal = fc.showSaveDialog(frame);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File exportfile = new File(fc.getSelectedFile() + ".csv");
- try {
- fileManager.exportContacts(exportContactList, exportfile);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- JOptionPane.showMessageDialog(frame, "File has been exported", "Confirmed", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- });
- JButton contactPanelEditContactButton = new JButton("Edit selected contact");
- contactPanelEditContactButton.setBounds(314, 115, 179, 24);
- contactsPanel.add(contactPanelEditContactButton);
- contactPanelEditContactButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(contactsTable.getSelectedRowCount() > 1) {
- JOptionPane.showMessageDialog(frame, "Only one at a time", "Warning", JOptionPane.WARNING_MESSAGE);
- }
- else {
- editContactPressed = true;
- int selectedRow = contactsTable.getSelectedRow();
- PersonInContacts editContact = filteredPersonICList.get(contactsTable.convertRowIndexToModel(selectedRow)); //This is definitely the selected patient, I have checked via debug mode
- setEditText(editContact);
- }
- }
- });
- JButton contactPanelDeleteContacttButton = new JButton("Delete selected contact");
- contactPanelDeleteContacttButton.setBounds(26, 115, 200, 24);
- contactsPanel.add(contactPanelDeleteContacttButton);
- contactPanelDeleteContacttButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(contactsTable.getSelectedRowCount() > 1) {
- JOptionPane.showMessageDialog(frame, "Only one at a time", "Warning", JOptionPane.WARNING_MESSAGE);
- }
- else {
- int modelRow = contactsTable.convertRowIndexToModel(contactsTable.getSelectedRow());
- PersonInContacts deleteContact = filteredPersonICList.get(modelRow);
- int reply = JOptionPane.showConfirmDialog(null, "Delete "+deleteContact.getPersonsICFirstName()+" "+deleteContact.getPersonsICLastName()+ " ?", "Confirm delete", JOptionPane.YES_NO_OPTION);
- if(reply == JOptionPane.YES_OPTION) {
- personICList.removeAll(filteredPersonICList);
- filteredPersonICList.remove(deleteContact);
- contactsPaneModel.setList(filteredPersonICList);
- String imagepath = "resources/images/"+String.valueOf(deleteContact.getPersonsICInspectorUsername().replaceAll("\\s+","")+"/"+deleteContact.getPersonsICFirstName().toLowerCase()+"_"+deleteContact.getPersonsICLastName().toLowerCase());
- File imagefolder = new File(imagepath);
- deleteImages(imagefolder);
- try {
- personICList.addAll(filteredPersonICList);
- fileManager.addPeopleICToFile(personICList);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
- });
- containerPanel.add(contactsPanel);
- contactsPanel.setVisible(true);
- return contactsPanel;
- }
- public void deleteImages(File file) {
- if(file.isDirectory()) {
- File[] files = file.listFiles();
- for(int i=0;i<files.length;i++) {
- deleteImages(files[i]);
- }
- file.delete();
- }
- else {
- file.delete();
- }
- }
- // Left side menu while managing contacts
- private JPanel generateLeftSideMenuPanel() {
- // Make left side panel
- leftSideMenu = new JPanel(new GridLayout(20,0));
- leftSideMenu.setBorder(new LineBorder(new Color(0, 0, 0)));
- leftSideMenu.setBounds(0, 0, 213, 678);
- leftSideMenu.setBackground(new Color(153, 204, 204));
- loggedInspectorNameLabel = new JLabel("");
- leftSideMenu.add(loggedInspectorNameLabel);
- containerPanel.add(leftSideMenu);
- //Left side panel buttons
- JButton leftSideMenuAddContactButton = new JButton("Add new contact");
- leftSideMenu.add(leftSideMenuAddContactButton);
- leftSideMenuAddContactButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- editContactPressed = false;
- contactsPanel.setVisible(false);
- addNewContactPanel.setVisible(true);
- clearFields();
- }
- });
- JButton leftSideMenuViewContactButton = new JButton("View contact");
- leftSideMenu.add(leftSideMenuViewContactButton);
- leftSideMenuViewContactButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- editContactPressed = false;
- contactsPanel.setVisible(true);
- addNewContactPanel.setVisible(false);
- }
- });
- JButton leftSideMenuImportContactsButton = new JButton("Import Contacts (.csv)");
- leftSideMenu.add(leftSideMenuImportContactsButton);
- leftSideMenuImportContactsButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser fc = new JFileChooser("./databases");
- int returnVal = fc.showOpenDialog(frame);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File importFile = fc.getSelectedFile();
- ArrayList<PersonInContacts> importContactList = fileManager.getPersonICList(importFile);
- ArrayList<PersonInContacts> checkedContactList = new ArrayList<PersonInContacts>();
- boolean isUnique = true;
- for(PersonInContacts impoertedContact: importContactList){
- for(PersonInContacts contact: filteredPersonICList){
- if (impoertedContact.getPersonsICID() == contact.getPersonsICID()){
- isUnique = false;
- break;
- }
- else{
- isUnique = true;
- }
- }
- if(isUnique) {
- checkedContactList.add(impoertedContact);
- }
- }
- personICList.removeAll(filteredPersonICList);
- filteredPersonICList.addAll(checkedContactList);
- contactsPaneModel.setList(filteredPersonICList);
- personICList.addAll(filteredPersonICList);
- try {
- fileManager.addPeopleICToFile(personICList);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- }
- }
- });
- JButton leftSideMenuLogoutButton = new JButton("Logout");
- leftSideMenu.add(leftSideMenuLogoutButton);
- leftSideMenu.setVisible(true);
- leftSideMenuLogoutButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- editContactPressed = false;
- int reply = JOptionPane.showConfirmDialog(frame, "Logout?", null, JOptionPane.YES_NO_OPTION);
- switch(reply) {
- case JOptionPane.YES_OPTION:
- loggedInspector = null;
- containerPanel.setVisible(false);
- loginOrRegisterPanel.setVisible(true);
- Component[] registerEntries = registerInspectorPanel.getComponents();
- for(Component component: registerEntries) {
- if (component instanceof JTextField) {
- JTextComponent specificObject = (JTextComponent) component;
- specificObject.setText("");
- }
- }
- Component[] components = addNewContactPanel.getComponents();
- for (Component component: components) {
- if (component instanceof JTextField) {
- JTextComponent specificObject = (JTextComponent) component;
- specificObject.setText("");
- }
- else if(component.equals(newContactLabelContactProfileArea)){
- JLabel specificObject = (JLabel) component;
- specificObject.setIcon(null);
- }
- }
- break;
- default:
- containerPanel.setVisible(true);
- loginInspectorPanel.setVisible(false);
- break;
- }
- }
- });
- return leftSideMenu;
- }
- // Used in many Panels
- private void clearFields() {
- addContactCommentPane.setText("");
- Component[] components = addNewContactPanel.getComponents();
- for (Component component: components) {
- if (component instanceof JTextField) {
- JTextComponent specificObject = (JTextComponent) component;
- specificObject.setText("");
- }if(component.equals(newContactLabelContactProfileArea)){
- JLabel specificObject = (JLabel) component;
- specificObject.setIcon(null);
- }
- }
- }
- private void storeImages(ArrayList<PersonInContacts> personICList){
- for(PersonInContacts person: personICList){
- String imagepath = "resources/images/" + String.valueOf(person.getPersonsICInspectorUsername().replaceAll("\\s+","") + "/" + person.getPersonsICFirstName().toLowerCase() + "_" + person.getPersonsICLastName().toLowerCase());
- File folderForContactImages = new File(imagepath);
- folderForContactImages.mkdirs();
- }
- }
- //Overload storeImages
- private void storeImages(PersonInContacts contact) {
- String imagepath = "resources/images/" + String.valueOf(contact.getPersonsICInspectorUsername().replaceAll("\\s+","") + "/" + contact.getPersonsICFirstName().toLowerCase() + "_" + contact.getPersonsICLastName().toLowerCase());
- File folderForContactImages = new File(imagepath);
- folderForContactImages.mkdirs();
- try {
- if(contactAvatar != null) {
- BufferedImage bufferContactImage = contactAvatar;
- File contactAvatarFile = new File(imagepath + "/contactAvatar.png");
- ImageIO.write(bufferContactImage, "png", contactAvatarFile);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void setEditText(PersonInContacts editContact) {
- editContactPressed = true;
- addContactFirstName.setText(editContact.getPersonsICFirstName());
- addContactLastName.setText(editContact.getPersonsICLastName());
- addContactPhoneNumber.setText(editContact.getPersonsICPhoneNumber());
- addContactCommentPane.setText(editContact.getPersonsICComments());
- String imagepath = "resources/images/" + String.valueOf(editContact.getPersonsICInspectorUsername().replaceAll("\\s+","") + "/" + editContact.getPersonsICFirstName().toLowerCase() + "_" + editContact.getPersonsICLastName().toLowerCase());
- File fileOnContactProfile = new File(imagepath + "/profilephoto.png");
- try {
- if(fileOnContactProfile.exists()) {
- BufferedImage profilePhoto = ImageIO.read(fileOnContactProfile);
- Image finalImageLook = profilePhoto.getScaledInstance(120, 120,Image.SCALE_SMOOTH);
- newContactLabelContactProfileArea.setIcon(new ImageIcon(finalImageLook));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- addNewContactPanel.setVisible(true);
- contactsPanel.setVisible(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement