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: GetNewNameDialog.java
13   * Created: 2007-00-00
14   * Author: psroka, kzdebski
15   * $Id: GetNewNameDialog.java 2312 2009-01-12 22:53:55Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.ui.dialogs;
19  
20  import java.util.List;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.events.KeyAdapter;
24  import org.eclipse.swt.events.KeyEvent;
25  import org.eclipse.swt.events.ModifyEvent;
26  import org.eclipse.swt.events.ModifyListener;
27  import org.eclipse.swt.events.SelectionAdapter;
28  import org.eclipse.swt.events.SelectionEvent;
29  import org.eclipse.swt.events.TraverseEvent;
30  import org.eclipse.swt.events.TraverseListener;
31  import org.eclipse.swt.layout.FormAttachment;
32  import org.eclipse.swt.layout.FormData;
33  import org.eclipse.swt.layout.FormLayout;
34  import org.eclipse.swt.widgets.Button;
35  import org.eclipse.swt.widgets.Display;
36  import org.eclipse.swt.widgets.Label;
37  import org.eclipse.swt.widgets.Shell;
38  import org.eclipse.swt.widgets.Text;
39  
40  import pl.edu.agh.cast.ui.util.SWTHelper;
41  import pl.edu.agh.cast.util.Messages;
42  
43  /**
44   * Dialog used for renaming project name.
45   *
46   * @author AGH CAST Team
47   */
48  public class GetNewNameDialog extends org.eclipse.swt.widgets.Dialog {
49  
50  	private Shell dialogShell;
51  
52  	private Label errorLabel;
53  
54  	private Label promptLabel;
55  
56  	private Button okButton;
57  
58  	private Button cancelButton;
59  
60  	private Text newNameTextField;
61  
62  	private String dialogTitle = "Title"; //$NON-NLS-1$
63  
64  	private String errorMessage = "Error: "; //$NON-NLS-1$
65  
66  	private String dialogPrompt = "Enter name:"; //$NON-NLS-1$
67  
68  	private String result = null;
69  
70  	private boolean cancelled = false;
71  
72  	private String currentName = null;
73  
74  	private List<String> existLabelList = null;
75  
76  	/**
77  	 * Creates new dialog.
78  	 *
79  	 * @param parent
80  	 *            parent {@link Shell}
81  	 * @param style
82  	 *            SWT window style
83  	 * @param title
84  	 *            title of dialog window
85  	 * @param dialogPrompt
86  	 *            dialog prompt message
87  	 * @param errorMsg
88  	 *            message shown when new name match one of names specified on list set 'with setExistLabelList'
89  	 */
90  	public GetNewNameDialog(Shell parent, int style, String title, String dialogPrompt, String errorMsg) {
91  		super(parent, style);
92  		this.dialogTitle = title;
93  		this.dialogPrompt = dialogPrompt;
94  		this.errorMessage = errorMsg;
95  
96  	}
97  
98  	public String getResult() {
99  		return result;
100 	}
101 
102 	/**
103 	 * Checks whether the dialog was canceled.
104 	 *
105 	 * @return <code>true</code> if the dialog was canceled
106 	 */
107 	public boolean wasCancelled() {
108 		return cancelled;
109 	}
110 
111 	/**
112 	 * Opens dialog with current name specified for changes.
113 	 *
114 	 * @param name
115 	 *            current project name
116 	 */
117 	public void open(String name) {
118 		this.currentName = name;
119 
120 		try {
121 			Shell parent = getParent();
122 			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
123 
124 			dialogShell.setLayout(new FormLayout());
125 			dialogShell.setText(dialogTitle);
126 			dialogShell.layout();
127 			dialogShell.pack();
128 			dialogShell.setSize(363, 155);
129 
130 			FormData komunikatLData = new FormData();
131 			komunikatLData.width = 331;
132 			komunikatLData.height = 15;
133 			komunikatLData.left = new FormAttachment(0, 1000, 12);
134 			komunikatLData.top = new FormAttachment(0, 1000, 12);
135 			promptLabel = new Label(dialogShell, SWT.NONE);
136 			promptLabel.setLayoutData(komunikatLData);
137 			promptLabel.setText(dialogPrompt);
138 
139 			FormData errorLabelLData = new FormData();
140 			errorLabelLData.width = 331;
141 			errorLabelLData.height = 15;
142 			errorLabelLData.left = new FormAttachment(0, 1000, 12);
143 			errorLabelLData.top = new FormAttachment(0, 1000, 64);
144 			errorLabel = new Label(dialogShell, SWT.NONE);
145 			errorLabel.setLayoutData(errorLabelLData);
146 			errorLabel.setText(errorMessage);
147 			errorLabel.setVisible(false);
148 
149 			okButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
150 			FormData okButtonLData = new FormData();
151 			okButtonLData.left = new FormAttachment(0, 1000, 217);
152 			okButtonLData.top = new FormAttachment(0, 1000, 85);
153 			okButtonLData.width = 60;
154 			okButtonLData.height = 26;
155 			okButton.setLayoutData(okButtonLData);
156 			okButton.setText(Messages.Button_OK);
157 			okButton.setEnabled(false);
158 			okButton.addSelectionListener(new SelectionAdapter() {
159 				@Override
160 				public void widgetSelected(SelectionEvent evt) {
161 					okButtonWidgetSelected(evt);
162 				}
163 			});
164 
165 			cancelButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
166 			FormData cancelButtonLData = new FormData();
167 			cancelButtonLData.width = 60;
168 			cancelButtonLData.left = new FormAttachment(0, 1000, 284);
169 			cancelButtonLData.top = new FormAttachment(0, 1000, 85);
170 			cancelButtonLData.height = 26;
171 			cancelButton.setLayoutData(cancelButtonLData);
172 			cancelButton.setText(Messages.Button_Cancel);
173 			cancelButton.addSelectionListener(new SelectionAdapter() {
174 				@Override
175 				public void widgetSelected(SelectionEvent evt) {
176 					cancelButtonWidgetSelected(evt);
177 				}
178 			});
179 
180 			FormData newNameTextFieldLData = new FormData();
181 			newNameTextFieldLData.width = 253;
182 			newNameTextFieldLData.height = 13;
183 			newNameTextFieldLData.left = new FormAttachment(0, 1000, 24);
184 			newNameTextFieldLData.top = new FormAttachment(0, 1000, 37);
185 			newNameTextField = new Text(dialogShell, SWT.BORDER);
186 			newNameTextField.setText(currentName);
187 			newNameTextField.setLayoutData(newNameTextFieldLData);
188 			newNameTextField.setSelection(0, currentName.length());
189 			newNameTextField.addKeyListener(new KeyAdapter() {
190 				@Override
191 				public void keyReleased(KeyEvent evt) {
192 					newNameTextFieldKeyReleased(evt);
193 				}
194 			});
195 			newNameTextField.setFocus();
196 			newNameTextField.addModifyListener(new ModifyListener() {
197 				public void modifyText(ModifyEvent evt) {
198 					newNameTextFieldModifyText(evt);
199 				}
200 			});
201 
202 			dialogShell.addTraverseListener(new TraverseListener() {
203 				public void keyTraversed(TraverseEvent evt) {
204 					if (evt.detail == SWT.TRAVERSE_ESCAPE) {
205 						cancelButtonWidgetSelected(null);
206 						evt.doit = false;
207 					}
208 				}
209 			});
210 			dialogShell.addKeyListener(new KeyAdapter() {
211 				@Override
212 				public void keyPressed(KeyEvent e) {
213 					if (e.keyCode == SWT.ESC) {
214 						cancelButtonWidgetSelected(null);
215 					}
216 				}
217 			});
218 			SWTHelper.placeDialogInCenter(dialogShell, getParent());
219 			dialogShell.open();
220 			Display display = dialogShell.getDisplay();
221 			while (!dialogShell.isDisposed()) {
222 				if (!display.readAndDispatch()) {
223 					display.sleep();
224 				}
225 			}
226 		} catch (Exception e) {
227 			e.printStackTrace();
228 		}
229 	}
230 
231 	private void okButtonWidgetSelected(SelectionEvent evt) {
232 		submitChange();
233 	}
234 
235 	private void cancelButtonWidgetSelected(SelectionEvent evt) {
236 		cancelled = true;
237 		dialogShell.dispose();
238 	}
239 
240 	private void newNameTextFieldModifyText(ModifyEvent evt) {
241 		boolean valid = true;
242 
243 		if ("".equals(newNameTextField.getText())) { //$NON-NLS-1$
244 			valid = false;
245 		}
246 		if (currentName.equalsIgnoreCase(newNameTextField.getText())) {
247 			valid = false;
248 			errorLabel.setVisible(false);
249 		} else {
250 			if (projectExists(newNameTextField.getText())) {
251 				valid = false;
252 				errorLabel.setVisible(true);
253 			} else {
254 				errorLabel.setVisible(false);
255 			}
256 		}
257 
258 		okButton.setEnabled(valid);
259 	}
260 
261 	private void newNameTextFieldKeyReleased(KeyEvent evt) {
262 		if (evt.keyCode == SWT.CR && okButton.isEnabled()) {
263 			submitChange();
264 		} else if (evt.keyCode == SWT.ESC) {
265 			cancelButtonWidgetSelected(null);
266 		}
267 	}
268 
269 	private void submitChange() {
270 		result = newNameTextField.getText();
271 		dialogShell.dispose();
272 	}
273 
274 	protected boolean projectExists(String projectName) {
275 		for (String label : existLabelList) {
276 			if (label.equalsIgnoreCase(projectName)) {
277 				return true;
278 			}
279 		}
280 		return false;
281 	}
282 
283 	public void setExistLabelList(List<String> existLabelList) {
284 		this.existLabelList = existLabelList;
285 	}
286 }