aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/DownloadConfigPane.java
blob: 058cc077546c8c9281840e315219f357ccdacad3 (plain) (blame)
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
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() {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(900, 500);
        initializeTable();
        this.add(mainPanel);
        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);
    }


}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage