X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FRegionView.java;h=17a294908f6933a8915d19a5da672a923c9a2c51;hb=67134f5389b08ee7a1704755238c856f2e1983d5;hp=0f2f8cc790283382e722909e0552095c22fdc623;hpb=416b4c77ff435499154345f4992f27c12ac0933c;p=Persons_Comparator.git diff --git a/src/RegionView.java b/src/RegionView.java index 0f2f8cc..17a2949 100644 --- a/src/RegionView.java +++ b/src/RegionView.java @@ -1,2 +1,89 @@ -public class RegionView { +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 JTree tree; + + RegionView(Region regionObj) { + setRegionObj(regionObj); + DefaultMutableTreeNode top = new DefaultMutableTreeNode("Region"); + + for (String continent : regionObj.getContinents()) { + DefaultMutableTreeNode topContinent = new DefaultMutableTreeNode(continent); + 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); + } + } + } + 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) { + node = sortTree(node); + } + } + return root; + } + } + + public void setRegionObj(Region regionObj) { + this.regionObj = regionObj; + } + + public Region getRegionObj() { + return regionObj; + } }