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