tmf: Switch tmf.ui to Java 7 + fix warnings
[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 */
f26e290b 93 private final 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 */
f26e290b 101 private 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 */
f26e290b 108 private String fTitle;
73005152
BH
109
110 /**
111 * List of string expressions that have been searched already
112 */
f26e290b 113 private 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 */
f26e290b 119 private 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 protected void loadCriteria() {
217
218 String CRITERIA = FIND_CRITERIA;
eb63f5ff 219 if (!fIsFind) {
73005152 220 CRITERIA = FILTER_CRITERIA;
df0b8ff4 221 }
73005152 222
8fd82db5 223 DialogSettings section = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(CRITERIA);
5b5b7df5 224 List<GraphNode> selection = fSdView.getSDWidget().getSelection();
eb63f5ff 225 if ((selection == null || selection.size() != 1) || (!fIsFind)) {
73005152 226 if (section != null) {
eb63f5ff
BH
227 fCriteria = new Criteria();
228 fCriteria.load(section);
73005152
BH
229 }
230 } else {
5b5b7df5 231 GraphNode gn = selection.get(0);
eb63f5ff
BH
232 fCriteria = new Criteria();
233 fCriteria.setExpression(gn.getName());
234 fCriteria.setCaseSenstiveSelected(true);
235 if (gn instanceof Lifeline && fProvider.isNodeSupported(ISDGraphNodeSupporter.LIFELINE)) {
236 fCriteria.setLifeLineSelected(true);
237 } else if (gn instanceof SyncMessageReturn && fProvider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGERETURN)) {
238 fCriteria.setSyncMessageReturnSelected(true);
239 } else if ((gn instanceof SyncMessageReturn || gn instanceof SyncMessage) && fProvider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGE)) {
240 fCriteria.setSyncMessageSelected(true);
241 } else if (gn instanceof AsyncMessageReturn && fProvider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGERETURN)) {
242 fCriteria.setAsyncMessageReturnSelected(true);
243 } else if ((gn instanceof AsyncMessageReturn || gn instanceof AsyncMessage) && fProvider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGE)) {
244 fCriteria.setAsyncMessageSelected(true);
245 } else if (gn instanceof Stop && fProvider.isNodeSupported(ISDGraphNodeSupporter.STOP)) {
246 fCriteria.setStopSelected(true);
73005152
BH
247 }
248 }
249 }
250
251 /**
11252342
AM
252 * Called when the dialog box ok button is pressed and calls back the
253 * appropriate action provider (ISDFilterProvider or ISDFindProvider).
73005152
BH
254 */
255 @Override
256 public void okPressed() {
257 copyToCriteria();
eb63f5ff 258 if (!fIsFind) {
73005152
BH
259 saveCriteria();
260 super.close(); // Filter is modal
261 }
eb63f5ff
BH
262 if ((fProvider != null) && (fProvider instanceof ISDFindProvider) && fIsFind) {
263 boolean result = ((ISDFindProvider) fProvider).find(fCriteria);
73005152
BH
264 TabContents content = getTabContents();
265 content.setResult(result);
266 }
267 }
268
73005152
BH
269 @Override
270 public void cancelPressed() {
eb63f5ff 271 if (fIsFind) {
73005152 272 copyToCriteria();
eb63f5ff
BH
273 if (fProvider instanceof ISDFindProvider) {
274 ((ISDFindProvider) fProvider).cancel();
73005152
BH
275 }
276 saveCriteria();
277 }
278 super.cancelPressed();
279 }
280
281 /**
11252342
AM
282 * Saves the criteria to the dialog settings within the workspace.
283 */
73005152
BH
284 public void saveCriteria() {
285 String CRITERIA = FIND_CRITERIA;
286 String EXPRESSION_LIST = FIND_EXPRESSION_LIST;
eb63f5ff 287 if (!fIsFind) {
73005152
BH
288 CRITERIA = FILTER_CRITERIA;
289 EXPRESSION_LIST = FILTER_EXPRESSION_LIST;
290 }
8fd82db5 291 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings();
73005152
BH
292 DialogSettings section = (DialogSettings) settings.getSection(CRITERIA);
293 if (section == null) {
294 section = (DialogSettings) settings.addNewSection(CRITERIA);
295 }
eb63f5ff 296 fCriteria.save(section);
73005152 297
eb63f5ff 298 if (fCriteria.getExpression().length() > 0) {
507b1336 299 ArrayList<String> list = new ArrayList<>();
eb63f5ff
BH
300 for (int i = 0; i < fExpressionList.length; i++) {
301 list.add(fExpressionList[i]);
73005152
BH
302 }
303 // Remove the used expression if one from the dropdown list
eb63f5ff 304 list.remove(fCriteria.getExpression());
73005152 305 // Put the new expression at the beginning
eb63f5ff 306 list.add(0, fCriteria.getExpression());
73005152
BH
307 // Fill in the expressionList, truncating to MAX_EXPRESSION_LIST
308 int size = Math.min(list.size(), MAX_EXPRESSION_LIST);
309 String[] temp = new String[size];
310 for (int i = 0; i < size; i++) {
abbdd66a 311 temp[i] = list.get(i);
73005152 312 }
eb63f5ff
BH
313 fExpressionList = temp;
314 settings.put(EXPRESSION_LIST, fExpressionList);
73005152
BH
315 }
316 }
317
318 /**
11252342
AM
319 * Returns the criteria
320 *
321 * @return the criteria
322 */
73005152 323 public Criteria getCriteria() {
eb63f5ff 324 return fCriteria;
73005152
BH
325 }
326
327 /**
df0b8ff4 328 * Sets the criteria.
013a5f1c 329 *
11252342
AM
330 * @param criteria
331 * the criteria to set.
73005152 332 */
eb63f5ff
BH
333 public void setCriteria(Criteria criteria) {
334 fCriteria = criteria;
73005152
BH
335 }
336
337 /**
338 * Get the current end-user settings from the dialog to a Criteria
73005152
BH
339 */
340 public void copyToCriteria() {
eb63f5ff 341 fCriteria = new Criteria();
73005152 342 TabContents content = getTabContents();
eb63f5ff
BH
343 fCriteria.setLifeLineSelected(content.isLifelineButtonSelected());
344 fCriteria.setSyncMessageSelected(content.isSynMessageButtonSelected());
345 fCriteria.setSyncMessageReturnSelected(content.isSynMessageReturnButtonSelected());
346 fCriteria.setAsyncMessageSelected(content.isAsynMessageButtonSelected());
347 fCriteria.setAsyncMessageReturnSelected(content.isAsynMessageReturnButtonSelected());
348 fCriteria.setStopSelected(content.isStopButtonSelected());
349 fCriteria.setCaseSenstiveSelected(content.isCaseSensitiveSelected());
350 fCriteria.setExpression(content.getSearchText());
73005152
BH
351 }
352
353 /**
df0b8ff4 354 * Returns the tab content reference.
013a5f1c 355 *
0d9a6d76 356 * @return the tab content
73005152
BH
357 */
358 protected TabContents getTabContents() {
359 TabContents content = null;
eb63f5ff 360 if (fTabFolder == null) {
73005152 361 content = (TabContents) getDialogArea();
df0b8ff4 362 } else {
eb63f5ff 363 content = (TabContents) fTabFolder.getSelection()[0].getControl();
df0b8ff4 364 }
73005152
BH
365 return content;
366 }
367
368 /**
369 * Initialize the dialog with the settings of an existing Criteria<br>
370 * Criteria must not be null and the TabContents must have been created
013a5f1c 371 *
11252342
AM
372 * @param from
373 * the criteria to copy from
73005152
BH
374 */
375 public void copyFromCriteria(Criteria from) {
376 TabContents content = getTabContents();
377 content.setLifelineButtonSelection(from.isLifeLineSelected());
378 content.setSynMessageButtonSelection(from.isSyncMessageSelected());
379 content.setSynMessageReturnButtonSelection(from.isSyncMessageReturnSelected());
380 content.setAsynMessageButtonSelection(from.isAsyncMessageSelected());
381 content.setAsynMessageReturnButtonSelection(from.isSyncMessageReturnSelected());
382 content.setStopButtonSelection(from.isStopSelected());
383 content.setCaseSensitiveSelection(from.isCaseSenstiveSelected());
384 if (from.getExpression() != null) {
385 content.setSearchText(from.getExpression());
386 }
387 }
388
389 /**
df0b8ff4 390 * Sets the text to be used for the ok button
013a5f1c 391 *
11252342
AM
392 * @param okText
393 * text to set
73005152 394 */
eb63f5ff
BH
395 public void setOkText(String okText) {
396 fOkText = okText;
73005152
BH
397 }
398
399 /**
df0b8ff4 400 * Sets the title to be used for the dialog box.
013a5f1c 401 *
11252342
AM
402 * @param title
403 * The title to set
73005152 404 */
eb63f5ff
BH
405 public void setTitle(String title) {
406 fTitle = title;
73005152 407 }
f26e290b
BH
408
409 /**
410 * Gets the text to be used for the ok button
411 *
412 * @return the text to be used for the ok button
413 * @since 2.0
414 */
415 public String getOkText() {
416 return fOkText;
417 }
418
419 /**
420 * Sets the IsFind flag (true for find, else for filter)
421 *
422 * @param flag value to set
423 * @since 2.0
424 */
425 protected void setIsFind(boolean flag) {
426 fIsFind = flag;
427 }
428
429 /**
430 * Gets the title to be used for the dialog box.
431 *
432 * @return the title to be used for the dialog box.
433 * @since 2.0
434 */
435 public String getTitle() {
436 return fTitle;
437 }
438
439 /**
440 * Gets the IsFind flag (true for find, else for filter)
441 *
442 * @return true for find, else for filter
443 * @since 2.0
444 */
445 protected boolean isFind() {
446 return fIsFind;
447 }
448
449
450
73005152 451}
This page took 0.076002 seconds and 5 git commands to generate.