X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FRegionView.java;h=3c79ea6f4a294fb5c446c977b2c2a40807303d3d;hb=d8b03ca81cef363ec5f57e0dbe46d0b47023314d;hp=1a28f5a3c5100d0826619f3717def39b197c7493;hpb=f56708fa0a19755ac5ec0313d9ee9c45a9aef0ce;p=Persons_Comparator.git diff --git a/src/RegionView.java b/src/RegionView.java index 1a28f5a..3c79ea6 100644 --- a/src/RegionView.java +++ b/src/RegionView.java @@ -1,17 +1,88 @@ import javax.swing.*; +import javax.swing.tree.DefaultMutableTreeNode; +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 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); + 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; - } }