-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicalUserInterface.java
More file actions
485 lines (388 loc) · 16.4 KB
/
GraphicalUserInterface.java
File metadata and controls
485 lines (388 loc) · 16.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/** GUI implements a rectangle pop up that takes in
* various input options for user to interact with
* AlbumCollectionManager
*
* authored by Jackson W. Ayers
*/
package albumcollection;
import albumcollection.album.Album;
import albumcollection.album.CassetteTape;
import albumcollection.album.CompactDisc;
import albumcollection.album.MusicGenre;
import albumcollection.album.track.Track;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
/**
* Contains implementation for GUI
*/
public class GraphicalUserInterface {
//width and height of the monitor
private static int width = Toolkit.getDefaultToolkit().getScreenSize().width;
private static int height = Toolkit.getDefaultToolkit().getScreenSize().height;
//width and height of the window (JFrame)
private static int windowsWidth = 800;
private static int windowsHeight = 600;
//Components for the layout
private final JPanel topPanel = new JPanel();
private final JPanel bottomPanel = new JPanel();
private final JLabel commandLabel = new JLabel("Please select the command",JLabel.RIGHT);
private final JComboBox comboBox = new JComboBox();
//Output area. Set as global to be edit in different methods.
private final JTextArea outputArea = new JTextArea();
private final JScrollPane outputScrollPane = new JScrollPane(outputArea);
/**
* Creates two seperate panels for users to manipulate.
*
*/
public void createAndShowGUI() {
createTopPanel();
createBottomPanel();
JPanel panelContainer = new JPanel();
panelContainer.setLayout(new GridLayout(2,0));
panelContainer.add(topPanel);
panelContainer.add(bottomPanel);
JFrame frame = new JFrame("Album Collection");
// Save on quit.
frame.addWindowListener(new MainWindowAdapter());
//panelContainer.setOpaque(true);
frame.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
frame.setContentPane(panelContainer);
frame.setVisible(true);
}
/**
* Top panel implementation for user input.
* Creates permenant box for user choices.
*/
private void createTopPanel() {
comboBox.addItem("Please select...");
comboBox.addItem(" 1. Display list of all albums.");
comboBox.addItem(" 2. Add a new album to collection.");
comboBox.addItem(" 3. Search for an album.");
comboBox.addItem(" 4. Delete an album from collection.");
comboBox.addItem(" 5. Exit.");
comboBox.setSelectedIndex(0);
comboBox.addItemListener(new MainComboBoxListener());
GridLayout gl = new GridLayout(0,2,10,10);
topPanel.setLayout(gl);
resetTopPanel();
}
/**
* Bottom panel implementation for output of
* AlbumCollectionManager to display to.
*/
private void createBottomPanel() {
outputArea.setText("Album Collection.\n\n");
outputArea.setEditable(false);
outputScrollPane.createVerticalScrollBar();
outputScrollPane.createHorizontalScrollBar();
bottomPanel.setLayout(new GridLayout(1,0));
bottomPanel.add(outputScrollPane);
}
/**
* Clears top planel to ready space for different
* option buttons.
*/
private void resetTopPanel() {
topPanel.removeAll();
topPanel.add(commandLabel);
topPanel.add(comboBox);
//Keep the comboBox at the first line.
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.updateUI();
}
/**
* Implements buttons and text fields for user
* to print out AlbumCollectionManager
*
* @param albums
* @param numbered
*/
private void displayAlbums(ArrayList<Album> albums, boolean numbered) {
topPanel.removeAll();
topPanel.add(commandLabel);
topPanel.add(comboBox);
//Keep the comboBox at the first line.
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.updateUI();
for (int i = 0; i < albums.size(); i++) {
String numberingString;
if (numbered) {
numberingString = " " + Integer.toString(i + 1) + ". ";
} else {
numberingString = "";
}
Album album = albums.get(i);
outputArea.append(numberingString + album.toString() + "\n");
}
if (albums.size() > 0) {
outputArea.append("\n");
} else {
outputArea.append("There are no albums in the collection.\n\n");
}
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
/**
* Implements buttons and text fields in top panel
* for user to add a new album to collection
*/
private void addNewAlbumToCollection() {
topPanel.removeAll();
topPanel.add(commandLabel);
topPanel.add(comboBox);
JLabel albumTypeLabel = new JLabel("Album Type", JLabel.RIGHT);
topPanel.add(albumTypeLabel);
JComboBox albumTypeComboBox = new JComboBox();
albumTypeComboBox.addItem("Compact Disc");
albumTypeComboBox.addItem("Cassette Tape");
topPanel.add(albumTypeComboBox);
JLabel artistNameLabel = new JLabel("Artist", JLabel.RIGHT);
topPanel.add(artistNameLabel);
JTextField artistNameInputField = new JTextField("");
topPanel.add(artistNameInputField);
JLabel albumTitleLabel = new JLabel("Album Title", JLabel.RIGHT);
topPanel.add(albumTitleLabel);
JTextField albumTitleInputField = new JTextField("");
topPanel.add(albumTitleInputField);
JLabel albumReleaseYearLabel = new JLabel("Release Year", JLabel.RIGHT);
topPanel.add(albumReleaseYearLabel);
JComboBox albumReleaseYearComboBox = new JComboBox();
for (int i = 2020; i > 1900; i--) {
albumReleaseYearComboBox.addItem(Integer.toString(i));
}
topPanel.add(albumReleaseYearComboBox);
JLabel albumGenreLabel = new JLabel("Album Genre", JLabel.RIGHT);
topPanel.add(albumGenreLabel);
JComboBox albumGenreComboBox = new JComboBox();
albumGenreComboBox.addItem(MusicGenre.ROCK.toString());
albumGenreComboBox.addItem(MusicGenre.RAP.toString());
albumGenreComboBox.addItem(MusicGenre.COUNTRY.toString());
albumGenreComboBox.addItem(MusicGenre.POP.toString());
topPanel.add(albumGenreComboBox);
JLabel cassetteLengthLabel = new JLabel("Length of Album in Seconds", JLabel.RIGHT);
topPanel.add(cassetteLengthLabel);
cassetteLengthLabel.setVisible(false);
JTextField cassetteLenghtInputField = new JTextField("");
topPanel.add(cassetteLenghtInputField);
cassetteLenghtInputField.setVisible(false);
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
JButton confirmButton = new JButton("Confirm");
topPanel.add(confirmButton);
albumTypeComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == 1) {
if (e.getItem().equals("Compact Disc")) {
cassetteLengthLabel.setVisible(false);
cassetteLenghtInputField.setVisible(false);
} else if (e.getItem().equals("Cassette Tape")) {
cassetteLengthLabel.setVisible(true);
cassetteLenghtInputField.setVisible(true);
}
}
}
});
confirmButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Album album;
String artistName = artistNameInputField.getText();
String title = albumTitleInputField.getText();
int releaseYear = Integer.parseInt((String)albumReleaseYearComboBox.getSelectedItem());
MusicGenre genre = MusicGenre.fromString((String)albumGenreComboBox.getSelectedItem());
if (albumTypeComboBox.getSelectedItem() == "Compact Disc") {
album = new CompactDisc(title, artistName, releaseYear, genre);
} else {
double cassetteLengthInSeconds = Double.parseDouble(cassetteLenghtInputField.getText());
album = new CassetteTape(title, artistName, releaseYear, genre, cassetteLengthInSeconds);
}
AlbumCollectionManager.addAlbumToCollection(album);
outputArea.append(title + " by " + artistName + " added to collection.\n\n");
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
});
topPanel.updateUI();
}
/**
* Implements buttons and text fields in top panel
* for user to remove album from collection option.
*/
private void removeAlbumFromCollection() {
// Clear options
topPanel.removeAll();
topPanel.add(commandLabel);
topPanel.add(comboBox);
JLabel albumRemove = new JLabel("Enter title or artist album to remove:", JLabel.RIGHT);
topPanel.add(albumRemove);
JTextField albumRemoveTextField = new JTextField("");
topPanel.add(albumRemoveTextField);
topPanel.add(new JLabel());
JButton searchButton = new JButton("Search");
topPanel.add(searchButton);
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
JButton confirmButton = new JButton("Confirm index of Album");
topPanel.add(confirmButton);
JTextField confirmText = new JTextField("");
topPanel.add(confirmText);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Album album;
String numberingString;
String searched = albumRemoveTextField.getText();
String confirm = confirmText.getText();
ArrayList<Album> searchResults = AlbumCollectionManager.albumsMatchingSearchTerm(searched);
if (searchResults.size() > 0) {
outputArea.append("Select album to remove... \n");
if (searchResults.size() > 0) {
for (int i = 0; i < searchResults.size(); i++) {
numberingString = " " + Integer.toString(i + 1) + ". ";
album = searchResults.get(i);
outputArea.append(numberingString + album.toString() + " found from collection.\n\n");
}
} else {
outputArea.append("No albums matching search.\n");
}
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
}
});
confirmButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Album albumToRemove;
String searched = albumRemoveTextField.getText();
String confirm = confirmText.getText();
int indexToRemove = Integer.parseInt(confirm) - 1;
ArrayList<Album> searchResults = AlbumCollectionManager.albumsMatchingSearchTerm(searched);
if (searchResults.size() > 0) {
albumToRemove = searchResults.get(indexToRemove);
outputArea.append("Removing " + albumToRemove.toString() + "\n");
AlbumCollectionManager.removeAlbumFromCollection(albumToRemove);
}
else {
outputArea.append("No albums matching search.\n");
}
}
});
topPanel.updateUI();
}
/**
* Implements buttons and text fields in top panel
* to allow user to search for an album in the album
* collection manager.
*/
private void searchForAlbum() {
// Clear options
topPanel.removeAll();
topPanel.add(commandLabel);
topPanel.add(comboBox);
JLabel term = new JLabel("Search Term", JLabel.RIGHT);
topPanel.add(term);
JTextField termTextField = new JTextField("");
topPanel.add(termTextField);
JButton confirmButton = new JButton("Search");
topPanel.add(new JLabel());
topPanel.add(confirmButton);
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.add(new JLabel());
topPanel.updateUI();
confirmButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Album album;
String albumName = termTextField.getText();
ArrayList<Album> searchResults = AlbumCollectionManager.albumsMatchingSearchTerm(albumName);
if (searchResults.size() > 0) {
for (int i = 0; i < searchResults.size(); i++) {
album = searchResults.get(i);
outputArea.append(album.toString() + "found from collection.\n\n");
}
} else {
outputArea.append("No albums matching search\n\n");
}
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
});
}
class MainWindowAdapter extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
AlbumCollectionManager.saveAlbumCollectionToDatabase();
}
}
class MainComboBoxListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == 1) {
if (e.getItem().equals("Please select...")) {
outputArea.append("Please select a command to continue.\n\n");
resetTopPanel();
} else if (e.getItem().equals(" 1. Display list of all albums.")) {
displayAlbums(AlbumCollectionManager.getAlbumCollection(), false);
} else if (e.getItem().equals(" 2. Add a new album to collection.")) {
addNewAlbumToCollection();
} else if (e.getItem().equals(" 3. Search for an album.")) {
searchForAlbum();
} else if (e.getItem().equals(" 4. Delete an album from collection.")) {
removeAlbumFromCollection();
} else if (e.getItem().equals(" 5. Save to database.")) {
AlbumCollectionManager.saveAlbumCollectionToDatabase();
}
}
}
}
}