View Javadoc

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  				//filename = getExportFileName(props, window);
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 	public static void exportTestLabel (Properties props, MainWindow window)  {
225 		
226 		
227 		List dictMergeList = DictionaryUtility.getDictionaryNameList(props.getProperty(LTDTKeys.DICTIONARIES));
228 		
229 		if (window.editorPane.jta.getText().trim().length() >0 && 
230 				(dictMergeList.size()>0 || window.WDD.getDefinitions().size()>0)) {
231 		
232 			try {
233 				
234 				filename = getExportFileName(props, window);
235 				
236 				if (filename !=null) {
237 					
238 					boolean started = false; // change to true once the first dictionary is parsed
239 					                         // the rest will be treated as merge
240 				
241 					Dictionary vDictionary = null;
242 					
243 					for (int i=0; i<dictMergeList.size(); i++) {
244 						if (!started) {
245 							vDictionary = DictionaryParser.parse(Utility.toURL((String)dictMergeList.get(i)));
246 							started = true;
247 						}
248 						else {
249 							vDictionary.merge(DictionaryParser.parse(Utility.toURL((String)dictMergeList.get(i))), true);
250 
251 						}
252 					
253 					}
254 					
255 					// add wdd as the last dictionary if any wdd entry is around
256 					if (window.WDD.getDefinitions().size()>0) {
257 						if (started)
258 							vDictionary.merge(window.WDD, true);	
259 						else {
260 							vDictionary = window.WDD;
261 							started=true;
262 						}
263 					}
264 					
265 					InputStream instream = new ByteArrayInputStream ( window.editorPane.jta.getText().toString().getBytes());
266 					TestLabelGenerator testLabel = new TestLabelGenerator();
267 					testLabel.createTestLabel(instream, vDictionary, new FileOutputStream(filename));
268 				}
269 			}
270 			catch (IOException ie) { 
271 				JOptionPane.showMessageDialog (window, "IO Exception: \n\n"+ie.getMessage()); 
272 				return;
273 			}
274 			catch (ParseException pe) {
275 				JOptionPane.showMessageDialog(window, "Parse Exception: \n\n"+pe.getMessage()); 
276 				return; 
277 			}
278 			catch (InvalidTypeException it) {
279 				JOptionPane.showMessageDialog(window, "InvalidType Exception: \n\n"+it.getMessage()); 
280 				
281 				return; 
282 			}
283 			catch (UnsupportedTypeException ut) {
284 				JOptionPane.showMessageDialog(window, "UnsupportedType Exception: \n\n"+ut.getMessage()); 
285 				return; 
286 			}
287 			catch (UnknownDefinitionException ud) {
288 				JOptionPane.showMessageDialog(window, "UnknownDefinition Exception: \n\n"+ud.getMessage()	);
289 				return;
290 			}
291 		
292 			return;
293 		}
294 		
295 		String message="";
296 		
297 		if (window.editorPane.jta.getText().trim().length()==0)
298 			message = message+ "\nThe template content is empty.\n"; 
299 					
300 		if (dictMergeList.size()==0 && window.WDD.getDefinitions().size()==0)
301 			message = message +  
302 					"\nNo data dictionary information. "+
303 					"Please import data dictionary by choosing File->Import \n"+
304 					"or add WDD entries by choosing Dictionary->Add";
305 				
306 		JOptionPane.showMessageDialog(
307 				window, 
308 				"Can not export test label due to the followings:\n"+message);
309 		return;
310 	}
311 	*/
312 	
313 	
314 }