From 9c86f5cf3c1f8f3f824ff5a081a189ad9ec57838 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Thu, 20 Apr 2023 15:21:19 -0700 Subject: Added new window for interactively setting download songs --- src/main/java/DownloadConfigPane.form | 115 +++++++++++++++++++++ src/main/java/DownloadConfigPane.java | 183 ++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 39 ++++++-- 3 files changed, 331 insertions(+), 6 deletions(-) create mode 100644 src/main/java/DownloadConfigPane.form create mode 100644 src/main/java/DownloadConfigPane.java diff --git a/src/main/java/DownloadConfigPane.form b/src/main/java/DownloadConfigPane.form new file mode 100644 index 0000000..324f738 --- /dev/null +++ b/src/main/java/DownloadConfigPane.form @@ -0,0 +1,115 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/DownloadConfigPane.java b/src/main/java/DownloadConfigPane.java new file mode 100644 index 0000000..9d6a3b9 --- /dev/null +++ b/src/main/java/DownloadConfigPane.java @@ -0,0 +1,183 @@ +import javax.swing.*; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.DefaultTableModel; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.nio.file.Files; + +public class DownloadConfigPane extends JFrame{ + private JPanel mainPanel; + private JTable outputTable; + private JTextField urlField; + private JTextField fromField; + private JTextField toField; + private JButton loadFromFileButton; + private JLabel fromLabel; + private JLabel toLabel; + private JLabel urlLabel; + private JButton addButton; + private JButton saveButton; + private JButton removeButton; + private JScrollPane tableScrollPane; + + private String loadedPath; + + public DownloadConfigPane() { + // initialize intellijlaf + this.add(mainPanel); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + this.setLocationRelativeTo(null); + this.setSize(900, 500); + initializeTable(); + this.setVisible(true); + loadFromFileButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + loadConfigFromFile(); + } + }); + saveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + } + }); + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String url = urlField.getText(); + String from = fromField.getText(); + String to = toField.getText(); + if (url.length() == 0){ + return; + } + if (from.length() != 8){ + from = "00:00:00"; + } + if (to.length() != 8){ + to = "00:00:00"; + } + Object[] song = new Object[]{url, from, to}; + DefaultTableModel model = (DefaultTableModel) outputTable.getModel(); + model.addRow(song); + } + }); + removeButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int row = outputTable.getSelectedRow(); + if (row == -1){ + return; + } + DefaultTableModel model = (DefaultTableModel) outputTable.getModel(); + model.removeRow(row); + } + }); + saveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + saveConfigToFile(); + } + }); + } + + private void initializeTable() { + DefaultTableModel model = new DefaultTableModel(); + // center align the text in the table + + DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); + centerRenderer.setHorizontalAlignment( JLabel.CENTER ); + outputTable.setDefaultRenderer(String.class, centerRenderer); + model.addColumn("URL"); + model.addColumn("From"); + model.addColumn("To"); + outputTable.setModel(model); + outputTable.getTableHeader().setReorderingAllowed(false); + } + + private void clearTable(){ + DefaultTableModel model = (DefaultTableModel) outputTable.getModel(); + model.setRowCount(0); + } + + private File selectTextFileChooser(){ + JFileChooser chooser = new JFileChooser(); + chooser.setCurrentDirectory(new File(System.getProperty("user.home"))); + int result = chooser.showOpenDialog(this); + if (result == JFileChooser.APPROVE_OPTION) { + File selectedFile = chooser.getSelectedFile(); + System.out.println("Selected file: " + selectedFile.getAbsolutePath()); + return selectedFile; + } + return null; + } + + private void loadConfigFromFile(){ + File file = selectTextFileChooser(); + if (file == null){ + return; + } + clearTable(); + initializeTable(); + try{ + for (String line : Files.readAllLines(file.toPath())) { + String[] split = line.split(","); + String url = split[0]; + String[] timeRange = new String[2]; + if (split.length == 2){ + timeRange = split[1].split("-"); + } + else{ + timeRange[0] = "00:00:00"; + timeRange[1] = "00:00:00"; + } + String from = timeRange[0]; + String to = timeRange[1]; + if (from.length() != 8){ + from = "00:00:00"; + } + if (to.length() != 8){ + to = "00:00:00"; + } + Object[] song = new Object[]{url, from, to}; + DefaultTableModel model = (DefaultTableModel) outputTable.getModel(); + model.addRow(song); + // add headers to the table + + + + } + loadedPath = file.getAbsolutePath(); + } + catch (Exception e){ + e.printStackTrace(); + JOptionPane.showMessageDialog(null, "Please choose a file with the download config format"); + System.out.println("Invalid file selected"); + loadedPath = null; + } + + } + + private void saveConfigToFile(){ + if(loadedPath == null){ + System.out.println("No file loaded"); + return; + } + File saveFile = new File(loadedPath); + if(!saveFile.exists()){ + System.out.println("File does not exist"); + return; + } + for (int i = 0; i < outputTable.getRowCount(); i++) { + String url = (String) outputTable.getValueAt(i, 0); + String from = (String) outputTable.getValueAt(i, 1); + String to = (String) outputTable.getValueAt(i, 2); + String line = url + "," + from + "-" + to; + System.out.println(line); + } + JOptionPane.showConfirmDialog(null, "Saved to " + loadedPath, "Saved", JOptionPane.DEFAULT_OPTION); + } + + +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 0281001..ed04835 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -6,12 +6,9 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.Scanner; -import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatIntelliJLaf; -import com.formdev.flatlaf.FlatLightLaf; -import com.formdev.flatlaf.themes.FlatMacDarkLaf; -import com.formdev.flatlaf.themes.FlatMacLightLaf; import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.tag.FieldKey; @@ -37,6 +34,7 @@ public class Main extends JFrame { JButton songsGen = new JButton("Generate text file"); JButton editButton = new JButton("Edit Tags"); JButton startButton = new JButton("Set .txt File"); + JButton configureDownloadButton = new JButton("Configure Download File Interactively"); JCheckBox defaultFileBox = new JCheckBox("Use Default songs.txt file"); JCheckBox useBlacklistBox = new JCheckBox("Use Blacklist.txt"); JProgressBar progressBar = new JProgressBar(); @@ -86,7 +84,6 @@ public class Main extends JFrame { return; } - String info[] = fileUtil.parseInfoJSON(fileUtil.jsonToString(fileUtil.findJsonFile(DOWNLOADED_DIR))); //title,uploader String uploader = info[1]; String title = info[0]; @@ -255,8 +252,14 @@ public class Main extends JFrame { panel.add(scrollPane); panel.add(Box.createVerticalStrut(5)); panel.add(editButton); + panel.add(Box.createVerticalStrut(5)); panel.add(useBlacklistBox); panel.add(Box.createVerticalStrut(8)); + outputArea.setEditable(false); + // make configureDownloadButton centered + configureDownloadButton.setAlignmentX(Component.CENTER_ALIGNMENT); + panel.add(configureDownloadButton); + this.setSize(550, 450); this.setTitle("YTMP3Tagger"); @@ -276,13 +279,17 @@ public class Main extends JFrame { showWarning("Default File has been set.\nMake sure you add a new line for each URL"); readyState = true; startButton.setText("Start Download"); - outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button"); + outputArea.setText("Configuration Loaded:\n"); + writeFileContentsToOutputArea(textPath); + outputArea.setText(outputArea.getText() + "\n\n\n" + "Ready to begin downloading. Press the button"); System.out.println("Ready to begin downloading. Press the button"); useDefault = true; } else { useDefault = false; readyState = false; + outputArea.setText(outputArea.getText() + "\n" + "Cancelled. Please set a .txt file"); + System.out.println("Cancelled. Please set a .txt file"); startButton.setText("Set .txt file"); } } @@ -311,6 +318,7 @@ public class Main extends JFrame { if (!textPath.equals("")) { showWarning("File has been set.\nMake sure you add a new line for each URL"); readyState = true; + writeFileContentsToOutputArea(textPath); startButton.setText("Start Download"); outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button"); System.out.println("Ready to begin downloading. Press the button"); @@ -337,8 +345,27 @@ public class Main extends JFrame { new TagEditorScreen().setVisible(true); } }); + configureDownloadButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + new DownloadConfigPane(); + } + }); } + private void writeFileContentsToOutputArea(String path){ + File file = new File(path); + try { + Scanner scanner = new Scanner(file); + while (scanner.hasNextLine()){ + String line = scanner.nextLine(); + outputArea.setText(outputArea.getText() + "\n" + line); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + } /** * Convert mp4 to mp3 using ffmpeg * @param mp4File The mp4 file to convert -- cgit v1.2.3