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
|
package NET.worlds.console;
import java.awt.Frame;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileSysDialog implements Runnable {
public static final int OPEN = 0;
public static final int SAVE = 1;
private String title;
private String typesAndExts;
private String fileName;
private static File lastFile;
private int mode;
private DialogReceiver receiver;
private boolean parentDisabled = false;
private Frame parent;
JFileChooser fc = null;
private static String OpenFileText = "Open File";
private static String SaveFileText = "Save File";
private boolean disableParent;
public FileSysDialog(Frame parent, DialogReceiver receiver, String title, int mode, String typesAndExts, String fileName, boolean disableParent) {
this.receiver = receiver;
this.title = title;
this.typesAndExts = typesAndExts;
this.mode = mode;
this.fileName = fileName;
this.parent = parent;
this.disableParent = disableParent;
SwingUtilities.invokeLater(this);
}
@Override
public void run() {
if (this.disableParent && this.parent.isEnabled()) {
this.parent.setEnabled(false);
this.parentDisabled = true;
}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException var3) {
var3.printStackTrace();
return;
} catch (InstantiationException var4) {
var4.printStackTrace();
return;
} catch (IllegalAccessException var5) {
var5.printStackTrace();
return;
} catch (UnsupportedLookAndFeelException var6) {
var6.printStackTrace();
return;
}
File file = null;
try {
if (this.fileName != null && this.fileName != "") {
file = new File(this.fileName);
if (file != null && file.getParent() != null) {
this.fc = new JFileChooser(file.getParent());
}
}
if (this.fc == null && lastFile != null && lastFile.getParent() != null) {
this.fc = new JFileChooser(lastFile.getParent());
}
if (this.fc == null) {
this.fc = new JFileChooser(".");
}
} catch (Exception var7) {
var7.printStackTrace();
return;
}
this.fc.setFileSelectionMode(0);
this.fc.setDialogType(this.mode == 0 ? 0 : 1);
if (this.title != null) {
this.fc.setDialogTitle(this.title);
}
if (this.typesAndExts != null) {
buildFileChooser(this.fc, this.typesAndExts);
}
if (file != null) {
this.fc.setSelectedFile(file);
}
int retval = this.fc.showDialog(null, this.mode == 0 ? OpenFileText : SaveFileText);
if (retval == 0) {
this.approveSelection();
} else {
this.cancelSelection();
}
if (this.disableParent && this.parentDisabled) {
this.parent.setEnabled(true);
this.parentDisabled = false;
}
}
private static void buildFileChooser(JFileChooser fc2, String specString) {
String[] entries = specString.split("\\|");
boolean even = false;
String desc = "";
FileNameExtensionFilter first = null;
for (String entry : entries) {
if (!even) {
desc = entry;
even = true;
} else {
String[] fields = entry.split(";");
for (int i = 0; i < fields.length; i++) {
String[] ext = fields[i].split("\\.");
if (ext.length > 0) {
fields[i] = ext[ext.length - 1];
}
}
FileNameExtensionFilter ff = null;
switch (fields.length) {
case 1:
ff = new FileNameExtensionFilter(desc, fields[0]);
break;
case 2:
ff = new FileNameExtensionFilter(desc, fields[0], fields[1]);
break;
case 3:
ff = new FileNameExtensionFilter(desc, fields[0], fields[1], fields[2]);
break;
case 4:
ff = new FileNameExtensionFilter(desc, fields[0], fields[1], fields[2], fields[3]);
}
if (ff != null) {
fc2.addChoosableFileFilter(ff);
if (first == null) {
first = ff;
}
}
even = false;
}
}
if (first != null) {
fc2.setFileFilter(first);
}
}
public int getMode() {
return this.mode;
}
public void approveSelection() {
File file = this.fc.getSelectedFile();
lastFile = file;
this.fileName = file.getAbsolutePath();
this.receiver.dialogDone(this, this.fileName != null);
}
public void cancelSelection() {
this.receiver.dialogDone(this, false);
}
public String fileName() {
return this.fileName;
}
}
|