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.Collection;
21 import java.util.Collections;
22
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.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.FormAttachment;
29 import org.eclipse.swt.layout.FormData;
30 import org.eclipse.swt.layout.FormLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.MessageBox;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.PlatformUI;
39
40 import pl.edu.agh.cast.backward.editor.AbstractEditor;
41 import pl.edu.agh.cast.backward.editor.operation.FindNodesOperation;
42 import pl.edu.agh.cast.backward.editor.operation.FindNodesParameters;
43 import pl.edu.agh.cast.backward.resources.DiagramEditorInput;
44 import pl.edu.agh.cast.model.visual.backward.IDiagram;
45 import pl.edu.agh.cast.model.visual.backward.Node;
46 import pl.edu.agh.cast.util.Messages;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class FindDialog extends org.eclipse.swt.widgets.Dialog {
67
68 private Shell dialogShell;
69
70 private Label findCriteriaLabel;
71
72 private Button cancelButton;
73
74 private Button findAllButton;
75
76 private Button nextButton;
77
78 private Button useWildcardsCheckBox;
79
80 private Text findCriteriaTextField;
81
82 private Button prevButton;
83
84 private Label helpWildcardLabel;
85
86 private Button matchCaseCheckBox;
87
88 private Button wholeWordsCheckBox;
89
90 private FindNodesOperation nodesFinder;
91
92 private IEditorPart editor;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public FindDialog(Shell parent, int style, IEditorPart editor) {
118 super(parent, style);
119 nodesFinder = new FindNodesOperation();
120 this.editor = editor;
121 }
122
123
124
125
126 public void open() {
127 try {
128 Shell parent = getParent();
129 dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
130
131 dialogShell.setLayout(new FormLayout());
132 dialogShell.setText(Messages.FindElement_Find);
133 dialogShell.layout();
134 dialogShell.pack();
135 dialogShell.setSize(578, 178);
136
137 helpWildcardLabel = new Label(dialogShell, SWT.NONE);
138 FormData helpWildcardLabelLData = new FormData();
139 helpWildcardLabelLData.left = new FormAttachment(0, 1000, 139);
140 helpWildcardLabelLData.top = new FormAttachment(0, 1000, 83);
141 helpWildcardLabelLData.width = 299;
142 helpWildcardLabelLData.height = 16;
143 helpWildcardLabel.setVisible(false);
144 helpWildcardLabel.setLayoutData(helpWildcardLabelLData);
145 helpWildcardLabel.setText(Messages.FindElement_WildcardDescription);
146
147 FormData findCriteriaTextFieldLData = new FormData();
148 findCriteriaTextFieldLData.width = 436;
149 findCriteriaTextFieldLData.left = new FormAttachment(0, 1000, 110);
150 findCriteriaTextFieldLData.top = new FormAttachment(0, 1000, 19);
151 findCriteriaTextField = new Text(dialogShell, SWT.SINGLE | SWT.BORDER);
152 findCriteriaTextField.setLayoutData(findCriteriaTextFieldLData);
153 findCriteriaTextField.addKeyListener(new KeyAdapter() {
154 @Override
155 public void keyReleased(KeyEvent evt) {
156 findCriteriaTextFieldKeyReleased(evt);
157 }
158 });
159
160 wholeWordsCheckBox = new Button(dialogShell, SWT.CHECK | SWT.LEFT);
161 FormData wholeWordsCheckBoxLData = new FormData(-1, -1);
162 wholeWordsCheckBoxLData.left = new FormAttachment(0, 1000, 12);
163 wholeWordsCheckBoxLData.top = new FormAttachment(0, 1000, 49);
164 wholeWordsCheckBox.setLayoutData(wholeWordsCheckBoxLData);
165 wholeWordsCheckBox.setText(Messages.FindElement_WholeWord);
166 wholeWordsCheckBox.setSize(-1, -1);
167
168 matchCaseCheckBox = new Button(dialogShell, SWT.CHECK | SWT.LEFT);
169 FormData matchCaseCheckBoxLData = new FormData(-1, -1);
170 matchCaseCheckBoxLData.left = new FormAttachment(0, 1000, 12);
171 matchCaseCheckBoxLData.top = new FormAttachment(0, 1000, 65);
172 matchCaseCheckBox.setLayoutData(matchCaseCheckBoxLData);
173 matchCaseCheckBox.setText(Messages.FindElement_MatchCase);
174 matchCaseCheckBox.setSize(-1, -1);
175
176 FormData useWildcardsCheckBoxLData = new FormData();
177 useWildcardsCheckBoxLData.left = new FormAttachment(0, 1000, 12);
178 useWildcardsCheckBoxLData.top = new FormAttachment(0, 1000, 81);
179 useWildcardsCheckBox = new Button(dialogShell, SWT.CHECK | SWT.LEFT);
180 useWildcardsCheckBox.setLayoutData(useWildcardsCheckBoxLData);
181 useWildcardsCheckBox.setText(Messages.FindElement_UseWildcards);
182 useWildcardsCheckBox.addSelectionListener(new SelectionAdapter() {
183 @Override
184 public void widgetSelected(SelectionEvent evt) {
185 useWildcardsCheckBoxWidgetSelected(evt);
186 }
187 });
188
189 prevButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
190 FormData prevButtonLData = new FormData();
191 prevButtonLData.width = 97;
192 prevButtonLData.left = new FormAttachment(0, 1000, 12);
193 prevButtonLData.top = new FormAttachment(0, 1000, 109);
194 prevButton.setLayoutData(prevButtonLData);
195 prevButton.setText(Messages.FindElement_Prev);
196 prevButton.addSelectionListener(new SelectionAdapter() {
197 @Override
198 public void widgetSelected(SelectionEvent evt) {
199 prevButtonWidgetSelected(evt);
200 }
201 });
202
203 FormData nextButtonLData = new FormData();
204 nextButtonLData.width = 105;
205 nextButtonLData.left = new FormAttachment(0, 1000, 122);
206 nextButtonLData.top = new FormAttachment(0, 1000, 109);
207 nextButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
208 nextButton.setLayoutData(nextButtonLData);
209 nextButton.setText(Messages.FindElement_Next);
210 nextButton.addSelectionListener(new SelectionAdapter() {
211 @Override
212 public void widgetSelected(SelectionEvent evt) {
213 nextButtonWidgetSelected(evt);
214 }
215 });
216
217 FormData findAllButtonLData = new FormData();
218 findAllButtonLData.width = 145;
219 findAllButtonLData.left = new FormAttachment(0, 1000, 242);
220 findAllButtonLData.top = new FormAttachment(0, 1000, 109);
221 findAllButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
222 findAllButton.setLayoutData(findAllButtonLData);
223 findAllButton.setText(Messages.FindElement_FindAll);
224 findAllButton.addSelectionListener(new SelectionAdapter() {
225 @Override
226 public void widgetSelected(SelectionEvent evt) {
227 findAllButtonWidgetSelected(evt);
228 }
229 });
230
231 FormData cancelButtonLData = new FormData();
232 cancelButtonLData.width = 105;
233 cancelButtonLData.left = new FormAttachment(0, 1000, 453);
234 cancelButtonLData.top = new FormAttachment(0, 1000, 109);
235 cancelButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
236 cancelButton.setLayoutData(cancelButtonLData);
237 cancelButton.setText(Messages.FindElement_Cancel);
238 cancelButton.addSelectionListener(new SelectionAdapter() {
239 @Override
240 public void widgetSelected(SelectionEvent evt) {
241 cancelButtonWidgetSelected(evt);
242 }
243 });
244
245 FormData findCriteriaLabelLData = new FormData();
246 findCriteriaLabelLData.left = new FormAttachment(0, 1000, 12);
247 findCriteriaLabelLData.top = new FormAttachment(0, 1000, 18);
248 findCriteriaLabel = new Label(dialogShell, SWT.NONE);
249 findCriteriaLabel.setLayoutData(findCriteriaLabelLData);
250 findCriteriaLabel.setText(Messages.FindElement_Find + ":");
251
252 dialogShell.setLocation(getParent().toDisplay(100, 100));
253 dialogShell.open();
254 Display display = dialogShell.getDisplay();
255 while (!dialogShell.isDisposed()) {
256 if (!display.readAndDispatch()) {
257 display.sleep();
258 }
259 }
260 } catch (Exception e) {
261 e.printStackTrace();
262 }
263 }
264
265 private FindNodesParameters getParameters() {
266 FindNodesParameters params = new FindNodesParameters(findCriteriaTextField.getText());
267
268 params.setMatchCase(matchCaseCheckBox.getSelection());
269 params.setUseWildcards(useWildcardsCheckBox.getSelection());
270 params.setWholeWord(wholeWordsCheckBox.getSelection());
271
272 return params;
273 }
274
275 private void useWildcardsCheckBoxWidgetSelected(SelectionEvent evt) {
276 if (useWildcardsCheckBox.getSelection()) {
277 wholeWordsCheckBox.setEnabled(false);
278 wholeWordsCheckBox.setSelection(false);
279
280 matchCaseCheckBox.setEnabled(false);
281 matchCaseCheckBox.setSelection(false);
282 helpWildcardLabel.setVisible(true);
283
284 } else {
285 wholeWordsCheckBox.setEnabled(true);
286 matchCaseCheckBox.setEnabled(true);
287 helpWildcardLabel.setVisible(false);
288 }
289 }
290
291 private void findCriteriaTextFieldKeyReleased(KeyEvent evt) {
292 if (evt.keyCode == SWT.CR && diagramIsOpened()) {
293 selectNode(nodesFinder.findNext(getParameters(), getNodes()));
294 }
295 }
296
297 private void prevButtonWidgetSelected(SelectionEvent evt) {
298 if (diagramIsOpened()) {
299 selectNode(nodesFinder.findPrev(getParameters(), getNodes()));
300 }
301 }
302
303 private void nextButtonWidgetSelected(SelectionEvent evt) {
304 if (diagramIsOpened()) {
305 selectNode(nodesFinder.findNext(getParameters(), getNodes()));
306 }
307 }
308
309 private void findAllButtonWidgetSelected(SelectionEvent evt) {
310 if (diagramIsOpened()) {
311 selectNodes(nodesFinder.findAll(getParameters(), getNodes()));
312 }
313 }
314
315 private Collection<Node> getNodes() {
316 IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
317 .getActiveEditor();
318 IDiagram diagram = ((DiagramEditorInput)activeEditor.getEditorInput()).getDiagram();
319 return diagram.getNodes();
320 }
321
322 private void cancelButtonWidgetSelected(SelectionEvent evt) {
323 dialogShell.dispose();
324 }
325
326 private boolean diagramIsOpened() {
327 return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor() != null;
328 }
329
330 private void selectNodes(Collection<Node> nodes) {
331 ((AbstractEditor)editor).selectNodes(nodes);
332 }
333
334 private void selectNode(Node node) {
335 if (node == null) {
336 showInteractionFinished();
337 } else {
338 selectNodes(Collections.singleton(node));
339 }
340 }
341
342 private void showInteractionFinished() {
343 if (nodesFinder.foundCount() == 0) {
344 MessageBox box = new MessageBox(Display.getCurrent().getActiveShell(), SWT.OK);
345 box.setText(Messages.CAST);
346 box.setMessage(Messages.NodesFinder_0);
347 box.open();
348 } else {
349 MessageBox box = new MessageBox(Display.getCurrent().getActiveShell(), SWT.OK);
350 box.setText(Messages.CAST);
351 box.setMessage(Messages.NodesFinder_1);
352 box.open();
353 }
354 }
355
356 }