tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / dialogs / TimeGraphFilterDialog.java
CommitLineData
8f91a789 1/*******************************************************************************
c8422608 2 * Copyright (c) 2000, 2013 IBM Corporation and others.
8f91a789
AM
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 Corporation - initial API and implementation
10 * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
11 * activated and used by other components.
12 * Lubomir Marinov <lubomir.marinov@gmail.com> - Fix for bug 182122 -[Dialogs]
13 * CheckedTreeSelectionDialog#createSelectionButtons(Composite) fails to
14 * align the selection buttons to the right
15 * François Rajotte - Support for multiple columns + selection control
f1fae91f 16 * Patrick Tasse - Fix Sonar warnings
8f91a789
AM
17 *******************************************************************************/
18
19package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs;
20
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.List;
24
25import org.eclipse.core.runtime.IStatus;
26import org.eclipse.core.runtime.Status;
27import org.eclipse.jface.dialogs.IDialogConstants;
28import org.eclipse.jface.viewers.CheckStateChangedEvent;
29import org.eclipse.jface.viewers.CheckboxTreeViewer;
30import org.eclipse.jface.viewers.IBaseLabelProvider;
31import org.eclipse.jface.viewers.ICheckStateListener;
32import org.eclipse.jface.viewers.ITreeContentProvider;
8906e53c 33import org.eclipse.jface.viewers.TreeSelection;
8f91a789
AM
34import org.eclipse.jface.viewers.ViewerComparator;
35import org.eclipse.jface.viewers.ViewerFilter;
36import org.eclipse.linuxtools.internal.tmf.ui.Messages;
37import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
38import org.eclipse.swt.SWT;
39import org.eclipse.swt.custom.BusyIndicator;
40import org.eclipse.swt.events.SelectionAdapter;
41import org.eclipse.swt.events.SelectionEvent;
8f91a789
AM
42import org.eclipse.swt.layout.GridData;
43import org.eclipse.swt.layout.GridLayout;
44import org.eclipse.swt.widgets.Button;
45import org.eclipse.swt.widgets.Composite;
46import org.eclipse.swt.widgets.Control;
47import org.eclipse.swt.widgets.Label;
48import org.eclipse.swt.widgets.Shell;
49import org.eclipse.swt.widgets.Tree;
50import org.eclipse.swt.widgets.TreeColumn;
51import org.eclipse.ui.PlatformUI;
8f91a789
AM
52import org.eclipse.ui.dialogs.ISelectionStatusValidator;
53import org.eclipse.ui.dialogs.SelectionStatusDialog;
54
55/**
56 * Filter dialog for the time graphs
57 * This class is derived from the CheckedTreeSelectionDialog
58 * It was necessary to develop this similar dialog to allow multiple columns
59 *
60 * @version 1.0
61 * @since 2.0
62 * @author François Rajotte
63 */
64public class TimeGraphFilterDialog extends SelectionStatusDialog {
f1fae91f
PT
65 private static final int BUTTON_CHECK_SELECTED_ID = IDialogConstants.CLIENT_ID;
66 private static final int BUTTON_UNCHECK_SELECTED_ID = IDialogConstants.CLIENT_ID + 1;
67 private static final int BUTTON_CHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 2;
68 private static final int BUTTON_UNCHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 3;
03beb743 69
f1fae91f
PT
70 private static final int DEFAULT_WIDTH = 60;
71 private static final int DEFAULT_HEIGHT = 18;
8f91a789
AM
72
73 private CheckboxTreeViewer fViewer;
74
75 private IBaseLabelProvider fLabelProvider;
76
77 private ITreeContentProvider fContentProvider;
78
79 private String[] fColumnNames;
80
81 private ISelectionStatusValidator fValidator = null;
82
83 private ViewerComparator fComparator;
84
85 private String fEmptyListMessage = ""; //$NON-NLS-1$
86
87 private IStatus fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
88 0, "", null); //$NON-NLS-1$
89
90 private List<ViewerFilter> fFilters;
91
92 private Object fInput;
93
94 private boolean fIsEmpty;
95
f1fae91f 96 private int fWidth = DEFAULT_WIDTH;
8f91a789 97
f1fae91f 98 private int fHeight = DEFAULT_HEIGHT;
8f91a789 99
8f91a789
AM
100 private Object[] fExpandedElements;
101
102 /**
103 * Constructs an instance of <code>ElementTreeSelectionDialog</code>.
104 *
105 * @param parent
106 * The shell to parent from.
107 */
108 public TimeGraphFilterDialog(Shell parent) {
109 super(parent);
507b1336 110 setResult(new ArrayList<>(0));
8f91a789
AM
111 setStatusLineAboveButtons(true);
112 setHelpAvailable(false);
8f91a789
AM
113 fExpandedElements = null;
114 }
115
8f91a789
AM
116 /**
117 * Sets the initial selection. Convenience method.
118 *
119 * @param selection
120 * the initial selection.
121 */
122 public void setInitialSelection(Object selection) {
123 setInitialSelections(new Object[] { selection });
124 }
125
126 /**
127 * Sets the message to be displayed if the list is empty.
128 *
129 * @param message
130 * the message to be displayed.
131 */
132 public void setEmptyListMessage(String message) {
133 fEmptyListMessage = message;
134 }
135
136 /**
137 * Sets the comparator used by the tree viewer.
138 *
139 * @param comparator
140 * The comparator
141 */
142 public void setComparator(ViewerComparator comparator) {
143 fComparator = comparator;
144 }
145
146 /**
147 * Adds a filter to the tree viewer.
148 *
149 * @param filter
150 * a filter.
151 */
152 public void addFilter(ViewerFilter filter) {
153 if (fFilters == null) {
507b1336 154 fFilters = new ArrayList<>();
8f91a789
AM
155 }
156 fFilters.add(filter);
157 }
158
159 /**
160 * Sets an optional validator to check if the selection is valid. The
161 * validator is invoked whenever the selection changes.
162 *
163 * @param validator
164 * the validator to validate the selection.
165 */
166 public void setValidator(ISelectionStatusValidator validator) {
167 fValidator = validator;
168 }
169
170 /**
171 * Sets the tree input.
172 *
173 * @param input
174 * the tree input.
175 */
176 public void setInput(Object input) {
177 fInput = input;
178 }
179
180 /**
181 * Expands elements in the tree.
182 *
183 * @param elements
184 * The elements that will be expanded.
185 */
186 public void setExpandedElements(Object[] elements) {
f1fae91f
PT
187 if (elements != null) {
188 fExpandedElements = Arrays.copyOf(elements, elements.length);
189 } else {
190 fExpandedElements = null;
191 }
8f91a789
AM
192 }
193
194 /**
195 * Sets the size of the tree in unit of characters.
196 *
197 * @param width
198 * the width of the tree.
199 * @param height
200 * the height of the tree.
201 */
202 public void setSize(int width, int height) {
203 fWidth = width;
204 fHeight = height;
205 }
206
207 /**
208 * @param contentProvider The content provider for the table
209 */
210 public void setContentProvider(ITreeContentProvider contentProvider) {
211 fContentProvider = contentProvider;
212 }
213
214 /**
215 * @param labelProvider The label provider for the table
216 */
217 public void setLabelProvider(IBaseLabelProvider labelProvider) {
218 fLabelProvider = labelProvider;
219 }
220
221 /**
222 * @param columnNames An array of column names to display
223 */
224 public void setColumnNames(String[] columnNames) {
f1fae91f
PT
225 if (columnNames != null) {
226 fColumnNames = Arrays.copyOf(columnNames, columnNames.length);
227 } else {
228 fColumnNames = null;
229 }
8f91a789
AM
230 }
231
232 /**
233 * Validate the receiver and update the status with the result.
234 *
235 */
236 protected void updateOKStatus() {
237 if (!fIsEmpty) {
238 if (fValidator != null) {
239 fCurrStatus = fValidator.validate(fViewer.getCheckedElements());
240 updateStatus(fCurrStatus);
241 } else if (!fCurrStatus.isOK()) {
242 fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
243 IStatus.OK, "", //$NON-NLS-1$
244 null);
245 }
246 } else {
247 fCurrStatus = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
248 IStatus.OK, fEmptyListMessage, null);
249 }
250 updateStatus(fCurrStatus);
251 }
252
253 @Override
254 public int open() {
255 fIsEmpty = evaluateIfTreeEmpty(fInput);
256 super.open();
257 return getReturnCode();
258 }
259
8f91a789
AM
260 @Override
261 protected void cancelPressed() {
262 setResult(null);
263 super.cancelPressed();
264 }
265
266 @Override
267 protected void computeResult() {
268 setResult(Arrays.asList(fViewer.getCheckedElements()));
269 }
270
271 @Override
272 public void create() {
273 BusyIndicator.showWhile(null, new Runnable() {
274 @Override
275 public void run() {
f1fae91f 276 TimeGraphFilterDialog.super.create();
8f91a789
AM
277 fViewer.setCheckedElements(getInitialElementSelections()
278 .toArray());
279 if (fExpandedElements != null) {
280 fViewer.setExpandedElements(fExpandedElements);
281 }
282 updateOKStatus();
283 }
284 });
285 }
286
287 @Override
288 protected Control createDialogArea(Composite parent) {
289 Composite composite = (Composite) super.createDialogArea(parent);
290 Label messageLabel = createMessageArea(composite);
291 CheckboxTreeViewer treeViewer = createTreeViewer(composite);
292 Control buttonComposite = createSelectionButtons(composite);
293 GridData data = new GridData(GridData.FILL_BOTH);
294 data.widthHint = convertWidthInCharsToPixels(fWidth);
295 data.heightHint = convertHeightInCharsToPixels(fHeight);
296 Tree treeWidget = treeViewer.getTree();
297 treeWidget.setLayoutData(data);
298 treeWidget.setFont(parent.getFont());
299 if (fIsEmpty) {
300 messageLabel.setEnabled(false);
301 treeWidget.setEnabled(false);
302 buttonComposite.setEnabled(false);
303 }
304 return composite;
305 }
306
307 /**
308 * Creates the tree viewer.
309 *
310 * @param parent
311 * the parent composite
312 * @return the tree viewer
313 */
314 protected CheckboxTreeViewer createTreeViewer(Composite parent) {
8906e53c 315 fViewer = new CheckboxTreeViewer(parent, SWT.BORDER | SWT.MULTI);
8f91a789
AM
316
317 Tree tree = fViewer.getTree();
318 tree.setHeaderVisible(true);
319 for (String columnName : fColumnNames) {
320 TreeColumn column = new TreeColumn(tree, SWT.LEFT);
321 column.setText(columnName);
322 column.pack();
323 }
324
325 fViewer.setContentProvider(fContentProvider);
326 fViewer.setLabelProvider(fLabelProvider);
327 fViewer.addCheckStateListener(new CheckStateListener());
328 fViewer.addCheckStateListener(new ICheckStateListener() {
329 @Override
330 public void checkStateChanged(CheckStateChangedEvent event) {
331 updateOKStatus();
332 }
333 });
334 fViewer.setComparator(fComparator);
335 if (fFilters != null) {
336 for (int i = 0; i != fFilters.size(); i++) {
337 fViewer.addFilter(fFilters.get(i));
338 }
339 }
340 fViewer.setInput(fInput);
341
342 //pack the columns again for a nice view...
343 for (TreeColumn column : tree.getColumns()) {
344 column.pack();
345 }
346 return fViewer;
347 }
348
349 /**
350 * Returns the tree viewer.
351 *
352 * @return the tree viewer
353 */
354 protected CheckboxTreeViewer getTreeViewer() {
355 return fViewer;
356 }
357
358 /**
359 * Adds the selection and deselection buttons to the dialog.
360 *
361 * @param composite
362 * the parent composite
363 * @return Composite the composite the buttons were created in.
364 */
365 protected Composite createSelectionButtons(Composite composite) {
366 Composite buttonComposite = new Composite(composite, SWT.RIGHT);
367 GridLayout layout = new GridLayout();
8f91a789
AM
368 layout.marginWidth = 0;
369 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
370 buttonComposite.setLayout(layout);
371 buttonComposite.setFont(composite.getFont());
372 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END
373 | GridData.GRAB_HORIZONTAL);
374 data.grabExcessHorizontalSpace = true;
375 buttonComposite.setLayoutData(data);
8906e53c
SM
376
377 /* Create the buttons in the good order to place them as we want */
03beb743
SM
378 Button checkSelectedButton = createButton(buttonComposite,
379 BUTTON_CHECK_SELECTED_ID, Messages.TmfTimeFilterDialog_CHECK_SELECTED,
380 false);
381 Button checkSubtreeButton = createButton(buttonComposite,
382 BUTTON_CHECK_SUBTREE_ID, Messages.TmfTimeFilterDialog_CHECK_SUBTREE,
383 false);
8906e53c
SM
384 Button checkAllButton = createButton(buttonComposite,
385 IDialogConstants.SELECT_ALL_ID, Messages.TmfTimeFilterDialog_CHECK_ALL,
386 false);
03beb743
SM
387
388 Button uncheckSelectedButton = createButton(buttonComposite,
389 BUTTON_UNCHECK_SELECTED_ID, Messages.TmfTimeFilterDialog_UNCHECK_SELECTED,
390 false);
391 Button uncheckSubtreeButton = createButton(buttonComposite,
392 BUTTON_UNCHECK_SUBTREE_ID, Messages.TmfTimeFilterDialog_UNCHECK_SUBTREE,
8906e53c
SM
393 false);
394 Button uncheckAllButton = createButton(buttonComposite,
395 IDialogConstants.DESELECT_ALL_ID, Messages.TmfTimeFilterDialog_UNCHECK_ALL,
396 false);
03beb743 397
8906e53c
SM
398
399 /*
400 * Apply the layout again after creating the buttons to override
401 * createButton messing with the columns
402 */
03beb743 403 layout.numColumns = 3;
8906e53c
SM
404 buttonComposite.setLayout(layout);
405
406 /* Add a listener to each button */
03beb743 407 checkSelectedButton.addSelectionListener(new SelectionAdapter() {
8f91a789
AM
408 @Override
409 public void widgetSelected(SelectionEvent e) {
03beb743 410 TreeSelection selection = (TreeSelection) fViewer.getSelection();
8906e53c 411
03beb743
SM
412 for (Object element : selection.toArray()) {
413 checkElement(element);
8f91a789 414 }
8906e53c 415
8f91a789
AM
416 updateOKStatus();
417 }
8906e53c
SM
418 });
419
03beb743 420 checkSubtreeButton.addSelectionListener(new SelectionAdapter() {
8f91a789
AM
421 @Override
422 public void widgetSelected(SelectionEvent e) {
03beb743
SM
423 TreeSelection selection = (TreeSelection) fViewer.getSelection();
424
425 for (Object element : selection.toArray()) {
426 checkElementAndSubtree(element);
427 }
428 }
429 });
430
431 checkAllButton.addSelectionListener(new SelectionAdapter() {
432 @Override
433 public void widgetSelected(SelectionEvent e) {
434 Object[] viewerElements = fContentProvider.getElements(fInput);
435
436 for (int i = 0; i < viewerElements.length; i++) {
437 fViewer.setSubtreeChecked(viewerElements[i], true);
438 }
439
8f91a789
AM
440 updateOKStatus();
441 }
8906e53c
SM
442 });
443
03beb743 444 uncheckSelectedButton.addSelectionListener(new SelectionAdapter() {
8906e53c
SM
445 @Override
446 public void widgetSelected(SelectionEvent e) {
447 TreeSelection selection = (TreeSelection) fViewer.getSelection();
448
449 for (Object element : selection.toArray()) {
03beb743 450 uncheckElement(element);
8906e53c
SM
451 }
452
453 updateOKStatus();
454 }
455 });
456
03beb743 457 uncheckSubtreeButton.addSelectionListener(new SelectionAdapter() {
8906e53c
SM
458 @Override
459 public void widgetSelected(SelectionEvent e) {
460 TreeSelection selection = (TreeSelection) fViewer.getSelection();
461
462 for (Object element : selection.toArray()) {
463 uncheckElement(element);
464 }
465
466 updateOKStatus();
467 }
468 });
469
03beb743
SM
470 uncheckAllButton.addSelectionListener(new SelectionAdapter() {
471 @Override
472 public void widgetSelected(SelectionEvent e) {
473 fViewer.setCheckedElements(new Object[0]);
474 updateOKStatus();
475 }
476 });
477
8f91a789
AM
478 return buttonComposite;
479 }
480
03beb743
SM
481 /**
482 * Check an element and all its parents.
483 *
484 * @param element
485 * The element to check.
486 */
8906e53c 487 private void checkElement(Object element) {
03beb743
SM
488 fViewer.setChecked(element, true);
489
490 Object parent = fContentProvider.getParent(element);
491
492 if (parent != null) {
493 checkElement(parent);
8906e53c
SM
494 }
495 }
496
03beb743
SM
497 /**
498 * Check an element, all its parents and all its children.
499 *
500 * @param element
501 * The element to check.
502 */
503 private void checkElementAndSubtree(Object element) {
504 checkElement(element);
505
506 for (Object child : fContentProvider.getChildren(element)) {
507 checkElementAndSubtree(child);
508 }
509 }
8906e53c 510
03beb743
SM
511 /**
512 * Uncheck an element and all its children.
513 *
514 * @param element
515 * The element to uncheck.
516 */
517 private void uncheckElement(Object element) {
518 fViewer.setChecked(element, false);
8906e53c 519
03beb743 520 for (Object child : fContentProvider.getChildren(element)) {
8906e53c
SM
521 uncheckElement(child);
522 }
523 }
524
8f91a789
AM
525 private boolean evaluateIfTreeEmpty(Object input) {
526 Object[] elements = fContentProvider.getElements(input);
f1fae91f
PT
527 if (elements.length > 0 && fFilters != null) {
528 for (int i = 0; i < fFilters.size(); i++) {
529 ViewerFilter curr = fFilters.get(i);
530 elements = curr.filter(fViewer, input, elements);
8f91a789
AM
531 }
532 }
533 return elements.length == 0;
534 }
535
536 /**
537 * Private classes
538 */
539
540 private class CheckStateListener implements ICheckStateListener {
541
542 CheckStateListener() {
543 }
8906e53c 544
8f91a789
AM
545 @Override
546 public void checkStateChanged(CheckStateChangedEvent event) {
547 try {
548 ITimeGraphEntry entry = (ITimeGraphEntry) event.getElement();
549 boolean checked = event.getChecked();
8906e53c
SM
550 if (checked) {
551 checkElement(entry);
552 } else {
553 uncheckElement(entry);
8f91a789
AM
554 }
555 } catch (ClassCastException e) {
556 return;
557 }
558 }
8f91a789
AM
559 }
560}
This page took 0.063807 seconds and 5 git commands to generate.