1 /**********************************************************************
2 * Copyright (c) 2005, 2014 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
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.dialogs
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
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
.swt
.SWT
;
22 import org
.eclipse
.swt
.widgets
.Button
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.TabFolder
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
27 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.SDView
;
28 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.AsyncMessage
;
29 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.AsyncMessageReturn
;
30 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.GraphNode
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.Lifeline
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.Stop
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.SyncMessage
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.core
.SyncMessageReturn
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.handlers
.provider
.ISDFindProvider
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.handlers
.provider
.ISDGraphNodeSupporter
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.uml2sd
.util
.Messages
;
40 * This is the common dialog to define Find and/or Filter Criteria(s)
43 * @author Bernd Hufmann
45 public class SearchFilterDialog
extends Dialog
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 * The find criteria property name
53 protected static final String FIND_CRITERIA
= "findCriteria"; //$NON-NLS-1$
55 * The find expression list property name
57 protected static final String FIND_EXPRESSION_LIST
= "findExpressionList"; //$NON-NLS-1$
59 * The filter criteria poperty name
61 protected static final String FILTER_CRITERIA
= "filterCriteria"; //$NON-NLS-1$
63 * The filter expression list property name
65 protected static final String FILTER_EXPRESSION_LIST
= "filterExpressionList"; //$NON-NLS-1$
67 * The maximum number of expressions stored.
69 protected static final int MAX_EXPRESSION_LIST
= 7;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * The sequence diagram view reference.
78 private final SDView fSdView
;
81 * The tab with the controls for a Criteria
83 private final TabFolder fTabFolder
= null;
86 * The Criteria updated by this dialog
88 private Criteria fCriteria
= null;
91 * The find/filter provider telling which graph nodes are supported
93 private final ISDGraphNodeSupporter fProvider
;
96 * The okText is the text for the Ok button and title is the title of the
98 * Both depend (okText and title (below)) on the usage that is done of this
99 * dialog (find or filter).
101 private String fOkText
;
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
108 private String fTitle
;
111 * List of string expressions that have been searched already
113 private String
[] fExpressionList
;
116 * find is true if the dialog is for the find feature and false for filter
119 private boolean fIsFind
;
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
125 * Standard constructor
128 * A sequence diagram view reference
130 * A graph node supporter provider
132 * A flag to indicate filtering (true) or finding (false)
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
;
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 public Control
createDialogArea(Composite arg0
) {
151 fExpressionList
= Activator
.getDefault().getDialogSettings().getArray(FIND_EXPRESSION_LIST
);
153 fExpressionList
= Activator
.getDefault().getDialogSettings().getArray(FILTER_EXPRESSION_LIST
);
155 if (fExpressionList
== null) {
156 fExpressionList
= new String
[0];
158 return new TabContents(arg0
, fProvider
, getButton(IDialogConstants
.OK_ID
), fExpressionList
);
162 * Open the dialog box
168 if (fCriteria
== null) {
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
));
181 copyFromCriteria(fCriteria
);
183 if (fOkText
!= null) {
184 getButton(IDialogConstants
.OK_ID
).setText(fOkText
);
186 getButton(IDialogConstants
.OK_ID
).setText(Messages
.SequenceDiagram_Find
);
190 getButton(IDialogConstants
.CANCEL_ID
).setText(Messages
.SequenceDiagram_Close
);
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);
200 if (fTitle
!= null) {
201 getShell().setText(fTitle
);
203 getShell().setText(Messages
.SequenceDiagram_SequenceDiagramFind
);
207 getShell().setLocation(getShell().getDisplay().getCursorLocation());
214 * Loads criteria from the dialog settings which are saved in the workspace.
216 protected void loadCriteria() {
218 String CRITERIA
= FIND_CRITERIA
;
220 CRITERIA
= FILTER_CRITERIA
;
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
);
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);
252 * Called when the dialog box ok button is pressed and calls back the
253 * appropriate action provider (ISDFilterProvider or ISDFindProvider).
256 public void okPressed() {
260 super.close(); // Filter is modal
262 if ((fProvider
!= null) && (fProvider
instanceof ISDFindProvider
) && fIsFind
) {
263 boolean result
= ((ISDFindProvider
) fProvider
).find(fCriteria
);
264 TabContents content
= getTabContents();
265 content
.setResult(result
);
270 public void cancelPressed() {
273 if (fProvider
instanceof ISDFindProvider
) {
274 ((ISDFindProvider
) fProvider
).cancel();
278 super.cancelPressed();
282 * Saves the criteria to the dialog settings within the workspace.
284 public void saveCriteria() {
285 String CRITERIA
= FIND_CRITERIA
;
286 String EXPRESSION_LIST
= FIND_EXPRESSION_LIST
;
288 CRITERIA
= FILTER_CRITERIA
;
289 EXPRESSION_LIST
= FILTER_EXPRESSION_LIST
;
291 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings();
292 DialogSettings section
= (DialogSettings
) settings
.getSection(CRITERIA
);
293 if (section
== null) {
294 section
= (DialogSettings
) settings
.addNewSection(CRITERIA
);
296 fCriteria
.save(section
);
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
]);
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
);
313 fExpressionList
= temp
;
314 settings
.put(EXPRESSION_LIST
, fExpressionList
);
319 * Returns the criteria
321 * @return the criteria
323 public Criteria
getCriteria() {
331 * the criteria to set.
333 public void setCriteria(Criteria criteria
) {
334 fCriteria
= criteria
;
338 * Get the current end-user settings from the dialog to a Criteria
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());
354 * Returns the tab content reference.
356 * @return the tab content
358 protected TabContents
getTabContents() {
359 TabContents content
= null;
360 if (fTabFolder
== null) {
361 content
= (TabContents
) getDialogArea();
363 content
= (TabContents
) fTabFolder
.getSelection()[0].getControl();
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
373 * the criteria to copy from
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());
390 * Sets the text to be used for the ok button
395 public void setOkText(String okText
) {
400 * Sets the title to be used for the dialog box.
405 public void setTitle(String title
) {
410 * Gets the text to be used for the ok button
412 * @return the text to be used for the ok button
415 public String
getOkText() {
420 * Sets the IsFind flag (true for find, else for filter)
422 * @param flag value to set
425 protected void setIsFind(boolean flag
) {
430 * Gets the title to be used for the dialog box.
432 * @return the title to be used for the dialog box.
435 public String
getTitle() {
440 * Gets the IsFind flag (true for find, else for filter)
442 * @return true for find, else for filter
445 protected boolean isFind() {