Implement and modify to display JTree with countries
authorSylvain Papa <sylvain.papa@yahoo.fr>
Thu, 17 Jan 2019 18:44:52 +0000 (19:44 +0100)
committerSylvain Papa <sylvain.papa@yahoo.fr>
Thu, 17 Jan 2019 18:44:52 +0000 (19:44 +0100)
src/Country.java
src/Main.java
src/PersonView.java
src/Region.java
src/RegionView.java

index 8b711084bb81e145f7ac8317a83cafdbd0c9b950..2bb786a2c33d92fb2d64eb9fe6302bc1472062f4 100644 (file)
@@ -16,6 +16,10 @@ public class Country {
         this.name = name;
     }
 
+    public String getName() {
+        return name;
+    }
+
     public void setRegion(String region) {
         this.region = region;
     }
index 194ff31cfe087196c593cd53d19e03a63f1b142b..17cf896c521dc308a5b1fb595f539beb48bb3963 100644 (file)
@@ -75,9 +75,6 @@ public class Main {
 
         Person emptyPerson = new Person();
         PersonView emptyPersonView = new PersonView(emptyPerson, personArrayList);
-        Region r = new Region();
-        r.getCsvFile();
-
 
         //Schedule a job for the event-dispatching thread:
         //creating and showing this application's GUI.
index 1998cece9e3afdbc071b6de389bba316eb06e397..803fcf50498e42fbaa70e723e8faffbc38010578 100644 (file)
@@ -11,6 +11,7 @@ public class PersonView extends JComponent implements ActionListener {
     private ArrayList<Person> personArrayList;
     private FirstnameView firstnameView;
     private OriginView originView;
+    private RegionView regionView;
     private SizeView sizeView;
     private WeightView weightView;
     private EyeView eyeView;
@@ -25,6 +26,7 @@ public class PersonView extends JComponent implements ActionListener {
         setPersonObj(personObj);
         setFirstnameView(new FirstnameView(14, this.personObj.getFirstname()));
         setOriginView(new OriginView(this.personObj.getOrigin()));
+        setRegionView(new RegionView(new Region()));
         setSizeView(new SizeView(this.personObj.getPersonSize()));
         setWeightView(new WeightView(this.personObj.getWeight()));
         setEyeView(new EyeView(this.personObj.getEye()));
@@ -42,7 +44,8 @@ public class PersonView extends JComponent implements ActionListener {
         eastPanel.add(eyeView);
         westPanel.setLayout(new BoxLayout(this.westPanel, BoxLayout.PAGE_AXIS));
         westPanel.setBorder(BorderFactory.createRaisedSoftBevelBorder());
-        westPanel.add(originView);
+        //westPanel.add(originView);
+        westPanel.add(regionView);
     }
 
     public Person getPersonObj() {
@@ -81,6 +84,11 @@ public class PersonView extends JComponent implements ActionListener {
         this.originView = originView;
     }
 
+    public void setRegionView(RegionView regionView) {
+        this.regionView = regionView;
+    }
+
+
     /**
      * @return
      */
index b20d0cb02cede1fbd3c7376759ec9c2bf0bc6247..eac5c9561839d29a74ce91efd0768c24b236616a 100644 (file)
@@ -7,6 +7,7 @@ import java.util.Scanner;
 public class Region {
 
     private String csvFile = "data/countries.csv";
+    private String[] continents = {"Africa", "Americas", "Asia", "Europe", "Oceania"};
     private ArrayList<Country> countryArrayListEurope = new ArrayList<>();
     private ArrayList<Country> countryArrayListAfrica = new ArrayList<>();
     private ArrayList<Country> countryArrayListAmericas = new ArrayList<>();
@@ -15,9 +16,34 @@ public class Region {
 
 
     public Region() {
+        this.loadCSVCountries();
     }
 
-    public void getCsvFile() {
+    public String[] getContinents() {
+        return continents;
+    }
+
+    public ArrayList<Country> getCountryArrayListAfrica() {
+        return countryArrayListAfrica;
+    }
+
+    public ArrayList<Country> getCountryArrayListAsia() {
+        return countryArrayListAsia;
+    }
+
+    public ArrayList<Country> getCountryArrayListAmericas() {
+        return countryArrayListAmericas;
+    }
+
+    public ArrayList<Country> getCountryArrayListEurope() {
+        return countryArrayListEurope;
+    }
+
+    public ArrayList<Country> getCountryArrayListOceania() {
+        return countryArrayListOceania;
+    }
+
+    public void loadCSVCountries() {
         CSVUtils csvUtils = new CSVUtils();
         Scanner scanner = null;
         try {
@@ -59,6 +85,7 @@ public class Region {
         Utils.displayArrayList(countryArrayListAmericas);
         Utils.displayArrayList(countryArrayListAsia);
         Utils.displayArrayList(countryArrayListEurope);*/
+
     }
 
 }
\ No newline at end of file
index 1a28f5a3c5100d0826619f3717def39b197c7493..25b646dc19c1fd9508ccce3dbf723e6b98a5522d 100644 (file)
@@ -1,13 +1,65 @@
 import javax.swing.*;
+import javax.swing.tree.DefaultMutableTreeNode;
+import java.util.ListIterator;
 
 public class RegionView extends JPanel {
     private Region regionObj;
-    private JLabel label;
+    //private JLabel label;
+    private JTree tree;
 
     RegionView(Region regionObj) {
         setRegionObj(regionObj);
-        this.label = new JLabel();
-        this.label.setText("Region");
+        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Region");
+
+        for (String continent : regionObj.getContinents()) {
+            DefaultMutableTreeNode topContinent = new DefaultMutableTreeNode(continent);
+            System.out.println(continent.equals("Europe"));
+
+            if (continent.equals("Europe")) {
+                ListIterator<Country> iter = regionObj.getCountryArrayListEurope().listIterator();
+                while (iter.hasNext()) {
+                    Country countryCursor = iter.next();
+                    topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
+                    top.add(topContinent);
+
+                }
+            } else if (continent.equals("Africa")) {
+                ListIterator<Country> iter = regionObj.getCountryArrayListAfrica().listIterator();
+                while (iter.hasNext()) {
+                    Country countryCursor = iter.next();
+                    topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
+                    top.add(topContinent);
+
+                }
+            } else if (continent.equals("Americas")) {
+                ListIterator<Country> iter = regionObj.getCountryArrayListAmericas().listIterator();
+                while (iter.hasNext()) {
+                    Country countryCursor = iter.next();
+                    topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
+                    top.add(topContinent);
+
+                }
+            } else if (continent.equals("Asia")) {
+                ListIterator<Country> iter = regionObj.getCountryArrayListAsia().listIterator();
+                while (iter.hasNext()) {
+                    Country countryCursor = iter.next();
+                    topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
+                    top.add(topContinent);
+
+                }
+            } else if (continent.equals("Oceania")) {
+                ListIterator<Country> iter = regionObj.getCountryArrayListOceania().listIterator();
+                while (iter.hasNext()) {
+                    Country countryCursor = iter.next();
+                    topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
+                    top.add(topContinent);
+
+                }
+            }
+        }
+        tree = new JTree(top);
+        add(tree);
+
     }
 
     public void setRegionObj(Region regionObj) {