-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayArtistAlbumsForm.java
260 lines (229 loc) · 10.7 KB
/
DisplayArtistAlbumsForm.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
/**
* @author Yannis Exidaridis <[email protected]>
* @brief display artist albums form
**/
package albums;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.MalformedURLException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import be.ceau.itunesapi.response.Response;
import be.ceau.itunesapi.Search;
import be.ceau.itunesapi.request.search.Media;
import be.ceau.itunesapi.response.Result;
/**
*
* @author yannis
*/
public class DisplayArtistAlbumsForm extends javax.swing.JFrame {
private static final String WIKIPEDIA_LINK = "http://en.wikipedia.org/wiki/";
private static final String SUFFIX_LINK = "_discography";
private JButton jButtonBack;
private JButton jButtonDelete;
private JButton jButtonUpdate;
private JButton jButtonWikipedia;
private JTable jTableData;
private JScrollPane TableScrollPane;
private Container container;
private JLabel AlbumCoverArt;
private Integer AlbumID;
private ArtistAlbumsModel TableModel;
private final Database db;
private String Link;
private URL ArtworkURL;
public DisplayArtistAlbumsForm(String SearchName, Integer NumberOfAlbums) throws MalformedURLException {
super();
db = new Database();
AlbumID = -1;
ArtworkURL = null;
ResultDataForm(SearchName, NumberOfAlbums);
}
/**
* @brief draws a table with results
* @param SearchName
*/
private void ResultDataForm(String SearchName, Integer NumberOfAlbums) throws MalformedURLException {
// construct artist discography link to wikipedia
Link = WIKIPEDIA_LINK + SearchName.replace(' ', '_') + SUFFIX_LINK;
setResizable(false);
// add events
AlbumCoverArt = new JLabel();
jButtonBack = new JButton("Back");
jButtonBack.addActionListener((java.awt.event.ActionEvent evt) -> {
jButtonBackActionPerformed(evt);
});
jButtonDelete = new JButton("Delete");
jButtonDelete.addActionListener((java.awt.event.ActionEvent evt) -> {
jButtonDeleteActionPerformer(evt);
});
jButtonUpdate = new JButton("Update");
jButtonUpdate.addActionListener((java.awt.event.ActionEvent evt) -> {
jButtonUpdateActionPerformed(evt);
});
jButtonWikipedia = new JButton("Wikipedia");
jButtonWikipedia.addActionListener((java.awt.event.ActionEvent evt) -> {
try {
jButtonWikipediaActionPerformed(evt);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
});
try {
// count album
ResultSet rs = db.SearchAlbum(SearchName);
// search for artists albums in iTunes
Response response = new Search(SearchName).setMedia(Media.MUSIC).execute();
List<Result> iTunesResults = response.getResults();
TableModel = new ArtistAlbumsModel(NumberOfAlbums);
while (rs.next()) {
Integer RowNumber = rs.getRow()-1;
Integer RowID = Integer.parseInt(rs.getString(db.get_fld_id()));
TableModel.setAlbumID(RowNumber, RowID);
TableModel.setValueAt(rs.getString(db.get_fld_title()), RowNumber, 0);
TableModel.setValueAt(rs.getString(db.get_fld_format()), RowNumber, 1);
TableModel.setValueAt(rs.getString(db.get_fld_year()), RowNumber, 2);
TableModel.setValueAt(rs.getString(db.get_fld_label()), RowNumber, 3);
}
// table creation
jTableData = new JTable(TableModel);
jTableData.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTableData.getColumnModel().getColumn(0).setPreferredWidth(340);
jTableData.getColumnModel().getColumn(1).setPreferredWidth(60);
jTableData.getColumnModel().getColumn(2).setPreferredWidth(100);
jTableData.getColumnModel().getColumn(3).setPreferredWidth(200);
AlbumCoverArt.setIcon(new ImageIcon(getClass().getResource("imagemissing.png"))); // display it
// table row selection
jTableData.setCellSelectionEnabled(true);
ListSelectionModel cellSelectionModel = jTableData.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int selectedRow = jTableData.getSelectedRow();
for (int i = 0; i < TableModel.getColumnCount(); i++) {
AlbumID = TableModel.getAlbumID(selectedRow);
for (Result result : iTunesResults) {
// search for album cover art in iTunes
if (result.getCollectionName() != null) { // artist found
if (result.getCollectionName().toLowerCase().contains(String.valueOf(TableModel.getValueAt(selectedRow, 0)).toLowerCase())) {
try {
if (!result.getArtworkUrl100().isEmpty() && result.getArtworkUrl100() != null) { // artwork found
ArtworkURL = new URL(result.getArtworkUrl100()); // create url album cover art
AlbumCoverArt.setIcon(new ImageIcon(ArtworkURL)); // display it
} else {
AlbumCoverArt.setIcon(new ImageIcon(getClass().getResource("imagemissing.png"))); // display default icon
}
} catch (MalformedURLException ex) {
AlbumCoverArt.setIcon(new ImageIcon(getClass().getResource("imagemissing.png"))); // display default icon
}
break;
}
} else {
AlbumCoverArt.setIcon(new ImageIcon(getClass().getResource("imagemissing.png"))); // display default icon
}
}
}
}
});
jTableData.setPreferredScrollableViewportSize(new java.awt.Dimension(700, 200));
TableScrollPane = new JScrollPane(jTableData);
container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER));
// add everything to container
container.add(TableScrollPane);
container.add(AlbumCoverArt);
container.add(jButtonBack);
container.add(jButtonDelete);
container.add(jButtonUpdate);
container.add(jButtonWikipedia);
// draw form
setTitle(FirstUpperCase(SearchName) + " - " + NumberOfAlbums.toString() + " albums");
setLocation(new java.awt.Point(200, 200));
setSize(new java.awt.Dimension(850, 300));
} catch (SQLException ex) {
Logger.getLogger(DisplayArtistAlbumsForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @brief delete action event
* @param evt
*/
private void jButtonDeleteActionPerformer(java.awt.event.ActionEvent evt) {
if (AlbumID == -1) {
JOptionPane.showMessageDialog(null, "Album was not selected", "Error", JOptionPane.WARNING_MESSAGE);
} else {
if (db.DeleteAlbum(AlbumID)) {
JOptionPane.showMessageDialog(null, "Album deleted", "Delete", JOptionPane.INFORMATION_MESSAGE);
}
dispose();
}
}
/**
* @brief update action event
* @param evt
*/
private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = 0; i < TableModel.getRowCount(); i++) {
db.UpdateAlbum(TableModel.getAlbumID(i),
TableModel.getTableDataAt(i, 0),
TableModel.getTableDataAt(i, 1),
TableModel.getTableDataAt(i, 2),
TableModel.getTableDataAt(i, 3));
}
db.Close();
JOptionPane.showMessageDialog(null, "Update was successful", "Info", JOptionPane.INFORMATION_MESSAGE);
}
/**
* @brief link to wikipedia action event
* @param evt
*/
private void jButtonWikipediaActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
URL url = new URL(Link);
try {
Desktop.getDesktop().browse(url.toURI());
} catch (URISyntaxException ex) {
Logger.getLogger(DisplayArtistAlbumsForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @brief back action event
* @param evt
*/
private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
/**
* @brief convert to upper case the first letter of a string
* see https://stackoverflow.com/questions/1149855/how-to-upper-case-every-first-letter-of-word-in-a-string#
* @param s
* @return
*/
private String FirstUpperCase(String s) {
StringBuilder result = new StringBuilder(s.length());
String words[] = s.split("\\ ");
for (int i = 0; i < words.length; i++) {
if (i > 0) {
result.append(" ");
}
result.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1));
}
return result.toString();
}
}