Country + Origin + RegionView modify
[Persons_Comparator.git] / src / RegionView.java
index 25b646dc19c1fd9508ccce3dbf723e6b98a5522d..3c79ea6f4a294fb5c446c977b2c2a40807303d3d 100644 (file)
@@ -4,7 +4,6 @@ import java.util.ListIterator;
 
 public class RegionView extends JPanel {
     private Region regionObj;
-    //private JLabel label;
     private JTree tree;
 
     RegionView(Region regionObj) {
@@ -13,15 +12,12 @@ public class RegionView extends JPanel {
 
         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();
@@ -29,7 +25,6 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Americas")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListAmericas().listIterator();
@@ -37,7 +32,6 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Asia")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListAsia().listIterator();
@@ -45,7 +39,6 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             } else if (continent.equals("Oceania")) {
                 ListIterator<Country> iter = regionObj.getCountryArrayListOceania().listIterator();
@@ -53,17 +46,43 @@ public class RegionView extends JPanel {
                     Country countryCursor = iter.next();
                     topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
                     top.add(topContinent);
-
                 }
             }
         }
+        sortTree(top);
         tree = new JTree(top);
-        add(tree);
+        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();
+
+                    System.out.println(nt + " " + np);
+                    if (nt.compareToIgnoreCase(np) > 0) {
+
+                        root.insert(node, j);
+                        break;
+                    }
+                }
+                if (node.getChildCount() > 0) {
+                    node = sortTree(node);
+                }
+            }
+
+            return root;
+        }
     }
 
     public void setRegionObj(Region regionObj) {
         this.regionObj = regionObj;
-
     }
 }