diff options
| -rw-r--r-- | src/main/java/TagEditorScreen.java | 109 |
1 files changed, 93 insertions, 16 deletions
diff --git a/src/main/java/TagEditorScreen.java b/src/main/java/TagEditorScreen.java index 39d1542..2bf66c8 100644 --- a/src/main/java/TagEditorScreen.java +++ b/src/main/java/TagEditorScreen.java @@ -15,11 +15,18 @@ 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; @@ -42,6 +49,8 @@ public class TagEditorScreen extends JFrame { 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(); @@ -81,8 +90,9 @@ public class TagEditorScreen extends JFrame { if (songTable == null) { songTable = new JTable(); } - songTable.setDefaultEditor(Object.class, null); - songTable.setModel(new DefaultTableModel(null, new String[]{"Title", "Artist", "Filepath"})); + 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); } @@ -96,18 +106,36 @@ public class TagEditorScreen extends JFrame { 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.getAbsolutePath()}); + 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); @@ -174,12 +202,26 @@ public class TagEditorScreen extends JFrame { gbc.weightx = 1.0; mainPanel.add(uploaderField, gbc); - // Image path row + // 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; @@ -195,12 +237,12 @@ public class TagEditorScreen extends JFrame { // Art icon gbc.gridx = 3; gbc.gridy = 0; - gbc.gridheight = 3; + gbc.gridheight = 4; mainPanel.add(artIconLabel, gbc); // Search field gbc.gridx = 0; - gbc.gridy = 3; + gbc.gridy = 4; gbc.gridheight = 1; gbc.gridwidth = 3; gbc.fill = GridBagConstraints.HORIZONTAL; @@ -208,7 +250,7 @@ public class TagEditorScreen extends JFrame { // Song table gbc.gridx = 0; - gbc.gridy = 4; + gbc.gridy = 5; gbc.gridwidth = 4; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; @@ -216,7 +258,7 @@ public class TagEditorScreen extends JFrame { mainPanel.add(scrollPane, gbc); // Bottom buttons - gbc.gridy = 5; + gbc.gridy = 6; gbc.gridwidth = 1; gbc.weighty = 0; gbc.fill = GridBagConstraints.HORIZONTAL; @@ -241,8 +283,8 @@ public class TagEditorScreen extends JFrame { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); - populateFields(new File(songTable.getModel().getValueAt(songTable.getSelectedRow(), 2).toString())); - currPath = songTable.getModel().getValueAt(songTable.getSelectedRow(), 2).toString(); + populateFields(new File(getPathForRow(songTable.getSelectedRow()))); + currPath = getPathForRow(songTable.getSelectedRow()); imageSelected = false; } }); @@ -253,16 +295,16 @@ public class TagEditorScreen extends JFrame { super.keyPressed(e); if (e.getKeyCode() == KeyEvent.VK_DOWN) { try { - populateFields(new File(songTable.getModel().getValueAt(songTable.getSelectedRow() + 1, 2).toString())); - currPath = songTable.getModel().getValueAt(songTable.getSelectedRow() + 1, 2).toString(); + 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(songTable.getModel().getValueAt(songTable.getSelectedRow() - 1, 2).toString())); - currPath = songTable.getModel().getValueAt(songTable.getSelectedRow() - 1, 2).toString(); + 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..."); @@ -272,7 +314,43 @@ public class TagEditorScreen extends JFrame { }); applyChangesButton.addActionListener(e -> { try { - System.out.println("CURRENT PATH " + currPath); + 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()); @@ -314,7 +392,6 @@ public class TagEditorScreen extends JFrame { }); listenButton.addActionListener(e -> { - System.out.println("CURRENT PATH " + currPath); playMP3(currPath); |
