tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / dialogs / FilterListDialog.java
CommitLineData
73005152 1/**********************************************************************
c8422608 2 * Copyright (c) 2005, 2012 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;
24import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
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 */
eb63f5ff 66 protected IViewPart fViewer = null;
df0b8ff4
BH
67 /**
68 * The filter provider implementation
69 */
eb63f5ff 70 protected ISDFilterProvider fProvider = null;
73005152 71 /**
df0b8ff4 72 * The filters are the result of editing this list
73005152 73 */
eb63f5ff 74 protected List<FilterCriteria> fFilters;
73005152 75 /**
df0b8ff4 76 * The add button.
73005152 77 */
eb63f5ff 78 protected Button fAdd;
73005152 79 /**
df0b8ff4 80 * The remove button.
73005152 81 */
eb63f5ff 82 protected Button fRemove;
73005152 83 /**
df0b8ff4 84 * The edit button.
73005152 85 */
eb63f5ff 86 protected Button fEdit;
df0b8ff4
BH
87 /**
88 * The table with list of filters.
89 */
eb63f5ff 90 protected 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
df0b8ff4
BH
146 /*
147 * (non-Javadoc)
73005152
BH
148 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
149 */
150 @Override
151 public Control createDialogArea(Composite parent) {
152
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);
166
eb63f5ff
BH
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() {
73005152
BH
171 /*
172 * (non-Javadoc)
173 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
174 */
175 @Override
176 public void widgetDefaultSelected(SelectionEvent e) {
eb63f5ff 177 int count = fTable.getSelectionCount();
73005152 178 if (count == 1) {
eb63f5ff 179 Criteria criteria = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), SDMessages._63);
df0b8ff4
BH
180 if (criteria != null) {
181 replaceSelectedCriteria(criteria);
73005152
BH
182 }
183 }
184 }
185
186 /*
187 * (non-Javadoc)
188 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
189 */
190 @Override
191 public void widgetSelected(SelectionEvent e) {
192 handleTableSelectionCount();
193 }
194 });
eb63f5ff
BH
195 if (fFilters != null) {
196 for (Iterator<FilterCriteria> i = fFilters.iterator(); i.hasNext();) {
abbdd66a 197 FilterCriteria filterCriteria = i.next();
73005152
BH
198 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
199 }
200 }
201
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);
eb63f5ff
BH
214 fAdd = new Button(commands, SWT.NONE);
215 fAdd.setText(SDMessages._61);
216 fAdd.addSelectionListener(new SelectionListener() {
73005152
BH
217 /*
218 * (non-Javadoc)
219 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
220 */
221 @Override
222 public void widgetDefaultSelected(SelectionEvent e) {
223 // Nothing to do
224 }
225
226 /*
227 * (non-Javadoc)
228 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
229 */
230 @Override
231 public void widgetSelected(SelectionEvent e) {
232 Criteria init = new Criteria();
233 Criteria c = openFilterDialog(init, SDMessages._62);
234 if (c != null) {
235 addCriteria(c, true, false, null);
236 }
237 }
238 });
239
eb63f5ff
BH
240 fEdit = new Button(commands, SWT.NONE);
241 fEdit.setText(SDMessages._60);
242 fEdit.addSelectionListener(new SelectionListener() {
73005152
BH
243 /*
244 * (non-Javadoc)
245 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
246 */
247 @Override
248 public void widgetDefaultSelected(SelectionEvent e) {
249 // Nothing to do
250 }
abbdd66a 251
73005152
BH
252 /*
253 * (non-Javadoc)
254 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
255 */
256 @Override
257 public void widgetSelected(SelectionEvent e) {
eb63f5ff 258 Criteria c = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), SDMessages._63);
73005152
BH
259 if (c != null) {
260 replaceSelectedCriteria(c);
261 }
262 }
263 });
eb63f5ff 264 fEdit.setEnabled(false);
73005152 265
eb63f5ff
BH
266 fRemove = new Button(commands, SWT.NONE);
267 fRemove.setText(SDMessages._64);
268 fRemove.addSelectionListener(new SelectionListener() {
73005152
BH
269 /*
270 * (non-Javadoc)
271 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
272 */
273 @Override
274 public void widgetDefaultSelected(SelectionEvent e) {
275 // Nothing to do
276 }
277
278 /*
279 * (non-Javadoc)
280 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
281 */
282 @Override
283 public void widgetSelected(SelectionEvent e) {
eb63f5ff 284 fTable.remove(fTable.getSelectionIndices());
73005152
BH
285 handleTableSelectionCount();
286 }
287 });
eb63f5ff 288 fRemove.setEnabled(false);
73005152
BH
289
290 getShell().setText(SDMessages._65);
291 /*
292 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
293 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
294 */
295 return ret;
296 }
297
298 /**
df0b8ff4 299 * Opens the filter dialog box with given parameter.
abbdd66a 300 *
df0b8ff4
BH
301 * @param criteria The criteria reference to pass
302 * @param action to distinguish between "Update" and "Create"
73005152
BH
303 * @return the criteria that has been updated or created
304 */
305 protected Criteria openFilterDialog(Criteria criteria, String action) {
eb63f5ff 306 SearchFilterDialog filter = new SearchFilterDialog((SDView) fViewer, fProvider, true, SWT.APPLICATION_MODAL);
73005152
BH
307 filter.setCriteria(criteria);
308 filter.setOkText(action);
309 filter.setTitle(SDMessages._66);
310 filter.open();
311 return filter.getCriteria();
312 }
313
df0b8ff4
BH
314 /*
315 * (non-Javadoc)
316 * @see org.eclipse.jface.window.Window#open()
73005152
BH
317 */
318 @Override
319 public int open() {
320 create();
321 getShell().pack();
322 getShell().setLocation(getShell().getDisplay().getCursorLocation());
323 loadFiltersCriteria();
324 return super.open();
325 }
326
df0b8ff4
BH
327 /*
328 * (non-Javadoc)
329 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
73005152
BH
330 */
331 @Override
332 public void okPressed() {
eb63f5ff
BH
333 if (fTable.getItemCount() > 0) {
334 fFilters = new ArrayList<FilterCriteria>();
73005152 335 } else {
eb63f5ff 336 fFilters = null;
73005152 337 }
eb63f5ff
BH
338 for (int i = 0; i < fTable.getItemCount(); i++) {
339 TableItem item = fTable.getItem(i);
73005152 340 CriteriaTableItem cti = (CriteriaTableItem) item.getData();
eb63f5ff
BH
341 FilterCriteria fc = new FilterCriteria(cti.getCriteria(), item.getChecked(), cti.isPositive(), cti.getLoaderClassName());
342 FilterCriteria efc = FilterCriteria.find(fc, fFilters);
73005152 343 if (efc == null) {
eb63f5ff 344 fFilters.add(fc);
73005152
BH
345 } else {
346 efc.setActive(efc.isActive() || fc.isActive());
347 }
348 }
349 super.close();
eb63f5ff
BH
350 fProvider.filter(fFilters);
351 saveFiltersCriteria(fFilters);
73005152
BH
352 }
353
354 /**
df0b8ff4 355 * Sets the list of filters.
abbdd66a 356 *
eb63f5ff 357 * @param filters The list of filters to set.
73005152 358 */
eb63f5ff
BH
359 public void setFilters(List<FilterCriteria> filters) {
360 fFilters = filters;
73005152
BH
361 }
362
363 /**
df0b8ff4 364 * Returns the filters list after editing.
abbdd66a 365 *
73005152
BH
366 * @return the filters list after editing
367 */
368 public List<FilterCriteria> getFilters() {
eb63f5ff 369 return fFilters;
73005152
BH
370 }
371
df0b8ff4
BH
372 /**
373 * Loads the filter criteria from global filters which are saved in the dialog settings.
374 */
73005152
BH
375 protected void loadFiltersCriteria() {
376 List<FilterCriteria> globalFilters = getGlobalFilters();
377 for (Iterator<FilterCriteria> i = globalFilters.iterator(); i.hasNext();) {
abbdd66a 378 FilterCriteria filterCriteria = i.next();
73005152
BH
379 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
380 }
381 }
382
df0b8ff4
BH
383 /**
384 * Returns the global filters which are saved in the dialog settings..
abbdd66a 385 *
df0b8ff4
BH
386 * @return the saved global filters
387 */
64636df8 388
73005152 389 public static List<FilterCriteria> getGlobalFilters() {
8fd82db5 390 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA);
73005152
BH
391 int i = 0;
392 DialogSettings section = null;
393 int size = 0;
64636df8 394 List<FilterCriteria> globalFilters = new ArrayList<FilterCriteria>();
73005152
BH
395 if (settings != null) {
396 try {
397 size = settings.getInt(FILTERS_LIST_SIZE);
398 } catch (NumberFormatException e) {
399 // This is not a problem
400 size = 0;
401 }
402 section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA + i);
73005152 403
64636df8
BH
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));
410 }
73005152
BH
411 }
412
413 return globalFilters;
414 }
415
df0b8ff4
BH
416 /**
417 * Saves the filter criteria in the dialog settings.
abbdd66a 418 *
df0b8ff4
BH
419 * @param globalFilters A list of filters to save.
420 */
73005152 421 public static void saveFiltersCriteria(List<FilterCriteria> globalFilters) {
8fd82db5 422 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings();
73005152
BH
423 DialogSettings section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA);
424 if (section == null) {
425 section = (DialogSettings) settings.addNewSection(FILTERS_LIST_CRITERIA);
426 }
427
428 if (globalFilters == null) {
429 section.put(FILTERS_LIST_SIZE, 0);
430 return;
431 }
432
433 section.put(FILTERS_LIST_SIZE, globalFilters.size());
434
435 FilterCriteria criteria;
436
437 for (int j = 0; j < globalFilters.size(); j++) {
abbdd66a 438 if (globalFilters.get(j) == null) {
73005152 439 return;
df0b8ff4 440 }
73005152 441
abbdd66a 442 criteria = globalFilters.get(j);
73005152 443 DialogSettings subSection = (DialogSettings) section.getSection(FILTERS_LIST_CRITERIA + j);
df0b8ff4 444
73005152
BH
445 if (subSection == null) {
446 subSection = (DialogSettings) section.addNewSection(FILTERS_LIST_CRITERIA + j);
447 }
448 criteria.save(subSection);
449 }
450 }
abbdd66a 451
df0b8ff4
BH
452 /**
453 * Deactivates the saved global filters.
454 */
73005152
BH
455 public static void deactivateSavedGlobalFilters() {
456 // Deactivate all filters
457 List<FilterCriteria> filters = getGlobalFilters();
458 for(FilterCriteria criteria : filters) {
459 criteria.setActive(false);
460 }
461 // Save settings
462 FilterListDialog.saveFiltersCriteria(filters);
463 }
464
df0b8ff4
BH
465 // ------------------------------------------------------------------------
466 // Helper classes
467 // ------------------------------------------------------------------------
468 /**
469 * A class to map TableItems that can be toggled active or inactive and Criteria
470 */
471 protected class CriteriaTableItem {
472
473 /**
474 * The criteria reference
475 */
eb63f5ff 476 protected Criteria fCriteria;
df0b8ff4
BH
477 /**
478 * The "positive" value.
479 */
eb63f5ff 480 protected boolean fIsPositive;
df0b8ff4
BH
481 /**
482 * The loader class name
483 */
eb63f5ff 484 protected String fLoaderClassName;
df0b8ff4
BH
485 /**
486 * The actual table item.
487 */
eb63f5ff 488 protected TableItem fTableItem;
df0b8ff4
BH
489
490 /**
491 * Constructor
abbdd66a 492 *
df0b8ff4 493 * @param parent The parent table
abbdd66a 494 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
eb63f5ff
BH
495 * @param isPositive <code>true</code> for positive filter else <code>false</code>
496 * @param loaderClassName The loader class name
df0b8ff4 497 */
eb63f5ff
BH
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;
df0b8ff4
BH
504 }
505
506 /**
507 * Constructor
abbdd66a 508 *
df0b8ff4 509 * @param parent The parent table
abbdd66a 510 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
eb63f5ff
BH
511 * @param isPositive <code>true</code> for positive filter else <code>false</code>
512 * @param loaderClassName The loader class name
df0b8ff4
BH
513 * @param index The table item index
514 */
eb63f5ff
BH
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;
df0b8ff4
BH
520 }
521
522 /**
523 * Sets the criteria.
abbdd66a 524 *
eb63f5ff 525 * @param criteria The criteria to set
df0b8ff4 526 */
eb63f5ff
BH
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$
df0b8ff4
BH
530 }
531
532 /**
533 * Returns the criteria.
534 * @return the criteria
535 */
536 public Criteria getCriteria() {
eb63f5ff 537 return fCriteria;
df0b8ff4
BH
538 }
539
540 /**
541 * Returns whether positive filtering is active or not.
abbdd66a 542 *
df0b8ff4
BH
543 * @return <code>true</code> for positive filter else <code>false</code>
544 */
eb63f5ff
BH
545 public boolean isPositive() {
546 return fIsPositive;
df0b8ff4
BH
547 }
548
549 /**
550 * Returns the loader class name.
abbdd66a 551 *
df0b8ff4
BH
552 * @return the loader class name
553 */
554 public String getLoaderClassName() {
eb63f5ff 555 return fLoaderClassName;
df0b8ff4
BH
556 }
557 }
558
73005152 559}
This page took 0.0612 seconds and 5 git commands to generate.