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
42
43
44
45 if (returning==false) {
46
47
48 try {
49 if (Utility.getKeyList(LTDTKeys.LTDTPROPFILE).size()==0) {
50 props.setProperty(LTDTKeys.PROJECTDIR,System.getProperty("user.dir"));
51 }
52
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
67 break;
68 default:;
69 }
70
71 }
72
73 }
74 catch (IOException ex) {
75
76 }
77
78 }
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 }
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
101
102
103
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) {
131 return;
132 }
133 else if (urlFilename.length()==0) {
134
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 }
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
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
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
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) { }
235 }
236
237
238 window.editorPane.jta.setCaretPosition(0);
239 window.editorPane.jta.grabFocus();
240
241
242 return LTDTKeys.SUCCESS;
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 * 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 }