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
1 /**********************************************************************
2 * Copyright (c) 2005, 2012 IBM Corporation, Ericsson
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
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Bernd Hufmann - Updated for TMF
11 **********************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.DialogSettings;
21 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
22 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView;
23 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider;
24 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.layout.RowData;
29 import org.eclipse.swt.layout.RowLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableItem;
36 import 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>
41 *
42 * @version 1.0
43 * @author sveyrier
44 */
45 public class FilterListDialog extends Dialog {
46
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50 /**
51 * Filter list criteria property name
52 */
53 protected static final String FILTERS_LIST_CRITERIA = "filtersListsCriteria"; //$NON-NLS-1$
54 /**
55 * Filter list size property name
56 */
57 protected static final String FILTERS_LIST_SIZE = "filtersListSize"; //$NON-NLS-1$
58
59 // ------------------------------------------------------------------------
60 // Attributes
61 // ------------------------------------------------------------------------
62
63 /**
64 * The viewer and provided are kept here as attributes
65 */
66 protected IViewPart fViewer = null;
67 /**
68 * The filter provider implementation
69 */
70 protected ISDFilterProvider fProvider = null;
71 /**
72 * The filters are the result of editing this list
73 */
74 protected List<FilterCriteria> fFilters;
75 /**
76 * The add button.
77 */
78 protected Button fAdd;
79 /**
80 * The remove button.
81 */
82 protected Button fRemove;
83 /**
84 * The edit button.
85 */
86 protected Button fEdit;
87 /**
88 * The table with list of filters.
89 */
90 protected Table fTable;
91
92 // ------------------------------------------------------------------------
93 // Constructor
94 // ------------------------------------------------------------------------
95
96 /**
97 * Standard constructor
98 *
99 * @param view The view reference
100 * @param loader The filter provider implementation
101 */
102 public FilterListDialog(IViewPart view, ISDFilterProvider loader) {
103 super(view.getSite().getShell());
104 fViewer = view;
105 fProvider = loader;
106 fFilters = null;
107 // filters = provider.getCurrentFilters();
108 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
109 }
110
111 // ------------------------------------------------------------------------
112 // Methods
113 // ------------------------------------------------------------------------
114 /**
115 * Adds a criteria to the table
116 *
117 * @param criteria A criteria to add
118 * @param checked A flag whether criteria is checked (selected) or not
119 * @param positive A flag whether criteria is for positive filter or not
120 * @param loaderClassName A loader class name for the filters
121 */
122 protected void addCriteria(Criteria criteria, boolean checked, boolean positive, String loaderClassName) {
123 CriteriaTableItem cti = new CriteriaTableItem(fTable, checked, positive, loaderClassName);
124 cti.setCriteria(criteria);
125 }
126
127 /**
128 * Replaces a selected criteria with a new criteria.
129 *
130 * @param newCriteria A new criteria.
131 */
132 protected void replaceSelectedCriteria(Criteria newCriteria) {
133 CriteriaTableItem cti = (CriteriaTableItem) fTable.getSelection()[0].getData();
134 cti.setCriteria(newCriteria);
135 }
136
137 /**
138 * Handles table selection count.
139 */
140 protected void handleTableSelectionCount() {
141 int count = fTable.getSelectionCount();
142 fEdit.setEnabled(count == 1);
143 fRemove.setEnabled(count > 0);
144 }
145
146 /*
147 * (non-Javadoc)
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
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() {
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) {
177 int count = fTable.getSelectionCount();
178 if (count == 1) {
179 Criteria criteria = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), SDMessages._63);
180 if (criteria != null) {
181 replaceSelectedCriteria(criteria);
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 });
195 if (fFilters != null) {
196 for (Iterator<FilterCriteria> i = fFilters.iterator(); i.hasNext();) {
197 FilterCriteria filterCriteria = i.next();
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);
214 fAdd = new Button(commands, SWT.NONE);
215 fAdd.setText(SDMessages._61);
216 fAdd.addSelectionListener(new SelectionListener() {
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
240 fEdit = new Button(commands, SWT.NONE);
241 fEdit.setText(SDMessages._60);
242 fEdit.addSelectionListener(new SelectionListener() {
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 }
251
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) {
258 Criteria c = openFilterDialog(((CriteriaTableItem) fTable.getSelection()[0].getData()).getCriteria(), SDMessages._63);
259 if (c != null) {
260 replaceSelectedCriteria(c);
261 }
262 }
263 });
264 fEdit.setEnabled(false);
265
266 fRemove = new Button(commands, SWT.NONE);
267 fRemove.setText(SDMessages._64);
268 fRemove.addSelectionListener(new SelectionListener() {
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) {
284 fTable.remove(fTable.getSelectionIndices());
285 handleTableSelectionCount();
286 }
287 });
288 fRemove.setEnabled(false);
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 /**
299 * Opens the filter dialog box with given parameter.
300 *
301 * @param criteria The criteria reference to pass
302 * @param action to distinguish between "Update" and "Create"
303 * @return the criteria that has been updated or created
304 */
305 protected Criteria openFilterDialog(Criteria criteria, String action) {
306 SearchFilterDialog filter = new SearchFilterDialog((SDView) fViewer, fProvider, true, SWT.APPLICATION_MODAL);
307 filter.setCriteria(criteria);
308 filter.setOkText(action);
309 filter.setTitle(SDMessages._66);
310 filter.open();
311 return filter.getCriteria();
312 }
313
314 /*
315 * (non-Javadoc)
316 * @see org.eclipse.jface.window.Window#open()
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
327 /*
328 * (non-Javadoc)
329 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
330 */
331 @Override
332 public void okPressed() {
333 if (fTable.getItemCount() > 0) {
334 fFilters = new ArrayList<FilterCriteria>();
335 } else {
336 fFilters = null;
337 }
338 for (int i = 0; i < fTable.getItemCount(); i++) {
339 TableItem item = fTable.getItem(i);
340 CriteriaTableItem cti = (CriteriaTableItem) item.getData();
341 FilterCriteria fc = new FilterCriteria(cti.getCriteria(), item.getChecked(), cti.isPositive(), cti.getLoaderClassName());
342 FilterCriteria efc = FilterCriteria.find(fc, fFilters);
343 if (efc == null) {
344 fFilters.add(fc);
345 } else {
346 efc.setActive(efc.isActive() || fc.isActive());
347 }
348 }
349 super.close();
350 fProvider.filter(fFilters);
351 saveFiltersCriteria(fFilters);
352 }
353
354 /**
355 * Sets the list of filters.
356 *
357 * @param filters The list of filters to set.
358 */
359 public void setFilters(List<FilterCriteria> filters) {
360 fFilters = filters;
361 }
362
363 /**
364 * Returns the filters list after editing.
365 *
366 * @return the filters list after editing
367 */
368 public List<FilterCriteria> getFilters() {
369 return fFilters;
370 }
371
372 /**
373 * Loads the filter criteria from global filters which are saved in the dialog settings.
374 */
375 protected void loadFiltersCriteria() {
376 List<FilterCriteria> globalFilters = getGlobalFilters();
377 for (Iterator<FilterCriteria> i = globalFilters.iterator(); i.hasNext();) {
378 FilterCriteria filterCriteria = i.next();
379 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
380 }
381 }
382
383 /**
384 * Returns the global filters which are saved in the dialog settings..
385 *
386 * @return the saved global filters
387 */
388
389 public static List<FilterCriteria> getGlobalFilters() {
390 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA);
391 int i = 0;
392 DialogSettings section = null;
393 int size = 0;
394 List<FilterCriteria> globalFilters = new ArrayList<FilterCriteria>();
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);
403
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 }
411 }
412
413 return globalFilters;
414 }
415
416 /**
417 * Saves the filter criteria in the dialog settings.
418 *
419 * @param globalFilters A list of filters to save.
420 */
421 public static void saveFiltersCriteria(List<FilterCriteria> globalFilters) {
422 DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings();
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++) {
438 if (globalFilters.get(j) == null) {
439 return;
440 }
441
442 criteria = globalFilters.get(j);
443 DialogSettings subSection = (DialogSettings) section.getSection(FILTERS_LIST_CRITERIA + j);
444
445 if (subSection == null) {
446 subSection = (DialogSettings) section.addNewSection(FILTERS_LIST_CRITERIA + j);
447 }
448 criteria.save(subSection);
449 }
450 }
451
452 /**
453 * Deactivates the saved global filters.
454 */
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
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 */
476 protected Criteria fCriteria;
477 /**
478 * The "positive" value.
479 */
480 protected boolean fIsPositive;
481 /**
482 * The loader class name
483 */
484 protected String fLoaderClassName;
485 /**
486 * The actual table item.
487 */
488 protected TableItem fTableItem;
489
490 /**
491 * Constructor
492 *
493 * @param parent The parent table
494 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
495 * @param isPositive <code>true</code> for positive filter else <code>false</code>
496 * @param loaderClassName The loader class name
497 */
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;
504 }
505
506 /**
507 * Constructor
508 *
509 * @param parent The parent table
510 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
511 * @param isPositive <code>true</code> for positive filter else <code>false</code>
512 * @param loaderClassName The loader class name
513 * @param index The table item index
514 */
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;
520 }
521
522 /**
523 * Sets the criteria.
524 *
525 * @param criteria The criteria to set
526 */
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$
530 }
531
532 /**
533 * Returns the criteria.
534 * @return the criteria
535 */
536 public Criteria getCriteria() {
537 return fCriteria;
538 }
539
540 /**
541 * Returns whether positive filtering is active or not.
542 *
543 * @return <code>true</code> for positive filter else <code>false</code>
544 */
545 public boolean isPositive() {
546 return fIsPositive;
547 }
548
549 /**
550 * Returns the loader class name.
551 *
552 * @return the loader class name
553 */
554 public String getLoaderClassName() {
555 return fLoaderClassName;
556 }
557 }
558
559 }
This page took 0.046364 seconds and 5 git commands to generate.