-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApplication.java
139 lines (121 loc) · 4.36 KB
/
Application.java
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
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class Application extends JFrame {
private static final long serialVersionUID = 1L;
handler h = new handler();
FileDialog fd = new FileDialog(this);
JTextField bupPathField = new JTextField(20);
JTextField midiPathField = new JTextField(20);
JButton convertButton = new JButton("Convert");
JButton bupPathBtn = new JButton("...");
JButton midiPathBtn = new JButton("...");
JPanel panel = new JPanel();
JPanel textFieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JProgressBar bar = new JProgressBar();
String[] fdDir = { "C:\\", "C:\\" };
File inputFile;
File outputFile;
public Application() {
JLabel bupPathLabel = new JLabel("Input:", JLabel.TRAILING);
JLabel midiPathLabel = new JLabel("Output:", JLabel.TRAILING);
bupPathBtn.addActionListener(h);
midiPathBtn.addActionListener(h);
textFieldPanel.setLayout(new SpringLayout());
bupPathLabel.setLabelFor(bupPathField);
midiPathLabel.setLabelFor(midiPathField);
textFieldPanel.add(bupPathLabel);
textFieldPanel.add(bupPathField);
textFieldPanel.add(bupPathBtn);
textFieldPanel.add(midiPathLabel);
textFieldPanel.add(midiPathField);
textFieldPanel.add(midiPathBtn);
SpringUtilities.makeCompactGrid(textFieldPanel, 2, 3, 5, 5, 5, 5);
convertButton.addActionListener(h);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(convertButton);
bar.setPreferredSize(new Dimension(0, 20));
bar.setStringPainted(true);
bar.setString("");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
panel.add(textFieldPanel);
panel.add(bar);
panel.add(buttonPanel);
this.setTitle("BUP-Converter");
this.setContentPane(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
public void chooseFile(boolean inorout) {
fd.setTitle(inorout ? "Choose an input file..." : "Choose a destination dir and file name beginning...");
fd.setDirectory(fdDir[inorout ? 0 : 1]);
fd.setFile("");
fd.setVisible(true);
String path = fd.getDirectory();
String fileName = fd.getFile();
String extension = "";
try {
extension = fileName.substring(fileName.lastIndexOf(".") + 1);
} catch (Exception ex) {
}
if (fileName != null) {
if (inorout) { // Input
if (extension.toLowerCase().equals("bup")) {
inputFile = new File(path + "\\" + fileName);
outputFile = new File(path + "\\" + fileName.substring(0, fileName.lastIndexOf(".")));
bupPathField.setText(inputFile.getAbsolutePath());
midiPathField.setText(outputFile.getAbsolutePath());
} else {
JOptionPane.showMessageDialog(this, "You can only choose '.bup'-Files!");
}
fdDir[0] = fd.getDirectory();
} else { // Output
if (!fileName.contains(".")) {
outputFile = new File(path + "\\" + fileName);
midiPathField.setText(outputFile.getAbsolutePath());
} else {
JOptionPane.showMessageDialog(this, "You can only choose directories!");
}
fdDir[1] = fd.getDirectory();
}
}
}
public class handler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bupPathBtn || e.getSource() == midiPathBtn) {
chooseFile(e.getSource() == bupPathBtn);
}
if (e.getSource() == convertButton) {
if (inputFile.exists() && !(outputFile.isDirectory()) && outputFile.getParentFile().exists()) {
new Convert(bar, inputFile, outputFile).execute();
}
}
}
}
}