-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
355 lines (311 loc) · 10.4 KB
/
Controller.java
File metadata and controls
355 lines (311 loc) · 10.4 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
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
/** RandomCircularListGenerator
* @author Chase Carnaroli
*
* This is the controller, which runs the whole program.
*/
import java.util.*; // import lists
import java.awt.Desktop;
import java.net.*;
import java.io.*;
public class Controller
{
// INSTANCE VARIABLES
Window ui;
List<Group> groups;
Group selectedGroup;
ListViewType selectedViewType;
RandomCircularList list;
boolean showGroup;
// CONSTRUCTOR
public Controller(Window ui)
{
this.ui = ui;
groups = new ArrayList<Group>();
selectedGroup = null;
selectedViewType = ListViewType.NUMBER;
list = null;
showGroup = false;
}
// METHODS
// Add person to selectedGroup
// post: new person created and added to selectedGroup, rosterDisplay updated
public void addPerson(String name)
{
Person person = new Person(name, selectedGroup); // create new person
selectedGroup.add(person); // add person to selectedGroup
// Update UI
ui.updateListDisplay(""); // clear listDisplay since roster changed
ui.updateRosterDisplay(); // update rosterDisplay
}
// Add group to Groups
// post: new group created, set as selectedGroup, and added to groups
public void addGroup(String groupName)
{
Group group = new Group(groupName); // create new group
groups.add(group); // add group to groups
selectedGroup = group; // set group as selectedGroup
ui.addGroupToComboBox(group); // update ComboBox
}
// Get groups
// post: returns List<Group> groups
public List<Group> getGroups()
{
return groups;
}
// Set the selectedGroup
// post: sets selectedGroup as group, rosterDisplay updated
public void setSelectedGroup(Group group)
{
selectedGroup = group; // set selectedGroup as group
// Update UI
ui.updateRosterDisplay(); // update rosterDisplay
}
// Get selectedGroup
// post: returns selectedGroup
public Group getSelectedGroup()
{
return selectedGroup;
}
// Remove current selectedGroup
// post: removes selectedGroup from groups and returns its index
private int removeSelectedGroup()
{
// Scans through groups until it finds selectedGroup
// Removes selectedGroup from groups
for(int i = 0; i < groups.size(); i++)
{
if(groups.get(i) == selectedGroup)
{
groups.remove(i);
return i;
}
}
// returns -1 if selectedGroup is not in groups
return -1;
}
// Set the roster for selectedGroup
// pre: editedRoster is a string, with each person's name on a seperate line
// post: selectedGroup's roster is set as the inputted string
public void setSelectedGroupRoster(String editedRoster)
{
// Convert editedRoster into an array of names (strings)
// Splits editedRoster at each new line and skips empty lines
String[] lines = editedRoster.split("[\\r\\n]+");
selectedGroup.setRoster(lines); // sets selectedGroups roster as the new array
// uUpdate UI
ui.updateRosterDisplay(); // update rosterDisplay
}
/**
* Starts the process to add a group
* First the addGroupPopUp is called
* If a name is inputted, then a the new group is created and added
* Components will also become enabled
* If no name is inputted, then nothing happens
*/
public void addGroupClicked()
{
// Creates addGroupPopUp, which returns the groupName
String groupName = ui.addGroupPopUp();
// Checks to make sure groupName isn't null
// It will be null if "cancel" is clicked
if(groupName != null)
{
if(groupName.equals(""))
{
ui.invalidGroupNamePopUp();
addGroupClicked();
} else {
addGroup(groupName);
ui.enableComponents(true);
// Update UI
clearListDisplay();
}
}
}
/**
* Starts the process to remove a group
* First the removeGroupPopUp is called
* If 'yes' is selected, then the group is removed from groups and the comboBox
* If there are no groups left, then the components are disabled
*/
public void removeGroupClicked()
{
// Triggers removeGroupPopUp and records the response
boolean response = ui.removeGroupPopUp(selectedGroup);
// If the response is true remove the group, otherwise do nothing
if(response)
{
// remove selectedGroup and record its index
int groupIndex = removeSelectedGroup();
// pass index on to remove it from the comboBox
ui.removeGroupFromComboBox(groupIndex);
// check to see if there are no groups left
// if there are none, disable the components
if(groups.size() == 0)
{
selectedGroup = null;
ui.enableComponents(false);
}
}
// Update UI
clearListDisplay();
}
/**
* Starts the process to rename a group
* First the renameGroupPopUp is called
* If a name is inputted, then the group will be renamed to that name
*/
public void renameGroupClicked()
{
// Creates renameGroupPopUP and returns the updatedName
String updatedName = ui.renameGroupPopUp(selectedGroup.getGroupName());
// If group name is not null, rename the selectedGroup
if(updatedName != null)
{
selectedGroup.setGroupName(updatedName); // set selectedGroup's name
setSelectedGroup(selectedGroup); // updates rosterDisplay to show correct title border
}
// Update UI
if(list != null)
{
ui.updateListDisplay(getSelectedViewText()); // update listDisplay
}
}
/**
* Starts the process to edit a group's roster
* editRosterPopUp is called and handles the rest
* then clears listDisplay since the roster changed
*/
public void editRosterClicked()
{
// Creates editRosterPopUp
ui.editRosterPopUp(selectedGroup);
// Update UI
clearListDisplay();
}
/**
* Starts the process to generate a random list
* generates a RandomCircularList from the groups
* updates listDisplay with the new random list
*/
public void generateListClicked()
{
// Check to make sure all groups have at least one person in them
boolean groupsValid = true;
for(Group group: groups)
{
if(group.size() == 0)
{
groupsValid = false;
}
}
// If the groups are valid, generate a list and update the UI
if(groupsValid)
{
// Create new random list
list = new RandomCircularList(groups);
// Update UI
ui.updateListDisplay(getSelectedViewText()); // sets listDisplay to the random list
} else {
ui.generateListErrorPopUp();
}
}
/**
* Starts the process to save the list
* generates a fileChooserPopUp
* if a file is returned from the fileChooser, then save text to the file
*/
public void saveClicked()
{
if(list == null)
{
ui.saveErrorNoListPopUp();
} else {
File file = ui.fileChooserPopUp();
if (file != null)
{
try(PrintStream fileContent = new PrintStream(file)){
fileContent.print(list.getFileText());
} catch (FileNotFoundException e){}
}
}
}
/**
* Starts the process to reset the program
* resetPopUp is called and the result is recorded
* If yes was selected, then reset() is called to reset the program
*/
public void resetClicked()
{
boolean resetResponse = ui.resetPopUp();
if(resetResponse)
{
reset();
}
}
// Resets the controller and the UI
// post: the controller and UI are reset to how they were when the program launched
private void reset()
{
// Reset instance varaibles
groups = new ArrayList<Group>();
selectedGroup = null;
selectedViewType = ListViewType.NUMBER;
list = null;
showGroup = false;
// Reset UI
ui.reset();
}
// Enable or disable the UI
// post: if enable is true all components are enabled, else they are disabled
public void enableUI(boolean enable)
{
ui.enableUI(enable);
}
// Set the selectedView type
// post: selectedViewType is set as viewType and listDisplay is updated
public void setSelectedViewType(ListViewType viewType)
{
selectedViewType = viewType;
// checks to make sure there is a list
if(list != null)
{
ui.updateListDisplay(getSelectedViewText());
}
}
// Get a text version of the list based off selectedViewType
// post: returns a string of the random list, in the format of the selectedViewType
private String getSelectedViewText()
{
return list.getText(selectedViewType, showGroup);
}
// Toggles whether showGroup is checked or not
// post: showGroup is set to the state that it is in on the UI
public void showGroupChecked(boolean checked)
{
showGroup = checked;
// checks to make sure there is a list
if(list != null)
{
ui.updateListDisplay(getSelectedViewText());
}
}
// Clears the list display in the ui and sets list to null
// post: ui listDisplay is empty and list = null
private void clearListDisplay()
{
ui.updateListDisplay(""); // clear listDisplay
list = null; // clear list
}
// Open website URL
// post: website is opened in user's default browser
public void openWebsite(String website)
{
try {
if(Desktop.isDesktopSupported())
{
Desktop.getDesktop().browse(new URI(website));
}
} catch (URISyntaxException e){} catch (IOException e){}
}
}