-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorStudentsFrame.java
392 lines (352 loc) · 16.2 KB
/
ErrorStudentsFrame.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package exchangeallocatorframe;
import static exchangeallocatorframe.ExchangeAllocator.studentArray;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
/**
*
* @author e.verkade
*/
public class ErrorStudentsFrame extends javax.swing.JFrame {
private File lastPath = exchangeallocatorframe.ExchangeAllocatorFrame.lastPath;
static boolean errorDone;
/**
* Creates new form ErrorStudentsFrame
*
* @param studentArray
*/
public ErrorStudentsFrame(Student[] studentArray) {
students = studentArray;
numberOfStudents = studentArray.length;
errorDone = false;
initComponents();
fillRows();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
studentViewer = new javax.swing.JScrollPane();
errorStudents = new javax.swing.JTable();
runButton = new javax.swing.JButton();
saveInputButton = new javax.swing.JButton();
approveButton = new javax.swing.JButton();
textFileSaver = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
mainMenu = new javax.swing.JMenuBar();
fileButton = new javax.swing.JMenu();
exitButton = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Students to Check");
errorStudents.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Ranking", "Student ID", "First Name", "Last Name", "Prefix", "Email", "University", "Equally Weighted Requirement", "GPA Requirement", "Other Requirement", "Approve"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false, true
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
errorStudents.setColumnSelectionAllowed(true);
errorStudents.getTableHeader().setReorderingAllowed(false);
studentViewer.setViewportView(errorStudents);
errorStudents.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
if (errorStudents.getColumnModel().getColumnCount() > 0) {
errorStudents.getColumnModel().getColumn(10).setResizable(false);
errorStudents.getColumnModel().getColumn(10).setPreferredWidth(5);
}
runButton.setText("Accept Changes");
runButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
runButtonActionPerformed(evt);
}
});
saveInputButton.setText("Create New Input File");
saveInputButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveInputButtonActionPerformed(evt);
}
});
approveButton.setText("Approve All");
approveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
approveButtonActionPerformed(evt);
}
});
textFileSaver.setText("Save List of Students");
textFileSaver.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textFileSaverActionPerformed(evt);
}
});
jLabel1.setText("Please double check approvals");
fileButton.setText("File");
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
fileButton.add(exitButton);
mainMenu.add(fileButton);
setJMenuBar(mainMenu);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(studentViewer)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(approveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(saveInputButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFileSaver)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(runButton)
.addGap(36, 36, 36))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(studentViewer, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(runButton)
.addComponent(saveInputButton)
.addComponent(approveButton)
.addComponent(textFileSaver)
.addComponent(jLabel1))
.addGap(11, 11, 11))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
// TODO add your handling code here:
dispose();
}//GEN-LAST:event_exitButtonActionPerformed
private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed
// TODO add your handling code here:
if (!beenSaved) {
this.runTableStuff();
}
errorDone = true;
this.dispose();
if (lastIndex != exchangeallocatorframe.ExchangeAllocator.studentArray[studentArray.length - 1].getRanking() + 1) {
exchangeallocatorframe.ExchangeAllocator.studentClear(studentArray, lastIndex);
}
try {
ExchangeAllocator.run(exchangeallocatorframe.ExchangeAllocatorFrame.outputs,
exchangeallocatorframe.ExchangeAllocatorFrame.studentFile,
exchangeallocatorframe.ExchangeAllocatorFrame.uniFile,
exchangeallocatorframe.ExchangeAllocatorFrame.studyFile,
exchangeallocatorframe.ExchangeAllocatorFrame.fileOutput);
} catch (IOException ex) {
Logger.getLogger(ErrorStudentsFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex, "Error running the exchange allocator again.",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}//GEN-LAST:event_runButtonActionPerformed
private void saveInputButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveInputButtonActionPerformed
this.runTableStuff();
JFileChooser selector = new JFileChooser();
String path;
String filename;
selector.setCurrentDirectory((File) lastPath);
selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
selector.setDialogTitle("Please select the folder where you want to save the new input file");
if (selector.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = selector.getSelectedFile();
path = file.getParent();
filename = selector.getSelectedFile().getName();
System.out.println(path);
System.out.println(filename);
try {
CSVFileWriter.writeCSVFileInputs(filename, studentArray, path);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e, "Error writing the new Student Choices File.",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
beenSaved = true;
}//GEN-LAST:event_saveInputButtonActionPerformed
private void approveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_approveButtonActionPerformed
for (int i = 0; i < numberOfStudents; i++) {
errorStudents.setValueAt(true, i, 10);
}
}//GEN-LAST:event_approveButtonActionPerformed
private void textFileSaverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFileSaverActionPerformed
// TODO add your handling code here:
JFileChooser selector = new JFileChooser();
String path;
String filename;
selector.setCurrentDirectory((File) lastPath);
selector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
selector.setDialogTitle("Please select the folder where you want to save the list of students");
if (selector.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = selector.getSelectedFile();
path = file.getParent();
filename = selector.getSelectedFile().getName();
System.out.println(path);
System.out.println(filename);
try {
writeTextFileStudents(filename, path);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e, "Error writing the student list file.",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}//GEN-LAST:event_textFileSaverActionPerformed
private void writeTextFileStudents(String filename, String directory) throws IOException {
if (!filename.endsWith(".csv")) {
filename += ".csv";
}
FileWriter fileWriter;
File dir = new File(directory);
File actualFile = new File(dir, filename);
fileWriter = new FileWriter(actualFile);
int numberColumns = errorStudents.getColumnCount();
for (int i = 0; i < numberColumns; i++) {
fileWriter.append(String.valueOf(errorStudents.getColumnName(i)));
fileWriter.append(";");
}
fileWriter.append("\n");
for (int i = 0; i < numberOfStudents; i++) {
for (int j = 0; j < numberColumns - 1; j++) {
fileWriter.append(String.valueOf(errorStudents.getValueAt(i, j)));
fileWriter.append(";");
}
fileWriter.append("\n");
}
fileWriter.flush();
fileWriter.close();
}
private void runTableStuff() {
boolean chk;
int choiceNumber = 0;
lastIndex = exchangeallocatorframe.ExchangeAllocator.studentArray[studentArray.length - 1].getRanking() + 1;
for (int i = numberOfStudents - 1; i >= 0; i--) {
chk = (boolean) errorStudents.getValueAt(i, 10);
if (chk) {
choiceNumber = students[i].getChoiceNumber();
students[i].setApproval(choiceNumber);
} else {
students[i].studentWipe();
lastIndex = (int) errorStudents.getValueAt(i, 0);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(ErrorStudentsFrame.class.getName()).log(Level.SEVERE, null, ex);
}
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ErrorStudentsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
}
private void fillRows() {
DefaultTableModel model = (DefaultTableModel) errorStudents.getModel();
Object[] data = new Object[11];
boolean isApproved = false;
for (Student student : students) {
data[0] = student.getRanking();
data[1] = student.getID();
data[2] = student.getFirstName();
data[3] = student.getLastName();
data[4] = student.getPrefix();
data[5] = student.getEmail1();
data[6] = student.getUniName();
University uni = student.getUni();
data[7] = "";
data[8] = "";
if (uni.getGPAReq() > 0.0) {
String GPAReq = String.valueOf(uni.getGPAReq());
GPAReq += " GPA";
data[8] = GPAReq;
}
if (uni.getEquallyWeightedReq() > 0.0) {
String equalReq = String.valueOf(uni.getEquallyWeightedReq());
equalReq += " EWA";
data[7] = equalReq;
}
data[9] = uni.getOtherReq();
data[10] = isApproved;
model.addRow(data);
}
}
private boolean beenSaved;
static ErrorStudentsFrame frame;
static int numberOfStudents;
static Student[] students;
static int lastIndex = -1;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton approveButton;
private javax.swing.JTable errorStudents;
private javax.swing.JMenuItem exitButton;
private javax.swing.JMenu fileButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenuBar mainMenu;
private javax.swing.JButton runButton;
private javax.swing.JButton saveInputButton;
private javax.swing.JScrollPane studentViewer;
private javax.swing.JButton textFileSaver;
// End of variables declaration//GEN-END:variables
}