Commit | Line | Data |
---|---|---|
f56708fa | 1 | import javax.swing.*; |
822afd4f SP |
2 | import javax.swing.tree.DefaultMutableTreeNode; |
3 | import java.util.ListIterator; | |
f56708fa SP |
4 | |
5 | public class RegionView extends JPanel { | |
6 | private Region regionObj; | |
822afd4f | 7 | private JTree tree; |
f56708fa SP |
8 | |
9 | RegionView(Region regionObj) { | |
10 | setRegionObj(regionObj); | |
822afd4f SP |
11 | DefaultMutableTreeNode top = new DefaultMutableTreeNode("Region"); |
12 | ||
13 | for (String continent : regionObj.getContinents()) { | |
14 | DefaultMutableTreeNode topContinent = new DefaultMutableTreeNode(continent); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
49 | } |
50 | } | |
51 | } | |
d8b03ca8 | 52 | sortTree(top); |
822afd4f | 53 | tree = new JTree(top); |
d8b03ca8 SP |
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 | |
61 | .getChildAt(i); | |
62 | String nt = node.getUserObject().toString(); | |
63 | ||
64 | for (int j = i + 1; j <= root.getChildCount() - 1; j++) { | |
65 | DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root | |
66 | .getChildAt(j); | |
67 | String np = prevNode.getUserObject().toString(); | |
68 | ||
69 | System.out.println(nt + " " + np); | |
70 | if (nt.compareToIgnoreCase(np) > 0) { | |
71 | ||
72 | root.insert(node, j); | |
73 | break; | |
74 | } | |
75 | } | |
76 | if (node.getChildCount() > 0) { | |
77 | node = sortTree(node); | |
78 | } | |
79 | } | |
80 | ||
81 | return root; | |
82 | } | |
f56708fa SP |
83 | } |
84 | ||
85 | public void setRegionObj(Region regionObj) { | |
86 | this.regionObj = regionObj; | |
f56708fa | 87 | } |
71754579 | 88 | } |