Switch person class usage of Origin class to Country class and use it for distance...
[Persons_Comparator.git] / src / RegionView.java
index 1a28f5a3c5100d0826619f3717def39b197c7493..42941ac2d6f27247f749a309994f98fdd3854172 100644 (file)
@@ -1,17 +1,93 @@
 import javax.swing.*;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.TreeSelectionModel;
+import java.util.ListIterator;
 
 public class RegionView extends JPanel {
     private Region regionObj;
-    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);
+            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);
+                }
+            }
+        }
+        sortTree(top);
+        tree = new JTree(top);
+        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+        add(new JScrollPane(tree));
+    }
+
+    public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) {
+        {
+            for (int i = 0; i < root.getChildCount() - 1; i++) {
+                DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
+                String nt = node.getUserObject().toString();
+
+                for (int j = i + 1; j <= root.getChildCount() - 1; j++) {
+                    DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root
+                            .getChildAt(j);
+                    String np = prevNode.getUserObject().toString();
+                    if (nt.compareToIgnoreCase(np) > 0) {
+                        root.insert(node, j);
+                        break;
+                    }
+                }
+                if (node.getChildCount() > 0) {
+                    sortTree(node);
+                }
+            }
+            return root;
+        }
+    }
+
+    public JTree getTree() {
+        return tree;
     }
 
     public void setRegionObj(Region regionObj) {
         this.regionObj = regionObj;
+    }
 
+    public Region getRegionObj() {
+        return regionObj;
     }
 }