View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: CreateProjectDialog.java
13   * Created: 2007-00-00
14   * Author: awos
15   * $Id: CreateProjectDialog.java 3277 2009-08-31 14:07:24Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.ui.dialogs;
19  
20  import java.io.File;
21  
22  import org.eclipse.osgi.util.NLS;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.events.KeyAdapter;
25  import org.eclipse.swt.events.KeyEvent;
26  import org.eclipse.swt.events.ModifyEvent;
27  import org.eclipse.swt.events.ModifyListener;
28  import org.eclipse.swt.events.SelectionAdapter;
29  import org.eclipse.swt.events.SelectionEvent;
30  import org.eclipse.swt.events.TraverseEvent;
31  import org.eclipse.swt.events.TraverseListener;
32  import org.eclipse.swt.layout.GridData;
33  import org.eclipse.swt.layout.GridLayout;
34  import org.eclipse.swt.widgets.Button;
35  import org.eclipse.swt.widgets.DirectoryDialog;
36  import org.eclipse.swt.widgets.Display;
37  import org.eclipse.swt.widgets.Event;
38  import org.eclipse.swt.widgets.Label;
39  import org.eclipse.swt.widgets.Listener;
40  import org.eclipse.swt.widgets.Shell;
41  import org.eclipse.swt.widgets.Text;
42  
43  import pl.edu.agh.cast.project.ProjectUtil;
44  import pl.edu.agh.cast.ui.util.SWTHelper;
45  import pl.edu.agh.cast.util.Configuration;
46  import pl.edu.agh.cast.util.Messages;
47  
48  /**
49   * Dialog used for creating a new project.
50   *
51   * @author AGH CAST Team
52   */
53  public class CreateProjectDialog extends org.eclipse.swt.widgets.Dialog {
54  
55  	private Shell dialogShell;
56  
57  	private Label errorLabel;
58  
59  	private Label promptLabel;
60  
61  	private Button okButton;
62  
63  	private Button cancelButton;
64  
65  	private Text newNameTextField;
66  
67  	private String dialogTitle = "Title"; //$NON-NLS-1$
68  
69  	@SuppressWarnings("unused")
70  	private Label emptyLbl;
71  
72  	private Text locationTxt;
73  
74  	private String locationPrompt = "Choose location:"; //$NON-NLS-1$
75  
76  	private String location = ""; //$NON-NLS-1$
77  
78  	private Label locationPromptLabel;
79  
80  	private Button chooseLocation;
81  
82  	private String dialogPrompt = "Enter name:"; //$NON-NLS-1$
83  
84  	private String result = null;
85  
86  	private boolean cancelled = false;
87  
88  	private String currentName = null;
89  
90  	/**
91  	 * Creates new dialog for defining new projects.
92  	 *
93  	 * @param parent
94  	 *            parent shell
95  	 * @param style
96  	 *            SWT window style
97  	 * @param title
98  	 *            title of dialog window
99  	 * @param dialogPrompt
100 	 *            dialog prompt message
101 	 * @param locationPrompt
102 	 *            project location selection prompt message
103 	 */
104 	public CreateProjectDialog(Shell parent, int style, String title, String dialogPrompt, String locationPrompt) {
105 		super(parent, style);
106 		this.dialogTitle = title;
107 		this.dialogPrompt = dialogPrompt;
108 		this.locationPrompt = locationPrompt;
109 	}
110 
111 	public String getName() {
112 		return result;
113 	}
114 
115 	/**
116 	 * Checks whether the dialog was canceled.
117 	 *
118 	 * @return <code>true</code> if the dialog was canceled
119 	 */
120 	public boolean wasCancelled() {
121 		return cancelled;
122 	}
123 
124 	/**
125 	 * Opens dialog with current name specified for changes.
126 	 *
127 	 * @param name
128 	 *            current project name
129 	 */
130 	public void open(String name) {
131 		this.currentName = name;
132 
133 		try {
134 			Shell parent = getParent();
135 			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
136 
137 			GridLayout dialogShellLayout = new GridLayout();
138 			dialogShellLayout.numColumns = 2;
139 			dialogShell.setLayout(dialogShellLayout);
140 			dialogShell.setText(dialogTitle);
141 			dialogShell.layout();
142 			dialogShell.pack();
143 			dialogShell.setSize(381, 188);
144 
145 			locationPromptLabel = new Label(dialogShell, SWT.NONE);
146 			GridData label1LData = new GridData();
147 			label1LData.horizontalSpan = 2;
148 			locationPromptLabel.setLayoutData(label1LData);
149 			locationPromptLabel.setText(locationPrompt);
150 			locationPromptLabel.setSize(-1, -1);
151 
152 			GridData locationTxtLData = new GridData();
153 			locationTxtLData.grabExcessHorizontalSpace = true;
154 			locationTxtLData.horizontalAlignment = GridData.FILL;
155 			locationTxt = new Text(dialogShell, SWT.BORDER);
156 			locationTxt.setLayoutData(locationTxtLData);
157 			locationTxt.setSize(286, -1);
158 			locationTxt.setFocus();
159 			if (location != null) {
160 				locationTxt.setText(location);
161 			}
162 			locationTxt.addModifyListener(new ModifyListener() {
163 				public void modifyText(ModifyEvent e) {
164 					inputTextModifyText(e);
165 				}
166 			});
167 
168 			dialogShell.addTraverseListener(new TraverseListener() {
169 				public void keyTraversed(TraverseEvent evt) {
170 					if (evt.detail == SWT.TRAVERSE_ESCAPE) {
171 						cancelButtonWidgetSelected(null);
172 						evt.doit = false;
173 					}
174 				}
175 			});
176 			dialogShell.addListener(SWT.Close, new Listener() {
177 				public void handleEvent(Event event) {
178 					cancelled = true;
179 				}
180 			});
181 
182 			chooseLocation = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
183 			GridData chooseLocationLData = new GridData();
184 			chooseLocation.setLayoutData(chooseLocationLData);
185 			chooseLocation.setText("..."); //$NON-NLS-1$
186 			chooseLocation.setToolTipText(locationPrompt);
187 			chooseLocation.addSelectionListener(new SelectionAdapter() {
188 				@Override
189 				public void widgetSelected(SelectionEvent evt) {
190 					chooseLocationWidgetSelected(evt);
191 				}
192 			});
193 
194 			GridData komunikatLData = new GridData();
195 			komunikatLData.horizontalSpan = 2;
196 			promptLabel = new Label(dialogShell, SWT.NONE);
197 			promptLabel.setLayoutData(komunikatLData);
198 			promptLabel.setText(dialogPrompt);
199 			promptLabel.setSize(-1, -1);
200 
201 			GridData newNameTextFieldLData = new GridData();
202 			newNameTextFieldLData.horizontalAlignment = GridData.FILL;
203 			newNameTextFieldLData.grabExcessHorizontalSpace = true;
204 			newNameTextField = new Text(dialogShell, SWT.BORDER);
205 			newNameTextField.setText(currentName);
206 			newNameTextField.setLayoutData(newNameTextFieldLData);
207 			newNameTextField.setSelection(0, currentName.length());
208 			newNameTextField.setSize(286, -1);
209 			newNameTextField.addKeyListener(new KeyAdapter() {
210 				@Override
211 				public void keyReleased(KeyEvent evt) {
212 					newNameTextFieldKeyReleased(evt);
213 				}
214 			});
215 			newNameTextField.addModifyListener(new ModifyListener() {
216 				public void modifyText(ModifyEvent evt) {
217 					inputTextModifyText(evt);
218 				}
219 			});
220 
221 			emptyLbl = new Label(dialogShell, SWT.NONE);
222 
223 			GridData errorLabelLData = new GridData();
224 			errorLabelLData.heightHint = 13;
225 			errorLabelLData.grabExcessHorizontalSpace = true;
226 			errorLabelLData.horizontalAlignment = GridData.FILL;
227 			errorLabelLData.horizontalSpan = 2;
228 			errorLabel = new Label(dialogShell, SWT.NONE);
229 			errorLabel.setLayoutData(errorLabelLData);
230 			errorLabel.setVisible(false);
231 
232 			okButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
233 			GridData okButtonLData = new GridData();
234 			okButtonLData.horizontalAlignment = GridData.END;
235 			okButtonLData.minimumWidth = 100;
236 			okButtonLData.widthHint = 60;
237 			okButton.setLayoutData(okButtonLData);
238 			okButton.setText(Messages.Button_OK);
239 			okButton.setEnabled(false);
240 			okButton.setSize(60, -1);
241 			okButton.addSelectionListener(new SelectionAdapter() {
242 				@Override
243 				public void widgetSelected(SelectionEvent evt) {
244 					okButtonWidgetSelected(evt);
245 				}
246 			});
247 
248 			cancelButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
249 			GridData cancelButtonLData = new GridData();
250 			cancelButtonLData.widthHint = 60;
251 			cancelButton.setLayoutData(cancelButtonLData);
252 			cancelButton.setText(Messages.Button_Cancel);
253 			cancelButton.setSize(60, -1);
254 			cancelButton.addSelectionListener(new SelectionAdapter() {
255 				@Override
256 				public void widgetSelected(SelectionEvent evt) {
257 					cancelButtonWidgetSelected(evt);
258 				}
259 			});
260 
261 			dialogShell.addKeyListener(new KeyAdapter() {
262 				@Override
263 				public void keyPressed(KeyEvent e) {
264 					if (e.keyCode == SWT.ESC) {
265 						cancelButtonWidgetSelected(null);
266 					}
267 				}
268 			});
269 			SWTHelper.placeDialogInCenter(dialogShell, getParent());
270 			dialogShell.open();
271 			Display display = dialogShell.getDisplay();
272 			while (!dialogShell.isDisposed()) {
273 				if (!display.readAndDispatch()) {
274 					display.sleep();
275 				}
276 			}
277 		} catch (Exception e) {
278 			e.printStackTrace();
279 		}
280 	}
281 
282 	private void okButtonWidgetSelected(SelectionEvent evt) {
283 		submitChange();
284 	}
285 
286 	private void cancelButtonWidgetSelected(SelectionEvent evt) {
287 		cancelled = true;
288 		dialogShell.dispose();
289 	}
290 
291 	private void inputTextModifyText(ModifyEvent evt) {
292 		boolean validLocation = true;
293 		boolean validName = true;
294 
295 		if ("".equals(locationTxt.getText())) { //$NON-NLS-1$
296 			validLocation = false;
297 			errorLabel.setText(""); //$NON-NLS-1$
298 			errorLabel.update();
299 		}
300 		if (validLocation) {
301 			File locationFile = new File(locationTxt.getText());
302 			if (!locationFile.exists() || !locationFile.isDirectory()) {
303 				errorLabel.setText(Messages.CreateProjectAction_6);
304 				errorLabel.update();
305 				validLocation = false;
306 			}
307 		}
308 
309 		String newName = newNameTextField.getText();
310 		if ("".equals(newName)) { //$NON-NLS-1$
311 			validName = false;
312 			if (validLocation) {
313 				errorLabel.setText(""); //$NON-NLS-1$
314 				errorLabel.update();
315 			}
316 
317 		}
318 
319 		if (validName) {
320 			for (CharSequence charSeq : ProjectUtil.PROJECT_BAD_CHARS) {
321 				if (newName.contains(charSeq)) {
322 					validName = false;
323 					errorLabel.setText(NLS.bind(Messages.CreateProjectDialog_4, charSeq));
324 					errorLabel.update();
325 					break;
326 				}
327 			}
328 
329 		}
330 
331 		if (validName) {
332 			if (currentName.equalsIgnoreCase(newName)) {
333 				validName = false;
334 				if (validLocation) {
335 					errorLabel.setText(""); //$NON-NLS-1$
336 					errorLabel.update();
337 				}
338 			} else {
339 				if (newName.equalsIgnoreCase(Configuration.TEMPLATES_PROJECT_NAME)) {
340 					validName = false;
341 					errorLabel.setText(NLS.bind(Messages.CreateProjectDialog_1, Configuration.TEMPLATES_PROJECT_NAME));
342 					errorLabel.update();
343 				} else {
344 					String newProjectDummyFileStr = locationTxt.getText() + File.separator + newName + File.separator
345 					        + newName + "." + Configuration.DUMMY_PROJECT_FILE_EXTENSION; //$NON-NLS-1$
346 					File newProjectDummyFile = new File(newProjectDummyFileStr);
347 					if (newProjectDummyFile.exists()) {
348 						validName = false;
349 						errorLabel.setText(NLS.bind(Messages.CreateProjectDialog_3, newName));
350 						errorLabel.update();
351 
352 					}
353 				}
354 			}
355 		}
356 		errorLabel.setVisible(!(validLocation & validName));
357 		okButton.setEnabled(validLocation & validName);
358 	}
359 
360 	private void newNameTextFieldKeyReleased(KeyEvent evt) {
361 		if (evt.keyCode == SWT.CR && okButton.isEnabled()) {
362 			submitChange();
363 		} else if (evt.keyCode == SWT.ESC) {
364 			cancelButtonWidgetSelected(null);
365 		}
366 	}
367 
368 	private void submitChange() {
369 		result = newNameTextField.getText();
370 		location = locationTxt.getText();
371 		dialogShell.dispose();
372 	}
373 
374 	private void chooseLocationWidgetSelected(SelectionEvent evt) {
375 		DirectoryDialog dd = new DirectoryDialog(getParent(), SWT.APPLICATION_MODAL);
376 		dd.setMessage(locationPrompt);
377 		dd.setText(dialogTitle);
378 		String choosenLocation = dd.open();
379 		if (choosenLocation != null) {
380 			locationTxt.setText(choosenLocation);
381 		} else {
382 			locationTxt.setText(""); //$NON-NLS-1$
383 		}
384 		location = locationTxt.getText();
385 		inputTextModifyText(null);
386 	}
387 
388 	public String getLocation() {
389 		return location;
390 	}
391 
392 	public void setLocation(String location) {
393 		this.location = location;
394 	}
395 }