Skip to content

Commit 5d68f48

Browse files
committed
Added GUI
1 parent 9733e7c commit 5d68f48

3 files changed

Lines changed: 128 additions & 15 deletions

File tree

src/LiveSplitAddRun/Content.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package LiveSplitAddRun;
2+
3+
import javax.swing.*;
4+
import javax.swing.filechooser.*;
5+
import java.awt.*;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
import java.io.File;
9+
10+
public class Content extends JPanel implements ActionListener {
11+
12+
public static File lssFile;
13+
public static File txtFile;
14+
private static final JTextArea lssText = new JTextArea();
15+
private static final JTextArea txtText = new JTextArea();
16+
private static final JTextArea outputText = new JTextArea("Waiting...");
17+
18+
19+
public Content() {
20+
this.setFocusable(true);
21+
this.setSize(300, 300);
22+
this.setLayout(new GridLayout(3, 1));
23+
24+
JButton lssButton = new JButton("Pick .lss file");
25+
lssButton.setActionCommand("lss");
26+
lssButton.addActionListener(this);
27+
this.add(lssButton);
28+
lssText.setLineWrap(true);
29+
this.add(lssText);
30+
31+
JButton txtButton = new JButton("Pick times file");
32+
txtButton.setActionCommand("txt");
33+
txtButton.addActionListener(this);
34+
this.add(txtButton);
35+
txtText.setLineWrap(true);
36+
this.add(txtText);
37+
38+
JButton execute = new JButton("Generate File");
39+
execute.setActionCommand("exe");
40+
execute.addActionListener(this);
41+
this.add(execute);
42+
outputText.setLineWrap(true);
43+
this.add(outputText);
44+
}
45+
46+
@Override
47+
public void actionPerformed(ActionEvent e) {
48+
if (e.getActionCommand().equals("exe")) {
49+
try {
50+
outputText.setText(Main.convert(lssFile.getPath(), Main.getTimes(txtFile)));
51+
}
52+
catch (Exception ex) {
53+
outputText.setText("There was an error converting the files.\n" + ex.getMessage());
54+
}
55+
return;
56+
}
57+
58+
boolean isSplits = e.getActionCommand().equals("lss");
59+
JFileChooser chooser = new JFileChooser();
60+
chooser.setFileFilter(isSplits ? new LssOnly() : new TxtOnly());
61+
62+
int result = chooser.showOpenDialog(this);
63+
if (result == JFileChooser.APPROVE_OPTION) {
64+
if (isSplits) lssFile = chooser.getSelectedFile();
65+
else txtFile = chooser.getSelectedFile();
66+
}
67+
updateTexts();
68+
}
69+
70+
private static void updateTexts() {
71+
if (lssFile != null) lssText.setText(lssFile.getName());
72+
if (txtFile != null) txtText.setText(txtFile.getName());
73+
}
74+
75+
private static class LssOnly extends FileFilter {
76+
77+
@Override
78+
public boolean accept(File f) {
79+
return f.isDirectory() || f.getName().toLowerCase().endsWith(".lss");
80+
}
81+
82+
@Override
83+
public String getDescription() {
84+
return ".lss files";
85+
}
86+
}
87+
88+
private static class TxtOnly extends FileFilter {
89+
90+
@Override
91+
public boolean accept(File f) {
92+
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
93+
}
94+
95+
@Override
96+
public String getDescription() {
97+
return ".txt files";
98+
}
99+
}
100+
101+
}

src/LiveSplitAddRun/Main.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class Main {
1515

16-
public static void convert(String fileName, long[] times) throws IOException {
16+
public static String convert(String fileName, long[] times) throws IOException {
1717
ArrayList<String> contents = new ArrayList<>(times.length * 5);
1818
Scanner s = new Scanner(new File(fileName));
1919
while (s.hasNext()) {
@@ -152,7 +152,7 @@ public static void convert(String fileName, long[] times) throws IOException {
152152
}
153153
writer.close();
154154

155-
System.out.println("Created file at: " + name);
155+
return "Created file at: " + name;
156156
}
157157

158158
private static boolean attemptStarter(String line) {
@@ -204,8 +204,7 @@ private static long getCurrentPB(ArrayList<String> lines) {
204204
throw new IllegalStateException("WTF???");
205205
}
206206

207-
private static long[] getTimes(String path) throws FileNotFoundException {
208-
File f = new File(path);
207+
public static long[] getTimes(File f) throws FileNotFoundException {
209208
Queue<Long> times = new LinkedList<>();
210209
Scanner s = new Scanner(f);
211210

@@ -221,18 +220,11 @@ private static long[] getTimes(String path) throws FileNotFoundException {
221220
}
222221
return ret;
223222
}
223+
public static long[] getTimes(String path) throws FileNotFoundException {
224+
return getTimes(new File(path));
225+
}
224226

225227
public static void main(String[] args) {
226-
try {
227-
if (args.length == 2) {
228-
long[] times = getTimes(args[0]);
229-
convert(args[1], times);
230-
}
231-
else {
232-
convert("src/LiveSplitAddRun/SPLITS.lss", getTimes("src/LiveSplitAddRun/TIMES.txt"));
233-
}
234-
} catch (IOException e) {
235-
throw new RuntimeException(e);
236-
}
228+
new UI();
237229
}
238230
}

src/LiveSplitAddRun/UI.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package LiveSplitAddRun;
2+
3+
import javax.swing.JFrame;
4+
import java.awt.Dimension;
5+
6+
public class UI extends JFrame {
7+
8+
public UI() {
9+
Content c = new Content();
10+
this.setPreferredSize(new Dimension(800, 400));
11+
this.setContentPane(c);
12+
pack();
13+
14+
this.setTitle("Livesplit Run Adder");
15+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16+
this.setLocationRelativeTo(null);
17+
this.setVisible(true);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)