-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordCollection.java
347 lines (324 loc) · 10.7 KB
/
RecordCollection.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
package main;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.JList;
import javax.swing.ScrollPaneConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.awt.event.ActionEvent;
public class RecordCollection implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frmMyRecordCollection;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField searchQuery;
private DefaultListModel<String> test;
private JList<String> recordList;
private ArrayList<RecordInfo> recordArrayList = new ArrayList<RecordInfo>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RecordCollection window = new RecordCollection();
window.frmMyRecordCollection.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public RecordCollection() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmMyRecordCollection = new JFrame();
frmMyRecordCollection.getContentPane().setBackground(new Color(240, 240, 240));
frmMyRecordCollection.setTitle("My Record Collection");
frmMyRecordCollection.setResizable(false);
frmMyRecordCollection.setBounds(100, 100, 800, 440);
frmMyRecordCollection.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMyRecordCollection.getContentPane().setLayout(null);
/*
*
* Initialize the data from a serialized object file that contains the records
*
*/
try{
InputStream file = new FileInputStream("records.ser");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
try{
@SuppressWarnings("unchecked")
ArrayList<RecordInfo> readRecords = (ArrayList<RecordInfo>)input.readObject();
recordArrayList = readRecords;
} finally {
input.close();
}
} catch(ClassNotFoundException ex) {
System.out.println(ex);
} catch(IOException ex) {
System.out.println(ex);
}
/*
*
* the data that goes into the main JList. It is initialized with an array list
*
*/
test = new DefaultListModel<>();
if(!recordArrayList.isEmpty()) {
test = setArrayToListModel(recordArrayList);
}
JLabel lblSoryBy = new JLabel("Sort by:");
lblSoryBy.setBounds(6, 6, 65, 28);
lblSoryBy.setHorizontalAlignment(SwingConstants.CENTER);
frmMyRecordCollection.getContentPane().add(lblSoryBy);
/*
*
* sorts the records by their title, artist, then year
*
*/
JRadioButton sortTitle = new JRadioButton("Title");
sortTitle.setBounds(83, 6, 65, 28);
buttonGroup.add(sortTitle);
sortTitle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(sortTitle.isSelected()) {
Collections.sort(recordArrayList, new TitleCompare());
test = new DefaultListModel<>();
test = setArrayToListModel(recordArrayList);
recordList.setModel(test);
recordList.revalidate();
System.out.println("Sorting by Title.");
}
}
});
frmMyRecordCollection.getContentPane().add(sortTitle);
/*
*
* sorts the records by artist, title, then year
*
*/
JRadioButton sortArtist = new JRadioButton("Artist");
sortArtist.setBounds(160, 6, 72, 28);
buttonGroup.add(sortArtist);
sortArtist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(sortArtist.isSelected()) {
Collections.sort(recordArrayList, new ArtistCompare());
test = new DefaultListModel<>();
test = setArrayToListModel(recordArrayList);
recordList.setModel(test);
recordList.revalidate();
System.out.println("Sorting by Artist.");
}
}
});
frmMyRecordCollection.getContentPane().add(sortArtist);
/*
*
* sorts the records in the array list by the year first, then title,
* then artist
*
*/
JRadioButton sortYear = new JRadioButton("Year");
sortYear.setBounds(244, 6, 65, 28);
buttonGroup.add(sortYear);
sortYear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(sortYear.isSelected()) {
Collections.sort(recordArrayList, new YearCompare());
test = new DefaultListModel<>();
test = setArrayToListModel(recordArrayList);
recordList.setModel(test);
recordList.revalidate();
System.out.println("Sorting by Artist.");
}
}
});
frmMyRecordCollection.getContentPane().add(sortYear);
/*
*
* search textbox
*
*/
searchQuery = new JTextField();
searchQuery.setBounds(410, 6, 292, 29);
frmMyRecordCollection.getContentPane().add(searchQuery);
searchQuery.setColumns(10);
/*
*
* Search button, will search for album and artist keywords
*
*/
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(searchQuery.getText() == null || searchQuery.getText() == "") {
test = new DefaultListModel<>();
test = setArrayToListModel(recordArrayList);
recordList.setModel(test);
recordList.revalidate();
} else {
String query = searchQuery.getText().toLowerCase();
ArrayList<RecordInfo> searchList = new ArrayList<RecordInfo>();
for(RecordInfo record : recordArrayList) {
if (record.getAlbumTitle().toLowerCase().contains(query) || record.getArtist().toLowerCase().contains(query)) {
searchList.add(record);
}
}
System.out.println("Found " + searchList.size() + " records containing " + query + ".");
test = new DefaultListModel<>();
test = setArrayToListModel(searchList);
recordList.setModel(test);
recordList.revalidate();
}
}
});
searchButton.setBounds(714, 6, 80, 29);
frmMyRecordCollection.getContentPane().add(searchButton);
/*
*
* This button opens a dialog that allows the user to import new record info
* the record info is returned, the dialog is disposed, and the record is added to
* the array list which gets imported into the main list.
*
*/
JButton addNewButton = new JButton("Add New");
addNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddRecord dialog = new AddRecord();
dialog.setVisible(true);
RecordInfo newRecord = dialog.getAddedRecord();
dialog.dispose();
recordArrayList.add(newRecord);
System.out.println(newRecord.recordToString());
test = new DefaultListModel<>();
test = setArrayToListModel(recordArrayList);
recordList.setModel(test);
recordList.revalidate();
try (
OutputStream file = new FileOutputStream("records.ser");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
{
output.writeObject(recordArrayList);
}
}catch(IOException ex) {
System.out.println(ex);
}
}
});
addNewButton.setBounds(694, 383, 100, 29);
frmMyRecordCollection.getContentPane().add(addNewButton);
/*
*
* this button will remove whatever the user has selected in the list.
* it will store the multiple selections and for each selection, remove
* them from the array list and update the main list.
*
*/
JButton removeSelectedButton = new JButton("Remove Selected");
removeSelectedButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] selected = recordList.getSelectedIndices();
for(int i = selected.length-1; i >= 0; i--) {
recordArrayList.remove(selected[i]);
}
try (
OutputStream file = new FileOutputStream("records.ser");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
{
output.writeObject(recordArrayList);
}
}catch(IOException ex) {
System.out.println(ex);
}
test = new DefaultListModel<>();
test = setArrayToListModel(recordArrayList);
recordList.setModel(test);
recordList.revalidate();
System.out.println("Removed " + selected.length + " records.");
}
});
removeSelectedButton.setBounds(6, 383, 142, 29);
frmMyRecordCollection.getContentPane().add(removeSelectedButton);
/*
*
* the pane that allows the user to scroll through the record list
*
*/
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(6, 78, 788, 293);
frmMyRecordCollection.getContentPane().add(scrollPane);
/*
*
* The actual list where the records are contained. The viewport of the scrollpane is set to this
* and the font is monospaced for readability and layout
*
*/
recordList = new JList<String>(test);
recordList.setFont(new Font("Monospaced", recordList.getFont().getStyle(), recordList.getFont().getSize()));
scrollPane.setViewportView(recordList);
recordList.setBorder(null);
/*
*
* this is just the main label with the column information, nothing interesting here
*
*/
JLabel lblRecord = new JLabel(" Cat # Album Title Artist Year");
lblRecord.setHorizontalAlignment(SwingConstants.LEFT);
lblRecord.setBounds(6, 50, 788, 28);
frmMyRecordCollection.getContentPane().add(lblRecord);
}
/*
*
* this method updates the list model that goes into the JList by importing data from the array list.
*
*/
public DefaultListModel<String> setArrayToListModel(ArrayList<RecordInfo> arraylist){
DefaultListModel<String> newListModel = new DefaultListModel<String>();
for(int i = 0; i < arraylist.size(); i++) {
newListModel.addElement(arraylist.get(i).recordToString());
}
return newListModel;
}
}