d44cd0494f9f10dd063eb4b59ec09ad4d230691d
[Persons_Comparator.git] / src / RegionView.java
1 import javax.swing.*;
2 import javax.swing.tree.DefaultMutableTreeNode;
3 import java.util.ListIterator;
4
5 public class RegionView extends JPanel {
6 private Region regionObj;
7 private JTree tree;
8
9 RegionView(Region regionObj) {
10 setRegionObj(regionObj);
11 DefaultMutableTreeNode top = new DefaultMutableTreeNode("Region");
12
13 for (String continent : regionObj.getContinents()) {
14 DefaultMutableTreeNode topContinent = new DefaultMutableTreeNode(continent);
15 if (continent.equals("Europe")) {
16 ListIterator<Country> iter = regionObj.getCountryArrayListEurope().listIterator();
17 while (iter.hasNext()) {
18 Country countryCursor = iter.next();
19 topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
20 top.add(topContinent);
21 }
22 } else if (continent.equals("Africa")) {
23 ListIterator<Country> iter = regionObj.getCountryArrayListAfrica().listIterator();
24 while (iter.hasNext()) {
25 Country countryCursor = iter.next();
26 topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
27 top.add(topContinent);
28 }
29 } else if (continent.equals("Americas")) {
30 ListIterator<Country> iter = regionObj.getCountryArrayListAmericas().listIterator();
31 while (iter.hasNext()) {
32 Country countryCursor = iter.next();
33 topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
34 top.add(topContinent);
35 }
36 } else if (continent.equals("Asia")) {
37 ListIterator<Country> iter = regionObj.getCountryArrayListAsia().listIterator();
38 while (iter.hasNext()) {
39 Country countryCursor = iter.next();
40 topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
41 top.add(topContinent);
42 }
43 } else if (continent.equals("Oceania")) {
44 ListIterator<Country> iter = regionObj.getCountryArrayListOceania().listIterator();
45 while (iter.hasNext()) {
46 Country countryCursor = iter.next();
47 topContinent.add(new DefaultMutableTreeNode(countryCursor.getName()));
48 top.add(topContinent);
49 }
50 }
51 }
52 sortTree(top);
53 tree = new JTree(top);
54 add(new JScrollPane(tree));
55 }
56
57 public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) {
58 {
59 for (int i = 0; i < root.getChildCount() - 1; i++) {
60 DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
61 String nt = node.getUserObject().toString();
62
63 for (int j = i + 1; j <= root.getChildCount() - 1; j++) {
64 DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root
65 .getChildAt(j);
66 String np = prevNode.getUserObject().toString();
67 if (nt.compareToIgnoreCase(np) > 0) {
68 root.insert(node, j);
69 break;
70 }
71 }
72 if (node.getChildCount() > 0) {
73 node = sortTree(node);
74 }
75 }
76 return root;
77 }
78 }
79
80 public void setRegionObj(Region regionObj) {
81 this.regionObj = regionObj;
82 }
83 }