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  package pl.edu.agh.cast.ui.dialogs.search;
12  
13  import java.util.Collection;
14  import java.util.LinkedList;
15  import java.util.List;
16  
17  import org.eclipse.swt.SWT;
18  import org.eclipse.swt.custom.CTabFolder;
19  import org.eclipse.swt.custom.CTabItem;
20  import org.eclipse.swt.custom.ScrolledComposite;
21  import org.eclipse.swt.events.MouseAdapter;
22  import org.eclipse.swt.events.MouseEvent;
23  import org.eclipse.swt.layout.FormAttachment;
24  import org.eclipse.swt.layout.FormData;
25  import org.eclipse.swt.layout.FormLayout;
26  import org.eclipse.swt.widgets.Button;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Display;
29  import org.eclipse.swt.widgets.Shell;
30  import org.eclipse.ui.IEditorPart;
31  import org.eclipse.ui.PlatformUI;
32  
33  import pl.edu.agh.cast.backward.editor.AbstractEditor;
34  import pl.edu.agh.cast.backward.editor.operation.search.SearchOperation;
35  import pl.edu.agh.cast.backward.editor.operation.search.filters.IParameterFilter;
36  import pl.edu.agh.cast.model.attributes.Attribute;
37  import pl.edu.agh.cast.model.attributes.NodeAttributeManager;
38  import pl.edu.agh.cast.model.visual.backward.Diagram;
39  import pl.edu.agh.cast.model.visual.backward.Node;
40  import pl.edu.agh.cast.util.Messages;
41  
42  /**
43   * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI Builder, which is free for non-commercial
44   * use. If Jigloo is being used commercially (ie, by a corporation, company or business for any purpose whatever) then
45   * you should purchase a license for each developer using Jigloo. Please visit www.cloudgarden.com for details. Use of
46   * Jigloo implies acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR THIS MACHINE, SO
47   * JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
48   */
49  public class AdvancedSearchDialog extends org.eclipse.swt.widgets.Dialog {
50  
51  	private Shell dialogShell;
52  
53  	private Button okButton;
54  
55  	private Composite nodeComposite;
56  
57  	private ScrolledComposite scrolledComposite1;
58  
59  	private CTabItem pairSearchTab;
60  
61  	private CTabItem cTabItem1;
62  
63  	private CTabFolder cTab;
64  
65  	private Button cancelButton;
66  
67  	private Diagram diagram;
68  
69  	private List<FilterPanel> filterPanels = null;
70  
71  	/**
72  	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog inside a new Shell.
73  	 */
74  	public static void main(String[] args) {
75  		try {
76  			Display display = Display.getDefault();
77  			Shell shell = new Shell(display);
78  			AdvancedSearchDialog inst = new AdvancedSearchDialog(shell, SWT.NULL, null);
79  			inst.open();
80  		} catch (Exception e) {
81  			e.printStackTrace();
82  		}
83  	}
84  
85  	public AdvancedSearchDialog(Shell parent, int style, Diagram diagram) {
86  		super(parent, style);
87  		this.diagram = diagram;
88  		filterPanels = new LinkedList<FilterPanel>();
89  	}
90  
91  	public void open() {
92  		try {
93  			Shell parent = getParent();
94  			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
95  
96  			dialogShell.setLayout(new FormLayout());
97  			dialogShell.layout();
98  			dialogShell.pack();
99  			dialogShell.setSize(506, 350);
100 			dialogShell.setText(Messages.AdvancedSearchDialog_0);
101 			{
102 				FormData cTabLData = new FormData();
103 				cTabLData.width = 470;
104 				cTabLData.height = 250;
105 				cTabLData.left = new FormAttachment(20, 1000, 0);
106 				cTabLData.top = new FormAttachment(20, 1000, 0);
107 				cTab = new CTabFolder(dialogShell, SWT.NONE);
108 				cTab.setLayoutData(cTabLData);
109 				{
110 					cTabItem1 = new CTabItem(cTab, SWT.NONE);
111 					cTabItem1.setText(Messages.AdvancedSearchDialog_1);
112 					{
113 						scrolledComposite1 = new ScrolledComposite(cTab, SWT.V_SCROLL);
114 						cTabItem1.setControl(scrolledComposite1);
115 						scrolledComposite1.setLayout(null);
116 						{
117 							nodeComposite = new Composite(scrolledComposite1, SWT.NONE);
118 							nodeComposite.setLayout(null);
119 							scrolledComposite1.setContent(nodeComposite);
120 							nodeComposite.setBounds(6, 5, 444, 177);
121 
122 							drawFilters();
123 						}
124 					}
125 				}
126 				{
127 					pairSearchTab = new CTabItem(cTab, SWT.NONE);
128 					pairSearchTab.setText(Messages.AdvancedSearchDialog_2);
129 				}
130 				cTab.setSelection(0);
131 			}
132 			{
133 				FormData cancelButtonLData = new FormData();
134 				cancelButtonLData.width = 60;
135 				cancelButtonLData.height = 23;
136 				cancelButtonLData.left = new FormAttachment(0, 1000, 420);
137 				cancelButtonLData.top = new FormAttachment(0, 1000, 288);
138 				cancelButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
139 				cancelButton.setLayoutData(cancelButtonLData);
140 				cancelButton.setText(Messages.AdvancedSearchDialog_3);
141 				cancelButton.addMouseListener(new MouseAdapter() {
142 					@Override
143 					public void mouseUp(MouseEvent evt) {
144 						cancelButtonMouseUp(evt);
145 					}
146 				});
147 			}
148 			{
149 				FormData okButtonLData = new FormData();
150 				okButtonLData.width = 62;
151 				okButtonLData.height = 23;
152 				okButtonLData.left = new FormAttachment(0, 1000, 346);
153 				okButtonLData.top = new FormAttachment(0, 1000, 288);
154 				okButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
155 				okButton.setLayoutData(okButtonLData);
156 				okButton.setText(Messages.AdvancedSearchDialog_4);
157 				okButton.addMouseListener(new MouseAdapter() {
158 					@Override
159 					public void mouseUp(MouseEvent evt) {
160 						okButtonMouseUp(evt);
161 					}
162 				});
163 			}
164 
165 			dialogShell.setLocation(getParent().toDisplay(100, 100));
166 			dialogShell.open();
167 			Display display = dialogShell.getDisplay();
168 			while (!dialogShell.isDisposed()) {
169 				if (!display.readAndDispatch()) {
170 					display.sleep();
171 				}
172 			}
173 		} catch (Exception e) {
174 			e.printStackTrace();
175 		}
176 	}
177 
178 	private void okButtonMouseUp(MouseEvent evt) {
179 
180 		List<IParameterFilter> filters = new LinkedList<IParameterFilter>();
181 		for (FilterPanel filterPanel : filterPanels) {
182 			if (filterPanel.isFilterEnabled()) {
183 				filters.add(filterPanel.getFilter());
184 			}
185 		}
186 
187 		SearchOperation searchOperation = new SearchOperation(diagram.getNodes(), filters);
188 		List<Node> foundNodes = searchOperation.execute();
189 
190 		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
191 		((AbstractEditor)editor).selectNodes(foundNodes);
192 
193 		dialogShell.dispose();
194 	}
195 
196 	private void cancelButtonMouseUp(MouseEvent evt) {
197 		dialogShell.dispose();
198 	}
199 
200 	private void drawFilters() {
201 		NodeAttributeManager manager = diagram.getNodeAttributeManager();
202 		Collection<Attribute> attributes = manager.getAttributes();
203 
204 		int verticalLocation = 5;
205 
206 		for (Attribute attribute : attributes) {
207 			FilterPanel composite = null;
208 
209 			switch (attribute.getType()) {
210 				case Color:
211 					composite = new ColorFilterPanel(nodeComposite, SWT.NONE, attribute.getName());
212 					break;
213 				case Date:
214 					composite = new DateFilterPanel(nodeComposite, SWT.NONE, attribute.getName());
215 					break;
216 				case Integer:
217 					composite = new NumericFilterPanel(nodeComposite, SWT.NONE, attribute.getName());
218 					break;
219 				case String:
220 					composite = new StringFilterPanel(nodeComposite, SWT.NONE, attribute.getName());
221 					break;
222 				default:
223 					continue;
224 			}
225 
226 			composite.setBounds(0, verticalLocation, 450, 25);
227 			filterPanels.add(composite);
228 			verticalLocation = verticalLocation + 25;
229 			nodeComposite.setBounds(0, 0, 444, verticalLocation);
230 		}
231 
232 	}
233 }