From e488cca68663a96cd3ac0166efde74855f14ce3f Mon Sep 17 00:00:00 2001
From: "vincent.steinman" <vincent.steinmann@etu.hesge.ch>
Date: Tue, 24 Jan 2023 11:48:46 +0100
Subject: [PATCH] check text fields

---
 .../main/java/com/example/tpfx/Contact.java   |  4 +-
 tp/src/main/java/com/example/tpfx/Family.java |  4 +-
 .../com/example/tpfx/HelloController.java     | 63 +++++++++++++------
 .../java/com/example/tpfx/Professional.java   |  4 +-
 .../com/example/tpfx/hello-view.fxml          |  6 +-
 5 files changed, 54 insertions(+), 27 deletions(-)

diff --git a/tp/src/main/java/com/example/tpfx/Contact.java b/tp/src/main/java/com/example/tpfx/Contact.java
index 87c0d8a..19dbfc6 100644
--- a/tp/src/main/java/com/example/tpfx/Contact.java
+++ b/tp/src/main/java/com/example/tpfx/Contact.java
@@ -126,8 +126,8 @@ public class Contact implements Comparable<Contact>{
             w_social += elem + " ";
         }
 
-        return String.valueOf(id) + " - " + w_names + " - " + this.last_name + " - " + this.addr + " - " + w_phone + " - " + w_email + " - "
-                + w_social + " - " + this.job;
+        return String.valueOf(id) + " - " + w_names + "- " + this.last_name + " - " + this.addr + " - " + w_phone
+                + "- " + w_email + "- " + w_social + "- " + this.job;
     }
     @Override
     public int compareTo(Contact c){
diff --git a/tp/src/main/java/com/example/tpfx/Family.java b/tp/src/main/java/com/example/tpfx/Family.java
index 0b154a2..0242b55 100644
--- a/tp/src/main/java/com/example/tpfx/Family.java
+++ b/tp/src/main/java/com/example/tpfx/Family.java
@@ -36,7 +36,7 @@ public class Family extends Contact{
             w_social += elem + " ";
         }
 
-        return String.valueOf(id) + " - " + w_names + " - " + this.last_name + " - " + this.addr + " - " + w_phone + " - " + w_email + " - "
-                + w_social + " - " + this.job + " - " + this.relation;
+        return String.valueOf(id) + " - " + w_names + "- " + this.last_name + " - " + this.addr + " - " + w_phone
+                + "- " + w_email + "- " + w_social + "- " + this.job + " - " + this.relation;
     }
 }
diff --git a/tp/src/main/java/com/example/tpfx/HelloController.java b/tp/src/main/java/com/example/tpfx/HelloController.java
index 17a7930..9226659 100644
--- a/tp/src/main/java/com/example/tpfx/HelloController.java
+++ b/tp/src/main/java/com/example/tpfx/HelloController.java
@@ -8,10 +8,13 @@ import javafx.scene.control.*;
 
 import java.io.*;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public class HelloController {
     static ArrayList<Contact> contactArray = new ArrayList<Contact>();
     int selectedID = 0;
+    boolean imported = false;
     ObservableList<String> ctType = FXCollections.observableArrayList("Friend", "Family", "Professional");
     @FXML
     private TextField txtFirst;
@@ -59,6 +62,7 @@ public class HelloController {
         txtOther.setVisible(false);
         btnEdit.setDisable(true);
         btnDelete.setDisable(true);
+        btnAdd.setDisable(true);
     }
     @FXML
     protected void AddCt() {
@@ -131,16 +135,16 @@ public class HelloController {
             for (Contact c: contactArray){
                 List<String> str_array = SplitLine(c.toString(), " - ");
                 writer.write(c.get_type() + "\n");
-                writer.write(String.valueOf(c.getId()) + "/");
                 writer.write(str_array.get(0) + "/");
                 writer.write(str_array.get(1) + "/");
                 writer.write(str_array.get(2) + "/");
                 writer.write(str_array.get(3) + "/");
                 writer.write(str_array.get(4) + "/");
                 writer.write(str_array.get(5) + "/");
-                writer.write(str_array.get(6));
+                writer.write(str_array.get(6) + "/");
+                writer.write(str_array.get(7));
                 if(c.get_type().equals("Family") || c.get_type().equals("Professional")){
-                    writer.write("/" + str_array.get(7) + "\n");
+                    writer.write("/" + str_array.get(8) + "\n");
                 } else{
                     writer.write("/" + "\n");
                 }
@@ -152,10 +156,12 @@ public class HelloController {
     }
     @FXML
     protected void Search() {
+        Pattern pat = Pattern.compile(txtSearch.getText());
         LstCt.getItems().clear();
         for(Contact c: contactArray){
             for(String n: c.getFirst_name()){
-                if(n.equals(txtSearch.getText())){
+                Matcher match = pat.matcher(n);
+                if(match.find()){
                     LstCt.getItems().add(c);
                 }
             }
@@ -199,6 +205,9 @@ public class HelloController {
             lblOther.setVisible(false);
             cbxType.setValue("Friend");
         }
+        cbxType.setDisable(true);
+        imported = true;
+        TestFields();
     }
     @FXML
     protected void Delete() {
@@ -231,6 +240,24 @@ public class HelloController {
         ShowAll();
     }
     @FXML
+    public void TestFields(){
+        Pattern pat = Pattern.compile(txtEmail.getText());
+        Matcher match = pat.matcher("^(.+)@(.+)$*");
+
+        if(txtFirst.getText() != "" && txtLast.getText() != "" && match.find()){
+            if(imported){
+                btnEdit.setDisable(false);
+                btnAdd.setDisable(false);
+            } else{
+                btnEdit.setDisable(true);
+                btnAdd.setDisable(false);
+            }
+        } else {
+            btnEdit.setDisable(true);
+            btnAdd.setDisable(true);
+        }
+    }
+    @FXML
     protected void ShowAll() {
         int cpt = 0;
         for(Contact cont: contactArray){
@@ -241,6 +268,16 @@ public class HelloController {
         for(Contact c: contactArray){
             LstCt.getItems().add(c.toString());
         }
+        txtFirst.setText("");
+        txtLast.setText("");
+        txtAdd.setText("");
+        txtPhone.setText("");
+        txtEmail.setText("");
+        txtSocial.setText("");
+        txtJob.setText("");
+        txtOther.setText("");
+        cbxType.setDisable(false);
+        imported = false;
     }
     public void AddPal(){
         String firstName = txtFirst.getText();
@@ -299,14 +336,6 @@ public class HelloController {
         contactArray.add(c);
         Collections.sort(contactArray);
         ShowAll();
-        txtFirst.setText("");
-        txtLast.setText("");
-        txtAdd.setText("");
-        txtPhone.setText("");
-        txtEmail.setText("");
-        txtSocial.setText("");
-        txtJob.setText("");
-        txtOther.setText("");
     }
     public void ImportFriend(List<String> str_array){
         Friend f = new Friend(
@@ -370,26 +399,24 @@ public class HelloController {
         return data;
     }
     public List<String> SplitArray(String str){
-        //str = str.substring(1, str.length() - 1);
         String new_str = "";
         for(char c: str.toCharArray()){
             if(c !='[' && c !=']'){
                 new_str += c;
             }
         }
-        List<String> names = Arrays.asList(new_str.split(","));
+        List<String> names = Arrays.asList(new_str.split(" "));
         return names;
     }
 
 
+
     //TODO
     /*
-    Better toString in export
-    List first names & co
     Add comments
     Test contact exist
-    Control sur les champs
-    Virgules dans champs text
+    Unit Test
+    Edit relation/job?
      */
 
 }
\ No newline at end of file
diff --git a/tp/src/main/java/com/example/tpfx/Professional.java b/tp/src/main/java/com/example/tpfx/Professional.java
index 8609131..ba5c4d0 100644
--- a/tp/src/main/java/com/example/tpfx/Professional.java
+++ b/tp/src/main/java/com/example/tpfx/Professional.java
@@ -34,7 +34,7 @@ public class Professional extends Contact{
             w_social += elem + " ";
         }
 
-        return String.valueOf(id) + " - " + w_names + " - " + last_name + " - " + this.addr + " - " + w_phone + " - " + w_email + " - "
-                + w_social + " - " + this.job + " - " + this.work_place;
+        return String.valueOf(id) + " - " + w_names + "- " + last_name + " - " + this.addr + " - " + w_phone + "- "
+                + w_email + "- " + w_social + "- " + this.job + " - " + this.work_place;
     }
 }
diff --git a/tp/src/main/resources/com/example/tpfx/hello-view.fxml b/tp/src/main/resources/com/example/tpfx/hello-view.fxml
index f6da731..d1a72c2 100644
--- a/tp/src/main/resources/com/example/tpfx/hello-view.fxml
+++ b/tp/src/main/resources/com/example/tpfx/hello-view.fxml
@@ -46,15 +46,15 @@
       <children>
             <ListView fx:id="LstCt" layoutX="427.0" layoutY="38.0" onMouseClicked="#Selected" prefHeight="489.0" prefWidth="412.0" />
             <Button fx:id="btnAdd" layoutX="229.0" layoutY="51.0" mnemonicParsing="false" onAction="#AddCt" prefHeight="24.0" prefWidth="62.0" text="Add" />
-            <TextField fx:id="txtFirst" layoutX="32.0" layoutY="52.0" />
+            <TextField fx:id="txtFirst" layoutX="32.0" layoutY="52.0" onInputMethodTextChanged="#TestFields" onKeyReleased="#TestFields" />
             <Label layoutX="230.0" layoutY="300.0" text="First Name" />
-            <TextField fx:id="txtLast" layoutX="32.0" layoutY="107.0" />
+            <TextField fx:id="txtLast" layoutX="32.0" layoutY="107.0" onInputMethodTextChanged="#TestFields" onKeyReleased="#TestFields" />
             <Label layoutX="32.0" layoutY="85.0" text="Last Name" />
             <TextField fx:id="txtAdd" layoutX="32.0" layoutY="162.0" />
             <Label layoutX="32.0" layoutY="140.0" text="Address" />
             <TextField fx:id="txtPhone" layoutX="32.0" layoutY="216.0" />
             <Label layoutX="32.0" layoutY="194.0" text="Phones" />
-            <TextField fx:id="txtEmail" layoutX="32.0" layoutY="270.0" />
+            <TextField fx:id="txtEmail" layoutX="32.0" layoutY="270.0" onInputMethodTextChanged="#TestFields" onKeyReleased="#TestFields" />
             <Label layoutX="32.0" layoutY="248.0" text="Emails" />
             <TextField fx:id="txtSocial" layoutX="32.0" layoutY="322.0" />
             <Label layoutX="32.0" layoutY="300.0" text="Socials" />
-- 
GitLab