-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedulerApp.java
More file actions
326 lines (254 loc) · 8.79 KB
/
SchedulerApp.java
File metadata and controls
326 lines (254 loc) · 8.79 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
322
323
324
325
326
/***************************************************\
| |
| Copyright ©2005, 2006 |
| Michael Cook |
| |
\***************************************************/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.util.ArrayList;
public class SchedulerApp extends JFrame implements ListSelectionListener, ActionListener {
private static SchedulePanel sp;
private static Scheduler sched;
private static JSplitPane hPane;
private static JSplitPane vPane;
private static JLabel courseLabel;
private static JLabel scheduleLabel;
private static Box courseBox;
private static Box scheduleBox;
private static JButton makeButton;
private static JButton resetButton;
private static CourseList cl;
private static ScheduleList sl;
private static JScrollPane coursePane;
private static JScrollPane schedulePane;
private static Schedule[] schedList;
public SchedulerApp() {
super("Scheduler");
makeGUI();
pack();
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// With any luck, that's it
// System.out.println("End of SchedulerApp()");
}
private void makeGUI() {
Container c = getContentPane();
/* Here is what our layout looks like:
*
* ---------------------------------------------------
* |/-----------------\| |
* || label || |
* || CourseList || |
* || make || |
* || reset || |
* ||-----------------|| SchedulePanel |
* || label || |
* || ScheduleList || |
* || || |
* |\-----------------/| |
* ---------------------------------------------------
*
* Where each of the containers is a JSplitPane
*
* Inside the left hand parts, there is a box layout model with
* label and a JScrollPane containing a JList implementation
*
*/
courseLabel = new JLabel("Course Choices:");
scheduleLabel = new JLabel("Schedule Choices:");
sp = new SchedulePanel(640, 480);
cl = new CourseList();
sl = new ScheduleList();
sl.addListSelectionListener(this);
makeButton = new JButton("Calculate Schedules");
makeButton.addActionListener(this);
makeButton.setActionCommand("recalc");
resetButton = new JButton("Reset All");
resetButton.addActionListener(this);
resetButton.setActionCommand("reset");
coursePane = new JScrollPane(cl);
schedulePane = new JScrollPane(sl);
courseBox = Box.createVerticalBox();
scheduleBox = Box.createVerticalBox();
courseBox.add(courseLabel);
courseBox.add(coursePane);
courseBox.add(makeButton);
courseBox.add(resetButton);
scheduleBox.add(scheduleLabel);
scheduleBox.add(schedulePane);
vPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, courseBox, scheduleBox);
hPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, vPane, sp);
c.add(hPane, "Center");
// System.out.println("End of makeGUI()");
}
public static void main(String[] args) {
System.out.println("");
schedList = new Schedule[0];
sched = new Scheduler();
if (sched == null) {
System.out.println("Unable to create a new scheduler!");
System.out.println("Giving up and exiting.");
System.exit(0);
}
// sched.fillFromFile("/Users/michael/Programming/Java/Scheduler/testdata.txt");
try {
sched.fillFromFile("testdata.txt");
} catch (SchedulerException e) {
System.out.println("Caught exception during fillFromFile: " +
e.getType() + ": " + e.getMessage());
e.printStackTrace();
System.out.println("");
System.exit(0);
}
// System.out.println("");
SchedulerApp us = new SchedulerApp();
cl.setScheduler(sched);
resetEverything();
}
public void recalcEverything() {
ArrayList<String[]> possibilities = null;
String[] classList = cl.getSelected();
if ((classList == null) || (classList.length == 0)) {
sl.clearList();
return; // Nothing else to do
}
try {
possibilities = sched.buildPossibles(classList);
} catch (SchedulerException e) {
if (e.getType() == "Conflict") {
JOptionPane.showMessageDialog(this, "There are no valid schedules for those courses." +
"\n" + e.getMessage(), "Conflict", JOptionPane.WARNING_MESSAGE);
sl.clearList();
return; // Bail out since we can't continue.
} else {
JOptionPane.showMessageDialog(this, "Couldn't build possibilities list:\n\n" +
"Type: " + e.getType() + "\n\n" +
e.getMessage() + "\n\nExiting.",
"Error", JOptionPane.ERROR_MESSAGE);
// System.out.println("Couldn't build possibilities list: " +
// e.getType() + ": " + e.getMessage());
e.printStackTrace();
System.out.println("");
System.exit(0);
}
}
String[] sections;
// System.out.println("");
// System.out.println("Now checking for valid schedules...");
// System.out.println("");
ArrayList<Schedule> validList = null;
try {
validList = sched.possiblesToSchedules(classList, possibilities);
} catch (SchedulerException e) {
JOptionPane.showMessageDialog(this, "Couldn't build schedules list:\n\n" +
"Type: " + e.getType() + "\n\n" + e.getMessage() +
"\n\nExiting.",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.out.println("");
System.exit(0);
}
// System.out.println("Found " + validList.size() + " valid schedules.");
// System.out.println("");
// Now we check to see if all courses are present that will be needed
if (schedList.length > 0) {
ArrayList<String> missing = schedList[0].getMissingCoReqs();
if (missing != null) {
String theMessage = "The following co-requirements are missing:\n\n";
theMessage = theMessage + missing.get(0);
for (int i = 1; i < missing.size(); i++)
theMessage = theMessage + ", " + missing.get(i);
JOptionPane.showMessageDialog(this, theMessage, "Missing Co-Reqs",
JOptionPane.WARNING_MESSAGE);
}
}
schedList = validList.toArray(schedList);
sl.setScheduleList(schedList.length);
try {
sp.setScheduleToDraw(schedList[0]);
} catch (SchedulerException e) {
JOptionPane.showMessageDialog(this, "Couldn't get schedule to draw:\n\n" +
"Type: " + e.getType() + "\n\n" + e.getMessage() +
"\n\nExiting.",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.out.println("");
System.exit(0);
}
// System.out.println("Looking at schedule " + schedNum + ".");
// System.out.println("");
// Now select the first schedule if there is a valid one
if (schedList.length >= 1) {
sl.setSelectedIndex(0);
}
}
public static void resetEverything() {
schedList = new Schedule[0];
sl.clearList();
cl.unselectAll();
sp.dontDraw();
sp.repaint();
cl.repaint();
sl.repaint();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == makeButton) {
if (e.getActionCommand() == "recalc") {
// The coursee selection changed.
recalcEverything();
} else {
System.out.println("Asked to perform unknown action for button: " +
e.getActionCommand());
}
} else if (e.getSource() == resetButton) {
if (e.getActionCommand() == "reset") {
// Reset everything
resetEverything();
} else {
System.out.println("Asked to perform unknown action for button: " +
e.getActionCommand());
}
} else {
System.out.println("Action sent to us by unknown object: " + e.getSource());
}
}
// This gets called when the selections change
public void valueChanged(ListSelectionEvent e) {
// Find out who had their selection changed
if (e.getSource() == sl) {
// The schedule list had a selection change
if (schedList.length != 0) {
int selectedNum = sl.getSelectedSchedule();
if (selectedNum != -1) {
try {
sp.setScheduleToDraw(schedList[selectedNum]);
} catch (SchedulerException ee) {
// If we are here, then schedList[selectedNum] was null
JOptionPane.showMessageDialog(this, "Couldn't set schedule to draw:\n\n" +
"Type: " + ee.getType() +
"\n\n" + ee.getMessage() +
"\n\nExiting.",
"Error", JOptionPane.ERROR_MESSAGE);
ee.printStackTrace();
System.out.println("");
System.exit(0);
}
sp.repaint();
}
}
// System.out.println("");
// System.out.println("Schedule selection changed.");
// System.out.println("");
} else {
System.out.println("");
System.out.println("Got a valueChanged notification from unknown object: " +
e.getSource());
System.out.println("");
}
}
}