1 /**********************************************************************
2 * Copyright (c) 2005, 2012 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
.SDMessages
;
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 protected IViewPart fViewer
= null;
68 * The filter provider implementation
70 protected ISDFilterProvider fProvider
= null;
72 * The filters are the result of editing this list
74 protected List
<FilterCriteria
> fFilters
;
78 protected Button fAdd
;
82 protected Button fRemove
;
86 protected Button fEdit
;
88 * The table with list of filters.
90 protected Table fTable
;
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);
148 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
151 public Control
createDialogArea(Composite parent
) {
153 Group ret
= new Group(parent
, SWT
.NONE
);
154 ret
.setText(SDMessages
._57
);
155 RowLayout rowLayout
= new RowLayout();
156 rowLayout
.wrap
= false;
157 rowLayout
.pack
= true;
158 rowLayout
.justify
= false;
159 rowLayout
.type
= SWT
.HORIZONTAL
;
160 rowLayout
.marginLeft
= 4;
161 rowLayout
.marginTop
= 4;
162 rowLayout
.marginRight
= 4;
163 rowLayout
.marginBottom
= 4;
164 rowLayout
.spacing
= 8;
165 ret
.setLayout(rowLayout
);
167 fTable
= new Table(ret
, SWT
.MULTI
| SWT
.CHECK
);
168 fTable
.setLayoutData(new RowData(220, 84));
169 fTable
.setHeaderVisible(false);
170 fTable
.addSelectionListener(new SelectionListener() {
173 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
176 public void widgetDefaultSelected(SelectionEvent e
) {
177 int count
= fTable
.getSelectionCount();
179 Criteria criteria
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), SDMessages
._63
);
180 if (criteria
!= null) {
181 replaceSelectedCriteria(criteria
);
188 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
191 public void widgetSelected(SelectionEvent e
) {
192 handleTableSelectionCount();
195 if (fFilters
!= null) {
196 for (Iterator
<FilterCriteria
> i
= fFilters
.iterator(); i
.hasNext();) {
197 FilterCriteria filterCriteria
= i
.next();
198 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
202 Composite commands
= new Composite(ret
, SWT
.NONE
);
203 RowLayout rowLayoutCommands
= new RowLayout();
204 rowLayoutCommands
.wrap
= false;
205 rowLayoutCommands
.pack
= false;
206 rowLayoutCommands
.justify
= true;
207 rowLayoutCommands
.type
= SWT
.VERTICAL
;
208 rowLayoutCommands
.marginLeft
= 0;
209 rowLayoutCommands
.marginTop
= 4;
210 rowLayoutCommands
.marginRight
= 0;
211 rowLayoutCommands
.marginBottom
= 4;
212 rowLayoutCommands
.spacing
= 8;
213 commands
.setLayout(rowLayoutCommands
);
214 fAdd
= new Button(commands
, SWT
.NONE
);
215 fAdd
.setText(SDMessages
._61
);
216 fAdd
.addSelectionListener(new SelectionListener() {
219 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
222 public void widgetDefaultSelected(SelectionEvent e
) {
228 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
231 public void widgetSelected(SelectionEvent e
) {
232 Criteria init
= new Criteria();
233 Criteria c
= openFilterDialog(init
, SDMessages
._62
);
235 addCriteria(c
, true, false, null);
240 fEdit
= new Button(commands
, SWT
.NONE
);
241 fEdit
.setText(SDMessages
._60
);
242 fEdit
.addSelectionListener(new SelectionListener() {
245 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
248 public void widgetDefaultSelected(SelectionEvent e
) {
254 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
257 public void widgetSelected(SelectionEvent e
) {
258 Criteria c
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), SDMessages
._63
);
260 replaceSelectedCriteria(c
);
264 fEdit
.setEnabled(false);
266 fRemove
= new Button(commands
, SWT
.NONE
);
267 fRemove
.setText(SDMessages
._64
);
268 fRemove
.addSelectionListener(new SelectionListener() {
271 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
274 public void widgetDefaultSelected(SelectionEvent e
) {
280 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
283 public void widgetSelected(SelectionEvent e
) {
284 fTable
.remove(fTable
.getSelectionIndices());
285 handleTableSelectionCount();
288 fRemove
.setEnabled(false);
290 getShell().setText(SDMessages
._65
);
292 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
293 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
299 * Opens the filter dialog box with given parameter.
301 * @param criteria The criteria reference to pass
302 * @param action to distinguish between "Update" and "Create"
303 * @return the criteria that has been updated or created
305 protected Criteria
openFilterDialog(Criteria criteria
, String action
) {
306 SearchFilterDialog filter
= new SearchFilterDialog((SDView
) fViewer
, fProvider
, true, SWT
.APPLICATION_MODAL
);
307 filter
.setCriteria(criteria
);
308 filter
.setOkText(action
);
309 filter
.setTitle(SDMessages
._66
);
311 return filter
.getCriteria();
316 * @see org.eclipse.jface.window.Window#open()
322 getShell().setLocation(getShell().getDisplay().getCursorLocation());
323 loadFiltersCriteria();
329 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
332 public void okPressed() {
333 if (fTable
.getItemCount() > 0) {
334 fFilters
= new ArrayList
<FilterCriteria
>();
338 for (int i
= 0; i
< fTable
.getItemCount(); i
++) {
339 TableItem item
= fTable
.getItem(i
);
340 CriteriaTableItem cti
= (CriteriaTableItem
) item
.getData();
341 FilterCriteria fc
= new FilterCriteria(cti
.getCriteria(), item
.getChecked(), cti
.isPositive(), cti
.getLoaderClassName());
342 FilterCriteria efc
= FilterCriteria
.find(fc
, fFilters
);
346 efc
.setActive(efc
.isActive() || fc
.isActive());
350 fProvider
.filter(fFilters
);
351 saveFiltersCriteria(fFilters
);
355 * Sets the list of filters.
357 * @param filters The list of filters to set.
359 public void setFilters(List
<FilterCriteria
> filters
) {
364 * Returns the filters list after editing.
366 * @return the filters list after editing
368 public List
<FilterCriteria
> getFilters() {
373 * Loads the filter criteria from global filters which are saved in the dialog settings.
375 protected void loadFiltersCriteria() {
376 List
<FilterCriteria
> globalFilters
= getGlobalFilters();
377 for (Iterator
<FilterCriteria
> i
= globalFilters
.iterator(); i
.hasNext();) {
378 FilterCriteria filterCriteria
= i
.next();
379 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
384 * Returns the global filters which are saved in the dialog settings..
386 * @return the saved global filters
389 public static List
<FilterCriteria
> getGlobalFilters() {
390 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA
);
392 DialogSettings section
= null;
394 List
<FilterCriteria
> globalFilters
= new ArrayList
<FilterCriteria
>();
395 if (settings
!= null) {
397 size
= settings
.getInt(FILTERS_LIST_SIZE
);
398 } catch (NumberFormatException e
) {
399 // This is not a problem
402 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ i
);
404 while ((section
!= null) && (i
< size
)) {
405 FilterCriteria criteria
= new FilterCriteria();
406 criteria
.setCriteria(new Criteria());
407 criteria
.load(section
);
408 globalFilters
.add(criteria
);
409 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ (++i
));
413 return globalFilters
;
417 * Saves the filter criteria in the dialog settings.
419 * @param globalFilters A list of filters to save.
421 public static void saveFiltersCriteria(List
<FilterCriteria
> globalFilters
) {
422 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings();
423 DialogSettings section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
);
424 if (section
== null) {
425 section
= (DialogSettings
) settings
.addNewSection(FILTERS_LIST_CRITERIA
);
428 if (globalFilters
== null) {
429 section
.put(FILTERS_LIST_SIZE
, 0);
433 section
.put(FILTERS_LIST_SIZE
, globalFilters
.size());
435 FilterCriteria criteria
;
437 for (int j
= 0; j
< globalFilters
.size(); j
++) {
438 if (globalFilters
.get(j
) == null) {
442 criteria
= globalFilters
.get(j
);
443 DialogSettings subSection
= (DialogSettings
) section
.getSection(FILTERS_LIST_CRITERIA
+ j
);
445 if (subSection
== null) {
446 subSection
= (DialogSettings
) section
.addNewSection(FILTERS_LIST_CRITERIA
+ j
);
448 criteria
.save(subSection
);
453 * Deactivates the saved global filters.
455 public static void deactivateSavedGlobalFilters() {
456 // Deactivate all filters
457 List
<FilterCriteria
> filters
= getGlobalFilters();
458 for(FilterCriteria criteria
: filters
) {
459 criteria
.setActive(false);
462 FilterListDialog
.saveFiltersCriteria(filters
);
465 // ------------------------------------------------------------------------
467 // ------------------------------------------------------------------------
469 * A class to map TableItems that can be toggled active or inactive and Criteria
471 protected class CriteriaTableItem
{
474 * The criteria reference
476 protected Criteria fCriteria
;
478 * The "positive" value.
480 protected boolean fIsPositive
;
482 * The loader class name
484 protected String fLoaderClassName
;
486 * The actual table item.
488 protected TableItem fTableItem
;
493 * @param parent The parent table
494 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
495 * @param isPositive <code>true</code> for positive filter else <code>false</code>
496 * @param loaderClassName The loader class name
498 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
) {
499 fTableItem
= new TableItem(parent
, SWT
.NONE
);
500 fTableItem
.setData(this);
501 fTableItem
.setChecked(isActive
);
502 fIsPositive
= isPositive
;
503 fLoaderClassName
= loaderClassName
;
509 * @param parent The parent table
510 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
511 * @param isPositive <code>true</code> for positive filter else <code>false</code>
512 * @param loaderClassName The loader class name
513 * @param index The table item index
515 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
, int index
) {
516 fTableItem
= new TableItem(parent
, SWT
.NONE
, index
);
517 fTableItem
.setChecked(isActive
);
518 fIsPositive
= isPositive
;
519 fLoaderClassName
= loaderClassName
;
525 * @param criteria The criteria to set
527 public void setCriteria(Criteria criteria
) {
528 fCriteria
= criteria
;
529 fTableItem
.setText((fIsPositive ? SDMessages
._59
: SDMessages
._58
) + " " + fCriteria
.getExpression() + " " + fCriteria
.getGraphNodeSummary(fProvider
, fLoaderClassName
)); //$NON-NLS-1$ //$NON-NLS-2$
533 * Returns the criteria.
534 * @return the criteria
536 public Criteria
getCriteria() {
541 * Returns whether positive filtering is active or not.
543 * @return <code>true</code> for positive filter else <code>false</code>
545 public boolean isPositive() {
550 * Returns the loader class name.
552 * @return the loader class name
554 public String
getLoaderClassName() {
555 return fLoaderClassName
;