View Javadoc

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   * Main dialog window
28   * 
29   *  @author Marcin Kardys jimiasty(at)poczta.fm
30   * 
31   */
32  
33  /**
34   * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
35   * Builder, which is free for non-commercial use. If Jigloo is being used
36   * commercially (ie, by a corporation, company or business for any purpose
37   * whatever) then you should purchase a license for each developer using Jigloo.
38   * Please visit www.cloudgarden.com for details. Use of Jigloo implies
39   * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
40   * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
41   * ANY CORPORATE OR COMMERCIAL PURPOSE.
42   */
43  public class SearchDialog extends org.eclipse.swt.widgets.Dialog {
44  
45  	private Shell _dialogShell;
46  
47  	private Diagram _diagram;
48  	
49  	/* TODO: If another one algorithm is implemented 
50  			just uncomment code below
51  
52  	private Label _chooseAlgorithmComboLabel;
53  
54  	private Combo _chooseAlgorithmCombo;
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 			 * TODO: If another one algorithm is implemented 
116 					just uncomment code below
117 			
118 			{
119 				_chooseAlgorithmComboLabel = new Label(_dialogShell, SWT.NONE);
120 				_chooseAlgorithmComboLabel
121 						.setText(Path2Messages.SearchDialog_chooseAlgorithmComboLabel);
122 				FormData chooseAlgorithmComboLabelLData = new FormData();
123 				chooseAlgorithmComboLabelLData.left = new FormAttachment(0,
124 						1000, 14);
125 				chooseAlgorithmComboLabelLData.top = new FormAttachment(0,
126 						1000, 71);
127 				_chooseAlgorithmComboLabel
128 						.setLayoutData(chooseAlgorithmComboLabelLData);
129 			}
130 			{
131 				_chooseAlgorithmCombo = new Combo(_dialogShell, SWT.NONE);
132 				_chooseAlgorithmCombo
133 						.setText(Path2Messages.SearchDialog_chooseAlgorithmComboLabel_0);
134 				_chooseAlgorithmCombo.add(
135 						Path2Messages.SearchDialog_chooseAlgorithmComboLabel_0,
136 						0);
137 				_chooseAlgorithmCombo.add(
138 						Path2Messages.SearchDialog_chooseAlgorithmComboLabel_1,
139 						1);
140 				FormData chooseAlgorithmComboLData = new FormData();
141 				chooseAlgorithmComboLData.width = 100;
142 				chooseAlgorithmComboLData.height = 20;
143 				chooseAlgorithmComboLData.left = new FormAttachment(0, 1000,
144 						135);
145 				chooseAlgorithmComboLData.top = new FormAttachment(0, 1000, 71);
146 				_chooseAlgorithmCombo.setLayoutData(chooseAlgorithmComboLData);
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 	 * Action runs when CANCEL button is pressed
198 	 * 
199 	 */
200 	private void cancelButtonMouseUp(MouseEvent evt) {
201 		_dialogShell.dispose();
202 	}
203 
204 	/**
205 	 * 
206 	 * Action runs when OK button is pressed
207 	 * 
208 	 */
209 	private void okButtonMouseUp(MouseEvent evt) {
210 		
211 		String firstNodeName = _firstNodeInput.getText();
212 		String endNodeName = _endNodeInput.getText();
213 
214 		/* TODO: If another one algorithm is implemented 
215 				just uncomment code below	
216 		
217 		int mode = _chooseAlgorithmCombo.getSelectionIndex();
218 		
219 		 */
220 		
221 		int mode = 0;
222 
223 		if (firstNodeName.equals("")) { //$NON-NLS-1$
224 			errorMessageBox(Path2Messages.SearchDialog_err_firstNodeNotSet);
225 		} else if (endNodeName.equals("")) { //$NON-NLS-1$
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 	 * Runs path searching algorithm
241 	 * 
242 	 * @param firstNodeName
243 	 *            start node
244 	 * @param endNodeName
245 	 *            end node
246 	 * @param mode
247 	 *            0 - means Dijkstra's algorithm, 1 - another algorithm
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 	 * @param firstNodeName
277 	 *            name of node to return
278 	 * @return PositionedNode with searched label
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 	 * Error message window
303 	 * 
304 	 * @param message
305 	 *            message to display
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 	 * Constructor
319 	 * @param _editor 
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 	 * Main function for testing
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 }