Contribution for Bug352466: [TMF] Implement UML2 Sequence Diagram
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / handlers / widgets / SearchFilterDialog.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 * $Id: SearchFilterDialog.java,v 1.4 2008/01/24 02:29:09 apnan Exp $
8 *
9 * Contributors:
10 * IBM - Initial API and implementation
11 * Bernd Hufmann - Updated for TMF
12 **********************************************************************/
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.widgets;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.DialogSettings;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
22 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
23 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.AsyncMessage;
24 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.AsyncMessageReturn;
25 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode;
26 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline;
27 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Stop;
28 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.SyncMessage;
29 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.SyncMessageReturn;
30 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFindProvider;
31 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter;
32 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.TabFolder;
38
39 /**
40 * This is the common dialog to define Find and/or Filter Criteria(s)
41 */
42 public class SearchFilterDialog extends Dialog {
43
44 protected static final String FIND_CRITERIA = "findCriteria"; //$NON-NLS-1$
45 protected static final String FIND_EXPRESSION_LIST = "findExpressionList"; //$NON-NLS-1$
46 protected static final String FILTER_CRITERIA = "filterCriteria"; //$NON-NLS-1$
47 protected static final String FILTER_EXPRESSION_LIST = "filterExpressionList"; //$NON-NLS-1$
48 protected static final int MAX_EXPRESSION_LIST = 7;
49
50 /**
51 * viewer
52 */
53 protected SDView viewer = null;
54
55 /**
56 * tab with the controls for a Criteria
57 */
58 protected TabFolder tab = null;
59
60 /**
61 * Criteria updated by this dialog
62 */
63 protected Criteria criteria = null;
64
65 /**
66 * find/filter provider telling which graph nodes are supported
67 */
68 protected ISDGraphNodeSupporter provider = null;
69
70 /**
71 * okText is the text for the Ok button and title is the title of the dialog.<br>
72 * Both depend on the usage that is done of this dialog (find or filter).
73 */
74 protected String okText, title;
75
76 /**
77 * List of string expressions that have been searched already
78 */
79 protected String[] expressionList;
80
81 /**
82 * find is true if the dialog is for the find feature and false for filter feature
83 */
84 protected boolean find;
85
86 /*
87 * (non-Javadoc)
88 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
89 */
90 @Override
91 public Control createDialogArea(Composite arg0) {
92 if (find)
93 expressionList = TmfUiPlugin.getDefault().getDialogSettings().getArray(FIND_EXPRESSION_LIST);
94 else
95 expressionList = TmfUiPlugin.getDefault().getDialogSettings().getArray(FILTER_EXPRESSION_LIST);
96 if (expressionList == null) {
97 expressionList = new String[0];
98 }
99 return new TabContents(arg0, provider, getButton(IDialogConstants.OK_ID), expressionList);
100 }
101
102 /**
103 * @param theView
104 * @param _provider
105 */
106 public SearchFilterDialog(SDView view_, ISDGraphNodeSupporter provider_, boolean filter_, int style) {
107 super(view_.getSDWidget().getShell());
108 setShellStyle(SWT.DIALOG_TRIM | style);
109 provider = provider_;
110 viewer = view_;
111 find = !filter_;
112 }
113
114 /**
115 * Open the dialog box
116 */
117 @Override
118 public int open() {
119 create();
120
121 if (criteria == null)
122 loadCriteria();
123 if (criteria == null) {
124 criteria = new Criteria();
125 criteria.setLifeLineSelected(provider.isNodeSupported(ISDGraphNodeSupporter.LIFELINE));
126 criteria.setSyncMessageSelected(provider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGE));
127 criteria.setSyncMessageReturnSelected(provider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGERETURN));
128 criteria.setAsyncMessageSelected(provider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGE));
129 criteria.setAsyncMessageReturnSelected(provider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGERETURN));
130 criteria.setStopSelected(provider.isNodeSupported(ISDGraphNodeSupporter.STOP));
131 }
132 copyFromCriteria(criteria);
133
134 if (okText != null) {
135 getButton(IDialogConstants.OK_ID).setText(okText);
136 } else {
137 getButton(IDialogConstants.OK_ID).setText(SDMessages._21);
138 }
139
140 if (find) {
141 getButton(IDialogConstants.CANCEL_ID).setText(SDMessages._22);
142 }
143
144 Button okButton = getButton(IDialogConstants.OK_ID);
145 ((TabContents) getDialogArea()).setOkButton(okButton);
146 if (criteria == null || !((criteria.getExpression() != null && !criteria.getExpression().equals("")) && //$NON-NLS-1$
147 (criteria.isAsyncMessageReturnSelected() || criteria.isAsyncMessageSelected() || criteria.isLifeLineSelected() || criteria.isStopSelected() || criteria.isSyncMessageReturnSelected() || criteria.isSyncMessageSelected()))) {
148 okButton.setEnabled(false);
149 }
150
151 if (title != null) {
152 getShell().setText(title);
153 } else {
154 getShell().setText(SDMessages._24);
155 }
156
157 getShell().pack();
158 getShell().setLocation(getShell().getDisplay().getCursorLocation());
159
160 criteria = null;
161 return super.open();
162 }
163
164 /**
165 *
166 */
167 @SuppressWarnings("rawtypes")
168 protected void loadCriteria() {
169
170 String CRITERIA = FIND_CRITERIA;
171 if (!find)
172 CRITERIA = FILTER_CRITERIA;
173
174 DialogSettings section = (DialogSettings) TmfUiPlugin.getDefault().getDialogSettings().getSection(CRITERIA);
175 List selection = viewer.getSDWidget().getSelection();
176 if ((selection == null || selection.size() != 1) || (!find)) {
177 if (section != null) {
178 criteria = new Criteria();
179 criteria.load(section);
180 }
181 } else {
182 GraphNode gn = (GraphNode) selection.get(0);
183 criteria = new Criteria();
184 criteria.setExpression(gn.getName());
185 criteria.setCaseSenstiveSelected(true);
186 if (gn instanceof Lifeline && provider.isNodeSupported(ISDGraphNodeSupporter.LIFELINE)) {
187 criteria.setLifeLineSelected(true);
188 } else if (gn instanceof SyncMessageReturn && provider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGERETURN)) {
189 criteria.setSyncMessageReturnSelected(true);
190 } else if ((gn instanceof SyncMessageReturn || gn instanceof SyncMessage) && provider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGE)) {
191 criteria.setSyncMessageSelected(true);
192 } else if (gn instanceof AsyncMessageReturn && provider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGERETURN)) {
193 criteria.setAsyncMessageReturnSelected(true);
194 } else if ((gn instanceof AsyncMessageReturn || gn instanceof AsyncMessage) && provider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGE)) {
195 criteria.setAsyncMessageSelected(true);
196 } else if (gn instanceof Stop && provider.isNodeSupported(ISDGraphNodeSupporter.STOP)) {
197 criteria.setStopSelected(true);
198 }
199 }
200 }
201
202 /**
203 * Called when the dialog box ok button is pressed and call back the appropriate action provider (ISDFilterProvider
204 * or ISDFindProvider)
205 */
206 @Override
207 public void okPressed() {
208 copyToCriteria();
209 if (!find) {
210 saveCriteria();
211 super.close(); // Filter is modal
212 }
213 if ((provider != null) && (provider instanceof ISDFindProvider) && find) {
214 boolean result = ((ISDFindProvider) provider).find(criteria);
215 TabContents content = getTabContents();
216 content.setResult(result);
217 }
218 }
219
220 /**
221 *
222 */
223 @Override
224 public void cancelPressed() {
225 if (find) {
226 copyToCriteria();
227 if (provider instanceof ISDFindProvider) {
228 ((ISDFindProvider) provider).cancel();
229 }
230 saveCriteria();
231 }
232 super.cancelPressed();
233 }
234
235 /**
236 *
237 */
238 public void saveCriteria() {
239 String CRITERIA = FIND_CRITERIA;
240 String EXPRESSION_LIST = FIND_EXPRESSION_LIST;
241 if (!find) {
242 CRITERIA = FILTER_CRITERIA;
243 EXPRESSION_LIST = FILTER_EXPRESSION_LIST;
244 }
245 DialogSettings settings = (DialogSettings) TmfUiPlugin.getDefault().getDialogSettings();
246 DialogSettings section = (DialogSettings) settings.getSection(CRITERIA);
247 if (section == null) {
248 section = (DialogSettings) settings.addNewSection(CRITERIA);
249 }
250 criteria.save(section);
251
252 if (criteria.getExpression().length() > 0) {
253 ArrayList<String> list = new ArrayList<String>();
254 for (int i = 0; i < expressionList.length; i++) {
255 list.add(expressionList[i]);
256 }
257 // Remove the used expression if one from the dropdown list
258 list.remove(criteria.getExpression());
259 // Put the new expression at the beginning
260 list.add(0, criteria.getExpression());
261 // Fill in the expressionList, truncating to MAX_EXPRESSION_LIST
262 int size = Math.min(list.size(), MAX_EXPRESSION_LIST);
263 String[] temp = new String[size];
264 for (int i = 0; i < size; i++) {
265 temp[i] = (String) list.get(i);
266 }
267 expressionList = temp;
268 settings.put(EXPRESSION_LIST, expressionList);
269 }
270 }
271
272 /**
273 *
274 */
275 public Criteria getCriteria() {
276 return criteria;
277 }
278
279 /**
280 * @param criteria_
281 */
282 public void setCriteria(Criteria criteria_) {
283 criteria = criteria_;
284 }
285
286 /**
287 * Get the current end-user settings from the dialog to a Criteria
288 *
289 * @param content
290 */
291 public void copyToCriteria() {
292 criteria = new Criteria();
293 TabContents content = getTabContents();
294 criteria.setLifeLineSelected(content.getLifelineButtonSelection());
295 criteria.setSyncMessageSelected(content.getSynMessageButtonSelection());
296 criteria.setSyncMessageReturnSelected(content.getSynMessageReturnButtonSelection());
297 criteria.setAsyncMessageSelected(content.getAsynMessageButtonSelection());
298 criteria.setAsyncMessageReturnSelected(content.getAsynMessageReturnButtonSelection());
299 criteria.setStopSelected(content.getStopButtonSelection());
300 criteria.setCaseSenstiveSelected(content.getCaseSensitiveSelection());
301 criteria.setExpression(content.getSearchText());
302 }
303
304 /**
305 * @return
306 * @todo Generated comment
307 */
308 protected TabContents getTabContents() {
309 TabContents content = null;
310 if (tab == null)
311 content = (TabContents) getDialogArea();
312 else
313 content = (TabContents) tab.getSelection()[0].getControl();
314 return content;
315 }
316
317 /**
318 * Initialize the dialog with the settings of an existing Criteria<br>
319 * Criteria must not be null and the TabContents must have been created
320 *
321 * @param from
322 */
323 public void copyFromCriteria(Criteria from) {
324 TabContents content = getTabContents();
325 content.setLifelineButtonSelection(from.isLifeLineSelected());
326 content.setSynMessageButtonSelection(from.isSyncMessageSelected());
327 content.setSynMessageReturnButtonSelection(from.isSyncMessageReturnSelected());
328 content.setAsynMessageButtonSelection(from.isAsyncMessageSelected());
329 content.setAsynMessageReturnButtonSelection(from.isSyncMessageReturnSelected());
330 content.setStopButtonSelection(from.isStopSelected());
331 content.setCaseSensitiveSelection(from.isCaseSenstiveSelected());
332 if (from.getExpression() != null) {
333 content.setSearchText(from.getExpression());
334 }
335 }
336
337 /**
338 * @param okText_
339 */
340 public void setOkText(String okText_) {
341 okText = okText_;
342 }
343
344 /**
345 * @param title_
346 */
347 public void setTitle(String title_) {
348 title = title_;
349 }
350 }
This page took 0.038377 seconds and 5 git commands to generate.