1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.dialogs
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
20 import org
.eclipse
.jface
.dialogs
.Dialog
;
21 import org
.eclipse
.jface
.dialogs
.DialogSettings
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.SDView
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.handlers
.provider
.ISDFilterProvider
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.util
.SDMessages
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.SelectionEvent
;
28 import org
.eclipse
.swt
.events
.SelectionListener
;
29 import org
.eclipse
.swt
.layout
.RowData
;
30 import org
.eclipse
.swt
.layout
.RowLayout
;
31 import org
.eclipse
.swt
.widgets
.Button
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.swt
.widgets
.Group
;
35 import org
.eclipse
.swt
.widgets
.Table
;
36 import org
.eclipse
.swt
.widgets
.TableItem
;
37 import org
.eclipse
.ui
.IViewPart
;
40 * This is the filters list dialog.<br>
41 * It is associated to an SDView and to a ISDFilterProvider.<br>
46 public class FilterListDialog
extends Dialog
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 protected static final String FILTERS_LIST_CRITERIA
= "filtersListsCriteria"; //$NON-NLS-1$
53 protected static final String FILTERS_LIST_SIZE
= "filtersListSize"; //$NON-NLS-1$
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * The viewer and provided are kept here as attributes
62 protected IViewPart viewer
= null;
64 * The filter provider implementation
66 protected ISDFilterProvider provider
= null;
68 * The filters are the result of editing this list
70 protected List
<FilterCriteria
> filters
;
78 protected Button remove
;
82 protected Button edit
;
84 * The table with list of filters.
86 protected Table table
;
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * Standard constructor
95 * @param view_ The view reference
96 * @param loader_ The filter provider implementation
98 public FilterListDialog(IViewPart view_
, ISDFilterProvider loader_
) {
99 super(view_
.getSite().getShell());
103 // filters = provider.getCurrentFilters();
104 setShellStyle(SWT
.DIALOG_TRIM
| SWT
.APPLICATION_MODAL
);
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
111 * Adds a criteria to the table
113 * @param criteria A criteria to add
114 * @param checked A flag whether criteria is checked (selected) or not
115 * @param positive A flag whether criteria is for positive filter or not
116 * @param loaderClassName A loader class name for the filters
118 protected void addCriteria(Criteria criteria
, boolean checked
, boolean positive
, String loaderClassName
) {
119 CriteriaTableItem cti
= new CriteriaTableItem(table
, checked
, positive
, loaderClassName
);
120 cti
.setCriteria(criteria
);
124 * Replaces a selected criteria with a new criteria.
126 * @param new_ A new criteria.
128 protected void replaceSelectedCriteria(Criteria new_
) {
129 CriteriaTableItem cti
= (CriteriaTableItem
) table
.getSelection()[0].getData();
130 cti
.setCriteria(new_
);
134 * Handles table selection count.
136 protected void handleTableSelectionCount() {
137 int count
= table
.getSelectionCount();
138 edit
.setEnabled(count
== 1);
139 remove
.setEnabled(count
> 0);
144 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
147 public Control
createDialogArea(Composite parent
) {
149 Group ret
= new Group(parent
, SWT
.NONE
);
150 ret
.setText(SDMessages
._57
);
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 table
= new Table(ret
, SWT
.MULTI
| SWT
.CHECK
);
164 table
.setLayoutData(new RowData(220, 84));
165 table
.setHeaderVisible(false);
166 table
.addSelectionListener(new SelectionListener() {
169 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
172 public void widgetDefaultSelected(SelectionEvent e
) {
173 int count
= table
.getSelectionCount();
175 Criteria criteria
= openFilterDialog(((CriteriaTableItem
) table
.getSelection()[0].getData()).getCriteria(), SDMessages
._63
);
176 if (criteria
!= null) {
177 replaceSelectedCriteria(criteria
);
184 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
187 public void widgetSelected(SelectionEvent e
) {
188 handleTableSelectionCount();
191 if (filters
!= null) {
192 for (Iterator
<FilterCriteria
> i
= filters
.iterator(); i
.hasNext();) {
193 FilterCriteria filterCriteria
= (FilterCriteria
) i
.next();
194 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
198 Composite commands
= new Composite(ret
, SWT
.NONE
);
199 RowLayout rowLayoutCommands
= new RowLayout();
200 rowLayoutCommands
.wrap
= false;
201 rowLayoutCommands
.pack
= false;
202 rowLayoutCommands
.justify
= true;
203 rowLayoutCommands
.type
= SWT
.VERTICAL
;
204 rowLayoutCommands
.marginLeft
= 0;
205 rowLayoutCommands
.marginTop
= 4;
206 rowLayoutCommands
.marginRight
= 0;
207 rowLayoutCommands
.marginBottom
= 4;
208 rowLayoutCommands
.spacing
= 8;
209 commands
.setLayout(rowLayoutCommands
);
210 add
= new Button(commands
, SWT
.NONE
);
211 add
.setText(SDMessages
._61
);
212 add
.addSelectionListener(new SelectionListener() {
215 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
218 public void widgetDefaultSelected(SelectionEvent e
) {
224 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
227 public void widgetSelected(SelectionEvent e
) {
228 Criteria init
= new Criteria();
229 Criteria c
= openFilterDialog(init
, SDMessages
._62
);
231 addCriteria(c
, true, false, null);
236 edit
= new Button(commands
, SWT
.NONE
);
237 edit
.setText(SDMessages
._60
);
238 edit
.addSelectionListener(new SelectionListener() {
241 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
244 public void widgetDefaultSelected(SelectionEvent e
) {
250 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
253 public void widgetSelected(SelectionEvent e
) {
254 Criteria c
= openFilterDialog(((CriteriaTableItem
) table
.getSelection()[0].getData()).getCriteria(), SDMessages
._63
);
256 replaceSelectedCriteria(c
);
260 edit
.setEnabled(false);
262 remove
= new Button(commands
, SWT
.NONE
);
263 remove
.setText(SDMessages
._64
);
264 remove
.addSelectionListener(new SelectionListener() {
267 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
270 public void widgetDefaultSelected(SelectionEvent e
) {
276 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
279 public void widgetSelected(SelectionEvent e
) {
280 table
.remove(table
.getSelectionIndices());
281 handleTableSelectionCount();
284 remove
.setEnabled(false);
286 getShell().setText(SDMessages
._65
);
288 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
289 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
295 * Opens the filter dialog box with given parameter.
297 * @param criteria The criteria reference to pass
298 * @param action to distinguish between "Update" and "Create"
299 * @return the criteria that has been updated or created
301 protected Criteria
openFilterDialog(Criteria criteria
, String action
) {
302 SearchFilterDialog filter
= new SearchFilterDialog((SDView
) viewer
, provider
, true, SWT
.APPLICATION_MODAL
);
303 filter
.setCriteria(criteria
);
304 filter
.setOkText(action
);
305 filter
.setTitle(SDMessages
._66
);
307 return filter
.getCriteria();
312 * @see org.eclipse.jface.window.Window#open()
318 getShell().setLocation(getShell().getDisplay().getCursorLocation());
319 loadFiltersCriteria();
325 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
328 public void okPressed() {
329 if (table
.getItemCount() > 0) {
330 filters
= new ArrayList
<FilterCriteria
>();
334 for (int i
= 0; i
< table
.getItemCount(); i
++) {
335 TableItem item
= table
.getItem(i
);
336 CriteriaTableItem cti
= (CriteriaTableItem
) item
.getData();
337 FilterCriteria fc
= new FilterCriteria(cti
.getCriteria(), item
.getChecked(), cti
.getPositive(), cti
.getLoaderClassName());
338 FilterCriteria efc
= FilterCriteria
.find(fc
, filters
);
342 efc
.setActive(efc
.isActive() || fc
.isActive());
346 provider
.filter(filters
);
347 saveFiltersCriteria(filters
);
351 * Sets the list of filters.
353 * @param filters_ The list of filters to set.
355 public void setFilters(ArrayList
<FilterCriteria
> filters_
) {
360 * Returns the filters list after editing.
362 * @return the filters list after editing
364 public List
<FilterCriteria
> getFilters() {
369 * Loads the filter criteria from global filters which are saved in the dialog settings.
371 protected void loadFiltersCriteria() {
372 List
<FilterCriteria
> globalFilters
= getGlobalFilters();
373 for (Iterator
<FilterCriteria
> i
= globalFilters
.iterator(); i
.hasNext();) {
374 FilterCriteria filterCriteria
= (FilterCriteria
) i
.next();
375 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
380 * Returns the global filters which are saved in the dialog settings..
382 * @return the saved global filters
384 public static List
<FilterCriteria
> getGlobalFilters() {
385 DialogSettings settings
= (DialogSettings
) TmfUiPlugin
.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA
);
387 DialogSettings section
= null;
389 if (settings
!= null) {
391 size
= settings
.getInt(FILTERS_LIST_SIZE
);
392 } catch (NumberFormatException e
) {
393 // This is not a problem
396 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ i
);
399 List
<FilterCriteria
> globalFilters
= new ArrayList
<FilterCriteria
>();
401 while ((section
!= null) && (i
< size
)) {
402 FilterCriteria criteria
= new FilterCriteria();
403 criteria
.setCriteria(new Criteria());
404 criteria
.load(section
);
405 globalFilters
.add(criteria
);
406 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ (++i
));
409 return globalFilters
;
413 * Saves the filter criteria in the dialog settings.
415 * @param globalFilters A list of filters to save.
417 public static void saveFiltersCriteria(List
<FilterCriteria
> globalFilters
) {
418 DialogSettings settings
= (DialogSettings
) TmfUiPlugin
.getDefault().getDialogSettings();
419 DialogSettings section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
);
420 if (section
== null) {
421 section
= (DialogSettings
) settings
.addNewSection(FILTERS_LIST_CRITERIA
);
424 if (globalFilters
== null) {
425 section
.put(FILTERS_LIST_SIZE
, 0);
429 section
.put(FILTERS_LIST_SIZE
, globalFilters
.size());
431 FilterCriteria criteria
;
433 for (int j
= 0; j
< globalFilters
.size(); j
++) {
434 if (!(globalFilters
.get(j
) instanceof FilterCriteria
)) {
438 criteria
= (FilterCriteria
) globalFilters
.get(j
);
439 DialogSettings subSection
= (DialogSettings
) section
.getSection(FILTERS_LIST_CRITERIA
+ j
);
441 if (subSection
== null) {
442 subSection
= (DialogSettings
) section
.addNewSection(FILTERS_LIST_CRITERIA
+ j
);
444 criteria
.save(subSection
);
449 * Deactivates the saved global filters.
451 public static void deactivateSavedGlobalFilters() {
452 // Deactivate all filters
453 List
<FilterCriteria
> filters
= getGlobalFilters();
454 for(FilterCriteria criteria
: filters
) {
455 criteria
.setActive(false);
458 FilterListDialog
.saveFiltersCriteria(filters
);
461 // ------------------------------------------------------------------------
463 // ------------------------------------------------------------------------
465 * A class to map TableItems that can be toggled active or inactive and Criteria
467 protected class CriteriaTableItem
{
470 * The criteria reference
472 protected Criteria criteria
;
474 * The "positive" value.
476 protected boolean positive
;
478 * The loader class name
480 protected String loaderClassName
;
482 * The actual table item.
484 protected TableItem tableItem
;
489 * @param parent The parent table
490 * @param active_ <code>true</code> if filter criteria is active else <code>false</code>
491 * @param positive_ <code>true</code> for positive filter else <code>false</code>
492 * @param loaderClassName_ The loader class name
494 public CriteriaTableItem(Table parent
, boolean active_
, boolean positive_
, String loaderClassName_
) {
495 tableItem
= new TableItem(parent
, SWT
.NONE
);
496 tableItem
.setData(this);
497 tableItem
.setChecked(active_
);
498 positive
= positive_
;
499 loaderClassName
= loaderClassName_
;
505 * @param parent The parent table
506 * @param active_ <code>true</code> if filter criteria is active else <code>false</code>
507 * @param positive_ <code>true</code> for positive filter else <code>false</code>
508 * @param loaderClassName_ The loader class name
509 * @param index The table item index
511 public CriteriaTableItem(Table parent
, boolean checked_
, boolean positive_
, String loaderClassName_
, int index
) {
512 tableItem
= new TableItem(parent
, SWT
.NONE
, index
);
513 tableItem
.setChecked(checked_
);
514 positive
= positive_
;
515 loaderClassName
= loaderClassName_
;
521 * @param criteria_ The criteria to set
523 public void setCriteria(Criteria criteria_
) {
524 criteria
= criteria_
;
525 tableItem
.setText((positive ? SDMessages
._59
: SDMessages
._58
) + " " + criteria
.getExpression() + " " + criteria
.getGraphNodeSummary(provider
, loaderClassName
)); //$NON-NLS-1$ //$NON-NLS-2$
529 * Returns the criteria.
530 * @return the criteria
532 public Criteria
getCriteria() {
537 * Returns whether positive filtering is active or not.
539 * @return <code>true</code> for positive filter else <code>false</code>
541 public boolean getPositive() {
546 * Returns the loader class name.
548 * @return the loader class name
550 public String
getLoaderClassName() {
551 return loaderClassName
;