-
Notifications
You must be signed in to change notification settings - Fork 1
/
DisplayCandidateStatsView.java
150 lines (127 loc) · 3.89 KB
/
DisplayCandidateStatsView.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
import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class DisplayCandidateStatsView extends JPanel
{
private JLabel label1;
private double label1X, label1Y;
private JButton next;
private double nextX, nextY;
private JComboBox<String> state;
private double stateX, stateY;
JScrollPane scroll;
private JTextArea textArea;
private JFrame frame;
private Color defaultBackground = new Color(250, 250, 250);
private boolean loaded;
public DisplayCandidateStatsView ()
{
this.setLayout(null);
label1 = new JLabel("Please select the state for which you would like to view the stats:");
label1.setFont(new Font("Consolas", Font.PLAIN, 16));
label1.setSize(1000, 20);
label1.setVerticalAlignment(JLabel.BOTTOM);
label1.setHorizontalAlignment(JLabel.CENTER);
label1X = 2 / 7.0;
label1Y = 2 / 10.0;
state = new JComboBox<String>(Candidate.getStatesList());
state.insertItemAt("State",0); //Check they don't leave "State" selected
state.setSelectedIndex(0);
state.setBackground(defaultBackground);
state.setSize(100,20);
stateX = 5/10.0;
stateY = 3/7.0;
next = new JButton();
next.setEnabled(true);
next.setText("Display");
next.setSize(100,50);
nextX = 0.5;
nextY = 8/10.0;
next.addActionListener(e -> {
if(!(state.getSelectedItem().equals(" ")))
{
next.setEnabled(false);
displayStats((String)state.getSelectedItem());
}
else
{
//display that popup box to go back to the main screen
}
});
this.add(label1);
this.add(state);
this.add(next);
loaded = true;
}
public void repaint()
{
if(!loaded)
{
return;
}
int x = this.getWidth();
int y = this.getHeight();
label1.setBounds((int)(x*label1X - label1.getWidth()/2), (int)(y* label1Y - label1.getHeight()/2), label1.getWidth(), label1.getHeight());
state.setBounds((int)(x* stateX - state.getWidth()/2), (int)(y* stateY - state.getHeight()/2), state.getWidth(), state.getHeight());
next.setBounds((int)(x* nextX - next.getWidth()/2), (int)(y* nextY - next.getHeight()/2), next.getWidth(), next.getHeight());
}
private void displayStats(String state)
{
frame = new JFrame(state+" Voter Stats");
textArea = new JTextArea(30, 70); //Rows and cols to be displayed
textArea.setFont(new Font("Consolas", Font.PLAIN, 12));
textArea.setText(formatDisplay(state));
textArea.setEditable ( false ); // set textArea non-editable
scroll = new JScrollPane(textArea);
frame.add(scroll); //We add the scroll, since the scroll already contains the textArea
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
next.setEnabled(true);
}
});
}
private String formatDisplay(String state)
{
StringBuilder sb = new StringBuilder();
if(0 < Candidate.getPosNum()) {
if(Candidate.isPresElec()) {
sb.append(presi(state));
for(int i = 2; i < (Candidate.getPosNum() + 1); i++) {
sb.append("\n"+candy(state, i));
}
return sb.toString();
} else {
for(int i = 1; i < (Candidate.getPosNum() + 1); i++) {
sb.append(candy(state, i)+"\n");
}
return sb.toString();
}
} else {
return "There is no Election";
}
}
public static String candy(String state, int i)
{
Candidate.loadData(state);
String [][] cand = Candidate.getAllCandStats(i);
return TestDriver_Liam.formatPrint(cand);
}
public static String presi(String state)
{
Candidate.loadData(state);
String [][] cand = Candidate.getAllPresStats();
return TestDriver_Liam.formatPrint(cand);
}
}