1 package gov.nasa.pds.ltdt.gui;
2
3 import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4 import gov.nasa.pds.ltdt.gui.util.DictionaryUtility;
5 import gov.nasa.pds.ltdt.gui.util.Utility;
6 import gov.nasa.pds.ltdt.testLabel.TestLabelGenerator;
7 import gov.nasa.pds.tools.dict.Dictionary;
8 import gov.nasa.pds.tools.dict.parser.DictionaryParser;
9 import gov.nasa.pds.tools.dict.parser.UnknownDefinitionException;
10 import gov.nasa.pds.tools.dict.type.InvalidTypeException;
11 import gov.nasa.pds.tools.dict.type.UnsupportedTypeException;
12 import gov.nasa.pds.tools.label.parser.ParseException;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.FileOutputStream;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.List;
22 import java.util.Properties;
23
24 import javax.swing.JFileChooser;
25 import javax.swing.JOptionPane;
26
27 /***
28 * Export (template, WDD, etc) related event handling
29 * @author jwang
30 *
31 */
32 public class Export {
33
34 static private String filename = null;
35 static private JFileChooser jfc = null;
36
37 /***
38 * Export a template from editor pane to a file
39 * @param props Properties
40 * @param window Main Window
41 */
42 public static void exportTemplate (Properties props, MainWindow window) {
43 exportTemplate(props, window, false);
44 }
45
46 /***
47 * Export template with returning flag specified.
48 *
49 * @param props Properties
50 * @param window Main Window
51 * @param returning flag. True if it's revisited because there's a need to keep the file chooser displayed,
52 * i.e. such a condition possibly caused by user specified an existing filename and does not want to overwrite it, or
53 * a directory name was specified instead of a file name.
54 * The default is false, i.e. to create a new file or replace an existing one.
55 */
56 private static void exportTemplate (Properties props, MainWindow window, boolean returning) {
57
58 if (window.editorPane.jta.getText().trim().length() >0) {
59 try {
60
61 getExportFileName (props, window, LTDTKeys.EXPORTTEMPLATE);
62
63 if (filename != null) {
64 Utility.writeTemplate(window, filename);
65 }
66
67 }
68 catch (FileNotFoundException fe) {
69 JOptionPane.showMessageDialog(window, fe.getMessage(), "Template Export Error",
70 JOptionPane.ERROR_MESSAGE);
71 return;
72 }
73 catch (IOException ie) {
74 JOptionPane.showMessageDialog(window, ie.getMessage(), "Template Export Error",
75 JOptionPane.ERROR_MESSAGE);
76 return;
77 }
78
79 return;
80
81 }
82
83 else {
84
85 JOptionPane.showMessageDialog(window, "Template export cannot be performed with a blank template.",
86 "Blank Template", JOptionPane.WARNING_MESSAGE);
87 }
88 return;
89 }
90
91 /***
92 * Export WDD of the current project to a file.
93 * @param props Properties
94 * @param window Main Window
95 */
96 public static void exportWDD (Properties props, MainWindow window) {
97
98 if (window.WDD.getDefinitions().size()>0) {
99 try {
100 getExportFileName(props, window, LTDTKeys.EXPORTWDD);
101
102 if (filename!=null) {
103 DictionaryUtility.writeWDD(window, filename);
104 }
105 }
106 catch (FileNotFoundException fe) {
107 JOptionPane.showMessageDialog (window, "FileNotFound Exception: "+fe.getMessage());
108 return;
109 }
110 catch (IOException ie) {
111 JOptionPane.showMessageDialog (window, "IO Exception: "+ie.getMessage());
112 return;
113 }
114
115 }
116 else {
117 JOptionPane.showMessageDialog (
118 window,
119 "Working Data Dictionary export cannot be performed when the WDD is empty.\n"+
120 "This means that there have been no objects or elements that have been added\n"+
121 "or modified for this project.",
122 "Empty WDD", JOptionPane.WARNING_MESSAGE);
123 }
124 }
125
126
127 private static void getExportFileName(Properties props, MainWindow window, int exportType )
128 throws FileNotFoundException, IOException {
129
130 getExportFileName (props, window, exportType, false);
131
132 }
133
134
135 private static void getExportFileName (Properties props, MainWindow window, int exportType, boolean returning) throws IOException {
136
137 if (returning==false) {
138 try {
139 if ( !(LTDTKeys.UNTITLED).equals(props.getProperty(LTDTKeys.PROJECTNAME))) {
140 jfc = new JFileChooser(new File(Utility.getProjectLocation(props.getProperty(LTDTKeys.PROJECTNAME))));
141 }
142 else jfc = new JFileChooser(new File(System.getProperty("user.dir")));
143
144 }
145 catch (FileNotFoundException fe) {
146 throw new FileNotFoundException("Problem locating export location.");
147 }
148 catch (IOException ie) {
149 throw new IOException ("Problem read or write to target directory during export.");
150 }
151
152 jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
153 }
154
155
156 int result = jfc.showSaveDialog(window);
157
158 switch (result) {
159 case JFileChooser.APPROVE_OPTION:
160 filename = jfc.getSelectedFile().toString().trim();
161
162 if (filename.length()==0) {
163 exportEmptyFilenameWarning(window, exportType);
164 filename = null;
165 getExportFileName (props, window, exportType, true);
166 }
167 else if ( new File(filename).isDirectory()) {
168 exportSelectedDictory (window, exportType);
169 filename = null;
170 getExportFileName (props, window, exportType, true);
171 }
172 else if ( new File(filename).exists()) {
173 if (exportOverwriteWarning (window, 0, filename)!=LTDTKeys.SUCCESS) {
174 filename=null;
175 getExportFileName (props, window, exportType, true);
176 }
177 }
178 break;
179 case JFileChooser.CANCEL_OPTION:
180 default:
181 filename = null;
182 break;
183 }
184 }
185
186 private static void exportSelectedDictory(MainWindow window, int exportType) {
187
188 StringBuffer message = new StringBuffer();
189 message.append(filename + " is a directory. \nPlease select or enter a file name.");
190 JOptionPane.showMessageDialog(window, message.toString(), "Export File Selection Error", JOptionPane.WARNING_MESSAGE);
191 }
192
193 private static void exportEmptyFilenameWarning (MainWindow window, int exportType) {
194
195 String message = "Please select or enter a file name.";
196 JOptionPane.showMessageDialog(window, message, "Missing Export File Name", JOptionPane.WARNING_MESSAGE);
197 }
198
199 private static int exportOverwriteWarning (MainWindow window, int exportType, String filename) {
200
201
202 StringBuffer message = new StringBuffer();
203
204 message.append("File "+filename+" exists. \nDo you want to replace it?");
205
206 int response = JOptionPane.showConfirmDialog(window, message.toString(), "File Exists",
207 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE );
208
209
210 switch (response) {
211
212 case JOptionPane.YES_OPTION:
213 return LTDTKeys.SUCCESS;
214 case JOptionPane.NO_OPTION:
215 case JOptionPane.CANCEL_OPTION:
216 case JOptionPane.CLOSED_OPTION:
217 default:
218 return LTDTKeys.CANCELLED;
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 }