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.Utility;
5
6 import java.awt.Dimension;
7 import java.awt.FlowLayout;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URL;
15 import java.util.Enumeration;
16 import java.util.Properties;
17
18
19 import javax.swing.BorderFactory;
20 import javax.swing.Box;
21 import javax.swing.JButton;
22 import javax.swing.JDialog;
23 import javax.swing.JFileChooser;
24 import javax.swing.JLabel;
25 import javax.swing.JOptionPane;
26 import javax.swing.JTextField;
27 import javax.swing.SwingConstants;
28
29 /***
30 * Handling of creating a new project space.
31 * Posssible usage are:
32 * Save a project as
33 * Save an untitled project
34 * Create new template in an untitled project
35 * etc.
36 *
37 * @author jwang
38 *
39 */
40 public class ProjectCreateDialog extends JDialog {
41
42 private Properties props;
43 private MainWindow window;
44
45 private String projectLocation;
46
47 private Box box1, box2, box3, box4, box5= null;
48 private JLabel jlabName, jlabLocation, jlabNameRule, jlabWhitespace = null;
49 private JTextField jtfProjectName, jtfProjectLocation = null;
50 private JButton jbtnCreate,jbtnCancel, jbtnBrowse;
51
52 private int status=0;
53 private boolean cancelled = false;
54 private int newProjectType = 0;
55 /***
56 *
57 * @param props
58 * @param window
59 */
60
61 public ProjectCreateDialog (Properties props, MainWindow window, String title, int newProjectType){
62 super(window, title, true);
63
64 this.props = props;
65 this.window = window;
66 this.newProjectType = newProjectType;
67
68 status = constructPanel(props, window, newProjectType);
69
70 }
71
72 private int constructPanel(Properties props, MainWindow window, int newProjectType) {
73
74 this.setSize(600,200);
75 this.getContentPane().setLayout(new FlowLayout());
76
77 box1= Box.createVerticalBox();
78 box1.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
79
80 box2= Box.createHorizontalBox();
81 box2.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
82
83 box3= Box.createHorizontalBox();
84 box3.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
85
86 box4= Box.createHorizontalBox();
87 box4.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));
88
89 box5= Box.createHorizontalBox();
90 box5.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));
91
92
93
94 jlabName = new JLabel("Project Name: ");
95 jlabName.setPreferredSize(new Dimension(120,20));
96 jlabName.setHorizontalAlignment(SwingConstants.RIGHT);
97
98 jtfProjectName = new JTextField(20);
99 jtfProjectName.setHorizontalAlignment(SwingConstants.LEFT);
100
101 jlabWhitespace = new JLabel(" ");
102 box2.add(jlabName);
103 box2.add(jtfProjectName);
104 box2.add(jlabWhitespace);
105
106 jlabNameRule = new JLabel("(Project Name is case insensitive and will be converted to lowercase.)");
107 jlabNameRule.setPreferredSize(new Dimension(150,20));
108 jlabNameRule.setHorizontalAlignment(SwingConstants.LEFT);
109 box3.add(jlabNameRule);
110
111 jlabLocation = new JLabel("Location: ");
112 jlabLocation.setPreferredSize(new Dimension(120,20));
113 jlabLocation.setHorizontalAlignment(SwingConstants.RIGHT);
114
115 jtfProjectLocation = new JTextField(30);
116 jtfProjectLocation.setHorizontalAlignment(SwingConstants.LEFT);
117
118 jbtnBrowse = new JButton("Browse");
119 jbtnBrowse.setSize(10,10);
120
121 jbtnBrowse.addActionListener ( new ActionListener() {
122
123 public void actionPerformed(ActionEvent ae) {
124 projectLocation = getProjectLocation();
125 jtfProjectLocation.setText(projectLocation);
126
127 }
128 });
129
130 box4.add(jlabLocation);
131 box4.add(jtfProjectLocation);
132 box4.add(jbtnBrowse);
133
134 jbtnCreate = new JButton();
135 jbtnCreate.setSelected(true);
136
137 switch (newProjectType) {
138 case LTDTKeys.NEWEMPTYPROJECT:
139 jbtnCreate.setText("CreateNew");
140 break;
141 case LTDTKeys.NEWPROJECT:
142 jbtnCreate.setText("Create");
143 break;
144 case LTDTKeys.SAVEAS:
145 jbtnCreate.setText("Save");
146 break;
147 default:;
148 }
149
150
151 jbtnCreate.addActionListener ( new ActionListener() {
152 public void actionPerformed(ActionEvent ae) {
153
154 if ("CreateNew".equals(jbtnCreate.getActionCommand())) {
155 status=doCreateNewEmpty();
156 }
157 else status=doCreateSaveAs();
158 return;
159 }
160 });
161
162
163 jbtnCancel = new JButton();
164 jbtnCancel.setText("Cancel");
165 jbtnCancel.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent ae) {
167 cancelled = true;
168 doCreateSaveAs();
169
170 }
171 });
172
173
174 this.addWindowListener(new WindowAdapter() {
175 public void windowClosing(WindowEvent we) {
176 cancelled = true;
177 doCreateSaveAs();
178 }
179 } );
180
181 box5.add(jbtnCreate);
182 box5.add(jbtnCancel);
183
184
185 box1.add(box2);
186 box1.add(box3);
187 box1.add(box4);
188 box1.add(box5);
189
190 this.getContentPane().add(box1);
191
192
193 this.getRootPane().setDefaultButton(jbtnCreate);
194
195 this.setVisible(true);
196
197 if (cancelled==true) return LTDTKeys.CANCELLED;
198 else return LTDTKeys.SUCCESS;
199 }
200
201 /***
202 * Retrieve project location chosen from file chooser
203 * @return
204 */
205 private String getProjectLocation() {
206
207 String selectedDir = "";
208
209 JFileChooser jfc = new JFileChooser(System.getProperty("user.dir"));
210 jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
211 int result = jfc.showOpenDialog(window);
212
213
214 switch (result) {
215
216 case JFileChooser.APPROVE_OPTION:
217 return jfc.getSelectedFile().toString();
218 case JFileChooser.CANCEL_OPTION:
219 return "";
220 default:;
221 }
222
223 return selectedDir;
224 }
225
226 private int doCreateNewEmpty() {
227
228 if (cancelled==true) {
229 super.setVisible(false);
230 return LTDTKeys.SUCCESS;
231 }
232
233 int createStatus = 0;
234
235 String newProjectName = jtfProjectName.getText().trim();
236 String newProjectLocation = jtfProjectLocation.getText().trim()+System.getProperty("file.separator")+newProjectName.toLowerCase();
237
238 try {
239 if (validateInput(newProjectName, newProjectLocation) == LTDTKeys.ERROR)
240 return LTDTKeys.ERROR;
241 }
242 catch (IOException ex) {
243 JOptionPane.showMessageDialog (window, "IOException during Create New Empty Project \n"+ex.getMessage());
244 return LTDTKeys.FAIL;
245 }
246
247
248
249
250 Utility.initializeEmptyProject(props, window);
251
252
253
254 createStatus = ProjectSave.saveAs(props, window, newProjectName, newProjectLocation);
255
256
257 createStatus = ProjectOpen.open(props, window, newProjectName , false);
258
259 if (createStatus==LTDTKeys.SUCCESS) super.setVisible(false);
260
261 return createStatus;
262
263 }
264
265 /***
266 * Called by both Create New and Saved As.
267 * Write out all settings and file contents to a new location.
268 * Set up runtime properties
269 * Change window title with new project name
270 *
271 * @return
272 */
273 private int doCreateSaveAs() {
274
275 if (cancelled==true) {
276 super.setVisible(false);
277 return LTDTKeys.SUCCESS;
278 }
279
280 int validateStatus = 0;
281 int saveAsStatus = 0;
282
283
284 String newProjectName = jtfProjectName.getText().trim();
285 String newProjectLocation = jtfProjectLocation.getText().trim();
286 if (newProjectLocation.length()>0)
287 newProjectLocation = newProjectLocation+System.getProperty("file.separator")+newProjectName.toLowerCase();
288
289 try {
290 validateStatus = validateInput(newProjectName, newProjectLocation);
291
292 if (validateStatus != LTDTKeys.SUCCESS)
293 return validateStatus;
294
295 }
296 catch (IOException ex) {
297 switch (newProjectType) {
298 case LTDTKeys.NEWPROJECT:
299 JOptionPane.showMessageDialog (window, "IOException during Create New Project \n"+ex.getMessage());
300 break;
301 case LTDTKeys.SAVEAS:
302 JOptionPane.showMessageDialog (window, "IOException during Save Project As \n"+ex.getMessage());
303 break;
304
305 }
306 return LTDTKeys.FAIL;
307 }
308
309
310 saveAsStatus = ProjectSave.saveAs(props, window, newProjectName, newProjectLocation);
311
312 if (saveAsStatus==LTDTKeys.SUCCESS) super.setVisible(false);
313
314 return saveAsStatus;
315
316 }
317
318 /***
319 * Validate new project name, location information entered by user
320 * @param newProjectName Name of new project. Name is case insensitive and has to be unique
321 * @param newProjectLocation Path of the new project. It has to be an existing directory and read/write permitted.
322 * @return SUCCESS, ERROR
323 * @throws IOException
324 */
325 private int validateInput(String newProjectName, String newProjectLocation) throws IOException {
326
327 String newProjectNameL = newProjectName.toLowerCase();
328 String message = "";
329 File location = null;
330 boolean missingName=false;
331 boolean missingLocation=false;
332
333
334 if (newProjectName.length()==0) {
335 missingName=true;
336 }
337
338 if (newProjectLocation.length()==0 || newProjectLocation.equals(System.getProperty("file.separator")) ) {
339 missingLocation=true;
340 }
341
342 if (missingName && missingLocation) {
343 JOptionPane.showMessageDialog(window, "Please enter a project name and location.",
344 "Missing Project Information", JOptionPane.WARNING_MESSAGE);
345 return LTDTKeys.WARNING;
346 }
347 else if (missingName) {
348 JOptionPane.showMessageDialog(window, "Please enter a project name",
349 "Missing Project Name", JOptionPane.WARNING_MESSAGE);
350 return LTDTKeys.WARNING;
351 }
352 else if (missingLocation) {
353 JOptionPane.showMessageDialog(window, "Please enter location of a project",
354 "Missing Project Location", JOptionPane.WARNING_MESSAGE );
355 return LTDTKeys.WARNING;
356 }
357
358
359
360
361 if (new File(LTDTKeys.LTDTPROPFILE).exists() &&
362 Utility.getKeyList(LTDTKeys.LTDTPROPFILE).size() > 0 ) {
363
364 if (Utility.getProjectLocation(newProjectName)!=null) {
365
366 JOptionPane.showMessageDialog(window, "A project named \""+newProjectName+"\" already exists.\n"+
367 "Please provide a different name for this project.",
368 "Project Already Exists", JOptionPane.WARNING_MESSAGE);
369 return LTDTKeys.WARNING;
370 }
371
372
373 if (Utility.getProjectName(newProjectLocation)!=null) {
374 JOptionPane.showMessageDialog(window, "A project named \""+newProjectName+"\" already exists in the specific location.\n"+
375 "Please provide a different name for this project.",
376 "Project Already Exists", JOptionPane.WARNING_MESSAGE);
377 return LTDTKeys.WARNING;
378 }
379
380 }
381
382 location = new File(newProjectLocation);
383
384 if (!location.exists()) {
385
386 StringBuffer message2 = new StringBuffer();
387 message2.append("The following directory does not exist: \n"+newProjectLocation+"\n\n");
388 message2.append("Would you like to create the directory?\n");
389
390 int response=JOptionPane.showConfirmDialog(
391 window,
392 message2, "Create Project Directory", JOptionPane.YES_NO_OPTION,
393 JOptionPane.INFORMATION_MESSAGE);
394
395 switch (response) {
396 case JOptionPane.YES_OPTION:
397
398 if (location.mkdirs()==false) {
399 message = "cannot create directory "+newProjectLocation+"\n";
400 JOptionPane.showMessageDialog(window, message, "Project Directory Creation Error",
401 JOptionPane.ERROR_MESSAGE);
402 return LTDTKeys.ERROR;
403 }
404 break;
405 case JOptionPane.CLOSED_OPTION:
406 case JOptionPane.NO_OPTION:
407 return LTDTKeys.CANCELLED;
408 default:;
409 }
410
411 return LTDTKeys.SUCCESS;
412 }
413
414 if (!location.isDirectory()) {
415 message = newProjectLocation+ " is not a directory.";
416 JOptionPane.showMessageDialog(window, message, "Project Directory Creation Error",
417 JOptionPane.ERROR_MESSAGE);
418 return LTDTKeys.ERROR;
419 }
420
421 if (Utility.dirRWAllowed(newProjectLocation)==false) {
422 message = "The directory "+newProjectLocation+" is not Read or Write accessible. \n";
423 JOptionPane.showMessageDialog(window, message, "Directory Directory Creation Error",
424 JOptionPane.ERROR_MESSAGE);
425 return LTDTKeys.ERROR;
426 }
427
428
429 return LTDTKeys.SUCCESS;
430 }
431
432
433 public int getCreateSaveAsStatus () {
434 return status;
435 }
436 }