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 // filters = provider.getCurrentFilters();
108 setShellStyle(SWT
.DIALOG_TRIM
| SWT
.APPLICATION_MODAL
);
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
115 * Adds a criteria to the table
117 * @param criteria A criteria to add
118 * @param checked A flag whether criteria is checked (selected) or not
119 * @param positive A flag whether criteria is for positive filter or not
120 * @param loaderClassName A loader class name for the filters
122 protected void addCriteria(Criteria criteria
, boolean checked
, boolean positive
, String loaderClassName
) {
123 CriteriaTableItem cti
= new CriteriaTableItem(fTable
, checked
, positive
, loaderClassName
);
124 cti
.setCriteria(criteria
);
128 * Replaces a selected criteria with a new criteria.
130 * @param newCriteria A new criteria.
132 protected void replaceSelectedCriteria(Criteria newCriteria
) {
133 CriteriaTableItem cti
= (CriteriaTableItem
) fTable
.getSelection()[0].getData();
134 cti
.setCriteria(newCriteria
);
138 * Handles table selection count.
140 protected void handleTableSelectionCount() {
141 int count
= fTable
.getSelectionCount();
142 fEdit
.setEnabled(count
== 1);
143 fRemove
.setEnabled(count
> 0);
147 public Control
createDialogArea(Composite parent
) {
149 Group ret
= new Group(parent
, SWT
.NONE
);
150 ret
.setText(Messages
.SequenceDiagram_ListOfHideDisplayPatterns
);
151 RowLayout rowLayout
= new RowLayout();
152 rowLayout
.wrap
= false;
153 rowLayout
.pack
= true;
154 rowLayout
.justify
= false;
155 rowLayout
.type
= SWT
.HORIZONTAL
;
156 rowLayout
.marginLeft
= 4;
157 rowLayout
.marginTop
= 4;
158 rowLayout
.marginRight
= 4;
159 rowLayout
.marginBottom
= 4;
160 rowLayout
.spacing
= 8;
161 ret
.setLayout(rowLayout
);
163 fTable
= new Table(ret
, SWT
.MULTI
| SWT
.CHECK
);
164 fTable
.setLayoutData(new RowData(220, 84));
165 fTable
.setHeaderVisible(false);
166 fTable
.addSelectionListener(new SelectionListener() {
169 public void widgetDefaultSelected(SelectionEvent e
) {
170 int count
= fTable
.getSelectionCount();
172 Criteria criteria
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), Messages
.SequenceDiagram_Update
);
173 if (criteria
!= null) {
174 replaceSelectedCriteria(criteria
);
180 public void widgetSelected(SelectionEvent e
) {
181 handleTableSelectionCount();
184 if (fFilters
!= null) {
185 for (Iterator
<FilterCriteria
> i
= fFilters
.iterator(); i
.hasNext();) {
186 FilterCriteria filterCriteria
= i
.next();
187 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
191 Composite commands
= new Composite(ret
, SWT
.NONE
);
192 RowLayout rowLayoutCommands
= new RowLayout();
193 rowLayoutCommands
.wrap
= false;
194 rowLayoutCommands
.pack
= false;
195 rowLayoutCommands
.justify
= true;
196 rowLayoutCommands
.type
= SWT
.VERTICAL
;
197 rowLayoutCommands
.marginLeft
= 0;
198 rowLayoutCommands
.marginTop
= 4;
199 rowLayoutCommands
.marginRight
= 0;
200 rowLayoutCommands
.marginBottom
= 4;
201 rowLayoutCommands
.spacing
= 8;
202 commands
.setLayout(rowLayoutCommands
);
203 fAdd
= new Button(commands
, SWT
.NONE
);
204 fAdd
.setText(Messages
.SequenceDiagram_Add
);
205 fAdd
.addSelectionListener(new SelectionListener() {
208 public void widgetDefaultSelected(SelectionEvent e
) {
213 public void widgetSelected(SelectionEvent e
) {
214 Criteria init
= new Criteria();
215 Criteria c
= openFilterDialog(init
, Messages
.SequenceDiagram_Create
);
217 addCriteria(c
, true, false, null);
222 fEdit
= new Button(commands
, SWT
.NONE
);
223 fEdit
.setText(Messages
.SequenceDiagram_EditIt
);
224 fEdit
.addSelectionListener(new SelectionListener() {
227 public void widgetDefaultSelected(SelectionEvent e
) {
232 public void widgetSelected(SelectionEvent e
) {
233 Criteria c
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), Messages
.SequenceDiagram_Update
);
235 replaceSelectedCriteria(c
);
239 fEdit
.setEnabled(false);
241 fRemove
= new Button(commands
, SWT
.NONE
);
242 fRemove
.setText(Messages
.SequenceDiagram_Remove
);
243 fRemove
.addSelectionListener(new SelectionListener() {
246 public void widgetDefaultSelected(SelectionEvent e
) {
251 public void widgetSelected(SelectionEvent e
) {
252 fTable
.remove(fTable
.getSelectionIndices());
253 handleTableSelectionCount();
256 fRemove
.setEnabled(false);
258 getShell().setText(Messages
.SequenceDiagram_SequenceDiagramHidePatterns
);
260 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
261 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
267 * Opens the filter dialog box with given parameter.
269 * @param criteria The criteria reference to pass
270 * @param action to distinguish between "Update" and "Create"
271 * @return the criteria that has been updated or created
273 protected Criteria
openFilterDialog(Criteria criteria
, String action
) {
274 SearchFilterDialog filter
= new SearchFilterDialog((SDView
) fViewer
, fProvider
, true, SWT
.APPLICATION_MODAL
);
275 filter
.setCriteria(criteria
);
276 filter
.setOkText(action
);
277 filter
.setTitle(Messages
.SequenceDiagram_DefinitionOfHidePattern
);
279 return filter
.getCriteria();
286 getShell().setLocation(getShell().getDisplay().getCursorLocation());
287 loadFiltersCriteria();
292 public void okPressed() {
293 if (fTable
.getItemCount() > 0) {
294 fFilters
= new ArrayList
<FilterCriteria
>();
298 for (int i
= 0; i
< fTable
.getItemCount(); i
++) {
299 TableItem item
= fTable
.getItem(i
);
300 CriteriaTableItem cti
= (CriteriaTableItem
) item
.getData();
301 FilterCriteria fc
= new FilterCriteria(cti
.getCriteria(), item
.getChecked(), cti
.isPositive(), cti
.getLoaderClassName());
302 FilterCriteria efc
= FilterCriteria
.find(fc
, fFilters
);
306 efc
.setActive(efc
.isActive() || fc
.isActive());
310 fProvider
.filter(fFilters
);
311 saveFiltersCriteria(fFilters
);
315 * Sets the list of filters.
317 * @param filters The list of filters to set.
319 public void setFilters(List
<FilterCriteria
> filters
) {
324 * Returns the filters list after editing.
326 * @return the filters list after editing
328 public List
<FilterCriteria
> getFilters() {
333 * Loads the filter criteria from global filters which are saved in the dialog settings.
335 protected void loadFiltersCriteria() {
336 List
<FilterCriteria
> globalFilters
= getGlobalFilters();
337 for (Iterator
<FilterCriteria
> i
= globalFilters
.iterator(); i
.hasNext();) {
338 FilterCriteria filterCriteria
= i
.next();
339 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
344 * Returns the global filters which are saved in the dialog settings..
346 * @return the saved global filters
349 public static List
<FilterCriteria
> getGlobalFilters() {
350 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA
);
352 DialogSettings section
= null;
354 List
<FilterCriteria
> globalFilters
= new ArrayList
<FilterCriteria
>();
355 if (settings
!= null) {
357 size
= settings
.getInt(FILTERS_LIST_SIZE
);
358 } catch (NumberFormatException e
) {
359 // This is not a problem
362 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ i
);
364 while ((section
!= null) && (i
< size
)) {
365 FilterCriteria criteria
= new FilterCriteria();
366 criteria
.setCriteria(new Criteria());
367 criteria
.load(section
);
368 globalFilters
.add(criteria
);
369 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ (++i
));
373 return globalFilters
;
377 * Saves the filter criteria in the dialog settings.
379 * @param globalFilters A list of filters to save.
381 public static void saveFiltersCriteria(List
<FilterCriteria
> globalFilters
) {
382 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings();
383 DialogSettings section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
);
384 if (section
== null) {
385 section
= (DialogSettings
) settings
.addNewSection(FILTERS_LIST_CRITERIA
);
388 if (globalFilters
== null) {
389 section
.put(FILTERS_LIST_SIZE
, 0);
393 section
.put(FILTERS_LIST_SIZE
, globalFilters
.size());
395 FilterCriteria criteria
;
397 for (int j
= 0; j
< globalFilters
.size(); j
++) {
398 if (globalFilters
.get(j
) == null) {
402 criteria
= globalFilters
.get(j
);
403 DialogSettings subSection
= (DialogSettings
) section
.getSection(FILTERS_LIST_CRITERIA
+ j
);
405 if (subSection
== null) {
406 subSection
= (DialogSettings
) section
.addNewSection(FILTERS_LIST_CRITERIA
+ j
);
408 criteria
.save(subSection
);
413 * Deactivates the saved global filters.
415 public static void deactivateSavedGlobalFilters() {
416 // Deactivate all filters
417 List
<FilterCriteria
> filters
= getGlobalFilters();
418 for(FilterCriteria criteria
: filters
) {
419 criteria
.setActive(false);
422 FilterListDialog
.saveFiltersCriteria(filters
);
425 // ------------------------------------------------------------------------
427 // ------------------------------------------------------------------------
429 * A class to map TableItems that can be toggled active or inactive and Criteria
431 protected class CriteriaTableItem
{
434 * The criteria reference
436 protected Criteria fCriteria
;
438 * The "positive" value.
440 protected boolean fIsPositive
;
442 * The loader class name
444 protected String fLoaderClassName
;
446 * The actual table item.
448 protected TableItem fTableItem
;
453 * @param parent The parent table
454 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
455 * @param isPositive <code>true</code> for positive filter else <code>false</code>
456 * @param loaderClassName The loader class name
458 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
) {
459 fTableItem
= new TableItem(parent
, SWT
.NONE
);
460 fTableItem
.setData(this);
461 fTableItem
.setChecked(isActive
);
462 fIsPositive
= isPositive
;
463 fLoaderClassName
= loaderClassName
;
469 * @param parent The parent table
470 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
471 * @param isPositive <code>true</code> for positive filter else <code>false</code>
472 * @param loaderClassName The loader class name
473 * @param index The table item index
475 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
, int index
) {
476 fTableItem
= new TableItem(parent
, SWT
.NONE
, index
);
477 fTableItem
.setChecked(isActive
);
478 fIsPositive
= isPositive
;
479 fLoaderClassName
= loaderClassName
;
485 * @param criteria The criteria to set
487 public void setCriteria(Criteria criteria
) {
488 fCriteria
= criteria
;
489 fTableItem
.setText((fIsPositive ? Messages
.SequenceDiagram_display
: Messages
.SequenceDiagram_hide
) + " " + fCriteria
.getExpression() + " " + fCriteria
.getGraphNodeSummary(fProvider
, fLoaderClassName
)); //$NON-NLS-1$ //$NON-NLS-2$
493 * Returns the criteria.
494 * @return the criteria
496 public Criteria
getCriteria() {
501 * Returns whether positive filtering is active or not.
503 * @return <code>true</code> for positive filter else <code>false</code>
505 public boolean isPositive() {
510 * Returns the loader class name.
512 * @return the loader class name
514 public String
getLoaderClassName() {
515 return fLoaderClassName
;