c5abccad
pfrison
PercTeacher inter...
|
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
|
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ImportExport {
public static void exportWithDialog(ActionList list) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Array data", "adata");
chooser.setFileFilter(filter);
chooser.setApproveButtonText("Exporter");
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
export(list, chooser.getSelectedFile().getPath());
}
private static void export(ActionList list, String path) {
String str = list.getLeftDeltaArray() + "\n" + list.getRightDeltaArray() + "\n" + list.getLeftDelaiArray() + "\n" + list.getRightDelaiArray();
if(!path.endsWith(".adata"))
path += ".adata";
BufferedWriter bufferedWriter = null;
try {
File file = new File(path);
if (!file.exists())
file.createNewFile();
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(str);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ActionList importsWithDialog() {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Array data", "adata");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(false);
chooser.setApproveButtonText("Importer");
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().exists() && chooser.getSelectedFile().isFile())
return imports(chooser.getSelectedFile().getPath());
if(returnVal == JFileChooser.APPROVE_OPTION)
JOptionPane.showMessageDialog(null, "You have to choose an exisiting file", "Import error", JOptionPane.ERROR_MESSAGE);
return null;
}
private static ActionList imports(String path) {
try {
byte[] encoded = Files.readAllBytes(Paths.get(path));
String str = new String(encoded, Charset.defaultCharset());
long[][] longList = Util.getArraysFromImport(str);
if(longList != null)
return new ActionList(longList[0], longList[1], longList[2], longList[3]);
JOptionPane.showMessageDialog(null, "The choosen file is invalid or currupted", "Import error", JOptionPane.ERROR_MESSAGE);
} catch (IOException ignored) {}
return null;
}
}
|