-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainCitiesCriteriaPanel.java
176 lines (137 loc) · 5.52 KB
/
MainCitiesCriteriaPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Author: Israel Saeta Pérez
*/
import java.util.*;
import java.util.List;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
/**
Lets the user select criteria to find the main cities of the country.
*/
public class MainCitiesCriteriaPanel extends JPanel implements ActionListener, ListSelectionListener{
JLabel label;
JPanel labelPanel;
JRadioButton rb1, rb2, rb3;
DefaultListModel listModel;
JList list;
JScrollPane listPanel;
JFormattedTextField nCitiesField;
JFormattedTextField distField;
JButton submit;
JButton back;
JPanel buttonPanel;
String countryName;
List<String> typeCodes = new ArrayList<String>();
int nCities = 0;
public MainCitiesCriteriaPanel() {
super(new BorderLayout());
CountryController countryc = Application.getCountryController();
CitiesController citiesc = Application.getCitiesController();
countryName = Application.getCountryName();
label = new JLabel();
labelPanel = new JPanel();
labelPanel.add(label);
label.setText("Criteria to select main cities for " + countryName.replaceAll("_", " "));
listModel = new DefaultListModel();
Iterator<HashMap<String,String>> iter = citiesc.getToponymTypesIterator();
while(iter.hasNext()) {
String topTypeName = iter.next().get("code");
listModel.addElement(topTypeName);
}
list = new JList(listModel);
list.addListSelectionListener(this);
listPanel = new JScrollPane(list);
NumberFormat nCitiesFormat = NumberFormat.getInstance();
nCitiesFormat.setMaximumFractionDigits(0);
nCitiesFormat.setMaximumIntegerDigits(4);
NumberFormat distFormat = NumberFormat.getInstance();
distFormat.setMaximumFractionDigits(0);
distFormat.setMaximumIntegerDigits(3);
nCitiesField = new JFormattedTextField(nCitiesFormat);
distField = new JFormattedTextField(distFormat);
nCitiesField.setMaximumSize(new Dimension(50,1));
distField.setMaximumSize(new Dimension(50,1));
rb1 = new JRadioButton("Filter by type", true);
rb2 = new JRadioButton("Filter by number", false);
rb3 = new JRadioButton("Filter by distance to the borders (km)", false);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
bgroup.add(rb3);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
radioPanel.add(rb1);
radioPanel.add(listPanel);
radioPanel.add(rb2);
radioPanel.add(nCitiesField);
radioPanel.add(rb3);
radioPanel.add(distField);
submit = new JButton();
back = new JButton();
submit.setText("OK");
back.setText("GO BACK");
submit.addActionListener(this);
back.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.add(back);
buttonPanel.add(submit);
add(labelPanel, BorderLayout.NORTH);
add(radioPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit){
if (rb1.isSelected()) {
JFrame frame = Application.getFrame();
if (typeCodes.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Please select at least one toponym type.",
"Error", JOptionPane.ERROR_MESSAGE);
} else {
Application.getMainCitiesPanel(this, typeCodes, 0, 0.0);
}
}
else if (rb2.isSelected()) {
JFrame frame = Application.getFrame();
try {
nCities = new Integer(nCitiesField.getText());
if (nCities <= 0)
throw new NumberFormatException();
typeCodes = new ArrayList<String>();
Application.getMainCitiesPanel(this, typeCodes, nCities, 0.0);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter a positive integer number.",
"Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JFrame frame = Application.getFrame();
try {
Double dist = new Double(distField.getText());
if (dist <= 0)
throw new NumberFormatException();
typeCodes = new ArrayList<String>();
Application.getMainCitiesPanel(this, typeCodes, 0, dist);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter a positive number.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
else {
Application.getOptionPanel(this, countryName);
}
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
Object[] tcs = list.getSelectedValues();
typeCodes = new ArrayList<String>();
for (int i = 0; i < tcs.length; i++) {
String typeCode = (String) tcs[i];
typeCodes.add(typeCode);
}
}
}
}