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