X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FRegionView.java;h=42941ac2d6f27247f749a309994f98fdd3854172;hb=9f1e899962a92c9c1b8d3713eca0524964465014;hp=25b646dc19c1fd9508ccce3dbf723e6b98a5522d;hpb=822afd4fe0aa1669db1e3be57b277f8aff977254;p=Persons_Comparator.git diff --git a/src/RegionView.java b/src/RegionView.java index 25b646d..42941ac 100644 --- a/src/RegionView.java +++ b/src/RegionView.java @@ -1,10 +1,10 @@ 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) { @@ -13,15 +13,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 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(); @@ -29,7 +26,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 iter = regionObj.getCountryArrayListAmericas().listIterator(); @@ -37,7 +33,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 iter = regionObj.getCountryArrayListAsia().listIterator(); @@ -45,7 +40,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 iter = regionObj.getCountryArrayListOceania().listIterator(); @@ -53,17 +47,47 @@ 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); + 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; } }