-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractionPane.java
271 lines (257 loc) · 9.08 KB
/
InteractionPane.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
@SuppressWarnings("serial")
public class InteractionPane extends JComponent {
private MapGrid mapGrid;
private JRadioButton v1Radio, v2Radio, v3Radio, v4Radio, v5Radio;
private ButtonGroup versionRadios;
private JTextField numRowsEditorField, numColumnsEditorField;
private static JTextField regPopDisplay, regPercDisplay;
public InteractionPane(final JFrame appFrame){
this.setBorder(new EtchedBorder(EtchedBorder.RAISED));
// Interaction Pane is laid out horizontally
BoxLayout interactionPaneLayout = new BoxLayout(this, BoxLayout.X_AXIS);
this.setLayout(interactionPaneLayout);
// Interaction Pane includes a Version sub-panel laid out vertically
JPanel programVersions = new JPanel();
BoxLayout programVersionsLayout = new BoxLayout(programVersions, BoxLayout.Y_AXIS);
programVersions.setLayout(programVersionsLayout);
// create the radio buttons
v1Radio = new JRadioButton("V.1: Simple, Sequential", true);
v1Radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
USMaps.running = USMaps.Version.ONE;
USMaps.pqPreprocess();
}
});
v2Radio = new JRadioButton("V.2: Simple, Parallel", false);
v2Radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
USMaps.running = USMaps.Version.TWO;
USMaps.pqPreprocess();
}
});
v3Radio = new JRadioButton("V.3: Fancy, Sequential", false);
v3Radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
USMaps.running = USMaps.Version.THREE;
USMaps.pqPreprocess();
}
});
v4Radio = new JRadioButton("V.4: Fancy, Parallel", false);
v4Radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
USMaps.running = USMaps.Version.FOUR;
USMaps.pqPreprocess();
}
});
v5Radio = new JRadioButton("V.5: Fancy, Lock-Based", false);
v5Radio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
USMaps.running = USMaps.Version.FIVE;
USMaps.pqPreprocess();
}
});
// And a button to run the thing
final JButton runButton = new JButton("Run");
runButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
USMaps.runProgram(appFrame);
}
});
// add the radio buttons to a button group
versionRadios = new ButtonGroup();
versionRadios.add(v1Radio);
versionRadios.add(v2Radio);
versionRadios.add(v3Radio);
versionRadios.add(v4Radio);
versionRadios.add(v5Radio);
// add the radio buttons and run button to the Versions sub-panel
programVersions.add(v1Radio);
programVersions.add(v2Radio);
programVersions.add(v3Radio);
programVersions.add(v4Radio);
programVersions.add(v5Radio);
programVersions.add(runButton);
// at last, add the program Versions sub-panel to the Interaction Pane
add(programVersions);
// Create a sub-panel to change the line separation of the MapPane
JPanel rowColPanel = new JPanel();
// Add an editor (label + text field) for the number of rows
JPanel numRowsEditor = new JPanel();
JLabel numRowsEditorLabel = new JLabel("Number of Rows:");
numRowsEditorField = new JTextField(3);
numRowsEditorField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s = numRowsEditorField.getText();
int i;
try{
i = Integer.parseInt(s);
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(appFrame,
"Must enter a number in the \"Number of Rows\" field.",
"Number Entry Error", JOptionPane.ERROR_MESSAGE);
return;
}
mapGrid.setRows(i);
mapGrid.notifyObservers();
}
});
numRowsEditorField.addFocusListener(new FocusListener(){
private String text;
public void focusGained(FocusEvent e) {
text = numRowsEditorField.getText(); // save data that was already entered
}
public void focusLost(FocusEvent e) {
String s = numRowsEditorField.getText();
if(!s.equals(text)){
int i;
try{
i = Integer.parseInt(s);
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(appFrame,
"Must enter a number in the \"Number of Rows\" field.",
"Number Entry Error", JOptionPane.ERROR_MESSAGE);
return;
}
mapGrid.setRows(i);
mapGrid.notifyObservers();
}
}
});
numRowsEditor.add(numRowsEditorLabel);
numRowsEditor.add(numRowsEditorField);
// Add an editor (label + text field) for the number of columns also!
JPanel numColumnsEditor = new JPanel();
JLabel numColumnsEditorLabel = new JLabel("Number of Columns");
numColumnsEditorField = new JTextField(3);
numColumnsEditorField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s = numColumnsEditorField.getText();
int i;
try{
i = Integer.parseInt(s);
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(appFrame,
"Must enter a number in the \"Number of Columns\" field.",
"Number Entry Error", JOptionPane.ERROR_MESSAGE);
return;
}
mapGrid.setColumns(i);
mapGrid.notifyObservers();
}
});
numColumnsEditorField.addFocusListener(new FocusListener(){
private String text;
public void focusGained(FocusEvent e) {
text = numColumnsEditorField.getText(); // save data that was already entered
}
public void focusLost(FocusEvent e) {
String s = numColumnsEditorField.getText();
if(!s.equals(text)){
int i;
try{
i = Integer.parseInt(s);
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(appFrame,
"Must enter a number in the \"Number of Columns\" field.",
"Number Entry Error", JOptionPane.ERROR_MESSAGE);
return;
}
mapGrid.setColumns(i);
mapGrid.notifyObservers();
}
}
});
numColumnsEditor.add(numColumnsEditorLabel);
numColumnsEditor.add(numColumnsEditorField);
rowColPanel.add(numRowsEditor);
rowColPanel.add(numColumnsEditor);
// Finally, add sub-panel to the Interaction Pane
this.add(rowColPanel);
// Create a sub-panel to display the data received from PopulationQuery
JPanel dataDisplayPanel = new JPanel();
JPanel popDisplaySubPanel = new JPanel();
JLabel regPopLabel = new JLabel("Regional Population:");
regPopDisplay = new JTextField(8);
regPopDisplay.setEditable(false);
popDisplaySubPanel.add(regPopLabel);
popDisplaySubPanel.add(regPopDisplay);
JPanel percDisplaySubPanel = new JPanel();
JLabel regPercLabel = new JLabel("Percentage of total US:");
regPercDisplay = new JTextField(8);
regPercDisplay.setEditable(false);
percDisplaySubPanel.add(regPercLabel);
percDisplaySubPanel.add(regPercDisplay);
dataDisplayPanel.add(popDisplaySubPanel);
dataDisplayPanel.add(percDisplaySubPanel);
// add this data display sub-panel to the Interaction Pane
this.add(dataDisplayPanel);
}
public void initMapGrid(int rows, int columns, Observer o){
mapGrid = new MapGrid();
mapGrid.addObserver(o);
mapGrid.setRows(rows);
mapGrid.setColumns(columns);
numRowsEditorField.setText("" + rows);
numColumnsEditorField.setText("" + columns);
mapGrid.notifyObservers();
}
public static class MapGrid extends Observable{
private int rows, columns;
public void setRows(int rows){
if(this.rows != rows){
this.rows = rows;
setChanged();
}
}
public void setColumns(int columns){
if(this.columns != columns){
this.columns = columns;
setChanged();
}
}
public int getRows(){ return rows; }
public int getColumns(){ return columns; }
}
public MapGrid getMapGrid(){ return mapGrid; }
public void deselectAllButtons(){
versionRadios.setSelected(v1Radio.getModel(), false);
versionRadios.setSelected(v2Radio.getModel(), false);
versionRadios.setSelected(v3Radio.getModel(), false);
versionRadios.setSelected(v4Radio.getModel(), false);
versionRadios.setSelected(v5Radio.getModel(), false);
}
public void selectButton(int i){
switch(i){
case 1: versionRadios.setSelected(v1Radio.getModel(), true); break;
case 2: versionRadios.setSelected(v2Radio.getModel(), true); break;
case 3: versionRadios.setSelected(v3Radio.getModel(), true); break;
case 4: versionRadios.setSelected(v4Radio.getModel(), true); break;
case 5: versionRadios.setSelected(v5Radio.getModel(), true); break;
default: break;
}
}
public static void displayCensusData(int pop, double perc){
//System.out.println(pop + " and " + perc);
regPopDisplay.setText("" + pop);
DecimalFormat twoDecimals = new DecimalFormat("#.##");
regPercDisplay.setText(twoDecimals.format(perc) + "%");
}
}