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
|
import org.jaudiotagger.audio.*;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.datatype.Artwork;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
public class TagEditorScreen extends JFrame {
private static final int COLUMN_TITLE = 0;
private static final int COLUMN_ARTIST = 1;
private static final int COLUMN_FILE_NAME = 2;
private static final int COLUMN_FILE_PATH = 3;
private JPanel mainPanel;
private JTextField titleField;
private JLabel titleLabel;
private JTextField uploaderField;
private JLabel uploaderLabel;
private JTextField fileNameField;
private JLabel fileNameLabel;
private JTextField imagePathField;
private JButton imageChooseButton;
private JTable songTable;
private JButton chooseAudioDirectoryButton;
private JButton applyChangesButton;
private JLabel artIconLabel;
private JTextField searchField;
private JButton listenButton;
private String setDirPath = "";
private String selectedAlbumArt = "";
private ArrayList<File> songList = new ArrayList<File>();
private String currPath = "";
private Boolean imageSelected = false;
public TagEditorScreen() {
// Initialize all components
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
titleField = new JTextField();
titleLabel = new JLabel("Title:");
uploaderField = new JTextField();
uploaderLabel = new JLabel("Uploader:");
fileNameField = new JTextField();
fileNameLabel = new JLabel("Filename:");
imagePathField = new JTextField();
imageChooseButton = new JButton("Choose Image");
songTable = new JTable();
chooseAudioDirectoryButton = new JButton("Choose Audio Directory");
applyChangesButton = new JButton("Apply Changes");
artIconLabel = new JLabel();
searchField = new JTextField();
listenButton = new JButton("Listen");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(700, 550);
initalizeListeners();
initializeTable();
setupLayout();
listenButton.setEnabled(false);
this.add(mainPanel);
this.setVisible(true);
searchField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
String searchTerm = searchField.getText();
clearSongTable();
for (File f : songList) {
if (f.getName().toLowerCase().contains(searchTerm.toLowerCase())) {
addSongTable(f);
}
}
}
});
}
private void initializeTable() {
if (songTable == null) {
songTable = new JTable();
}
songTable.setModel(new DefaultTableModel(null, new String[]{"Title", "Artist", "Filename", "Filepath"}));
// Keep full path available internally but not visible in the UI.
songTable.removeColumn(songTable.getColumnModel().getColumn(COLUMN_FILE_PATH));
songTable.getTableHeader().setReorderingAllowed(false);
}
private void clearSongTable() {
DefaultTableModel dtm = (DefaultTableModel) songTable.getModel();
dtm.setRowCount(0);
}
private void addSongTable(File audioFile) {
try {
AudioFile f = AudioFileIO.read(audioFile);
Tag tag = f.getTag();
DefaultTableModel model = (DefaultTableModel) songTable.getModel();
model.addRow(new Object[]{
tag.getFirst(FieldKey.TITLE),
tag.getFirst(FieldKey.ARTIST),
audioFile.getName(),
audioFile.getAbsolutePath()
});
} catch (Exception e) {
UI.Modal.showError("Error while adding song to table, wasn't able to read file: " + audioFile.getAbsolutePath());
}
}
private String getPathForRow(int row) {
return songTable.getModel().getValueAt(row, COLUMN_FILE_PATH).toString();
}
private String getFileExtension(String fileName) {
int lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex == -1) {
return "";
}
return fileName.substring(lastDotIndex);
}
private void populateFields(File audioFile) {
try {
AudioFile f = AudioFileIO.read(audioFile);
Tag tag = f.getTag();
titleField.setText(tag.getFirst(FieldKey.TITLE));
uploaderField.setText(tag.getFirst(FieldKey.ARTIST));
fileNameField.setText(audioFile.getName());
Artwork albumArt = tag.getFirstArtwork();
ImageIcon albumArtIcon = new ImageIcon(resizeImage(albumArt.getImage(), 320, 180));
artIconLabel.setIcon(albumArtIcon);
artIconLabel.setText("");
listenButton.setEnabled(true);
} catch (Exception e) {
UI.Modal.showError("Error while populating fields, wasn't able to read file: " + audioFile.getAbsolutePath());
}
}
private BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}
private void playMP3(String filepath) {
try {
File file = new File(filepath);
Desktop.getDesktop().open(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void populateSongList(){
songList = FileUtility.getMp3FilesAsList(setDirPath); //get arraylist of all files in the directory
for (File file : songList) {
addSongTable(file);
}
}
private void setupLayout() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
// Title row
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
mainPanel.add(titleLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
mainPanel.add(titleField, gbc);
// Uploader row
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(uploaderLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
mainPanel.add(uploaderField, gbc);
// Filename row
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(fileNameLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
mainPanel.add(fileNameField, gbc);
// Image path row
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(new JLabel("Album Art:"), gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
mainPanel.add(imagePathField, gbc);
gbc.gridx = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
mainPanel.add(imageChooseButton, gbc);
// Art icon
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridheight = 4;
mainPanel.add(artIconLabel, gbc);
// Search field
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridheight = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(searchField, gbc);
// Song table
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 4;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
JScrollPane scrollPane = new JScrollPane(songTable);
mainPanel.add(scrollPane, gbc);
// Bottom buttons
gbc.gridy = 6;
gbc.gridwidth = 1;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(chooseAudioDirectoryButton, gbc);
gbc.gridx = 1;
mainPanel.add(listenButton, gbc);
gbc.gridx = 2;
gbc.gridwidth = 2;
mainPanel.add(applyChangesButton, gbc);
}
private void initalizeListeners() {
chooseAudioDirectoryButton.addActionListener(e -> {
clearSongTable();
setDirPath = UI.Modal.showDirectoryChooser();
populateSongList();
});
songTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
populateFields(new File(getPathForRow(songTable.getSelectedRow())));
currPath = getPathForRow(songTable.getSelectedRow());
imageSelected = false;
}
});
songTable.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
try {
populateFields(new File(getPathForRow(songTable.getSelectedRow() + 1)));
currPath = getPathForRow(songTable.getSelectedRow() + 1);
imageSelected = false;
} catch (Exception ex) {
UI.Modal.showError("Seems that we aren't able to move down a row for some reason...");
}
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
try {
populateFields(new File(getPathForRow(songTable.getSelectedRow() - 1)));
currPath = getPathForRow(songTable.getSelectedRow() - 1);
imageSelected = false;
} catch (Exception ex) {
UI.Modal.showError("Seems that we aren't able to move up a row for some reason...");
}
}
}
});
applyChangesButton.addActionListener(e -> {
try {
int selectedRow = songTable.getSelectedRow();
if (selectedRow == -1 || currPath.isEmpty()) {
UI.Modal.showError("Please select a song first.");
return;
}
DefaultTableModel model = (DefaultTableModel) songTable.getModel();
String editedFileName = fileNameField.getText().trim();
if (editedFileName.isEmpty()) {
editedFileName = model.getValueAt(selectedRow, COLUMN_FILE_NAME).toString().trim();
}
if (editedFileName.isEmpty() || editedFileName.contains("/") || editedFileName.contains("\\")) {
UI.Modal.showError("Invalid file name.");
return;
}
File currentFile = new File(currPath);
String currentExtension = getFileExtension(currentFile.getName());
if (!currentExtension.isEmpty() && !editedFileName.toLowerCase().endsWith(currentExtension.toLowerCase())) {
editedFileName += currentExtension;
}
File targetFile = new File(currentFile.getParentFile(), editedFileName);
if (!currentFile.getName().equals(editedFileName)) {
if (targetFile.exists()) {
UI.Modal.showError("A file with that name already exists.");
return;
}
if (!currentFile.renameTo(targetFile)) {
UI.Modal.showError("Failed to rename file.");
return;
}
currPath = targetFile.getAbsolutePath();
model.setValueAt(targetFile.getName(), selectedRow, COLUMN_FILE_NAME);
fileNameField.setText(targetFile.getName());
}
AudioFile f = AudioFileIO.read(new File(currPath));
Tag tag = f.getTag();
tag.setField(FieldKey.TITLE, titleField.getText());
tag.setField(FieldKey.ARTIST, uploaderField.getText());
if (imageSelected) {
tag.deleteArtworkField();
Artwork cover = Artwork.createArtworkFromFile(new File(selectedAlbumArt));
tag.addField(cover);
System.out.println("Changed the Artwork");
}
f.commit();
clearSongTable();
songList = FileUtility.getMp3FilesAsList(setDirPath); //get arraylist of all files in the directory
for (File file : songList) {
addSongTable(file);
}
} catch (Exception ex) {
ex.printStackTrace();
}
});
imageChooseButton.addActionListener(e -> {
try {
selectedAlbumArt = UI.Modal.showImageFileChooser();
if(selectedAlbumArt == null){
return;
}
File selectedFile = new File(selectedAlbumArt);
BufferedImage selectedImage;
selectedImage = ImageIO.read(selectedFile);
ImageIcon albumArtIcon = new ImageIcon(resizeImage(selectedImage, 260, 180));
artIconLabel.setText(selectedFile.getName());
artIconLabel.setIcon(albumArtIcon);
imageSelected = true;
} catch (Exception ex) {
UI.Modal.showError("Error while selecting image");
}
});
listenButton.addActionListener(e -> {
playMP3(currPath);
});
}
}
|