Commit | Line | Data |
---|---|---|
f56708fa | 1 | import javax.swing.*; |
822afd4f | 2 | import javax.swing.tree.DefaultMutableTreeNode; |
67134f53 | 3 | import javax.swing.tree.TreeSelectionModel; |
822afd4f | 4 | import java.util.ListIterator; |
f56708fa SP |
5 | |
6 | public class RegionView extends JPanel { | |
7 | private Region regionObj; | |
822afd4f | 8 | private JTree tree; |
f56708fa SP |
9 | |
10 | RegionView(Region regionObj) { | |
11 | setRegionObj(regionObj); | |
822afd4f SP |
12 | DefaultMutableTreeNode top = new DefaultMutableTreeNode("Region"); |
13 | ||
14 | for (String continent : regionObj.getContinents()) { | |
15 | DefaultMutableTreeNode topContinent = new DefaultMutableTreeNode(continent); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
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); | |
822afd4f SP |
50 | } |
51 | } | |
52 | } | |
d8b03ca8 | 53 | sortTree(top); |
822afd4f | 54 | tree = new JTree(top); |
67134f53 | 55 | tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
d8b03ca8 SP |
56 | add(new JScrollPane(tree)); |
57 | } | |
58 | ||
59 | public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) { | |
60 | { | |
61 | for (int i = 0; i < root.getChildCount() - 1; i++) { | |
40a33410 | 62 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i); |
d8b03ca8 SP |
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(); | |
d8b03ca8 | 69 | if (nt.compareToIgnoreCase(np) > 0) { |
d8b03ca8 SP |
70 | root.insert(node, j); |
71 | break; | |
72 | } | |
73 | } | |
74 | if (node.getChildCount() > 0) { | |
9f1e8999 | 75 | sortTree(node); |
d8b03ca8 SP |
76 | } |
77 | } | |
d8b03ca8 SP |
78 | return root; |
79 | } | |
f56708fa SP |
80 | } |
81 | ||
9f1e8999 JB |
82 | public JTree getTree() { |
83 | return tree; | |
84 | } | |
85 | ||
f56708fa SP |
86 | public void setRegionObj(Region regionObj) { |
87 | this.regionObj = regionObj; | |
f56708fa | 88 | } |
67134f53 JB |
89 | |
90 | public Region getRegionObj() { | |
91 | return regionObj; | |
92 | } | |
71754579 | 93 | } |