Java Doc and API clean up of TMF UML Sequence diagram framework
[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.TmfUiPlugin;
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 protected static final String FILTERS_LIST_CRITERIA = "filtersListsCriteria"; //$NON-NLS-1$
53 protected static final String FILTERS_LIST_SIZE = "filtersListSize"; //$NON-NLS-1$
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58
59 /**
60 * The viewer and provided are kept here as attributes
61 */
62 protected IViewPart viewer = null;
63 /**
64 * The filter provider implementation
65 */
66 protected ISDFilterProvider provider = null;
67 /**
68 * The filters are the result of editing this list
69 */
70 protected List<FilterCriteria> filters;
71 /**
72 * The add button.
73 */
74 protected Button add;
75 /**
76 * The remove button.
77 */
78 protected Button remove;
79 /**
80 * The edit button.
81 */
82 protected Button edit;
83 /**
84 * The table with list of filters.
85 */
86 protected Table table;
87
88 // ------------------------------------------------------------------------
89 // Constructor
90 // ------------------------------------------------------------------------
91
92 /**
93 * Standard constructor
94 *
95 * @param view_ The view reference
96 * @param loader_ The filter provider implementation
97 */
98 public FilterListDialog(IViewPart view_, ISDFilterProvider loader_) {
99 super(view_.getSite().getShell());
100 viewer = view_;
101 provider = loader_;
102 filters = null;
103 // filters = provider.getCurrentFilters();
104 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
105 }
106
107 // ------------------------------------------------------------------------
108 // Methods
109 // ------------------------------------------------------------------------
110 /**
111 * Adds a criteria to the table
112 *
113 * @param criteria A criteria to add
114 * @param checked A flag whether criteria is checked (selected) or not
115 * @param positive A flag whether criteria is for positive filter or not
116 * @param loaderClassName A loader class name for the filters
117 */
118 protected void addCriteria(Criteria criteria, boolean checked, boolean positive, String loaderClassName) {
119 CriteriaTableItem cti = new CriteriaTableItem(table, checked, positive, loaderClassName);
120 cti.setCriteria(criteria);
121 }
122
123 /**
124 * Replaces a selected criteria with a new criteria.
125 *
126 * @param new_ A new criteria.
127 */
128 protected void replaceSelectedCriteria(Criteria new_) {
129 CriteriaTableItem cti = (CriteriaTableItem) table.getSelection()[0].getData();
130 cti.setCriteria(new_);
131 }
132
133 /**
134 * Handles table selection count.
135 */
136 protected void handleTableSelectionCount() {
137 int count = table.getSelectionCount();
138 edit.setEnabled(count == 1);
139 remove.setEnabled(count > 0);
140 }
141
142 /*
143 * (non-Javadoc)
144 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
145 */
146 @Override
147 public Control createDialogArea(Composite parent) {
148
149 Group ret = new Group(parent, SWT.NONE);
150 ret.setText(SDMessages._57);
151 RowLayout rowLayout = new RowLayout();
152 rowLayout.wrap = false;
153 rowLayout.pack = true;
154 rowLayout.justify = false;
155 rowLayout.type = SWT.HORIZONTAL;
156 rowLayout.marginLeft = 4;
157 rowLayout.marginTop = 4;
158 rowLayout.marginRight = 4;
159 rowLayout.marginBottom = 4;
160 rowLayout.spacing = 8;
161 ret.setLayout(rowLayout);
162
163 table = new Table(ret, SWT.MULTI | SWT.CHECK);
164 table.setLayoutData(new RowData(220, 84));
165 table.setHeaderVisible(false);
166 table.addSelectionListener(new SelectionListener() {
167 /*
168 * (non-Javadoc)
169 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
170 */
171 @Override
172 public void widgetDefaultSelected(SelectionEvent e) {
173 int count = table.getSelectionCount();
174 if (count == 1) {
175 Criteria criteria = openFilterDialog(((CriteriaTableItem) table.getSelection()[0].getData()).getCriteria(), SDMessages._63);
176 if (criteria != null) {
177 replaceSelectedCriteria(criteria);
178 }
179 }
180 }
181
182 /*
183 * (non-Javadoc)
184 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
185 */
186 @Override
187 public void widgetSelected(SelectionEvent e) {
188 handleTableSelectionCount();
189 }
190 });
191 if (filters != null) {
192 for (Iterator<FilterCriteria> i = filters.iterator(); i.hasNext();) {
193 FilterCriteria filterCriteria = (FilterCriteria) i.next();
194 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
195 }
196 }
197
198 Composite commands = new Composite(ret, SWT.NONE);
199 RowLayout rowLayoutCommands = new RowLayout();
200 rowLayoutCommands.wrap = false;
201 rowLayoutCommands.pack = false;
202 rowLayoutCommands.justify = true;
203 rowLayoutCommands.type = SWT.VERTICAL;
204 rowLayoutCommands.marginLeft = 0;
205 rowLayoutCommands.marginTop = 4;
206 rowLayoutCommands.marginRight = 0;
207 rowLayoutCommands.marginBottom = 4;
208 rowLayoutCommands.spacing = 8;
209 commands.setLayout(rowLayoutCommands);
210 add = new Button(commands, SWT.NONE);
211 add.setText(SDMessages._61);
212 add.addSelectionListener(new SelectionListener() {
213 /*
214 * (non-Javadoc)
215 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
216 */
217 @Override
218 public void widgetDefaultSelected(SelectionEvent e) {
219 // Nothing to do
220 }
221
222 /*
223 * (non-Javadoc)
224 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
225 */
226 @Override
227 public void widgetSelected(SelectionEvent e) {
228 Criteria init = new Criteria();
229 Criteria c = openFilterDialog(init, SDMessages._62);
230 if (c != null) {
231 addCriteria(c, true, false, null);
232 }
233 }
234 });
235
236 edit = new Button(commands, SWT.NONE);
237 edit.setText(SDMessages._60);
238 edit.addSelectionListener(new SelectionListener() {
239 /*
240 * (non-Javadoc)
241 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
242 */
243 @Override
244 public void widgetDefaultSelected(SelectionEvent e) {
245 // Nothing to do
246 }
247
248 /*
249 * (non-Javadoc)
250 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
251 */
252 @Override
253 public void widgetSelected(SelectionEvent e) {
254 Criteria c = openFilterDialog(((CriteriaTableItem) table.getSelection()[0].getData()).getCriteria(), SDMessages._63);
255 if (c != null) {
256 replaceSelectedCriteria(c);
257 }
258 }
259 });
260 edit.setEnabled(false);
261
262 remove = new Button(commands, SWT.NONE);
263 remove.setText(SDMessages._64);
264 remove.addSelectionListener(new SelectionListener() {
265 /*
266 * (non-Javadoc)
267 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
268 */
269 @Override
270 public void widgetDefaultSelected(SelectionEvent e) {
271 // Nothing to do
272 }
273
274 /*
275 * (non-Javadoc)
276 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
277 */
278 @Override
279 public void widgetSelected(SelectionEvent e) {
280 table.remove(table.getSelectionIndices());
281 handleTableSelectionCount();
282 }
283 });
284 remove.setEnabled(false);
285
286 getShell().setText(SDMessages._65);
287 /*
288 * for (int i=0;i<filters.size();i++) { if (filters.get(i) instanceof FilterCriteria)
289 * addCriteria(((FilterCriteria)filters.get(i)).getCriteria(),true); }
290 */
291 return ret;
292 }
293
294 /**
295 * Opens the filter dialog box with given parameter.
296 *
297 * @param criteria The criteria reference to pass
298 * @param action to distinguish between "Update" and "Create"
299 * @return the criteria that has been updated or created
300 */
301 protected Criteria openFilterDialog(Criteria criteria, String action) {
302 SearchFilterDialog filter = new SearchFilterDialog((SDView) viewer, provider, true, SWT.APPLICATION_MODAL);
303 filter.setCriteria(criteria);
304 filter.setOkText(action);
305 filter.setTitle(SDMessages._66);
306 filter.open();
307 return filter.getCriteria();
308 }
309
310 /*
311 * (non-Javadoc)
312 * @see org.eclipse.jface.window.Window#open()
313 */
314 @Override
315 public int open() {
316 create();
317 getShell().pack();
318 getShell().setLocation(getShell().getDisplay().getCursorLocation());
319 loadFiltersCriteria();
320 return super.open();
321 }
322
323 /*
324 * (non-Javadoc)
325 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
326 */
327 @Override
328 public void okPressed() {
329 if (table.getItemCount() > 0) {
330 filters = new ArrayList<FilterCriteria>();
331 } else {
332 filters = null;
333 }
334 for (int i = 0; i < table.getItemCount(); i++) {
335 TableItem item = table.getItem(i);
336 CriteriaTableItem cti = (CriteriaTableItem) item.getData();
337 FilterCriteria fc = new FilterCriteria(cti.getCriteria(), item.getChecked(), cti.getPositive(), cti.getLoaderClassName());
338 FilterCriteria efc = FilterCriteria.find(fc, filters);
339 if (efc == null) {
340 filters.add(fc);
341 } else {
342 efc.setActive(efc.isActive() || fc.isActive());
343 }
344 }
345 super.close();
346 provider.filter(filters);
347 saveFiltersCriteria(filters);
348 }
349
350 /**
351 * Sets the list of filters.
352 *
353 * @param filters_ The list of filters to set.
354 */
355 public void setFilters(ArrayList<FilterCriteria> filters_) {
356 filters = filters_;
357 }
358
359 /**
360 * Returns the filters list after editing.
361 *
362 * @return the filters list after editing
363 */
364 public List<FilterCriteria> getFilters() {
365 return filters;
366 }
367
368 /**
369 * Loads the filter criteria from global filters which are saved in the dialog settings.
370 */
371 protected void loadFiltersCriteria() {
372 List<FilterCriteria> globalFilters = getGlobalFilters();
373 for (Iterator<FilterCriteria> i = globalFilters.iterator(); i.hasNext();) {
374 FilterCriteria filterCriteria = (FilterCriteria) i.next();
375 addCriteria(filterCriteria.getCriteria(), filterCriteria.isActive(), filterCriteria.isPositive(), filterCriteria.getLoaderClassName());
376 }
377 }
378
379 /**
380 * Returns the global filters which are saved in the dialog settings..
381 *
382 * @return the saved global filters
383 */
384 public static List<FilterCriteria> getGlobalFilters() {
385 DialogSettings settings = (DialogSettings) TmfUiPlugin.getDefault().getDialogSettings().getSection(FILTERS_LIST_CRITERIA);
386 int i = 0;
387 DialogSettings section = null;
388 int size = 0;
389 if (settings != null) {
390 try {
391 size = settings.getInt(FILTERS_LIST_SIZE);
392 } catch (NumberFormatException e) {
393 // This is not a problem
394 size = 0;
395 }
396 section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA + i);
397 }
398
399 List<FilterCriteria> globalFilters = new ArrayList<FilterCriteria>();
400
401 while ((section != null) && (i < size)) {
402 FilterCriteria criteria = new FilterCriteria();
403 criteria.setCriteria(new Criteria());
404 criteria.load(section);
405 globalFilters.add(criteria);
406 section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA + (++i));
407 }
408
409 return globalFilters;
410 }
411
412 /**
413 * Saves the filter criteria in the dialog settings.
414 *
415 * @param globalFilters A list of filters to save.
416 */
417 public static void saveFiltersCriteria(List<FilterCriteria> globalFilters) {
418 DialogSettings settings = (DialogSettings) TmfUiPlugin.getDefault().getDialogSettings();
419 DialogSettings section = (DialogSettings) settings.getSection(FILTERS_LIST_CRITERIA);
420 if (section == null) {
421 section = (DialogSettings) settings.addNewSection(FILTERS_LIST_CRITERIA);
422 }
423
424 if (globalFilters == null) {
425 section.put(FILTERS_LIST_SIZE, 0);
426 return;
427 }
428
429 section.put(FILTERS_LIST_SIZE, globalFilters.size());
430
431 FilterCriteria criteria;
432
433 for (int j = 0; j < globalFilters.size(); j++) {
434 if (!(globalFilters.get(j) instanceof FilterCriteria)) {
435 return;
436 }
437
438 criteria = (FilterCriteria) globalFilters.get(j);
439 DialogSettings subSection = (DialogSettings) section.getSection(FILTERS_LIST_CRITERIA + j);
440
441 if (subSection == null) {
442 subSection = (DialogSettings) section.addNewSection(FILTERS_LIST_CRITERIA + j);
443 }
444 criteria.save(subSection);
445 }
446 }
447
448 /**
449 * Deactivates the saved global filters.
450 */
451 public static void deactivateSavedGlobalFilters() {
452 // Deactivate all filters
453 List<FilterCriteria> filters = getGlobalFilters();
454 for(FilterCriteria criteria : filters) {
455 criteria.setActive(false);
456 }
457 // Save settings
458 FilterListDialog.saveFiltersCriteria(filters);
459 }
460
461 // ------------------------------------------------------------------------
462 // Helper classes
463 // ------------------------------------------------------------------------
464 /**
465 * A class to map TableItems that can be toggled active or inactive and Criteria
466 */
467 protected class CriteriaTableItem {
468
469 /**
470 * The criteria reference
471 */
472 protected Criteria criteria;
473 /**
474 * The "positive" value.
475 */
476 protected boolean positive;
477 /**
478 * The loader class name
479 */
480 protected String loaderClassName;
481 /**
482 * The actual table item.
483 */
484 protected TableItem tableItem;
485
486 /**
487 * Constructor
488 *
489 * @param parent The parent table
490 * @param active_ <code>true</code> if filter criteria is active else <code>false</code>
491 * @param positive_ <code>true</code> for positive filter else <code>false</code>
492 * @param loaderClassName_ The loader class name
493 */
494 public CriteriaTableItem(Table parent, boolean active_, boolean positive_, String loaderClassName_) {
495 tableItem = new TableItem(parent, SWT.NONE);
496 tableItem.setData(this);
497 tableItem.setChecked(active_);
498 positive = positive_;
499 loaderClassName = loaderClassName_;
500 }
501
502 /**
503 * Constructor
504 *
505 * @param parent The parent table
506 * @param active_ <code>true</code> if filter criteria is active else <code>false</code>
507 * @param positive_ <code>true</code> for positive filter else <code>false</code>
508 * @param loaderClassName_ The loader class name
509 * @param index The table item index
510 */
511 public CriteriaTableItem(Table parent, boolean checked_, boolean positive_, String loaderClassName_, int index) {
512 tableItem = new TableItem(parent, SWT.NONE, index);
513 tableItem.setChecked(checked_);
514 positive = positive_;
515 loaderClassName = loaderClassName_;
516 }
517
518 /**
519 * Sets the criteria.
520 *
521 * @param criteria_ The criteria to set
522 */
523 public void setCriteria(Criteria criteria_) {
524 criteria = criteria_;
525 tableItem.setText((positive ? SDMessages._59 : SDMessages._58) + " " + criteria.getExpression() + " " + criteria.getGraphNodeSummary(provider, loaderClassName)); //$NON-NLS-1$ //$NON-NLS-2$
526 }
527
528 /**
529 * Returns the criteria.
530 * @return the criteria
531 */
532 public Criteria getCriteria() {
533 return criteria;
534 }
535
536 /**
537 * Returns whether positive filtering is active or not.
538 *
539 * @return <code>true</code> for positive filter else <code>false</code>
540 */
541 public boolean getPositive() {
542 return positive;
543 }
544
545 /**
546 * Returns the loader class name.
547 *
548 * @return the loader class name
549 */
550 public String getLoaderClassName() {
551 return loaderClassName;
552 }
553 }
554
555 }
This page took 0.043168 seconds and 5 git commands to generate.