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
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.dialogs
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
19 import org
.eclipse
.jface
.dialogs
.Dialog
;
20 import org
.eclipse
.jface
.dialogs
.DialogSettings
;
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
.handlers
.provider
.ISDFilterProvider
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.Messages
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.events
.SelectionEvent
;
27 import org
.eclipse
.swt
.events
.SelectionListener
;
28 import org
.eclipse
.swt
.layout
.RowData
;
29 import org
.eclipse
.swt
.layout
.RowLayout
;
30 import org
.eclipse
.swt
.widgets
.Button
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Group
;
34 import org
.eclipse
.swt
.widgets
.Table
;
35 import org
.eclipse
.swt
.widgets
.TableItem
;
36 import org
.eclipse
.ui
.IViewPart
;
39 * This is the filters list dialog.<br>
40 * It is associated to an SDView and to a ISDFilterProvider.<br>
45 public class FilterListDialog
extends Dialog
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 * Filter list criteria property name
53 protected static final String FILTERS_LIST_CRITERIA
= "filtersListsCriteria"; //$NON-NLS-1$
55 * Filter list size property name
57 protected static final String FILTERS_LIST_SIZE
= "filtersListSize"; //$NON-NLS-1$
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 * The viewer and provided are kept here as attributes
66 private final IViewPart fViewer
;
68 * The filter provider implementation
70 private final ISDFilterProvider fProvider
;
72 * The filters are the result of editing this list
74 private List
<FilterCriteria
> fFilters
;
82 private Button fRemove
;
88 * The table with list of filters.
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 * Standard constructor
99 * @param view The view reference
100 * @param loader The filter provider implementation
102 public FilterListDialog(IViewPart view
, ISDFilterProvider loader
) {
103 super(view
.getSite().getShell());
107 setShellStyle(SWT
.DIALOG_TRIM
| SWT
.APPLICATION_MODAL
);
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
114 * Adds a criteria to the table
116 * @param criteria A criteria to add
117 * @param checked A flag whether criteria is checked (selected) or not
118 * @param positive A flag whether criteria is for positive filter or not
119 * @param loaderClassName A loader class name for the filters
121 protected void addCriteria(Criteria criteria
, boolean checked
, boolean positive
, String loaderClassName
) {
122 CriteriaTableItem cti
= new CriteriaTableItem(fTable
, checked
, positive
, loaderClassName
);
123 cti
.setCriteria(criteria
);
127 * Replaces a selected criteria with a new criteria.
129 * @param newCriteria A new criteria.
131 protected void replaceSelectedCriteria(Criteria newCriteria
) {
132 CriteriaTableItem cti
= (CriteriaTableItem
) fTable
.getSelection()[0].getData();
133 cti
.setCriteria(newCriteria
);
137 * Handles table selection count.
139 protected void handleTableSelectionCount() {
140 int count
= fTable
.getSelectionCount();
141 fEdit
.setEnabled(count
== 1);
142 fRemove
.setEnabled(count
> 0);
146 public Control
createDialogArea(Composite parent
) {
148 Group ret
= new Group(parent
, SWT
.NONE
);
149 ret
.setText(Messages
.SequenceDiagram_ListOfHideDisplayPatterns
);
150 RowLayout rowLayout
= new RowLayout();
151 rowLayout
.wrap
= false;
152 rowLayout
.pack
= true;
153 rowLayout
.justify
= false;
154 rowLayout
.type
= SWT
.HORIZONTAL
;
155 rowLayout
.marginLeft
= 4;
156 rowLayout
.marginTop
= 4;
157 rowLayout
.marginRight
= 4;
158 rowLayout
.marginBottom
= 4;
159 rowLayout
.spacing
= 8;
160 ret
.setLayout(rowLayout
);
162 fTable
= new Table(ret
, SWT
.MULTI
| SWT
.CHECK
);
163 fTable
.setLayoutData(new RowData(220, 84));
164 fTable
.setHeaderVisible(false);
165 fTable
.addSelectionListener(new SelectionListener() {
168 public void widgetDefaultSelected(SelectionEvent e
) {
169 int count
= fTable
.getSelectionCount();
171 Criteria criteria
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), Messages
.SequenceDiagram_Update
);
172 if (criteria
!= null) {
173 replaceSelectedCriteria(criteria
);
179 public void widgetSelected(SelectionEvent e
) {
180 handleTableSelectionCount();
183 if (fFilters
!= null) {
184 for (Iterator
<FilterCriteria
> i
= fFilters
.iterator(); i
.hasNext();) {
185 FilterCriteria filterCriteria
= i
.next();
186 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
190 Composite commands
= new Composite(ret
, SWT
.NONE
);
191 RowLayout rowLayoutCommands
= new RowLayout();
192 rowLayoutCommands
.wrap
= false;
193 rowLayoutCommands
.pack
= false;
194 rowLayoutCommands
.justify
= true;
195 rowLayoutCommands
.type
= SWT
.VERTICAL
;
196 rowLayoutCommands
.marginLeft
= 0;
197 rowLayoutCommands
.marginTop
= 4;
198 rowLayoutCommands
.marginRight
= 0;
199 rowLayoutCommands
.marginBottom
= 4;
200 rowLayoutCommands
.spacing
= 8;
201 commands
.setLayout(rowLayoutCommands
);
202 fAdd
= new Button(commands
, SWT
.NONE
);
203 fAdd
.setText(Messages
.SequenceDiagram_Add
);
204 fAdd
.addSelectionListener(new SelectionListener() {
207 public void widgetDefaultSelected(SelectionEvent e
) {
212 public void widgetSelected(SelectionEvent e
) {
213 Criteria init
= new Criteria();
214 Criteria c
= openFilterDialog(init
, Messages
.SequenceDiagram_Create
);
216 addCriteria(c
, true, false, null);
221 fEdit
= new Button(commands
, SWT
.NONE
);
222 fEdit
.setText(Messages
.SequenceDiagram_EditIt
);
223 fEdit
.addSelectionListener(new SelectionListener() {
226 public void widgetDefaultSelected(SelectionEvent e
) {
231 public void widgetSelected(SelectionEvent e
) {
232 Criteria c
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), Messages
.SequenceDiagram_Update
);
234 replaceSelectedCriteria(c
);
238 fEdit
.setEnabled(false);
240 fRemove
= new Button(commands
, SWT
.NONE
);
241 fRemove
.setText(Messages
.SequenceDiagram_Remove
);
242 fRemove
.addSelectionListener(new SelectionListener() {
245 public void widgetDefaultSelected(SelectionEvent e
) {
250 public void widgetSelected(SelectionEvent e
) {
251 fTable
.remove(fTable
.getSelectionIndices());
252 handleTableSelectionCount();
255 fRemove
.setEnabled(false);
257 getShell().setText(Messages
.SequenceDiagram_SequenceDiagramHidePatterns
);
259 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
260 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
266 * Opens the filter dialog box with given parameter.
268 * @param criteria The criteria reference to pass
269 * @param action to distinguish between "Update" and "Create"
270 * @return the criteria that has been updated or created
272 protected Criteria
openFilterDialog(Criteria criteria
, String action
) {
273 SearchFilterDialog filter
= new SearchFilterDialog((SDView
) fViewer
, fProvider
, true, SWT
.APPLICATION_MODAL
);
274 filter
.setCriteria(criteria
);
275 filter
.setOkText(action
);
276 filter
.setTitle(Messages
.SequenceDiagram_DefinitionOfHidePattern
);
278 return filter
.getCriteria();
285 getShell().setLocation(getShell().getDisplay().getCursorLocation());
286 loadFiltersCriteria();
291 public void okPressed() {
292 if (fTable
.getItemCount() > 0) {
293 fFilters
= new ArrayList
<>();
297 for (int i
= 0; i
< fTable
.getItemCount(); i
++) {
298 TableItem item
= fTable
.getItem(i
);
299 CriteriaTableItem cti
= (CriteriaTableItem
) item
.getData();
300 FilterCriteria fc
= new FilterCriteria(cti
.getCriteria(), item
.getChecked(), cti
.isPositive(), cti
.getLoaderClassName());
301 FilterCriteria efc
= FilterCriteria
.find(fc
, fFilters
);
305 efc
.setActive(efc
.isActive() || fc
.isActive());
309 fProvider
.filter(fFilters
);
310 saveFiltersCriteria(fFilters
);
314 * Sets the list of filters.
316 * @param filters The list of filters to set.
318 public void setFilters(List
<FilterCriteria
> filters
) {
323 * Returns the filters list after editing.
325 * @return the filters list after editing
327 public List
<FilterCriteria
> getFilters() {
332 * Loads the filter criteria from global filters which are saved in the dialog settings.
334 protected void loadFiltersCriteria() {
335 List
<FilterCriteria
> globalFilters
= getGlobalFilters();
336 for (Iterator
<FilterCriteria
> i
= globalFilters
.iterator(); i
.hasNext();) {
337 FilterCriteria filterCriteria
= i
.next();
338 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
343 * Returns the global filters which are saved in the dialog settings..
345 * @return the saved global filters
348 public static List
<FilterCriteria
> getGlobalFilters() {
349 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA
);
351 DialogSettings section
= null;
353 List
<FilterCriteria
> globalFilters
= new ArrayList
<>();
354 if (settings
!= null) {
356 size
= settings
.getInt(FILTERS_LIST_SIZE
);
357 } catch (NumberFormatException e
) {
358 // This is not a problem
361 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ i
);
363 while ((section
!= null) && (i
< size
)) {
364 FilterCriteria criteria
= new FilterCriteria();
365 criteria
.setCriteria(new Criteria());
366 criteria
.load(section
);
367 globalFilters
.add(criteria
);
368 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ (++i
));
372 return globalFilters
;
376 * Saves the filter criteria in the dialog settings.
378 * @param globalFilters A list of filters to save.
380 public static void saveFiltersCriteria(List
<FilterCriteria
> globalFilters
) {
381 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings();
382 DialogSettings section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
);
383 if (section
== null) {
384 section
= (DialogSettings
) settings
.addNewSection(FILTERS_LIST_CRITERIA
);
387 if (globalFilters
== null) {
388 section
.put(FILTERS_LIST_SIZE
, 0);
392 section
.put(FILTERS_LIST_SIZE
, globalFilters
.size());
394 FilterCriteria criteria
;
396 for (int j
= 0; j
< globalFilters
.size(); j
++) {
397 if (globalFilters
.get(j
) == null) {
401 criteria
= globalFilters
.get(j
);
402 DialogSettings subSection
= (DialogSettings
) section
.getSection(FILTERS_LIST_CRITERIA
+ j
);
404 if (subSection
== null) {
405 subSection
= (DialogSettings
) section
.addNewSection(FILTERS_LIST_CRITERIA
+ j
);
407 criteria
.save(subSection
);
412 * Deactivates the saved global filters.
414 public static void deactivateSavedGlobalFilters() {
415 // Deactivate all filters
416 List
<FilterCriteria
> filters
= getGlobalFilters();
417 for(FilterCriteria criteria
: filters
) {
418 criteria
.setActive(false);
421 FilterListDialog
.saveFiltersCriteria(filters
);
424 // ------------------------------------------------------------------------
426 // ------------------------------------------------------------------------
428 * A class to map TableItems that can be toggled active or inactive and Criteria
430 protected class CriteriaTableItem
{
433 * The criteria reference
435 protected Criteria fCriteria
;
437 * The "positive" value.
439 protected boolean fIsPositive
;
441 * The loader class name
443 protected String fLoaderClassName
;
445 * The actual table item.
447 protected TableItem fTableItem
;
452 * @param parent The parent table
453 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
454 * @param isPositive <code>true</code> for positive filter else <code>false</code>
455 * @param loaderClassName The loader class name
457 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
) {
458 fTableItem
= new TableItem(parent
, SWT
.NONE
);
459 fTableItem
.setData(this);
460 fTableItem
.setChecked(isActive
);
461 fIsPositive
= isPositive
;
462 fLoaderClassName
= loaderClassName
;
468 * @param parent The parent table
469 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
470 * @param isPositive <code>true</code> for positive filter else <code>false</code>
471 * @param loaderClassName The loader class name
472 * @param index The table item index
474 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
, int index
) {
475 fTableItem
= new TableItem(parent
, SWT
.NONE
, index
);
476 fTableItem
.setChecked(isActive
);
477 fIsPositive
= isPositive
;
478 fLoaderClassName
= loaderClassName
;
484 * @param criteria The criteria to set
486 public void setCriteria(Criteria criteria
) {
487 fCriteria
= criteria
;
488 fTableItem
.setText((fIsPositive ? Messages
.SequenceDiagram_display
: Messages
.SequenceDiagram_hide
) + " " + fCriteria
.getExpression() + " " + fCriteria
.getGraphNodeSummary(fProvider
, fLoaderClassName
)); //$NON-NLS-1$ //$NON-NLS-2$
492 * Returns the criteria.
493 * @return the criteria
495 public Criteria
getCriteria() {
500 * Returns whether positive filtering is active or not.
502 * @return <code>true</code> for positive filter else <code>false</code>
504 public boolean isPositive() {
509 * Returns the loader class name.
511 * @return the loader class name
513 public String
getLoaderClassName() {
514 return fLoaderClassName
;