-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppGUI.java
More file actions
321 lines (275 loc) · 11.1 KB
/
Copy pathAppGUI.java
File metadata and controls
321 lines (275 loc) · 11.1 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.View;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class AppGUI extends JFrame {
JTextField txtPractitionerId = new JTextField(20);
JButton btnLoadPatients = new JButton("Load Patients");
JTable patientsTable = new JTable();
JTable monitorTable = new JTable();
JButton btnUnmonitorPatient = new JButton("Remove Selected");
JTextField txtInterval = new JTextField(10);
JButton btnUpdateInterval = new JButton("Apply");
ArrayList<Patient> patients = new ArrayList<>();
ArrayList<String> monitoredPatientIDs = new ArrayList<>();
boolean isRunning = false;
int updateInterval = Config.DEFUALT_UPDATE_INTERVAL;
ScheduledExecutorService executorService;
public AppGUI() {
setLayout(new BorderLayout());
add(authPanel(), BorderLayout.PAGE_START);
add(splitPanel(), BorderLayout.CENTER);
add(settingsPanel(), BorderLayout.PAGE_END);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
JPanel authPanel() {
JPanel panel = new JPanel();
JLabel labelPractitionerId = new JLabel("Practioner ID: ");
panel.add(labelPractitionerId);
labelPractitionerId.setBounds(0, 0, 0, 0);
panel.add(txtPractitionerId);
txtPractitionerId.setBounds(10, 0, 0, 0);
panel.add(btnLoadPatients);
btnLoadPatients.setBounds(20, 00, 0, 0);
btnLoadPatients.addActionListener(new EventHandler());
return panel;
}
JSplitPane splitPanel() {
String[] patientsColumns = {"Patients"};
String[] monitorColumns = {"Name", "Cholesterol", "Effective", "Updated"};
patientsTable.setModel(new DefaultTableModel(patientsColumns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
});
patientsTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
addPatientToMonitor();
}
}
});
JScrollPane patientPane = new JScrollPane(patientsTable);
patientsTable.setFillsViewportHeight(true);
JPanel monitorPane = new JPanel(new BorderLayout());
monitorTable.setModel(new DefaultTableModel(monitorColumns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
});
monitorTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
viewPatient();
}
}
});
JScrollPane monitorTablePane = new JScrollPane(monitorTable);
monitorTable.setFillsViewportHeight(true);
monitorPane.add(monitorTablePane, BorderLayout.CENTER);
monitorPane.add(btnUnmonitorPatient, BorderLayout.PAGE_END);
btnUnmonitorPatient.addActionListener(new EventHandler());
JSplitPane splitPane = new JSplitPane(SwingConstants.VERTICAL, patientPane, monitorPane);
splitPane.setDividerLocation(200);
return splitPane;
}
JPanel settingsPanel() {
JPanel panel = new JPanel();
JLabel settingsLabel = new JLabel("Interval (secs)");
panel.add(settingsLabel);
settingsLabel.setBounds(0, 0, 0, 0);
panel.add(txtInterval);
txtInterval.setBounds(10, 0, 0, 0);
txtInterval.setText(Integer.toString(Config.DEFUALT_UPDATE_INTERVAL));
panel.add(btnUpdateInterval);
btnUpdateInterval.setBounds(20, 0, 0, 0);
btnUpdateInterval.addActionListener(new EventHandler());
return panel;
}
void loadPatients() {
if(isRunning) {
isRunning = false;
executorService.shutdownNow();
}
monitoredPatientIDs.clear();
patients.clear();
String practitionerId = txtPractitionerId.getText();
if(practitionerId.length() > 0) {
btnLoadPatients.setText("Loading...");
btnLoadPatients.setEnabled(false);
DefaultTableModel patientsTableModel = (DefaultTableModel) patientsTable.getModel();
DefaultTableModel monitorTableModel = (DefaultTableModel) monitorTable.getModel();
patientsTableModel.setRowCount(0);
monitorTableModel.setRowCount(0);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try{
patients = ApiService.getPatients(practitionerId);
for(Patient patient: patients) {
patientsTableModel.addRow(new String[]{patient.getName()});
}
btnLoadPatients.setText("Load Patients");
btnLoadPatients.setEnabled(true);
}
catch (PractitionerNotFoundException e) {
JOptionPane.showMessageDialog(getContentPane(), "Practitioner not found");
btnLoadPatients.setText("Load Patients");
btnLoadPatients.setEnabled(true);
}
}
});
}
}
void addPatientToMonitor() {
int selectedPatientIndex = patientsTable.getSelectedRow();
String patientId = patients.get(selectedPatientIndex).getId();
if(!monitoredPatientIDs.contains(patientId)) {
monitoredPatientIDs.add(patientId);
DefaultTableModel tableModel = (DefaultTableModel) monitorTable.getModel();
try {
Map<String, String> data = ApiService.getCholesterol(patients.get(selectedPatientIndex).getId());
tableModel.addRow(new String[]{
patients.get(selectedPatientIndex).getName(),
data.get("totalCholesterol") + " " + data.get("cholesterolUnit"),
data.get("effectiveDateTime"),
data.get("updated")
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if(!isRunning) {
isRunning = true;
runUpdates();
}
}
});
}
catch (ObservationNotFoundException e) {
JOptionPane.showMessageDialog(getContentPane(), "Patient has no cholesterol observation");
}
}
else {
JOptionPane.showMessageDialog(getContentPane(), "Patient is already monitored");
}
}
void removePatientFromMonitor() {
int selectedPatientIndex = monitorTable.getSelectedRow();
monitoredPatientIDs.remove(selectedPatientIndex);
DefaultTableModel tableModel = (DefaultTableModel) monitorTable.getModel();
tableModel.removeRow(selectedPatientIndex);
if(monitoredPatientIDs.size() == 0) {
isRunning = false;
}
}
void updateMonitoredPatient() {
patientsTable.setEnabled(false);
monitorTable.setEnabled(false);
btnUnmonitorPatient.setEnabled(false);
if(monitoredPatientIDs.size() > 0) {
DefaultTableModel tableModel = (DefaultTableModel) monitorTable.getModel();
for(int i = 0; i < monitoredPatientIDs.size(); i++) {
try {
Map<String, String> data = ApiService.getCholesterol(monitoredPatientIDs.get(i));
tableModel.setValueAt(
data.get("totalCholesterol") + " " + data.get("cholesterolUnit"),
i,
1
);
tableModel.setValueAt(
data.get("effectiveDateTime"),
i,
2
);
tableModel.setValueAt(
data.get("updated"),
i,
3
);
} catch (ObservationNotFoundException e) {
e.printStackTrace();
}
}
}
patientsTable.setEnabled(true);
monitorTable.setEnabled(true);
btnUnmonitorPatient.setEnabled(true);
}
void changeInterval() {
if(isRunning) {
executorService.shutdownNow();
updateInterval = Integer.parseInt(txtInterval.getText());
runUpdates();
}
}
void runUpdates() {
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(AppGUI.this::updateMonitoredPatient, updateInterval, updateInterval, TimeUnit.SECONDS);
}
void viewPatient() {
int patientIndex = monitorTable.getSelectedRow();
Patient patient = ApiService.getPatientDetails(monitoredPatientIDs.get(patientIndex));
if(patient != null) {
new ViewPatient(patient);
}
}
public static void main(String[] args) {
new AppGUI();
}
public class EventHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnLoadPatients) {
loadPatients();
}
else if(e.getSource() == btnUnmonitorPatient) {
removePatientFromMonitor();
}
else if(e.getSource() == btnUpdateInterval) {
changeInterval();
}
}
}
public class ViewPatient extends JFrame {
public ViewPatient(Patient patient){
setLayout(new GridLayout(0, 2));
add(new JLabel("ID"));
add(new JLabel(patient.getId()));
add(new JLabel("Name"));
add(new JLabel(patient.getName()));
add(new JLabel("Gender"));
add(new JLabel(patient.getGender()));
add(new JLabel("Address"));
add(new JLabel(patient.getAddress()));
add(new JLabel("City"));
add(new JLabel(patient.getCity()));
add(new JLabel("State"));
add(new JLabel(patient.getState()));
add(new JLabel("Country"));
add(new JLabel(patient.getCountry()));
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setSize(400, 300);
setTitle("Patient Details");
setVisible(true);
}
@Override
public Insets getInsets() {
return new Insets(20, 10, 10, 10);
}
}
}