tmf: API clean-up of sequence diagram framework
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / dialogs / FilterListDialog.java
CommitLineData
73005152 1/**********************************************************************
11252342 2 * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
73005152
BH
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
abbdd66a
AM
7 *
8 * Contributors:
c8422608
AM
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
73005152 11 **********************************************************************/
c8422608 12
df0b8ff4 13package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs;
73005152
BH
14
15import java.util.ArrayList;
16import java.util.Iterator;
17import java.util.List;
18
19import org.eclipse.jface.dialogs.Dialog;
20import org.eclipse.jface.dialogs.DialogSettings;
8fd82db5 21import org.eclipse.linuxtools.internal.tmf.ui.Activator;
73005152
BH
22import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
23import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider;
92330441 24import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.Messages;
73005152
BH
25import org.eclipse.swt.SWT;
26import org.eclipse.swt.events.SelectionEvent;
27import org.eclipse.swt.events.SelectionListener;
28import org.eclipse.swt.layout.RowData;
29import org.eclipse.swt.layout.RowLayout;
30import org.eclipse.swt.widgets.Button;
31import org.eclipse.swt.widgets.Composite;
32import org.eclipse.swt.widgets.Control;
33import org.eclipse.swt.widgets.Group;
34import org.eclipse.swt.widgets.Table;
35import org.eclipse.swt.widgets.TableItem;
36import org.eclipse.ui.IViewPart;
37
38/**
39 * This is the filters list dialog.<br>
40 * It is associated to an SDView and to a ISDFilterProvider.<br>
abbdd66a 41 *
df0b8ff4
BH
42 * @version 1.0
43 * @author sveyrier
73005152
BH
44 */
45public class FilterListDialog extends Dialog {
46
df0b8ff4
BH
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
eb63f5ff
BH
50 /**
51 * Filter list criteria property name
52 */
73005152 53 protected static final String FILTERS_LIST_CRITERIA = "filtersListsCriteria"; //$NON-NLS-1$
eb63f5ff
BH
54 /**
55 * Filter list size property name
56 */
73005152
BH
57 protected static final String FILTERS_LIST_SIZE = "filtersListSize"; //$NON-NLS-1$
58
df0b8ff4
BH
59 // ------------------------------------------------------------------------
60 // Attributes
61 // ------------------------------------------------------------------------
62
73005152 63 /**
df0b8ff4 64 * The viewer and provided are kept here as attributes
73005152 65 */
cab6c8ff 66 private final IViewPart fViewer;
df0b8ff4
BH
67 /**
68 * The filter provider implementation
69 */
cab6c8ff 70 private final ISDFilterProvider fProvider;
73005152 71 /**
df0b8ff4 72 * The filters are the result of editing this list
73005152 73 */
cab6c8ff 74 private List<FilterCriteria> fFilters;
73005152 75 /**
df0b8ff4 76 * The add button.
73005152 77 */
cab6c8ff 78 private Button fAdd;
73005152 79 /**
df0b8ff4 80 * The remove button.
73005152 81 */
cab6c8ff 82 private Button fRemove;
73005152 83 /**
df0b8ff4 84 * The edit button.
73005152 85 */
cab6c8ff 86 private Button fEdit;
df0b8ff4
BH
87 /**
88 * The table with list of filters.
89 */
cab6c8ff 90 private Table fTable;
73005152 91
df0b8ff4
BH
92 // ------------------------------------------------------------------------
93 // Constructor
94 // ------------------------------------------------------------------------
73005152 95
df0b8ff4
BH
96 /**
97 * Standard constructor
abbdd66a 98 *
eb63f5ff
BH
99 * @param view The view reference
100 * @param loader The filter provider implementation
df0b8ff4 101 */
eb63f5ff
BH
102 public FilterListDialog(IViewPart view, ISDFilterProvider loader) {
103 super(view.getSite().getShell());
104 fViewer = view;
105 fProvider = loader;
106 fFilters = null;
df0b8ff4
BH
107 // filters = provider.getCurrentFilters();
108 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
73005152
BH
109 }
110
df0b8ff4
BH
111 // ------------------------------------------------------------------------
112 // Methods
113 // ------------------------------------------------------------------------
73005152 114 /**
df0b8ff4 115 * Adds a criteria to the table
abbdd66a 116 *
df0b8ff4 117 * @param criteria A criteria to add
abbdd66a 118 * @param checked A flag whether criteria is checked (selected) or not
df0b8ff4
BH
119 * @param positive A flag whether criteria is for positive filter or not
120 * @param loaderClassName A loader class name for the filters
73005152 121 */
df0b8ff4 122 protected void addCriteria(Criteria criteria, boolean checked, boolean positive, String loaderClassName) {
eb63f5ff 123 CriteriaTableItem cti = new CriteriaTableItem(fTable, checked, positive, loaderClassName);
df0b8ff4 124 cti.setCriteria(criteria);
73005152
BH
125 }
126
127 /**
df0b8ff4 128 * Replaces a selected criteria with a new criteria.
abbdd66a 129 *
eb63f5ff 130 * @param newCriteria A new criteria.
73005152 131 */
eb63f5ff
BH
132 protected void replaceSelectedCriteria(Criteria newCriteria) {
133 CriteriaTableItem cti = (CriteriaTableItem) fTable.getSelection()[0].getData();
134 cti.setCriteria(newCriteria);
73005152
BH
135 }
136
137 /**
df0b8ff4 138 * Handles table selection count.
73005152
BH
139 */
140 protected void handleTableSelectionCount() {
eb63f5ff
BH
141 int count = fTable.getSelectionCount();
142 fEdit.setEnabled(count == 1);
143 fRemove.setEnabled(count > 0);
73005152
BH
144 }
145
73005152
BH
146 @Override
147 public Control createDialogArea(Composite parent) {
148
149 Group ret = new Group(parent, SWT.NONE);
92330441 150 ret.setText(Messages.SequenceDiagram_ListOfHideDisplayPatterns);
73005152
BH
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);
162
eb63f5ff
BH
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() {
11252342 167
73005152
BH
168 @Override
169 public void widgetDefaultSelected(SelectionEvent e) {
eb63f5ff 170 int count = fTable.getSelectionCount();
73005152 171 if (count == 1) {
92330441 172 Criteria criteria = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), Messages.SequenceDiagram_Update);
df0b8ff4
BH
173 if (criteria != null) {
174 replaceSelectedCriteria(criteria);
73005152
BH
175 }
176 }
177 }
178
73005152
BH
179 @Override
180 public void widgetSelected(SelectionEvent e) {
181 handleTableSelectionCount();
182 }
183 });
eb63f5ff
BH
184 if (fFilters != null) {
185 for (Iterator<FilterCriteria> i = fFilters.iterator(); i.hasNext();) {
abbdd66a 186 FilterCriteria filterCriteria = i.next();
73005152
BH
187 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
188 }
189 }
190
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);
eb63f5ff 203 fAdd = new Button(commands, SWT.NONE);
92330441 204 fAdd.setText(Messages.SequenceDiagram_Add);
eb63f5ff 205 fAdd.addSelectionListener(new SelectionListener() {
11252342 206
73005152
BH
207 @Override
208 public void widgetDefaultSelected(SelectionEvent e) {
209 // Nothing to do
210 }
211
73005152
BH
212 @Override
213 public void widgetSelected(SelectionEvent e) {
214 Criteria init = new Criteria();
92330441 215 Criteria c = openFilterDialog(init, Messages.SequenceDiagram_Create);
73005152
BH
216 if (c != null) {
217 addCriteria(c, true, false, null);
218 }
219 }
220 });
221
eb63f5ff 222 fEdit = new Button(commands, SWT.NONE);
92330441 223 fEdit.setText(Messages.SequenceDiagram_EditIt);
eb63f5ff 224 fEdit.addSelectionListener(new SelectionListener() {
11252342 225
73005152
BH
226 @Override
227 public void widgetDefaultSelected(SelectionEvent e) {
228 // Nothing to do
229 }
abbdd66a 230
73005152
BH
231 @Override
232 public void widgetSelected(SelectionEvent e) {
92330441 233 Criteria c = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), Messages.SequenceDiagram_Update);
73005152
BH
234 if (c != null) {
235 replaceSelectedCriteria(c);
236 }
237 }
238 });
eb63f5ff 239 fEdit.setEnabled(false);
73005152 240
eb63f5ff 241 fRemove = new Button(commands, SWT.NONE);
92330441 242 fRemove.setText(Messages.SequenceDiagram_Remove);
eb63f5ff 243 fRemove.addSelectionListener(new SelectionListener() {
11252342 244
73005152
BH
245 @Override
246 public void widgetDefaultSelected(SelectionEvent e) {
247 // Nothing to do
248 }
249
73005152
BH
250 @Override
251 public void widgetSelected(SelectionEvent e) {
eb63f5ff 252 fTable.remove(fTable.getSelectionIndices());
73005152
BH
253 handleTableSelectionCount();
254 }
255 });
eb63f5ff 256 fRemove.setEnabled(false);
73005152 257
92330441 258 getShell().setText(Messages.SequenceDiagram_SequenceDiagramHidePatterns);
73005152
BH
259 /*
260 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
261 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
262 */
263 return ret;
264 }
265
266 /**
df0b8ff4 267 * Opens the filter dialog box with given parameter.
abbdd66a 268 *
df0b8ff4
BH
269 * @param criteria The criteria reference to pass
270 * @param action to distinguish between "Update" and "Create"
73005152
BH
271 * @return the criteria that has been updated or created
272 */
273 protected Criteria openFilterDialog(Criteria criteria, String action) {
eb63f5ff 274 SearchFilterDialog filter = new SearchFilterDialog((SDView) fViewer, fProvider, true, SWT.APPLICATION_MODAL);
73005152
BH
275 filter.setCriteria(criteria);
276 filter.setOkText(action);
92330441 277 filter.setTitle(Messages.SequenceDiagram_DefinitionOfHidePattern);
73005152
BH
278 filter.open();
279 return filter.getCriteria();
280 }
281
73005152
BH
282 @Override
283 public int open() {
284 create();
285 getShell().pack();
286 getShell().setLocation(getShell().getDisplay().getCursorLocation());
287 loadFiltersCriteria();
288 return super.open();
289 }
290
73005152
BH
291 @Override
292 public void okPressed() {
eb63f5ff
BH
293 if (fTable.getItemCount() > 0) {
294 fFilters = new ArrayList<FilterCriteria>();
73005152 295 } else {
eb63f5ff 296 fFilters = null;
73005152 297 }
eb63f5ff
BH
298 for (int i = 0; i < fTable.getItemCount(); i++) {
299 TableItem item = fTable.getItem(i);
73005152 300 CriteriaTableItem cti = (CriteriaTableItem) item.getData();
eb63f5ff
BH
301 FilterCriteria fc = new FilterCriteria(cti.getCriteria(), item.getChecked(), cti.isPositive(), cti.getLoaderClassName());
302 FilterCriteria efc = FilterCriteria.find(fc, fFilters);
73005152 303 if (efc == null) {
eb63f5ff 304 fFilters.add(fc);
73005152
BH
305 } else {
306 efc.setActive(efc.isActive() || fc.isActive());
307 }
308 }
309 super.close();
eb63f5ff
BH
310 fProvider.filter(fFilters);
311 saveFiltersCriteria(fFilters);
73005152
BH
312 }
313
314 /**
df0b8ff4 315 * Sets the list of filters.
abbdd66a 316 *
eb63f5ff 317 * @param filters The list of filters to set.
73005152 318 */
eb63f5ff
BH
319 public void setFilters(List<FilterCriteria> filters) {
320 fFilters = filters;
73005152
BH
321 }
322
323 /**
df0b8ff4 324 * Returns the filters list after editing.
abbdd66a 325 *
73005152
BH
326 * @return the filters list after editing
327 */
328 public List<FilterCriteria> getFilters() {
eb63f5ff 329 return fFilters;
73005152
BH
330 }
331
df0b8ff4
BH
332 /**
333 * Loads the filter criteria from global filters which are saved in the dialog settings.
334 */
73005152
BH
335 protected void loadFiltersCriteria() {
336 List<FilterCriteria> globalFilters = getGlobalFilters();
337 for (Iterator<FilterCriteria> i = globalFilters.iterator(); i.hasNext();) {
abbdd66a 338 FilterCriteria filterCriteria = i.next();
73005152
BH
339 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
340 }
341 }
342
df0b8ff4
BH
343 /**
344 * Returns the global filters which are saved in the dialog settings..
abbdd66a 345 *
df0b8ff4
BH
346 * @return the saved global filters
347 */
64636df8 348
73005152 349 public static List<FilterCriteria> getGlobalFilters() {
8fd82db5 350 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA);
73005152
BH
351 int i = 0;
352 DialogSettings section = null;
353 int size = 0;
64636df8 354 List<FilterCriteria> globalFilters = new ArrayList<FilterCriteria>();
73005152
BH
355 if (settings != null) {
356 try {
357 size = settings.getInt(FILTERS_LIST_SIZE);
358 } catch (NumberFormatException e) {
359 // This is not a problem
360 size = 0;
361 }
362 section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA + i);
73005152 363
64636df8
BH
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));
370 }
73005152
BH
371 }
372
373 return globalFilters;
374 }
375
df0b8ff4
BH
376 /**
377 * Saves the filter criteria in the dialog settings.
abbdd66a 378 *
df0b8ff4
BH
379 * @param globalFilters A list of filters to save.
380 */
73005152 381 public static void saveFiltersCriteria(List<FilterCriteria> globalFilters) {
8fd82db5 382 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings();
73005152
BH
383 DialogSettings section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA);
384 if (section == null) {
385 section = (DialogSettings) settings.addNewSection(FILTERS_LIST_CRITERIA);
386 }
387
388 if (globalFilters == null) {
389 section.put(FILTERS_LIST_SIZE, 0);
390 return;
391 }
392
393 section.put(FILTERS_LIST_SIZE, globalFilters.size());
394
395 FilterCriteria criteria;
396
397 for (int j = 0; j < globalFilters.size(); j++) {
abbdd66a 398 if (globalFilters.get(j) == null) {
73005152 399 return;
df0b8ff4 400 }
73005152 401
abbdd66a 402 criteria = globalFilters.get(j);
73005152 403 DialogSettings subSection = (DialogSettings) section.getSection(FILTERS_LIST_CRITERIA + j);
df0b8ff4 404
73005152
BH
405 if (subSection == null) {
406 subSection = (DialogSettings) section.addNewSection(FILTERS_LIST_CRITERIA + j);
407 }
408 criteria.save(subSection);
409 }
410 }
abbdd66a 411
df0b8ff4
BH
412 /**
413 * Deactivates the saved global filters.
414 */
73005152
BH
415 public static void deactivateSavedGlobalFilters() {
416 // Deactivate all filters
417 List<FilterCriteria> filters = getGlobalFilters();
418 for(FilterCriteria criteria : filters) {
419 criteria.setActive(false);
420 }
421 // Save settings
422 FilterListDialog.saveFiltersCriteria(filters);
423 }
424
df0b8ff4
BH
425 // ------------------------------------------------------------------------
426 // Helper classes
427 // ------------------------------------------------------------------------
428 /**
429 * A class to map TableItems that can be toggled active or inactive and Criteria
430 */
431 protected class CriteriaTableItem {
432
433 /**
434 * The criteria reference
435 */
eb63f5ff 436 protected Criteria fCriteria;
df0b8ff4
BH
437 /**
438 * The "positive" value.
439 */
eb63f5ff 440 protected boolean fIsPositive;
df0b8ff4
BH
441 /**
442 * The loader class name
443 */
eb63f5ff 444 protected String fLoaderClassName;
df0b8ff4
BH
445 /**
446 * The actual table item.
447 */
eb63f5ff 448 protected TableItem fTableItem;
df0b8ff4
BH
449
450 /**
451 * Constructor
abbdd66a 452 *
df0b8ff4 453 * @param parent The parent table
abbdd66a 454 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
eb63f5ff
BH
455 * @param isPositive <code>true</code> for positive filter else <code>false</code>
456 * @param loaderClassName The loader class name
df0b8ff4 457 */
eb63f5ff
BH
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;
df0b8ff4
BH
464 }
465
466 /**
467 * Constructor
abbdd66a 468 *
df0b8ff4 469 * @param parent The parent table
abbdd66a 470 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
eb63f5ff
BH
471 * @param isPositive <code>true</code> for positive filter else <code>false</code>
472 * @param loaderClassName The loader class name
df0b8ff4
BH
473 * @param index The table item index
474 */
eb63f5ff
BH
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;
df0b8ff4
BH
480 }
481
482 /**
483 * Sets the criteria.
abbdd66a 484 *
eb63f5ff 485 * @param criteria The criteria to set
df0b8ff4 486 */
eb63f5ff
BH
487 public void setCriteria(Criteria criteria) {
488 fCriteria = criteria;
92330441 489 fTableItem.setText((fIsPositive ? Messages.SequenceDiagram_display : Messages.SequenceDiagram_hide) + " " + fCriteria.getExpression() + " " + fCriteria.getGraphNodeSummary(fProvider, fLoaderClassName)); //$NON-NLS-1$ //$NON-NLS-2$
df0b8ff4
BH
490 }
491
492 /**
493 * Returns the criteria.
494 * @return the criteria
495 */
496 public Criteria getCriteria() {
eb63f5ff 497 return fCriteria;
df0b8ff4
BH
498 }
499
500 /**
501 * Returns whether positive filtering is active or not.
abbdd66a 502 *
df0b8ff4
BH
503 * @return <code>true</code> for positive filter else <code>false</code>
504 */
eb63f5ff
BH
505 public boolean isPositive() {
506 return fIsPositive;
df0b8ff4
BH
507 }
508
509 /**
510 * Returns the loader class name.
abbdd66a 511 *
df0b8ff4
BH
512 * @return the loader class name
513 */
514 public String getLoaderClassName() {
eb63f5ff 515 return fLoaderClassName;
df0b8ff4
BH
516 }
517 }
518
73005152 519}
This page took 0.069222 seconds and 5 git commands to generate.