View Javadoc

1   package gov.nasa.pds.ltdt.gui.util;
2   
3   import gov.nasa.pds.ltdt.gui.MainWindow;
4   import gov.nasa.pds.ltdt.gui.ProjectGalleryDialog;
5   import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
6   
7   import java.awt.Color;
8   import java.io.BufferedReader;
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.io.InputStreamReader;
12  import java.net.MalformedURLException;
13  import java.net.URL;
14  import java.util.Properties;
15  
16  import javax.swing.JOptionPane;
17  
18  /***
19   * Utilities for project management related functions.
20   * @author jwang
21   *
22   */
23  public class ProjectUtility {
24  
25  	private final static String oneTemplateWarning = 
26  		"There is a limitation of one template per project. "+
27  		"\nThe existing template will be replaced if you choose to create a different one.\n"+
28  		"\nWould you like to keep the current tempalte?";
29  
30  	/***
31  	 * Prepare to open a local file. The file location is converted to URL
32  	 * for with file:/ as protocol
33  	 * @param returning This is a boolean flag to signal whether this method was
34  	 *            called from a new reuqest or as an error retry  
35  	 *        false - a new request selected from the menu 
36  	 *        true - invoked internally due to invalid entry
37  	 */
38  	
39  	public static void openFile (Properties props, MainWindow window, boolean returning) {
40  		
41  		// returning is "true" if this method is visited due for user to re-enter due to error 
42  		//    (the user should have agreed to replace the existing template)
43  		
44  		// only check TEMPLATEEXISTS when it's a new request (i.e. returning == false)
45  		if (returning==false) {
46  			
47  			// if there's no project at all, current directory as project dir
48  			try {
49  				if (Utility.getKeyList(LTDTKeys.LTDTPROPFILE).size()==0) {
50  					props.setProperty(LTDTKeys.PROJECTDIR,System.getProperty("user.dir"));
51  				}
52  				//else if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
53  				if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
54  					int response=JOptionPane.showConfirmDialog (window,
55  							oneTemplateWarning,
56  							"Template Exists.", 
57  							JOptionPane.YES_NO_OPTION
58  							);
59  					
60  					switch (response) {
61  						case JOptionPane.YES_OPTION:
62  							return;
63  						case JOptionPane.CLOSED_OPTION:
64  							return;
65  						case JOptionPane.NO_OPTION:
66  							// do nothing
67  							break;
68  						default:;
69  					}
70  					
71  				}
72  			
73  			}
74  			catch (IOException ex) {
75  				
76  			}
77  	
78  		} // if returning == false
79  		
80  		
81  		String projectDir= props.getProperty(LTDTKeys.PROJECTDIR);
82  		
83  		new ProjectGalleryDialog (props, window, LTDTKeys.PROJECTGALLERYTITLE, LTDTKeys.NEWFROMFILETABINDEX, projectDir);
84  		
85  		return;
86  		
87  	} // openFile
88  	
89  	
90  	/***
91  	 * Prepare to open a URL
92  	 *
93  	 */
94  	public static void openURL (Properties props, MainWindow window, boolean returning) {
95  		
96  		
97  		String urlFilename;
98  		URL url=null;
99  		
100 		// returning is "true" if this method is re-visited due to error 
101 		//    (the user should have agreed to replace the existing template)
102 		
103 		// only check TEMPLATEEXISTS when it's a new request (i.e. returning == false)
104 	
105 		if (returning == false) {				
106 
107 			if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
108 			
109 				int response=JOptionPane.showConfirmDialog (window,
110 						oneTemplateWarning,
111 						"Template Exists.", 
112 						JOptionPane.YES_NO_OPTION
113 						);
114 				
115 				switch (response) {
116 					case JOptionPane.YES_OPTION:
117 						return;
118 					case JOptionPane.CLOSED_OPTION:
119 						return;
120 					case JOptionPane.NO_OPTION:
121 						break;
122 				}
123 			}
124 		}
125 		
126 		
127 		urlFilename = JOptionPane.showInputDialog
128 		     (window, "Please specify a URL.");
129 		
130 		if (urlFilename==null)  { // if user click Cancel or closed
131 			return;
132 		}
133 		else if (urlFilename.length()==0) { 
134 			// do nothing, keep the dialog window open
135 		}
136 		
137 		urlFilename = urlFilename.trim();
138 		if (urlFilename.trim().length() ==0) {
139 			urlFilename = JOptionPane.showInputDialog
140 		     (window, "Please specify a URL");
141 			ProjectUtility.openURL(props, window, true);
142 		}
143 		else {
144 			if (!(urlFilename.startsWith("http:")) && !(urlFilename.startsWith("ftp:"))) {
145 				JOptionPane.showMessageDialog(
146 						window, 
147 						"Can not identify URL "+urlFilename+" \nPlease try again.");
148 	       	 	ProjectUtility.openURL(props, window, true);				
149 			}
150 			else {
151 				try {
152 					url = Utility.toURL(urlFilename);
153 				}
154 				catch (MalformedURLException e) {
155 					JOptionPane.showMessageDialog(
156 							window, 
157 							"Can not identify URL "+urlFilename+" \nPlease try again.");
158 		       	 	ProjectUtility.openURL(props, window, true);
159 				}
160 				
161 				if ( ProjectUtility.openURL(props, window, url) == LTDTKeys.ERROR) {
162 					ProjectUtility.openURL(props, window, true);
163 				}
164 				
165 			}
166 		}
167 	} // openURL with returning flag
168 	
169 	/***
170 	 * Open a named local or remote file through url.
171 	 * @param url
172 	 * @return  0=Successful -1=error
173 	 */
174 	
175 	public static int openURL (Properties props, MainWindow window, URL url) {
176 		
177 		BufferedReader reader=null;
178 		InputStream in = null;
179 		
180 		if (url==null) {
181 			return LTDTKeys.ERROR;
182 		}	
183 		
184 		if (url.toString().length()==0) {
185 			JOptionPane.showMessageDialog(
186 					window, "Please specify a URL");
187        	 	//openURL();
188        	 	return LTDTKeys.ERROR;
189 		}
190 		
191 		try {
192 				if (url.getProtocol().startsWith("http") && url.getHost().length()==0) {
193 					JOptionPane.showMessageDialog(
194 						window, 
195 						"Error opening "+url.toString()+" \nPlease try again.");
196 				return LTDTKeys.ERROR;
197 				
198 			}
199 
200 			in = url.openStream();
201 			reader = new BufferedReader (new InputStreamReader(in));
202 			window.editorPane.jta.setText("");
203 			
204 			String line = "";
205 			while ((line=reader.readLine())!=null) {
206 
207 
208 				//line=line.trim();
209 				window.editorPane.jta.append(line+System.getProperty("line.separator"));
210 			}
211 			in.close();
212 			reader.close();
213 		}
214 		catch (MalformedURLException ue){
215 			JOptionPane.showMessageDialog(
216 					window, 
217 					"Error opening "+url.toString()+" \nPlease try again.");
218 			// if the user tried to open an http url
219 			if (url.toString().indexOf("http")!=-1) ProjectUtility.openURL(props, window, true);
220 			else ProjectUtility.openFile(props, window, true);
221 		}
222 		catch (IOException ex) {
223 			JOptionPane.showMessageDialog(
224 					window, 
225 					"Error opening "+url.toString()+" \nPlease try again.");
226 			if (url.toString().indexOf("http")!=-1)  ProjectUtility.openURL(props, window, true);
227 			else ProjectUtility.openFile(props, window, true);
228 		}
229 		finally{
230 			try {
231 				if (reader!=null)  reader.close();
232 				if (in!=null) in.close();
233 			}
234 			catch (IOException e) { }// do nothing 
235 		}
236 		
237 		
238 		window.editorPane.jta.setCaretPosition(0);
239 		window.editorPane.jta.grabFocus();
240 		//config.put(LTDTKeys.PROJECTALTERED, "true");
241 		
242 		return LTDTKeys.SUCCESS;
243 		
244 	} // openURL (URL)
245 	
246 /*	
247 	public static void openPresetTemplate (Properties props, MainWindow window, String command)  {
248 		
249 		URL presetTemplate=null;
250 		
251 		
252 
253 		if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
254 			int response=JOptionPane.showConfirmDialog (
255 					window,
256 					oneTemplateWarning,
257 					"Template Exists.", 
258 					JOptionPane.YES_NO_OPTION
259 					);
260 			
261 			switch (response) {
262 				case JOptionPane.YES_OPTION:
263 					return;
264 				case JOptionPane.CLOSED_OPTION:
265 					return;
266 				case JOptionPane.NO_OPTION:
267 					break;
268 			}
269 			
270 		}
271 
272 	} // openPresetTemplate
273 */	
274 	
275 	/***
276 	 * Open an editing are for fragment template, which is blank
277 	 *
278 	 */
279 	public static void openBlankPage(Properties props, MainWindow window) {
280 				
281 			window.editorPane.jta.setText("");
282 			window.editorPane.jta.setCaretPosition(0);
283 			window.editorPane.jta.grabFocus();
284 	}
285 	
286 	
287 	/***
288 	 * Prompts user of their wish to keep the existing template.
289 	 * true - keep the template
290 	 * false - overwrite the template or don't care (possible due to no template or blank template)
291 	 * 
292 	 * @param props
293 	 * @return  true to keep the template, false to overwrite. Default is true.
294 	 */
295 	public static boolean keepCurrentTemplate (Properties props, MainWindow window) {
296 		
297 		if ("true".equals(props.getProperty(LTDTKeys.TEMPLATEEXISTS))) {
298 			int response=JOptionPane.showConfirmDialog (
299 					window,
300 					oneTemplateWarning,
301 					"Template Exists.", 
302 					JOptionPane.YES_NO_OPTION
303 					);
304 			
305 			switch (response) {
306 				case JOptionPane.YES_OPTION:
307 					return true;
308 				case JOptionPane.CLOSED_OPTION:
309 				case JOptionPane.NO_OPTION:
310 					return false;
311 			}			
312 		}
313 		else return false;
314 		
315 		return true;
316 	}
317 }