Add Registration functionality and tidy up
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / client / EditProfile.java
index de8b655..a30def6 100644 (file)
@@ -1,6 +1,38 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
 package uk.ac.ntu.n0521366.wsyd.client;
 
+import java.awt.Color;
+import java.lang.reflect.Array;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.TreeSet;
+import javax.swing.DefaultListModel;
+import javax.swing.JOptionPane;
+import javax.swing.ListModel;
+import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member;
 /*
  * To change this license header, choose License Headers in Project Properties.
  * To change this template file, choose Tools | Templates
@@ -21,6 +53,18 @@ public class EditProfile extends javax.swing.JDialog {
         initComponents();
     }
 
+    /**
+     *  Populate a model with the applications pre-defined interests.
+     * 
+     * @return the model
+     */
+    DefaultListModel getInterestsListModel() {
+        DefaultListModel<String> m = new DefaultListModel<>();
+        for (String i : WSYD_Member.Interests)
+            m.addElement(i);
+        System.err.println(m.toString());
+        return m;
+    }
     /**
      * This method is called from within the constructor to initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is always
@@ -39,8 +83,8 @@ public class EditProfile extends javax.swing.JDialog {
         gLabelPConfirmation = new javax.swing.JLabel();
         gPFieldPConfirmation = new javax.swing.JPasswordField();
         gLabelPWarning = new javax.swing.JLabel();
-        gLabelBirthplace = new javax.swing.JLabel();
-        gFieldBirthplace = new javax.swing.JTextField();
+        gLabelBirthDate = new javax.swing.JLabel();
+        gFieldBirthDate = new javax.swing.JTextField();
         gLabelCurrLocation = new javax.swing.JLabel();
         gFieldCurrLocation = new javax.swing.JTextField();
         gLabelInterests = new javax.swing.JLabel();
@@ -58,6 +102,7 @@ public class EditProfile extends javax.swing.JDialog {
         layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0};
         getContentPane().setLayout(layout);
 
+        gLabelUsername.setLabelFor(gFieldUsername);
         gLabelUsername.setText("Username:");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
@@ -84,6 +129,7 @@ public class EditProfile extends javax.swing.JDialog {
         gridBagConstraints.gridy = 0;
         getContentPane().add(gButtonConfirmation, gridBagConstraints);
 
+        gLabelPassword.setLabelFor(gPFieldPassword);
         gLabelPassword.setText("Password:");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
@@ -100,7 +146,9 @@ public class EditProfile extends javax.swing.JDialog {
         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
         getContentPane().add(gPFieldPassword, gridBagConstraints);
 
-        gLabelPConfirmation.setText("Confirm:");
+        gLabelPConfirmation.setLabelFor(gPFieldPConfirmation);
+        gLabelPConfirmation.setText("Password:");
+        gLabelPConfirmation.setToolTipText("Repeat password");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
         gridBagConstraints.gridy = 4;
@@ -121,25 +169,28 @@ public class EditProfile extends javax.swing.JDialog {
         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
         getContentPane().add(gLabelPWarning, gridBagConstraints);
 
-        gLabelBirthplace.setText("Birthplace:");
+        gLabelBirthDate.setLabelFor(gFieldBirthDate);
+        gLabelBirthDate.setText("Birth Date:");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
         gridBagConstraints.gridy = 6;
         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
-        getContentPane().add(gLabelBirthplace, gridBagConstraints);
+        getContentPane().add(gLabelBirthDate, gridBagConstraints);
 
-        gFieldBirthplace.setMinimumSize(new java.awt.Dimension(100, 25));
-        gFieldBirthplace.setPreferredSize(new java.awt.Dimension(200, 25));
-        gFieldBirthplace.addActionListener(new java.awt.event.ActionListener() {
+        gFieldBirthDate.setText("YYYY-mm-dd");
+        gFieldBirthDate.setMinimumSize(new java.awt.Dimension(100, 25));
+        gFieldBirthDate.setPreferredSize(new java.awt.Dimension(200, 25));
+        gFieldBirthDate.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
-                gFieldBirthplaceActionPerformed(evt);
+                gFieldBirthDateActionPerformed(evt);
             }
         });
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 2;
         gridBagConstraints.gridy = 6;
-        getContentPane().add(gFieldBirthplace, gridBagConstraints);
+        getContentPane().add(gFieldBirthDate, gridBagConstraints);
 
+        gLabelCurrLocation.setLabelFor(gLabelCurrLocation);
         gLabelCurrLocation.setText("Current City/Town:");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
@@ -154,6 +205,7 @@ public class EditProfile extends javax.swing.JDialog {
         gridBagConstraints.gridy = 8;
         getContentPane().add(gFieldCurrLocation, gridBagConstraints);
 
+        gLabelInterests.setLabelFor(gListInterests);
         gLabelInterests.setText("Interests:");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
@@ -161,11 +213,7 @@ public class EditProfile extends javax.swing.JDialog {
         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
         getContentPane().add(gLabelInterests, gridBagConstraints);
 
-        gListInterests.setModel(new javax.swing.AbstractListModel() {
-            String[] strings = { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8" };
-            public int getSize() { return strings.length; }
-            public Object getElementAt(int i) { return strings[i]; }
-        });
+        gListInterests.setModel(getInterestsListModel());
         gListInterests.setToolTipText("Select the interests you have, they will be displayed (Hold CTRL + Click)");
         jScrollPane1.setViewportView(gListInterests);
 
@@ -175,7 +223,8 @@ public class EditProfile extends javax.swing.JDialog {
         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
         getContentPane().add(jScrollPane1, gridBagConstraints);
 
-        gLabelBio.setText("Bio:");
+        gLabelBio.setLabelFor(gTAreaBio);
+        gLabelBio.setText("Biography:");
         gridBagConstraints = new java.awt.GridBagConstraints();
         gridBagConstraints.gridx = 0;
         gridBagConstraints.gridy = 12;
@@ -201,25 +250,102 @@ public class EditProfile extends javax.swing.JDialog {
         pack();
     }// </editor-fold>//GEN-END:initComponents
 
-    private void gFieldBirthplaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gFieldBirthplaceActionPerformed
+    private void gFieldBirthDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gFieldBirthDateActionPerformed
         // TODO add your handling code here:
-    }//GEN-LAST:event_gFieldBirthplaceActionPerformed
+    }//GEN-LAST:event_gFieldBirthDateActionPerformed
 
+    /**
+     * Verify the user-entered data before attempting to submit it to SocialServer.
+     * 
+     * Provides error feedback to the user in the form of a message dialog with a list of problems
+     * and sets the colour of field data to red if an error was found, or green otherwise.
+     * 
+     * @param evt
+     */
     private void gButtonConfirmationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gButtonConfirmationMouseClicked
         // TODO add your handling code here:
+        boolean dataGood = true; // assume user input is OK until errors found
+        ArrayList<String> errorMessages = new ArrayList<>();
+
+        String birthdateFormat = "YYYY-mm-dd";
+        SimpleDateFormat sd = new SimpleDateFormat(birthdateFormat);
+
+        WSYD_Member m = new WSYD_Member();
+
+        m._userName = this.gFieldUsername.getText();
+        if (m._userName.length() == 0) {
+            this.gFieldUsername.setForeground(Color.red);
+            errorMessages.add("Username cannot be empty");
+        }
+        else 
+            this.gFieldUsername.setForeground(Color.green);
+
+        if (Array.getLength(this.gPFieldPassword.getPassword()) == 0 || Array.getLength(this.gPFieldPConfirmation.getPassword()) == 0)  {
+            dataGood = false;
+            this.gPFieldPassword.setForeground(Color.red);
+            this.gPFieldPConfirmation.setForeground(Color.red);
+            errorMessages.add("Password cannot be empty");
+        } else if (!Arrays.equals(this.gPFieldPassword.getPassword(), this.gPFieldPConfirmation.getPassword())) {
+            dataGood = false;
+            this.gPFieldPassword.setForeground(Color.red);
+            this.gPFieldPConfirmation.setForeground(Color.red);
+            errorMessages.add("Passwords do not match");
+        } else {
+            this.gPFieldPassword.setForeground(Color.green);
+            this.gPFieldPConfirmation.setForeground(Color.green);            
+            m._password = new String(this.gPFieldPassword.getPassword());
+        }
+            
+        try {
+            m._birthDate = sd.parse(this.gFieldBirthDate.getText());
+            this.gFieldBirthDate.setForeground(Color.green);
+        } catch(java.text.ParseException e) {
+            dataGood = false;
+            this.gFieldBirthDate.setForeground(Color.red);
+            errorMessages.add("Birth Date format is " + birthdateFormat);
+        }
+
+        m._bio = this.gTAreaBio.getText();
+        m._currentLocation = this.gFieldCurrLocation.getText();
+
+        java.util.List<?> selectedInterests = this.gListInterests.getSelectedValuesList();
+        if (selectedInterests.isEmpty()) {
+            dataGood = false;
+            this.gListInterests.setForeground(Color.red);
+            errorMessages.add("Must select at least 1 interest");
+        } else
+            this.gListInterests.setForeground(Color.green);
+  
+        m._interests = new TreeSet<>();
+        m._interests.addAll(this.gListInterests.getSelectedValuesList());
+        if (!dataGood) {
+            DefaultListModel<String> errorModel = new DefaultListModel<>();
+            for (String s: errorMessages)
+                errorModel.addElement(s);
+            javax.swing.JList errorList = new javax.swing.JList();
+            errorList.setModel(errorModel);
+            JOptionPane.showMessageDialog(this, errorList, "Errors in Registration details", JOptionPane.ERROR_MESSAGE);
+        }
+        else {
+            // trigger sending of the data to SocialServer
+            ((ClientGUI)this.getOwner()).confirmRegistration(m);
+        }
+        // FIXME: gButtonConfirmationMouseClicked(): remove superceded code that has been commented out
+        /* 
         TreeSet<String> interests = null;
-        /*
+
         for (int i = 0; i < gListInterests.getModel().getSize(); i++)
         {
             interests.add(((String)gListInterests.getModel().getElementAt(i)));
-        }*/
+        }
         if (((ClientGUI)this.getOwner()).confirmRegistration(gFieldUsername.getText(),
                 new String(gPFieldPassword.getPassword()),
-                gFieldBirthplace.getText(),
+                gFieldBirthDate.getText(),
                 gFieldCurrLocation.getText(),
                 interests,
                 gTAreaBio.getText())) this.dispose();
         else gLabelPWarning.setText("Server failed to register. Retry.");
+        */
     }//GEN-LAST:event_gButtonConfirmationMouseClicked
 
     /**
@@ -267,11 +393,11 @@ public class EditProfile extends javax.swing.JDialog {
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton gButtonConfirmation;
-    private javax.swing.JTextField gFieldBirthplace;
+    private javax.swing.JTextField gFieldBirthDate;
     private javax.swing.JTextField gFieldCurrLocation;
     private javax.swing.JTextField gFieldUsername;
     private javax.swing.JLabel gLabelBio;
-    private javax.swing.JLabel gLabelBirthplace;
+    private javax.swing.JLabel gLabelBirthDate;
     private javax.swing.JLabel gLabelCurrLocation;
     private javax.swing.JLabel gLabelInterests;
     private javax.swing.JLabel gLabelPConfirmation;