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