1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46
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";
63
64 private String errorMessage = "Error: ";
65
66 private String dialogPrompt = "Enter name:";
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
78
79
80
81
82
83
84
85
86
87
88
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
104
105
106
107 public boolean wasCancelled() {
108 return cancelled;
109 }
110
111
112
113
114
115
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())) {
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 }