-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportExportFileDialog.java
More file actions
115 lines (108 loc) · 2.26 KB
/
ImportExportFileDialog.java
File metadata and controls
115 lines (108 loc) · 2.26 KB
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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
class ImportExportFileDialog
implements ImportExportDialog
{
CirSim cframe;
private static String circuitDump;
Action type;
private static String directory = ".";
ImportExportFileDialog(CirSim f, Action type)
{
if ( directory.equals(".") )
{
File file = new File("circuits");
if ( file.isDirectory() )
directory = "circuits";
}
this.type = type;
cframe = f;
}
public void setDump(String dump)
{
circuitDump = dump;
}
public String getDump()
{
return circuitDump;
}
public void execute()
{
FileDialog fd = new FileDialog(new Frame(),
(type == Action.EXPORT) ? "Save File" :
"Open File",
(type == Action.EXPORT) ? FileDialog.SAVE :
FileDialog.LOAD );
fd.setDirectory(directory);
fd.setVisible(true);
String file = fd.getFile();
String dir = fd.getDirectory();
if ( dir != null )
directory = dir;
if ( file == null )
return;
System.err.println(dir + File.separator + file);
if ( type == Action.EXPORT )
{
try
{
writeFile(dir + file);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
try
{
String dump = readFile(dir + file);
circuitDump = dump;
cframe.readSetup(circuitDump);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
private static String readFile(String path)
throws IOException, FileNotFoundException
{
FileInputStream stream = null;
try
{
stream = new FileInputStream(new File(path));
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY,
0, fc.size());
return Charset.forName("UTF-8").decode(bb).toString();
}
finally
{
stream.close();
}
}
private static void writeFile(String path)
throws IOException, FileNotFoundException
{
FileOutputStream stream = null;
try
{
stream = new FileOutputStream(new File(path));
FileChannel fc = stream.getChannel();
ByteBuffer bb = Charset.forName("UTF-8").encode(circuitDump);
fc.write(bb);
}
finally
{
stream.close();
}
}
}