From 822afd4fe0aa1669db1e3be57b277f8aff977254 Mon Sep 17 00:00:00 2001 From: Sylvain Papa Date: Thu, 17 Jan 2019 19:44:52 +0100 Subject: [PATCH] Implement and modify to display JTree with countries --- src/Country.java | 4 ++++ src/Main.java | 3 --- src/PersonView.java | 10 +++++++- src/Region.java | 29 ++++++++++++++++++++++- src/RegionView.java | 58 ++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 96 insertions(+), 8 deletions(-) diff --git a/src/Country.java b/src/Country.java index 8b71108..2bb786a 100644 --- a/src/Country.java +++ b/src/Country.java @@ -16,6 +16,10 @@ public class Country { this.name = name; } + public String getName() { + return name; + } + public void setRegion(String region) { this.region = region; } diff --git a/src/Main.java b/src/Main.java index 194ff31..17cf896 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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. diff --git a/src/PersonView.java b/src/PersonView.java index 1998cec..803fcf5 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -11,6 +11,7 @@ public class PersonView extends JComponent implements ActionListener { private ArrayList 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 */ diff --git a/src/Region.java b/src/Region.java index b20d0cb..eac5c95 100644 --- a/src/Region.java +++ b/src/Region.java @@ -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 countryArrayListEurope = new ArrayList<>(); private ArrayList countryArrayListAfrica = new ArrayList<>(); private ArrayList countryArrayListAmericas = new ArrayList<>(); @@ -15,9 +16,34 @@ public class Region { public Region() { + this.loadCSVCountries(); } - public void getCsvFile() { + public String[] getContinents() { + return continents; + } + + public ArrayList getCountryArrayListAfrica() { + return countryArrayListAfrica; + } + + public ArrayList getCountryArrayListAsia() { + return countryArrayListAsia; + } + + public ArrayList getCountryArrayListAmericas() { + return countryArrayListAmericas; + } + + public ArrayList getCountryArrayListEurope() { + return countryArrayListEurope; + } + + public ArrayList 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 diff --git a/src/RegionView.java b/src/RegionView.java index 1a28f5a..25b646d 100644 --- a/src/RegionView.java +++ b/src/RegionView.java @@ -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 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 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 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 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 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) { -- 2.34.1