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
1 /**********************************************************************
2 * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
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 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs;
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.internal.tmf.ui.Activator;
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.Messages;
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 * @version 1.0
43 * @author Bernd Hufmann
44 */
45 public class SearchFilterDialog extends Dialog {
46
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50 /**
51 * The find criteria property name
52 */
53 protected static final String FIND_CRITERIA = "findCriteria"; //$NON-NLS-1$
54 /**
55 * The find expression list property name
56 */
57 protected static final String FIND_EXPRESSION_LIST = "findExpressionList"; //$NON-NLS-1$
58 /**
59 * The filter criteria poperty name
60 */
61 protected static final String FILTER_CRITERIA = "filterCriteria"; //$NON-NLS-1$
62 /**
63 * The filter expression list property name
64 */
65 protected static final String FILTER_EXPRESSION_LIST = "filterExpressionList"; //$NON-NLS-1$
66 /**
67 * The maximum number of expressions stored.
68 */
69 protected static final int MAX_EXPRESSION_LIST = 7;
70
71 // ------------------------------------------------------------------------
72 // Attributes
73 // ------------------------------------------------------------------------
74
75 /**
76 * The sequence diagram view reference.
77 */
78 private final SDView fSdView;
79
80 /**
81 * The tab with the controls for a Criteria
82 */
83 private final TabFolder fTabFolder = null;
84
85 /**
86 * The Criteria updated by this dialog
87 */
88 private Criteria fCriteria = null;
89
90 /**
91 * The find/filter provider telling which graph nodes are supported
92 */
93 private final ISDGraphNodeSupporter fProvider;
94
95 /**
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).
100 */
101 private String fOkText;
102
103 /**
104 * The title is the title of the dialog.<br>
105 * Both depend (okText and title) on the usage that is done of this dialog
106 * (find or filter).
107 */
108 private String fTitle;
109
110 /**
111 * List of string expressions that have been searched already
112 */
113 private String[] fExpressionList;
114
115 /**
116 * find is true if the dialog is for the find feature and false for filter
117 * feature
118 */
119 private boolean fIsFind;
120
121 // ------------------------------------------------------------------------
122 // Constructors
123 // ------------------------------------------------------------------------
124 /**
125 * Standard constructor
126 *
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
135 */
136 public SearchFilterDialog(SDView view, ISDGraphNodeSupporter provider, boolean filter, int style) {
137 super(view.getSDWidget().getShell());
138 setShellStyle(SWT.DIALOG_TRIM | style);
139 fProvider = provider;
140 fSdView = view;
141 fIsFind = !filter;
142 }
143
144 // ------------------------------------------------------------------------
145 // Methods
146 // ------------------------------------------------------------------------
147
148 @Override
149 public Control createDialogArea(Composite arg0) {
150 if (fIsFind) {
151 fExpressionList = Activator.getDefault().getDialogSettings().getArray(FIND_EXPRESSION_LIST);
152 } else {
153 fExpressionList = Activator.getDefault().getDialogSettings().getArray(FILTER_EXPRESSION_LIST);
154 }
155 if (fExpressionList == null) {
156 fExpressionList = new String[0];
157 }
158 return new TabContents(arg0, fProvider, getButton(IDialogConstants.OK_ID), fExpressionList);
159 }
160
161 /**
162 * Open the dialog box
163 */
164 @Override
165 public int open() {
166 create();
167
168 if (fCriteria == null) {
169 loadCriteria();
170 }
171
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));
180 }
181 copyFromCriteria(fCriteria);
182
183 if (fOkText != null) {
184 getButton(IDialogConstants.OK_ID).setText(fOkText);
185 } else {
186 getButton(IDialogConstants.OK_ID).setText(Messages.SequenceDiagram_Find);
187 }
188
189 if (fIsFind) {
190 getButton(IDialogConstants.CANCEL_ID).setText(Messages.SequenceDiagram_Close);
191 }
192
193 Button okButton = getButton(IDialogConstants.OK_ID);
194 ((TabContents) getDialogArea()).setOkButton(okButton);
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()))) {
197 okButton.setEnabled(false);
198 }
199
200 if (fTitle != null) {
201 getShell().setText(fTitle);
202 } else {
203 getShell().setText(Messages.SequenceDiagram_SequenceDiagramFind);
204 }
205
206 getShell().pack();
207 getShell().setLocation(getShell().getDisplay().getCursorLocation());
208
209 fCriteria = null;
210 return super.open();
211 }
212
213 /**
214 * Loads criteria from the dialog settings which are saved in the workspace.
215 */
216 protected void loadCriteria() {
217
218 String CRITERIA = FIND_CRITERIA;
219 if (!fIsFind) {
220 CRITERIA = FILTER_CRITERIA;
221 }
222
223 DialogSettings section = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(CRITERIA);
224 List<GraphNode> selection = fSdView.getSDWidget().getSelection();
225 if ((selection == null || selection.size() != 1) || (!fIsFind)) {
226 if (section != null) {
227 fCriteria = new Criteria();
228 fCriteria.load(section);
229 }
230 } else {
231 GraphNode gn = selection.get(0);
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);
247 }
248 }
249 }
250
251 /**
252 * Called when the dialog box ok button is pressed and calls back the
253 * appropriate action provider (ISDFilterProvider or ISDFindProvider).
254 */
255 @Override
256 public void okPressed() {
257 copyToCriteria();
258 if (!fIsFind) {
259 saveCriteria();
260 super.close(); // Filter is modal
261 }
262 if ((fProvider != null) && (fProvider instanceof ISDFindProvider) && fIsFind) {
263 boolean result = ((ISDFindProvider) fProvider).find(fCriteria);
264 TabContents content = getTabContents();
265 content.setResult(result);
266 }
267 }
268
269 @Override
270 public void cancelPressed() {
271 if (fIsFind) {
272 copyToCriteria();
273 if (fProvider instanceof ISDFindProvider) {
274 ((ISDFindProvider) fProvider).cancel();
275 }
276 saveCriteria();
277 }
278 super.cancelPressed();
279 }
280
281 /**
282 * Saves the criteria to the dialog settings within the workspace.
283 */
284 public void saveCriteria() {
285 String CRITERIA = FIND_CRITERIA;
286 String EXPRESSION_LIST = FIND_EXPRESSION_LIST;
287 if (!fIsFind) {
288 CRITERIA = FILTER_CRITERIA;
289 EXPRESSION_LIST = FILTER_EXPRESSION_LIST;
290 }
291 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings();
292 DialogSettings section = (DialogSettings) settings.getSection(CRITERIA);
293 if (section == null) {
294 section = (DialogSettings) settings.addNewSection(CRITERIA);
295 }
296 fCriteria.save(section);
297
298 if (fCriteria.getExpression().length() > 0) {
299 ArrayList<String> list = new ArrayList<>();
300 for (int i = 0; i < fExpressionList.length; i++) {
301 list.add(fExpressionList[i]);
302 }
303 // Remove the used expression if one from the dropdown list
304 list.remove(fCriteria.getExpression());
305 // Put the new expression at the beginning
306 list.add(0, fCriteria.getExpression());
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++) {
311 temp[i] = list.get(i);
312 }
313 fExpressionList = temp;
314 settings.put(EXPRESSION_LIST, fExpressionList);
315 }
316 }
317
318 /**
319 * Returns the criteria
320 *
321 * @return the criteria
322 */
323 public Criteria getCriteria() {
324 return fCriteria;
325 }
326
327 /**
328 * Sets the criteria.
329 *
330 * @param criteria
331 * the criteria to set.
332 */
333 public void setCriteria(Criteria criteria) {
334 fCriteria = criteria;
335 }
336
337 /**
338 * Get the current end-user settings from the dialog to a Criteria
339 */
340 public void copyToCriteria() {
341 fCriteria = new Criteria();
342 TabContents content = getTabContents();
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());
351 }
352
353 /**
354 * Returns the tab content reference.
355 *
356 * @return the tab content
357 */
358 protected TabContents getTabContents() {
359 TabContents content = null;
360 if (fTabFolder == null) {
361 content = (TabContents) getDialogArea();
362 } else {
363 content = (TabContents) fTabFolder.getSelection()[0].getControl();
364 }
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
371 *
372 * @param from
373 * the criteria to copy from
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 /**
390 * Sets the text to be used for the ok button
391 *
392 * @param okText
393 * text to set
394 */
395 public void setOkText(String okText) {
396 fOkText = okText;
397 }
398
399 /**
400 * Sets the title to be used for the dialog box.
401 *
402 * @param title
403 * The title to set
404 */
405 public void setTitle(String title) {
406 fTitle = title;
407 }
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
451 }
This page took 0.044086 seconds and 5 git commands to generate.