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