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