tmf: Switch tmf.ui to Java 7 + fix warnings
[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 107 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
73005152
BH
108 }
109
df0b8ff4
BH
110 // ------------------------------------------------------------------------
111 // Methods
112 // ------------------------------------------------------------------------
73005152 113 /**
df0b8ff4 114 * Adds a criteria to the table
abbdd66a 115 *
df0b8ff4 116 * @param criteria A criteria to add
abbdd66a 117 * @param checked A flag whether criteria is checked (selected) or not
df0b8ff4
BH
118 * @param positive A flag whether criteria is for positive filter or not
119 * @param loaderClassName A loader class name for the filters
73005152 120 */
df0b8ff4 121 protected void addCriteria(Criteria criteria, boolean checked, boolean positive, String loaderClassName) {
eb63f5ff 122 CriteriaTableItem cti = new CriteriaTableItem(fTable, checked, positive, loaderClassName);
df0b8ff4 123 cti.setCriteria(criteria);
73005152
BH
124 }
125
126 /**
df0b8ff4 127 * Replaces a selected criteria with a new criteria.
abbdd66a 128 *
eb63f5ff 129 * @param newCriteria A new criteria.
73005152 130 */
eb63f5ff
BH
131 protected void replaceSelectedCriteria(Criteria newCriteria) {
132 CriteriaTableItem cti = (CriteriaTableItem) fTable.getSelection()[0].getData();
133 cti.setCriteria(newCriteria);
73005152
BH
134 }
135
136 /**
df0b8ff4 137 * Handles table selection count.
73005152
BH
138 */
139 protected void handleTableSelectionCount() {
eb63f5ff
BH
140 int count = fTable.getSelectionCount();
141 fEdit.setEnabled(count == 1);
142 fRemove.setEnabled(count > 0);
73005152
BH
143 }
144
73005152
BH
145 @Override
146 public Control createDialogArea(Composite parent) {
147
148 Group ret = new Group(parent, SWT.NONE);
92330441 149 ret.setText(Messages.SequenceDiagram_ListOfHideDisplayPatterns);
73005152
BH
150 RowLayout rowLayout = new RowLayout();
151 rowLayout.wrap = false;
152 rowLayout.pack = true;
153 rowLayout.justify = false;
154 rowLayout.type = SWT.HORIZONTAL;
155 rowLayout.marginLeft = 4;
156 rowLayout.marginTop = 4;
157 rowLayout.marginRight = 4;
158 rowLayout.marginBottom = 4;
159 rowLayout.spacing = 8;
160 ret.setLayout(rowLayout);
161
eb63f5ff
BH
162 fTable = new Table(ret, SWT.MULTI | SWT.CHECK);
163 fTable.setLayoutData(new RowData(220, 84));
164 fTable.setHeaderVisible(false);
165 fTable.addSelectionListener(new SelectionListener() {
11252342 166
73005152
BH
167 @Override
168 public void widgetDefaultSelected(SelectionEvent e) {
eb63f5ff 169 int count = fTable.getSelectionCount();
73005152 170 if (count == 1) {
92330441 171 Criteria criteria = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), Messages.SequenceDiagram_Update);
df0b8ff4
BH
172 if (criteria != null) {
173 replaceSelectedCriteria(criteria);
73005152
BH
174 }
175 }
176 }
177
73005152
BH
178 @Override
179 public void widgetSelected(SelectionEvent e) {
180 handleTableSelectionCount();
181 }
182 });
eb63f5ff
BH
183 if (fFilters != null) {
184 for (Iterator<FilterCriteria> i = fFilters.iterator(); i.hasNext();) {
abbdd66a 185 FilterCriteria filterCriteria = i.next();
73005152
BH
186 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
187 }
188 }
189
190 Composite commands = new Composite(ret, SWT.NONE);
191 RowLayout rowLayoutCommands = new RowLayout();
192 rowLayoutCommands.wrap = false;
193 rowLayoutCommands.pack = false;
194 rowLayoutCommands.justify = true;
195 rowLayoutCommands.type = SWT.VERTICAL;
196 rowLayoutCommands.marginLeft = 0;
197 rowLayoutCommands.marginTop = 4;
198 rowLayoutCommands.marginRight = 0;
199 rowLayoutCommands.marginBottom = 4;
200 rowLayoutCommands.spacing = 8;
201 commands.setLayout(rowLayoutCommands);
eb63f5ff 202 fAdd = new Button(commands, SWT.NONE);
92330441 203 fAdd.setText(Messages.SequenceDiagram_Add);
eb63f5ff 204 fAdd.addSelectionListener(new SelectionListener() {
11252342 205
73005152
BH
206 @Override
207 public void widgetDefaultSelected(SelectionEvent e) {
208 // Nothing to do
209 }
210
73005152
BH
211 @Override
212 public void widgetSelected(SelectionEvent e) {
213 Criteria init = new Criteria();
92330441 214 Criteria c = openFilterDialog(init, Messages.SequenceDiagram_Create);
73005152
BH
215 if (c != null) {
216 addCriteria(c, true, false, null);
217 }
218 }
219 });
220
eb63f5ff 221 fEdit = new Button(commands, SWT.NONE);
92330441 222 fEdit.setText(Messages.SequenceDiagram_EditIt);
eb63f5ff 223 fEdit.addSelectionListener(new SelectionListener() {
11252342 224
73005152
BH
225 @Override
226 public void widgetDefaultSelected(SelectionEvent e) {
227 // Nothing to do
228 }
abbdd66a 229
73005152
BH
230 @Override
231 public void widgetSelected(SelectionEvent e) {
92330441 232 Criteria c = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), Messages.SequenceDiagram_Update);
73005152
BH
233 if (c != null) {
234 replaceSelectedCriteria(c);
235 }
236 }
237 });
eb63f5ff 238 fEdit.setEnabled(false);
73005152 239
eb63f5ff 240 fRemove = new Button(commands, SWT.NONE);
92330441 241 fRemove.setText(Messages.SequenceDiagram_Remove);
eb63f5ff 242 fRemove.addSelectionListener(new SelectionListener() {
11252342 243
73005152
BH
244 @Override
245 public void widgetDefaultSelected(SelectionEvent e) {
246 // Nothing to do
247 }
248
73005152
BH
249 @Override
250 public void widgetSelected(SelectionEvent e) {
eb63f5ff 251 fTable.remove(fTable.getSelectionIndices());
73005152
BH
252 handleTableSelectionCount();
253 }
254 });
eb63f5ff 255 fRemove.setEnabled(false);
73005152 256
92330441 257 getShell().setText(Messages.SequenceDiagram_SequenceDiagramHidePatterns);
73005152
BH
258 /*
259 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
260 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
261 */
262 return ret;
263 }
264
265 /**
df0b8ff4 266 * Opens the filter dialog box with given parameter.
abbdd66a 267 *
df0b8ff4
BH
268 * @param criteria The criteria reference to pass
269 * @param action to distinguish between "Update" and "Create"
73005152
BH
270 * @return the criteria that has been updated or created
271 */
272 protected Criteria openFilterDialog(Criteria criteria, String action) {
eb63f5ff 273 SearchFilterDialog filter = new SearchFilterDialog((SDView) fViewer, fProvider, true, SWT.APPLICATION_MODAL);
73005152
BH
274 filter.setCriteria(criteria);
275 filter.setOkText(action);
92330441 276 filter.setTitle(Messages.SequenceDiagram_DefinitionOfHidePattern);
73005152
BH
277 filter.open();
278 return filter.getCriteria();
279 }
280
73005152
BH
281 @Override
282 public int open() {
283 create();
284 getShell().pack();
285 getShell().setLocation(getShell().getDisplay().getCursorLocation());
286 loadFiltersCriteria();
287 return super.open();
288 }
289
73005152
BH
290 @Override
291 public void okPressed() {
eb63f5ff 292 if (fTable.getItemCount() > 0) {
507b1336 293 fFilters = new ArrayList<>();
73005152 294 } else {
eb63f5ff 295 fFilters = null;
73005152 296 }
eb63f5ff
BH
297 for (int i = 0; i < fTable.getItemCount(); i++) {
298 TableItem item = fTable.getItem(i);
73005152 299 CriteriaTableItem cti = (CriteriaTableItem) item.getData();
eb63f5ff
BH
300 FilterCriteria fc = new FilterCriteria(cti.getCriteria(), item.getChecked(), cti.isPositive(), cti.getLoaderClassName());
301 FilterCriteria efc = FilterCriteria.find(fc, fFilters);
73005152 302 if (efc == null) {
eb63f5ff 303 fFilters.add(fc);
73005152
BH
304 } else {
305 efc.setActive(efc.isActive() || fc.isActive());
306 }
307 }
308 super.close();
eb63f5ff
BH
309 fProvider.filter(fFilters);
310 saveFiltersCriteria(fFilters);
73005152
BH
311 }
312
313 /**
df0b8ff4 314 * Sets the list of filters.
abbdd66a 315 *
eb63f5ff 316 * @param filters The list of filters to set.
73005152 317 */
eb63f5ff
BH
318 public void setFilters(List<FilterCriteria> filters) {
319 fFilters = filters;
73005152
BH
320 }
321
322 /**
df0b8ff4 323 * Returns the filters list after editing.
abbdd66a 324 *
73005152
BH
325 * @return the filters list after editing
326 */
327 public List<FilterCriteria> getFilters() {
eb63f5ff 328 return fFilters;
73005152
BH
329 }
330
df0b8ff4
BH
331 /**
332 * Loads the filter criteria from global filters which are saved in the dialog settings.
333 */
73005152
BH
334 protected void loadFiltersCriteria() {
335 List<FilterCriteria> globalFilters = getGlobalFilters();
336 for (Iterator<FilterCriteria> i = globalFilters.iterator(); i.hasNext();) {
abbdd66a 337 FilterCriteria filterCriteria = i.next();
73005152
BH
338 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
339 }
340 }
341
df0b8ff4
BH
342 /**
343 * Returns the global filters which are saved in the dialog settings..
abbdd66a 344 *
df0b8ff4
BH
345 * @return the saved global filters
346 */
64636df8 347
73005152 348 public static List<FilterCriteria> getGlobalFilters() {
8fd82db5 349 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA);
73005152
BH
350 int i = 0;
351 DialogSettings section = null;
352 int size = 0;
507b1336 353 List<FilterCriteria> globalFilters = new ArrayList<>();
73005152
BH
354 if (settings != null) {
355 try {
356 size = settings.getInt(FILTERS_LIST_SIZE);
357 } catch (NumberFormatException e) {
358 // This is not a problem
359 size = 0;
360 }
361 section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA + i);
73005152 362
64636df8
BH
363 while ((section != null) && (i < size)) {
364 FilterCriteria criteria = new FilterCriteria();
365 criteria.setCriteria(new Criteria());
366 criteria.load(section);
367 globalFilters.add(criteria);
368 section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA + (++i));
369 }
73005152
BH
370 }
371
372 return globalFilters;
373 }
374
df0b8ff4
BH
375 /**
376 * Saves the filter criteria in the dialog settings.
abbdd66a 377 *
df0b8ff4
BH
378 * @param globalFilters A list of filters to save.
379 */
73005152 380 public static void saveFiltersCriteria(List<FilterCriteria> globalFilters) {
8fd82db5 381 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings();
73005152
BH
382 DialogSettings section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA);
383 if (section == null) {
384 section = (DialogSettings) settings.addNewSection(FILTERS_LIST_CRITERIA);
385 }
386
387 if (globalFilters == null) {
388 section.put(FILTERS_LIST_SIZE, 0);
389 return;
390 }
391
392 section.put(FILTERS_LIST_SIZE, globalFilters.size());
393
394 FilterCriteria criteria;
395
396 for (int j = 0; j < globalFilters.size(); j++) {
abbdd66a 397 if (globalFilters.get(j) == null) {
73005152 398 return;
df0b8ff4 399 }
73005152 400
abbdd66a 401 criteria = globalFilters.get(j);
73005152 402 DialogSettings subSection = (DialogSettings) section.getSection(FILTERS_LIST_CRITERIA + j);
df0b8ff4 403
73005152
BH
404 if (subSection == null) {
405 subSection = (DialogSettings) section.addNewSection(FILTERS_LIST_CRITERIA + j);
406 }
407 criteria.save(subSection);
408 }
409 }
abbdd66a 410
df0b8ff4
BH
411 /**
412 * Deactivates the saved global filters.
413 */
73005152
BH
414 public static void deactivateSavedGlobalFilters() {
415 // Deactivate all filters
416 List<FilterCriteria> filters = getGlobalFilters();
417 for(FilterCriteria criteria : filters) {
418 criteria.setActive(false);
419 }
420 // Save settings
421 FilterListDialog.saveFiltersCriteria(filters);
422 }
423
df0b8ff4
BH
424 // ------------------------------------------------------------------------
425 // Helper classes
426 // ------------------------------------------------------------------------
427 /**
428 * A class to map TableItems that can be toggled active or inactive and Criteria
429 */
430 protected class CriteriaTableItem {
431
432 /**
433 * The criteria reference
434 */
eb63f5ff 435 protected Criteria fCriteria;
df0b8ff4
BH
436 /**
437 * The "positive" value.
438 */
eb63f5ff 439 protected boolean fIsPositive;
df0b8ff4
BH
440 /**
441 * The loader class name
442 */
eb63f5ff 443 protected String fLoaderClassName;
df0b8ff4
BH
444 /**
445 * The actual table item.
446 */
eb63f5ff 447 protected TableItem fTableItem;
df0b8ff4
BH
448
449 /**
450 * Constructor
abbdd66a 451 *
df0b8ff4 452 * @param parent The parent table
abbdd66a 453 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
eb63f5ff
BH
454 * @param isPositive <code>true</code> for positive filter else <code>false</code>
455 * @param loaderClassName The loader class name
df0b8ff4 456 */
eb63f5ff
BH
457 public CriteriaTableItem(Table parent, boolean isActive, boolean isPositive, String loaderClassName) {
458 fTableItem = new TableItem(parent, SWT.NONE);
459 fTableItem.setData(this);
460 fTableItem.setChecked(isActive);
461 fIsPositive = isPositive;
462 fLoaderClassName = loaderClassName;
df0b8ff4
BH
463 }
464
465 /**
466 * Constructor
abbdd66a 467 *
df0b8ff4 468 * @param parent The parent table
abbdd66a 469 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
eb63f5ff
BH
470 * @param isPositive <code>true</code> for positive filter else <code>false</code>
471 * @param loaderClassName The loader class name
df0b8ff4
BH
472 * @param index The table item index
473 */
eb63f5ff
BH
474 public CriteriaTableItem(Table parent, boolean isActive, boolean isPositive, String loaderClassName, int index) {
475 fTableItem = new TableItem(parent, SWT.NONE, index);
476 fTableItem.setChecked(isActive);
477 fIsPositive = isPositive;
478 fLoaderClassName = loaderClassName;
df0b8ff4
BH
479 }
480
481 /**
482 * Sets the criteria.
abbdd66a 483 *
eb63f5ff 484 * @param criteria The criteria to set
df0b8ff4 485 */
eb63f5ff
BH
486 public void setCriteria(Criteria criteria) {
487 fCriteria = criteria;
92330441 488 fTableItem.setText((fIsPositive ? Messages.SequenceDiagram_display : Messages.SequenceDiagram_hide) + " " + fCriteria.getExpression() + " " + fCriteria.getGraphNodeSummary(fProvider, fLoaderClassName)); //$NON-NLS-1$ //$NON-NLS-2$
df0b8ff4
BH
489 }
490
491 /**
492 * Returns the criteria.
493 * @return the criteria
494 */
495 public Criteria getCriteria() {
eb63f5ff 496 return fCriteria;
df0b8ff4
BH
497 }
498
499 /**
500 * Returns whether positive filtering is active or not.
abbdd66a 501 *
df0b8ff4
BH
502 * @return <code>true</code> for positive filter else <code>false</code>
503 */
eb63f5ff
BH
504 public boolean isPositive() {
505 return fIsPositive;
df0b8ff4
BH
506 }
507
508 /**
509 * Returns the loader class name.
abbdd66a 510 *
df0b8ff4
BH
511 * @return the loader class name
512 */
513 public String getLoaderClassName() {
eb63f5ff 514 return fLoaderClassName;
df0b8ff4
BH
515 }
516 }
517
73005152 518}
This page took 0.070012 seconds and 5 git commands to generate.