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
.Activator
;
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 * Filter list criteria property name
54 protected static final String FILTERS_LIST_CRITERIA
= "filtersListsCriteria"; //$NON-NLS-1$
56 * Filter list size property name
58 protected static final String FILTERS_LIST_SIZE
= "filtersListSize"; //$NON-NLS-1$
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * The viewer and provided are kept here as attributes
67 protected IViewPart fViewer
= null;
69 * The filter provider implementation
71 protected ISDFilterProvider fProvider
= null;
73 * The filters are the result of editing this list
75 protected List
<FilterCriteria
> fFilters
;
79 protected Button fAdd
;
83 protected Button fRemove
;
87 protected Button fEdit
;
89 * The table with list of filters.
91 protected Table fTable
;
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * Standard constructor
100 * @param view The view reference
101 * @param loader The filter provider implementation
103 public FilterListDialog(IViewPart view
, ISDFilterProvider loader
) {
104 super(view
.getSite().getShell());
108 // filters = provider.getCurrentFilters();
109 setShellStyle(SWT
.DIALOG_TRIM
| SWT
.APPLICATION_MODAL
);
112 // ------------------------------------------------------------------------
114 // ------------------------------------------------------------------------
116 * Adds a criteria to the table
118 * @param criteria A criteria to add
119 * @param checked A flag whether criteria is checked (selected) or not
120 * @param positive A flag whether criteria is for positive filter or not
121 * @param loaderClassName A loader class name for the filters
123 protected void addCriteria(Criteria criteria
, boolean checked
, boolean positive
, String loaderClassName
) {
124 CriteriaTableItem cti
= new CriteriaTableItem(fTable
, checked
, positive
, loaderClassName
);
125 cti
.setCriteria(criteria
);
129 * Replaces a selected criteria with a new criteria.
131 * @param newCriteria A new criteria.
133 protected void replaceSelectedCriteria(Criteria newCriteria
) {
134 CriteriaTableItem cti
= (CriteriaTableItem
) fTable
.getSelection()[0].getData();
135 cti
.setCriteria(newCriteria
);
139 * Handles table selection count.
141 protected void handleTableSelectionCount() {
142 int count
= fTable
.getSelectionCount();
143 fEdit
.setEnabled(count
== 1);
144 fRemove
.setEnabled(count
> 0);
149 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
152 public Control
createDialogArea(Composite parent
) {
154 Group ret
= new Group(parent
, SWT
.NONE
);
155 ret
.setText(SDMessages
._57
);
156 RowLayout rowLayout
= new RowLayout();
157 rowLayout
.wrap
= false;
158 rowLayout
.pack
= true;
159 rowLayout
.justify
= false;
160 rowLayout
.type
= SWT
.HORIZONTAL
;
161 rowLayout
.marginLeft
= 4;
162 rowLayout
.marginTop
= 4;
163 rowLayout
.marginRight
= 4;
164 rowLayout
.marginBottom
= 4;
165 rowLayout
.spacing
= 8;
166 ret
.setLayout(rowLayout
);
168 fTable
= new Table(ret
, SWT
.MULTI
| SWT
.CHECK
);
169 fTable
.setLayoutData(new RowData(220, 84));
170 fTable
.setHeaderVisible(false);
171 fTable
.addSelectionListener(new SelectionListener() {
174 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
177 public void widgetDefaultSelected(SelectionEvent e
) {
178 int count
= fTable
.getSelectionCount();
180 Criteria criteria
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), SDMessages
._63
);
181 if (criteria
!= null) {
182 replaceSelectedCriteria(criteria
);
189 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
192 public void widgetSelected(SelectionEvent e
) {
193 handleTableSelectionCount();
196 if (fFilters
!= null) {
197 for (Iterator
<FilterCriteria
> i
= fFilters
.iterator(); i
.hasNext();) {
198 FilterCriteria filterCriteria
= (FilterCriteria
) i
.next();
199 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
203 Composite commands
= new Composite(ret
, SWT
.NONE
);
204 RowLayout rowLayoutCommands
= new RowLayout();
205 rowLayoutCommands
.wrap
= false;
206 rowLayoutCommands
.pack
= false;
207 rowLayoutCommands
.justify
= true;
208 rowLayoutCommands
.type
= SWT
.VERTICAL
;
209 rowLayoutCommands
.marginLeft
= 0;
210 rowLayoutCommands
.marginTop
= 4;
211 rowLayoutCommands
.marginRight
= 0;
212 rowLayoutCommands
.marginBottom
= 4;
213 rowLayoutCommands
.spacing
= 8;
214 commands
.setLayout(rowLayoutCommands
);
215 fAdd
= new Button(commands
, SWT
.NONE
);
216 fAdd
.setText(SDMessages
._61
);
217 fAdd
.addSelectionListener(new SelectionListener() {
220 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
223 public void widgetDefaultSelected(SelectionEvent e
) {
229 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
232 public void widgetSelected(SelectionEvent e
) {
233 Criteria init
= new Criteria();
234 Criteria c
= openFilterDialog(init
, SDMessages
._62
);
236 addCriteria(c
, true, false, null);
241 fEdit
= new Button(commands
, SWT
.NONE
);
242 fEdit
.setText(SDMessages
._60
);
243 fEdit
.addSelectionListener(new SelectionListener() {
246 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
249 public void widgetDefaultSelected(SelectionEvent e
) {
255 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
258 public void widgetSelected(SelectionEvent e
) {
259 Criteria c
= openFilterDialog(((CriteriaTableItem
) fTable
.getSelection()[0].getData()).getCriteria(), SDMessages
._63
);
261 replaceSelectedCriteria(c
);
265 fEdit
.setEnabled(false);
267 fRemove
= new Button(commands
, SWT
.NONE
);
268 fRemove
.setText(SDMessages
._64
);
269 fRemove
.addSelectionListener(new SelectionListener() {
272 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
275 public void widgetDefaultSelected(SelectionEvent e
) {
281 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
284 public void widgetSelected(SelectionEvent e
) {
285 fTable
.remove(fTable
.getSelectionIndices());
286 handleTableSelectionCount();
289 fRemove
.setEnabled(false);
291 getShell().setText(SDMessages
._65
);
293 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
294 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
300 * Opens the filter dialog box with given parameter.
302 * @param criteria The criteria reference to pass
303 * @param action to distinguish between "Update" and "Create"
304 * @return the criteria that has been updated or created
306 protected Criteria
openFilterDialog(Criteria criteria
, String action
) {
307 SearchFilterDialog filter
= new SearchFilterDialog((SDView
) fViewer
, fProvider
, true, SWT
.APPLICATION_MODAL
);
308 filter
.setCriteria(criteria
);
309 filter
.setOkText(action
);
310 filter
.setTitle(SDMessages
._66
);
312 return filter
.getCriteria();
317 * @see org.eclipse.jface.window.Window#open()
323 getShell().setLocation(getShell().getDisplay().getCursorLocation());
324 loadFiltersCriteria();
330 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
333 public void okPressed() {
334 if (fTable
.getItemCount() > 0) {
335 fFilters
= new ArrayList
<FilterCriteria
>();
339 for (int i
= 0; i
< fTable
.getItemCount(); i
++) {
340 TableItem item
= fTable
.getItem(i
);
341 CriteriaTableItem cti
= (CriteriaTableItem
) item
.getData();
342 FilterCriteria fc
= new FilterCriteria(cti
.getCriteria(), item
.getChecked(), cti
.isPositive(), cti
.getLoaderClassName());
343 FilterCriteria efc
= FilterCriteria
.find(fc
, fFilters
);
347 efc
.setActive(efc
.isActive() || fc
.isActive());
351 fProvider
.filter(fFilters
);
352 saveFiltersCriteria(fFilters
);
356 * Sets the list of filters.
358 * @param filters The list of filters to set.
360 public void setFilters(List
<FilterCriteria
> filters
) {
365 * Returns the filters list after editing.
367 * @return the filters list after editing
369 public List
<FilterCriteria
> getFilters() {
374 * Loads the filter criteria from global filters which are saved in the dialog settings.
376 protected void loadFiltersCriteria() {
377 List
<FilterCriteria
> globalFilters
= getGlobalFilters();
378 for (Iterator
<FilterCriteria
> i
= globalFilters
.iterator(); i
.hasNext();) {
379 FilterCriteria filterCriteria
= (FilterCriteria
) i
.next();
380 addCriteria(filterCriteria
.getCriteria(), filterCriteria
.isActive(), filterCriteria
.isPositive(), filterCriteria
.getLoaderClassName());
385 * Returns the global filters which are saved in the dialog settings..
387 * @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 if (settings
!= null) {
396 size
= settings
.getInt(FILTERS_LIST_SIZE
);
397 } catch (NumberFormatException e
) {
398 // This is not a problem
401 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ i
);
404 List
<FilterCriteria
> globalFilters
= new ArrayList
<FilterCriteria
>();
406 while ((section
!= null) && (i
< size
)) {
407 FilterCriteria criteria
= new FilterCriteria();
408 criteria
.setCriteria(new Criteria());
409 criteria
.load(section
);
410 globalFilters
.add(criteria
);
411 section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
+ (++i
));
414 return globalFilters
;
418 * Saves the filter criteria in the dialog settings.
420 * @param globalFilters A list of filters to save.
422 public static void saveFiltersCriteria(List
<FilterCriteria
> globalFilters
) {
423 DialogSettings settings
= (DialogSettings
) Activator
.getDefault().getDialogSettings();
424 DialogSettings section
= (DialogSettings
) settings
.getSection(FILTERS_LIST_CRITERIA
);
425 if (section
== null) {
426 section
= (DialogSettings
) settings
.addNewSection(FILTERS_LIST_CRITERIA
);
429 if (globalFilters
== null) {
430 section
.put(FILTERS_LIST_SIZE
, 0);
434 section
.put(FILTERS_LIST_SIZE
, globalFilters
.size());
436 FilterCriteria criteria
;
438 for (int j
= 0; j
< globalFilters
.size(); j
++) {
439 if (!(globalFilters
.get(j
) instanceof FilterCriteria
)) {
443 criteria
= (FilterCriteria
) globalFilters
.get(j
);
444 DialogSettings subSection
= (DialogSettings
) section
.getSection(FILTERS_LIST_CRITERIA
+ j
);
446 if (subSection
== null) {
447 subSection
= (DialogSettings
) section
.addNewSection(FILTERS_LIST_CRITERIA
+ j
);
449 criteria
.save(subSection
);
454 * Deactivates the saved global filters.
456 public static void deactivateSavedGlobalFilters() {
457 // Deactivate all filters
458 List
<FilterCriteria
> filters
= getGlobalFilters();
459 for(FilterCriteria criteria
: filters
) {
460 criteria
.setActive(false);
463 FilterListDialog
.saveFiltersCriteria(filters
);
466 // ------------------------------------------------------------------------
468 // ------------------------------------------------------------------------
470 * A class to map TableItems that can be toggled active or inactive and Criteria
472 protected class CriteriaTableItem
{
475 * The criteria reference
477 protected Criteria fCriteria
;
479 * The "positive" value.
481 protected boolean fIsPositive
;
483 * The loader class name
485 protected String fLoaderClassName
;
487 * The actual table item.
489 protected TableItem fTableItem
;
494 * @param parent The parent table
495 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
496 * @param isPositive <code>true</code> for positive filter else <code>false</code>
497 * @param loaderClassName The loader class name
499 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
) {
500 fTableItem
= new TableItem(parent
, SWT
.NONE
);
501 fTableItem
.setData(this);
502 fTableItem
.setChecked(isActive
);
503 fIsPositive
= isPositive
;
504 fLoaderClassName
= loaderClassName
;
510 * @param parent The parent table
511 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
512 * @param isPositive <code>true</code> for positive filter else <code>false</code>
513 * @param loaderClassName The loader class name
514 * @param index The table item index
516 public CriteriaTableItem(Table parent
, boolean isActive
, boolean isPositive
, String loaderClassName
, int index
) {
517 fTableItem
= new TableItem(parent
, SWT
.NONE
, index
);
518 fTableItem
.setChecked(isActive
);
519 fIsPositive
= isPositive
;
520 fLoaderClassName
= loaderClassName
;
526 * @param criteria The criteria to set
528 public void setCriteria(Criteria criteria
) {
529 fCriteria
= criteria
;
530 fTableItem
.setText((fIsPositive ? SDMessages
._59
: SDMessages
._58
) + " " + fCriteria
.getExpression() + " " + fCriteria
.getGraphNodeSummary(fProvider
, fLoaderClassName
)); //$NON-NLS-1$ //$NON-NLS-2$
534 * Returns the criteria.
535 * @return the criteria
537 public Criteria
getCriteria() {
542 * Returns whether positive filtering is active or not.
544 * @return <code>true</code> for positive filter else <code>false</code>
546 public boolean isPositive() {
551 * Returns the loader class name.
553 * @return the loader class name
555 public String
getLoaderClassName() {
556 return fLoaderClassName
;