1 package pl.edu.agh.cast.path2;
2 import java.util.Iterator;
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.events.MouseAdapter;
8 import org.eclipse.swt.events.MouseEvent;
9 import org.eclipse.swt.layout.FormAttachment;
10 import org.eclipse.swt.layout.FormData;
11 import org.eclipse.swt.layout.FormLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Display;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.MessageBox;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.swt.widgets.Text;
18 import org.eclipse.ui.IEditorPart;
19
20 import pl.edu.agh.cast.backward.editor.AbstractEditor;
21 import pl.edu.agh.cast.model.visual.backward.Diagram;
22 import pl.edu.agh.cast.model.visual.backward.Node;
23 import pl.edu.agh.cast.path2.messages.Path2Messages;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class SearchDialog extends org.eclipse.swt.widgets.Dialog {
44
45 private Shell _dialogShell;
46
47 private Diagram _diagram;
48
49
50
51
52
53
54
55
56
57
58 private Text _endNodeInput;
59
60 private Label _endNodeLabel;
61
62 private Label _firstNodeLabel;
63
64 private Text _firstNodeInput;
65
66 private Button _okButton;
67
68 private Button _cancelButton;
69
70 private IEditorPart _editor;
71
72 private void open() {
73 try {
74 Shell parent = getParent();
75 _dialogShell = new Shell(parent, SWT.DIALOG_TRIM
76 | SWT.APPLICATION_MODAL);
77 _dialogShell.setLayout(new FormLayout());
78 _dialogShell.layout();
79 _dialogShell.pack();
80 _dialogShell.setSize(380, 180);
81 {
82 _firstNodeLabel = new Label(_dialogShell, SWT.NONE);
83 FormData firstNodeLabelLData = new FormData();
84 firstNodeLabelLData.left = new FormAttachment(0, 1000, 14);
85 firstNodeLabelLData.top = new FormAttachment(0, 1000, 20);
86 _firstNodeLabel.setLayoutData(firstNodeLabelLData);
87 _firstNodeLabel.setText(Path2Messages.SearchDialog_firstNode);
88 }
89 {
90 _endNodeLabel = new Label(_dialogShell, SWT.NONE);
91 FormData label1LData = new FormData();
92 label1LData.left = new FormAttachment(0, 1000, 14);
93 label1LData.top = new FormAttachment(0, 1000, 45);
94 _endNodeLabel.setLayoutData(label1LData);
95 _endNodeLabel.setText(Path2Messages.SearchDialog_endNode);
96 }
97 {
98 _firstNodeInput = new Text(_dialogShell, SWT.NONE);
99 FormData firstNodeLData = new FormData();
100 firstNodeLData.width = 200;
101 firstNodeLData.left = new FormAttachment(0, 1000, 135);
102 firstNodeLData.top = new FormAttachment(0, 1000, 20);
103 _firstNodeInput.setLayoutData(firstNodeLData);
104 }
105 {
106 _endNodeInput = new Text(_dialogShell, SWT.NONE);
107 FormData text1LData = new FormData();
108 text1LData.width = 200;
109 text1LData.left = new FormAttachment(0, 1000, 135);
110 text1LData.top = new FormAttachment(0, 1000, 45);
111 _endNodeInput.setLayoutData(text1LData);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 {
152 FormData okButtonLData = new FormData();
153 okButtonLData.width = 60;
154 okButtonLData.height = 25;
155 okButtonLData.left = new FormAttachment(0, 1000, 190);
156 okButtonLData.top = new FormAttachment(0, 1000, 110);
157 _okButton = new Button(_dialogShell, SWT.PUSH | SWT.CENTER);
158 _okButton.setLayoutData(okButtonLData);
159 _okButton.setText(Path2Messages.SearchDialog_ok);
160 _okButton.addMouseListener(new MouseAdapter() {
161 public void mouseUp(MouseEvent evt) {
162 okButtonMouseUp(evt);
163 }
164 });
165 }
166 {
167 FormData cancelButtonLData = new FormData();
168 cancelButtonLData.width = 60;
169 cancelButtonLData.height = 25;
170 cancelButtonLData.left = new FormAttachment(0, 1000, 265);
171 cancelButtonLData.top = new FormAttachment(0, 1000, 110);
172 _cancelButton = new Button(_dialogShell, SWT.PUSH | SWT.CENTER);
173 _cancelButton.setLayoutData(cancelButtonLData);
174 _cancelButton.setText(Path2Messages.SearchDialog_cancel);
175 _cancelButton.addMouseListener(new MouseAdapter() {
176 public void mouseUp(MouseEvent evt) {
177 cancelButtonMouseUp(evt);
178 }
179 });
180 }
181
182 _dialogShell.setText(Path2Messages.SearchDialog_path2);
183 _dialogShell.setLocation(getParent().toDisplay(100, 100));
184 _dialogShell.open();
185 Display display = _dialogShell.getDisplay();
186 while (!_dialogShell.isDisposed()) {
187 if (!display.readAndDispatch())
188 display.sleep();
189 }
190 } catch (Exception e) {
191 e.printStackTrace();
192 }
193 }
194
195
196
197
198
199
200 private void cancelButtonMouseUp(MouseEvent evt) {
201 _dialogShell.dispose();
202 }
203
204
205
206
207
208
209 private void okButtonMouseUp(MouseEvent evt) {
210
211 String firstNodeName = _firstNodeInput.getText();
212 String endNodeName = _endNodeInput.getText();
213
214
215
216
217
218
219
220
221 int mode = 0;
222
223 if (firstNodeName.equals("")) {
224 errorMessageBox(Path2Messages.SearchDialog_err_firstNodeNotSet);
225 } else if (endNodeName.equals("")) {
226 errorMessageBox(Path2Messages.SearchDialog_err_endNodeNotSet);
227 } else if (getNodeWithLabel(firstNodeName) == null) {
228 errorMessageBox(Path2Messages.SearchDialog_err_firstNodeNotFound);
229 } else if (getNodeWithLabel(endNodeName) == null) {
230 errorMessageBox(Path2Messages.SearchDialog_err_endNodeNotFound);
231 } else {
232 findPath(firstNodeName, endNodeName, mode);
233 _dialogShell.dispose();
234 }
235
236 }
237
238
239
240
241
242
243
244
245
246
247
248
249 private void findPath(String firstNodeName, String endNodeName, int mode) {
250
251 List<Node> path = new LinkedList<Node>();
252
253 Node firstNode = getNodeWithLabel(firstNodeName);
254 Node endNode = getNodeWithLabel(endNodeName);
255
256 switch (mode) {
257 case 0: {
258 path = SearchEngine.findPathDijkstraAlgorithm(firstNode, endNode,
259 _diagram.getNodes());
260 }
261 case 1: {
262
263 }
264 default: {
265 path = SearchEngine.findPathDijkstraAlgorithm(firstNode, endNode,
266 _diagram.getNodes());
267 }
268 }
269
270 path.add(firstNode);
271 ((AbstractEditor)_editor).selectNodes(path);
272 }
273
274
275
276
277
278
279
280 private Node getNodeWithLabel(String firstNodeName) {
281 Iterator<Node> it = _diagram.getNodes().iterator();
282 Node currNode = null;
283 boolean found = false;
284
285 while (it.hasNext() && !found) {
286 if (!found) {
287 currNode = (Node) it.next();
288 if (currNode.getLabel().equalsIgnoreCase(firstNodeName)) {
289 found = true;
290 }
291 }
292 }
293 if (found) {
294 return currNode;
295 } else {
296 return null;
297 }
298 }
299
300
301
302
303
304
305
306
307
308 private void errorMessageBox(String message) {
309 MessageBox box = new MessageBox(Display.getCurrent().getActiveShell(),
310 SWT.OK);
311 box.setText(Path2Messages.CAST);
312 box.setMessage(message);
313 box.open();
314 }
315
316
317
318
319
320
321
322 public SearchDialog(Shell parent, int style, Diagram diagram, IEditorPart editor) {
323 super(parent, style);
324 this._diagram = diagram;
325 this._editor = editor;
326 this.open();
327 }
328
329
330
331
332
333
334 public static void main(String[] args) {
335 try {
336 Display _display = Display.getDefault();
337 Shell shell = new Shell(_display);
338 new SearchDialog(shell, SWT.NULL, null,null);
339 } catch (Exception e) {
340 e.printStackTrace();
341 }
342 }
343 }