Commit | Line | Data |
---|---|---|
8f91a789 | 1 | /******************************************************************************* |
b97d61f0 | 2 | * Copyright (c) 2000, 2015 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 |
40b7b614 | 17 | * Generoso Pagano - Add tree filter |
b97d61f0 | 18 | * Christian Mansky - Add check active / uncheck inactive buttons |
8f91a789 AM |
19 | *******************************************************************************/ |
20 | ||
2bdf0193 | 21 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.dialogs; |
8f91a789 AM |
22 | |
23 | import java.util.ArrayList; | |
24 | import java.util.Arrays; | |
25 | import java.util.List; | |
26 | ||
27 | import org.eclipse.core.runtime.IStatus; | |
28 | import org.eclipse.core.runtime.Status; | |
29 | import org.eclipse.jface.dialogs.IDialogConstants; | |
30 | import org.eclipse.jface.viewers.CheckStateChangedEvent; | |
31 | import org.eclipse.jface.viewers.CheckboxTreeViewer; | |
32 | import org.eclipse.jface.viewers.IBaseLabelProvider; | |
33 | import org.eclipse.jface.viewers.ICheckStateListener; | |
34 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
8906e53c | 35 | import org.eclipse.jface.viewers.TreeSelection; |
8f91a789 AM |
36 | import org.eclipse.jface.viewers.ViewerComparator; |
37 | import org.eclipse.jface.viewers.ViewerFilter; | |
8f91a789 AM |
38 | import org.eclipse.swt.SWT; |
39 | import org.eclipse.swt.custom.BusyIndicator; | |
40 | import org.eclipse.swt.events.SelectionAdapter; | |
41 | import org.eclipse.swt.events.SelectionEvent; | |
8f91a789 AM |
42 | import org.eclipse.swt.layout.GridData; |
43 | import org.eclipse.swt.layout.GridLayout; | |
44 | import org.eclipse.swt.widgets.Button; | |
45 | import org.eclipse.swt.widgets.Composite; | |
46 | import org.eclipse.swt.widgets.Control; | |
47 | import org.eclipse.swt.widgets.Label; | |
48 | import org.eclipse.swt.widgets.Shell; | |
49 | import org.eclipse.swt.widgets.Tree; | |
50 | import org.eclipse.swt.widgets.TreeColumn; | |
2bdf0193 AM |
51 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; |
52 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
8f91a789 | 53 | import org.eclipse.ui.PlatformUI; |
8f91a789 | 54 | import org.eclipse.ui.dialogs.ISelectionStatusValidator; |
40b7b614 | 55 | import org.eclipse.ui.dialogs.PatternFilter; |
8f91a789 AM |
56 | import org.eclipse.ui.dialogs.SelectionStatusDialog; |
57 | ||
58 | /** | |
40b7b614 GP |
59 | * Filter dialog for the time graphs This class is derived from the |
60 | * CheckedTreeSelectionDialog It was necessary to develop this similar dialog to | |
61 | * allow multiple columns | |
8f91a789 | 62 | * |
8f91a789 AM |
63 | * @author François Rajotte |
64 | */ | |
65 | public class TimeGraphFilterDialog extends SelectionStatusDialog { | |
f1fae91f PT |
66 | private static final int BUTTON_CHECK_SELECTED_ID = IDialogConstants.CLIENT_ID; |
67 | private static final int BUTTON_UNCHECK_SELECTED_ID = IDialogConstants.CLIENT_ID + 1; | |
68 | private static final int BUTTON_CHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 2; | |
69 | private static final int BUTTON_UNCHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 3; | |
b97d61f0 CM |
70 | private static final int BUTTON_CHECK_ACTIVE_ID = IDialogConstants.CLIENT_ID + 4; |
71 | private static final int BUTTON_UNCHECK_INACTIVE_ID = IDialogConstants.CLIENT_ID + 5; | |
03beb743 | 72 | |
f1fae91f PT |
73 | private static final int DEFAULT_WIDTH = 60; |
74 | private static final int DEFAULT_HEIGHT = 18; | |
8f91a789 | 75 | |
40b7b614 | 76 | private FilteredCheckboxTree fTree; |
8f91a789 AM |
77 | |
78 | private IBaseLabelProvider fLabelProvider; | |
79 | ||
b97d61f0 CM |
80 | private ITimeGraphEntryActiveProvider fCheckActiveProvider; |
81 | private ITimeGraphEntryActiveProvider fUncheckInactiveProvider; | |
82 | ||
8f91a789 AM |
83 | private ITreeContentProvider fContentProvider; |
84 | ||
85 | private String[] fColumnNames; | |
86 | ||
87 | private ISelectionStatusValidator fValidator = null; | |
88 | ||
89 | private ViewerComparator fComparator; | |
90 | ||
91 | private String fEmptyListMessage = ""; //$NON-NLS-1$ | |
92 | ||
93 | private IStatus fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, | |
94 | 0, "", null); //$NON-NLS-1$ | |
95 | ||
96 | private List<ViewerFilter> fFilters; | |
97 | ||
98 | private Object fInput; | |
99 | ||
100 | private boolean fIsEmpty; | |
101 | ||
f1fae91f | 102 | private int fWidth = DEFAULT_WIDTH; |
8f91a789 | 103 | |
f1fae91f | 104 | private int fHeight = DEFAULT_HEIGHT; |
8f91a789 | 105 | |
8f91a789 AM |
106 | private Object[] fExpandedElements; |
107 | ||
108 | /** | |
109 | * Constructs an instance of <code>ElementTreeSelectionDialog</code>. | |
110 | * | |
111 | * @param parent | |
112 | * The shell to parent from. | |
113 | */ | |
114 | public TimeGraphFilterDialog(Shell parent) { | |
115 | super(parent); | |
8f91a789 AM |
116 | setStatusLineAboveButtons(true); |
117 | setHelpAvailable(false); | |
8f91a789 | 118 | fExpandedElements = null; |
b97d61f0 CM |
119 | fCheckActiveProvider = null; |
120 | fUncheckInactiveProvider = null; | |
8f91a789 AM |
121 | } |
122 | ||
8f91a789 AM |
123 | /** |
124 | * Sets the initial selection. Convenience method. | |
125 | * | |
126 | * @param selection | |
127 | * the initial selection. | |
128 | */ | |
129 | public void setInitialSelection(Object selection) { | |
130 | setInitialSelections(new Object[] { selection }); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Sets the message to be displayed if the list is empty. | |
135 | * | |
136 | * @param message | |
137 | * the message to be displayed. | |
138 | */ | |
139 | public void setEmptyListMessage(String message) { | |
140 | fEmptyListMessage = message; | |
141 | } | |
142 | ||
143 | /** | |
144 | * Sets the comparator used by the tree viewer. | |
145 | * | |
146 | * @param comparator | |
147 | * The comparator | |
148 | */ | |
149 | public void setComparator(ViewerComparator comparator) { | |
150 | fComparator = comparator; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Adds a filter to the tree viewer. | |
155 | * | |
156 | * @param filter | |
157 | * a filter. | |
158 | */ | |
159 | public void addFilter(ViewerFilter filter) { | |
160 | if (fFilters == null) { | |
507b1336 | 161 | fFilters = new ArrayList<>(); |
8f91a789 AM |
162 | } |
163 | fFilters.add(filter); | |
164 | } | |
165 | ||
166 | /** | |
167 | * Sets an optional validator to check if the selection is valid. The | |
168 | * validator is invoked whenever the selection changes. | |
169 | * | |
170 | * @param validator | |
171 | * the validator to validate the selection. | |
172 | */ | |
173 | public void setValidator(ISelectionStatusValidator validator) { | |
174 | fValidator = validator; | |
175 | } | |
176 | ||
177 | /** | |
178 | * Sets the tree input. | |
179 | * | |
180 | * @param input | |
181 | * the tree input. | |
182 | */ | |
183 | public void setInput(Object input) { | |
184 | fInput = input; | |
185 | } | |
186 | ||
187 | /** | |
188 | * Expands elements in the tree. | |
189 | * | |
190 | * @param elements | |
191 | * The elements that will be expanded. | |
192 | */ | |
193 | public void setExpandedElements(Object[] elements) { | |
f1fae91f PT |
194 | if (elements != null) { |
195 | fExpandedElements = Arrays.copyOf(elements, elements.length); | |
196 | } else { | |
197 | fExpandedElements = null; | |
198 | } | |
8f91a789 AM |
199 | } |
200 | ||
201 | /** | |
202 | * Sets the size of the tree in unit of characters. | |
203 | * | |
204 | * @param width | |
205 | * the width of the tree. | |
206 | * @param height | |
207 | * the height of the tree. | |
208 | */ | |
209 | public void setSize(int width, int height) { | |
210 | fWidth = width; | |
211 | fHeight = height; | |
212 | } | |
213 | ||
214 | /** | |
40b7b614 GP |
215 | * @param contentProvider |
216 | * The content provider for the table | |
8f91a789 AM |
217 | */ |
218 | public void setContentProvider(ITreeContentProvider contentProvider) { | |
219 | fContentProvider = contentProvider; | |
220 | } | |
221 | ||
222 | /** | |
40b7b614 GP |
223 | * @param labelProvider |
224 | * The label provider for the table | |
8f91a789 AM |
225 | */ |
226 | public void setLabelProvider(IBaseLabelProvider labelProvider) { | |
227 | fLabelProvider = labelProvider; | |
228 | } | |
229 | ||
b97d61f0 CM |
230 | /** |
231 | * @param activeProvider | |
232 | * Information about an additional view specific Button | |
233 | * @since 1.0 | |
234 | */ | |
235 | public void addTimeGraphFilterCheckActiveButton(ITimeGraphEntryActiveProvider activeProvider) { | |
236 | fCheckActiveProvider = activeProvider; | |
237 | } | |
238 | ||
239 | /** | |
240 | * @param inactiveProvider | |
241 | * Information about an additional view specific Button | |
242 | * @since 1.0 | |
243 | */ | |
244 | public void addTimeGraphFilterUncheckInactiveButton(ITimeGraphEntryActiveProvider inactiveProvider) { | |
245 | fUncheckInactiveProvider = inactiveProvider; | |
246 | } | |
247 | ||
8f91a789 | 248 | /** |
40b7b614 GP |
249 | * @param columnNames |
250 | * An array of column names to display | |
8f91a789 AM |
251 | */ |
252 | public void setColumnNames(String[] columnNames) { | |
f1fae91f PT |
253 | if (columnNames != null) { |
254 | fColumnNames = Arrays.copyOf(columnNames, columnNames.length); | |
255 | } else { | |
256 | fColumnNames = null; | |
257 | } | |
8f91a789 AM |
258 | } |
259 | ||
260 | /** | |
261 | * Validate the receiver and update the status with the result. | |
262 | * | |
263 | */ | |
264 | protected void updateOKStatus() { | |
265 | if (!fIsEmpty) { | |
266 | if (fValidator != null) { | |
40b7b614 | 267 | fCurrStatus = fValidator.validate(fTree.getCheckedElements()); |
8f91a789 AM |
268 | updateStatus(fCurrStatus); |
269 | } else if (!fCurrStatus.isOK()) { | |
270 | fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, | |
271 | IStatus.OK, "", //$NON-NLS-1$ | |
272 | null); | |
273 | } | |
274 | } else { | |
275 | fCurrStatus = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, | |
276 | IStatus.OK, fEmptyListMessage, null); | |
277 | } | |
278 | updateStatus(fCurrStatus); | |
279 | } | |
280 | ||
281 | @Override | |
282 | public int open() { | |
283 | fIsEmpty = evaluateIfTreeEmpty(fInput); | |
5fe39e4a | 284 | setResult(null); |
8f91a789 AM |
285 | super.open(); |
286 | return getReturnCode(); | |
287 | } | |
288 | ||
8f91a789 AM |
289 | @Override |
290 | protected void computeResult() { | |
40b7b614 | 291 | setResult(Arrays.asList(fTree.getCheckedElements())); |
8f91a789 AM |
292 | } |
293 | ||
294 | @Override | |
295 | public void create() { | |
296 | BusyIndicator.showWhile(null, new Runnable() { | |
297 | @Override | |
298 | public void run() { | |
f1fae91f | 299 | TimeGraphFilterDialog.super.create(); |
40b7b614 | 300 | fTree.setCheckedElements(getInitialElementSelections() |
8f91a789 AM |
301 | .toArray()); |
302 | if (fExpandedElements != null) { | |
40b7b614 | 303 | fTree.getViewer().setExpandedElements(fExpandedElements); |
8f91a789 AM |
304 | } |
305 | updateOKStatus(); | |
306 | } | |
307 | }); | |
308 | } | |
309 | ||
310 | @Override | |
311 | protected Control createDialogArea(Composite parent) { | |
312 | Composite composite = (Composite) super.createDialogArea(parent); | |
313 | Label messageLabel = createMessageArea(composite); | |
314 | CheckboxTreeViewer treeViewer = createTreeViewer(composite); | |
315 | Control buttonComposite = createSelectionButtons(composite); | |
316 | GridData data = new GridData(GridData.FILL_BOTH); | |
317 | data.widthHint = convertWidthInCharsToPixels(fWidth); | |
318 | data.heightHint = convertHeightInCharsToPixels(fHeight); | |
319 | Tree treeWidget = treeViewer.getTree(); | |
320 | treeWidget.setLayoutData(data); | |
321 | treeWidget.setFont(parent.getFont()); | |
322 | if (fIsEmpty) { | |
323 | messageLabel.setEnabled(false); | |
324 | treeWidget.setEnabled(false); | |
325 | buttonComposite.setEnabled(false); | |
326 | } | |
327 | return composite; | |
328 | } | |
329 | ||
330 | /** | |
331 | * Creates the tree viewer. | |
332 | * | |
333 | * @param parent | |
334 | * the parent composite | |
335 | * @return the tree viewer | |
336 | */ | |
337 | protected CheckboxTreeViewer createTreeViewer(Composite parent) { | |
40b7b614 GP |
338 | PatternFilter filter = new TreePatternFilter(); |
339 | filter.setIncludeLeadingWildcard(true); | |
340 | fTree = new FilteredCheckboxTree(parent, SWT.BORDER | SWT.MULTI, filter, true); | |
8f91a789 | 341 | |
40b7b614 | 342 | Tree tree = fTree.getViewer().getTree(); |
8f91a789 AM |
343 | tree.setHeaderVisible(true); |
344 | for (String columnName : fColumnNames) { | |
345 | TreeColumn column = new TreeColumn(tree, SWT.LEFT); | |
346 | column.setText(columnName); | |
347 | column.pack(); | |
348 | } | |
349 | ||
40b7b614 GP |
350 | fTree.getViewer().setContentProvider(fContentProvider); |
351 | fTree.getViewer().setLabelProvider(fLabelProvider); | |
352 | fTree.addCheckStateListener(new CheckStateListener()); | |
353 | fTree.getViewer().setComparator(fComparator); | |
8f91a789 AM |
354 | if (fFilters != null) { |
355 | for (int i = 0; i != fFilters.size(); i++) { | |
40b7b614 | 356 | fTree.getViewer().addFilter(fFilters.get(i)); |
8f91a789 AM |
357 | } |
358 | } | |
40b7b614 | 359 | fTree.getViewer().setInput(fInput); |
8f91a789 | 360 | |
40b7b614 | 361 | // pack the columns again for a nice view... |
8f91a789 AM |
362 | for (TreeColumn column : tree.getColumns()) { |
363 | column.pack(); | |
364 | } | |
40b7b614 | 365 | return (CheckboxTreeViewer) fTree.getViewer(); |
8f91a789 AM |
366 | } |
367 | ||
368 | /** | |
369 | * Returns the tree viewer. | |
370 | * | |
371 | * @return the tree viewer | |
372 | */ | |
373 | protected CheckboxTreeViewer getTreeViewer() { | |
40b7b614 | 374 | return (CheckboxTreeViewer) fTree.getViewer(); |
8f91a789 AM |
375 | } |
376 | ||
377 | /** | |
378 | * Adds the selection and deselection buttons to the dialog. | |
379 | * | |
380 | * @param composite | |
381 | * the parent composite | |
382 | * @return Composite the composite the buttons were created in. | |
383 | */ | |
384 | protected Composite createSelectionButtons(Composite composite) { | |
385 | Composite buttonComposite = new Composite(composite, SWT.RIGHT); | |
386 | GridLayout layout = new GridLayout(); | |
8f91a789 AM |
387 | layout.marginWidth = 0; |
388 | layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); | |
389 | buttonComposite.setLayout(layout); | |
390 | buttonComposite.setFont(composite.getFont()); | |
391 | GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END | |
392 | | GridData.GRAB_HORIZONTAL); | |
393 | data.grabExcessHorizontalSpace = true; | |
394 | buttonComposite.setLayoutData(data); | |
8906e53c SM |
395 | |
396 | /* Create the buttons in the good order to place them as we want */ | |
03beb743 SM |
397 | Button checkSelectedButton = createButton(buttonComposite, |
398 | BUTTON_CHECK_SELECTED_ID, Messages.TmfTimeFilterDialog_CHECK_SELECTED, | |
399 | false); | |
400 | Button checkSubtreeButton = createButton(buttonComposite, | |
401 | BUTTON_CHECK_SUBTREE_ID, Messages.TmfTimeFilterDialog_CHECK_SUBTREE, | |
402 | false); | |
8906e53c SM |
403 | Button checkAllButton = createButton(buttonComposite, |
404 | IDialogConstants.SELECT_ALL_ID, Messages.TmfTimeFilterDialog_CHECK_ALL, | |
405 | false); | |
b97d61f0 CM |
406 | Button checkActiveButton = null; |
407 | if (fCheckActiveProvider != null) { | |
408 | checkActiveButton = createButton(buttonComposite, | |
409 | BUTTON_CHECK_ACTIVE_ID, fCheckActiveProvider.getLabel(), | |
410 | false); | |
411 | checkActiveButton.setToolTipText(fCheckActiveProvider.getTooltip()); | |
412 | } else if (fUncheckInactiveProvider != null) { | |
413 | // Filler label to ensure correct layout. | |
414 | Label filler = new Label(buttonComposite, 0); | |
415 | filler.setText(""); //$NON-NLS-1$ | |
416 | } | |
03beb743 SM |
417 | |
418 | Button uncheckSelectedButton = createButton(buttonComposite, | |
419 | BUTTON_UNCHECK_SELECTED_ID, Messages.TmfTimeFilterDialog_UNCHECK_SELECTED, | |
420 | false); | |
421 | Button uncheckSubtreeButton = createButton(buttonComposite, | |
422 | BUTTON_UNCHECK_SUBTREE_ID, Messages.TmfTimeFilterDialog_UNCHECK_SUBTREE, | |
8906e53c SM |
423 | false); |
424 | Button uncheckAllButton = createButton(buttonComposite, | |
425 | IDialogConstants.DESELECT_ALL_ID, Messages.TmfTimeFilterDialog_UNCHECK_ALL, | |
426 | false); | |
b97d61f0 CM |
427 | Button uncheckInactiveButton = null; |
428 | if (fUncheckInactiveProvider != null) { | |
429 | uncheckInactiveButton = createButton(buttonComposite, | |
430 | BUTTON_UNCHECK_INACTIVE_ID, fUncheckInactiveProvider.getLabel(), | |
431 | false); | |
432 | uncheckInactiveButton.setToolTipText(fUncheckInactiveProvider.getTooltip()); | |
433 | } | |
03beb743 | 434 | |
8906e53c SM |
435 | /* |
436 | * Apply the layout again after creating the buttons to override | |
437 | * createButton messing with the columns | |
438 | */ | |
03beb743 | 439 | layout.numColumns = 3; |
b97d61f0 CM |
440 | if (fCheckActiveProvider != null || fUncheckInactiveProvider != null) { |
441 | layout.numColumns++; | |
442 | } | |
8906e53c SM |
443 | buttonComposite.setLayout(layout); |
444 | ||
445 | /* Add a listener to each button */ | |
03beb743 | 446 | checkSelectedButton.addSelectionListener(new SelectionAdapter() { |
8f91a789 AM |
447 | @Override |
448 | public void widgetSelected(SelectionEvent e) { | |
40b7b614 | 449 | TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection(); |
8906e53c | 450 | |
03beb743 SM |
451 | for (Object element : selection.toArray()) { |
452 | checkElement(element); | |
8f91a789 | 453 | } |
8906e53c | 454 | |
8f91a789 AM |
455 | updateOKStatus(); |
456 | } | |
8906e53c SM |
457 | }); |
458 | ||
03beb743 | 459 | checkSubtreeButton.addSelectionListener(new SelectionAdapter() { |
8f91a789 AM |
460 | @Override |
461 | public void widgetSelected(SelectionEvent e) { | |
40b7b614 | 462 | TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection(); |
03beb743 SM |
463 | |
464 | for (Object element : selection.toArray()) { | |
465 | checkElementAndSubtree(element); | |
466 | } | |
467 | } | |
468 | }); | |
469 | ||
470 | checkAllButton.addSelectionListener(new SelectionAdapter() { | |
471 | @Override | |
472 | public void widgetSelected(SelectionEvent e) { | |
473 | Object[] viewerElements = fContentProvider.getElements(fInput); | |
474 | ||
475 | for (int i = 0; i < viewerElements.length; i++) { | |
40b7b614 | 476 | fTree.setSubtreeChecked(viewerElements[i], true); |
03beb743 SM |
477 | } |
478 | ||
8f91a789 AM |
479 | updateOKStatus(); |
480 | } | |
8906e53c SM |
481 | }); |
482 | ||
b97d61f0 CM |
483 | if (checkActiveButton != null) { |
484 | checkActiveButton.addSelectionListener(new CheckActiveSelectionAdapter(true)); | |
485 | } | |
486 | ||
03beb743 | 487 | uncheckSelectedButton.addSelectionListener(new SelectionAdapter() { |
8906e53c SM |
488 | @Override |
489 | public void widgetSelected(SelectionEvent e) { | |
40b7b614 | 490 | TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection(); |
8906e53c SM |
491 | |
492 | for (Object element : selection.toArray()) { | |
03beb743 | 493 | uncheckElement(element); |
8906e53c SM |
494 | } |
495 | ||
496 | updateOKStatus(); | |
497 | } | |
498 | }); | |
499 | ||
03beb743 | 500 | uncheckSubtreeButton.addSelectionListener(new SelectionAdapter() { |
8906e53c SM |
501 | @Override |
502 | public void widgetSelected(SelectionEvent e) { | |
40b7b614 | 503 | TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection(); |
8906e53c SM |
504 | |
505 | for (Object element : selection.toArray()) { | |
506 | uncheckElement(element); | |
507 | } | |
508 | ||
509 | updateOKStatus(); | |
510 | } | |
511 | }); | |
512 | ||
03beb743 SM |
513 | uncheckAllButton.addSelectionListener(new SelectionAdapter() { |
514 | @Override | |
515 | public void widgetSelected(SelectionEvent e) { | |
40b7b614 GP |
516 | Object[] viewerElements = fContentProvider.getElements(fInput); |
517 | for (Object element : viewerElements) { | |
518 | if (fTree.getViewer().testFindItem(element) != null) { | |
519 | // uncheck only visible roots and their children | |
520 | uncheckElement(element); | |
521 | } | |
522 | } | |
03beb743 SM |
523 | updateOKStatus(); |
524 | } | |
525 | }); | |
526 | ||
b97d61f0 CM |
527 | if (uncheckInactiveButton != null) { |
528 | uncheckInactiveButton.addSelectionListener(new CheckActiveSelectionAdapter(false)); | |
529 | } | |
530 | ||
8f91a789 AM |
531 | return buttonComposite; |
532 | } | |
533 | ||
03beb743 SM |
534 | /** |
535 | * Check an element and all its parents. | |
536 | * | |
537 | * @param element | |
538 | * The element to check. | |
539 | */ | |
8906e53c | 540 | private void checkElement(Object element) { |
40b7b614 | 541 | fTree.setChecked(element, true); |
03beb743 SM |
542 | |
543 | Object parent = fContentProvider.getParent(element); | |
544 | ||
40b7b614 | 545 | if (parent != null && !fTree.getChecked(parent)) { |
03beb743 | 546 | checkElement(parent); |
8906e53c SM |
547 | } |
548 | } | |
549 | ||
03beb743 SM |
550 | /** |
551 | * Check an element, all its parents and all its children. | |
552 | * | |
553 | * @param element | |
554 | * The element to check. | |
555 | */ | |
556 | private void checkElementAndSubtree(Object element) { | |
557 | checkElement(element); | |
558 | ||
559 | for (Object child : fContentProvider.getChildren(element)) { | |
560 | checkElementAndSubtree(child); | |
561 | } | |
562 | } | |
8906e53c | 563 | |
03beb743 SM |
564 | /** |
565 | * Uncheck an element and all its children. | |
566 | * | |
567 | * @param element | |
568 | * The element to uncheck. | |
569 | */ | |
570 | private void uncheckElement(Object element) { | |
40b7b614 | 571 | fTree.setChecked(element, false); |
8906e53c | 572 | |
03beb743 | 573 | for (Object child : fContentProvider.getChildren(element)) { |
8906e53c SM |
574 | uncheckElement(child); |
575 | } | |
576 | } | |
577 | ||
8f91a789 AM |
578 | private boolean evaluateIfTreeEmpty(Object input) { |
579 | Object[] elements = fContentProvider.getElements(input); | |
f1fae91f PT |
580 | if (elements.length > 0 && fFilters != null) { |
581 | for (int i = 0; i < fFilters.size(); i++) { | |
582 | ViewerFilter curr = fFilters.get(i); | |
40b7b614 | 583 | elements = curr.filter(fTree.getViewer(), input, elements); |
8f91a789 AM |
584 | } |
585 | } | |
586 | return elements.length == 0; | |
587 | } | |
588 | ||
589 | /** | |
590 | * Private classes | |
591 | */ | |
592 | ||
593 | private class CheckStateListener implements ICheckStateListener { | |
594 | ||
595 | CheckStateListener() { | |
596 | } | |
8906e53c | 597 | |
8f91a789 AM |
598 | @Override |
599 | public void checkStateChanged(CheckStateChangedEvent event) { | |
600 | try { | |
601 | ITimeGraphEntry entry = (ITimeGraphEntry) event.getElement(); | |
602 | boolean checked = event.getChecked(); | |
8906e53c SM |
603 | if (checked) { |
604 | checkElement(entry); | |
605 | } else { | |
606 | uncheckElement(entry); | |
8f91a789 AM |
607 | } |
608 | } catch (ClassCastException e) { | |
609 | return; | |
40b7b614 GP |
610 | } finally { |
611 | updateOKStatus(); | |
8f91a789 AM |
612 | } |
613 | } | |
40b7b614 | 614 | |
8f91a789 | 615 | } |
b97d61f0 CM |
616 | |
617 | private class CheckActiveSelectionAdapter extends SelectionAdapter { | |
618 | ||
619 | boolean fIsCheckActive; | |
620 | ITimeGraphEntryActiveProvider fActiveProvider; | |
621 | ||
622 | CheckActiveSelectionAdapter(boolean isActive) { | |
623 | super(); | |
624 | fIsCheckActive = isActive; | |
625 | if (fIsCheckActive) { | |
626 | fActiveProvider = fCheckActiveProvider; | |
627 | } else { | |
628 | fActiveProvider = fUncheckInactiveProvider; | |
629 | } | |
630 | } | |
631 | ||
632 | @Override | |
633 | public void widgetSelected(SelectionEvent e) { | |
634 | /* Uncheck all elements that are not in this list */ | |
635 | Object[] viewerElements = fContentProvider.getElements(fInput); | |
636 | for (int j = 0; j < viewerElements.length; j++) { | |
637 | if (fIsCheckActive) { | |
638 | checkActive(viewerElements[j]); | |
639 | } else { | |
640 | uncheckInactive(viewerElements[j]); | |
641 | } | |
642 | } | |
643 | ||
644 | updateOKStatus(); | |
645 | } | |
646 | ||
647 | private boolean checkActive(Object element) { | |
648 | boolean wasChildChecked = false; | |
649 | boolean isActive = false; | |
650 | ||
651 | for (Object child : fContentProvider.getChildren(element)) { | |
652 | wasChildChecked |= checkActive(child); | |
653 | } | |
654 | ||
655 | if (!fTree.getChecked(element)) { | |
656 | /* Call isActive if none of this elements children are checked. */ | |
657 | if (!wasChildChecked) { | |
658 | isActive = element instanceof ITimeGraphEntry && | |
659 | fActiveProvider.isActive((ITimeGraphEntry) element); | |
660 | } | |
661 | /* | |
662 | * Check this element if its either active or if any of its | |
663 | * children are checked. | |
664 | */ | |
665 | if (isActive || wasChildChecked) { | |
666 | fTree.setChecked(element, true); | |
667 | } | |
668 | } | |
669 | ||
670 | return fTree.getChecked(element); | |
671 | } | |
672 | ||
673 | private boolean uncheckInactive(Object element) { | |
674 | boolean wasChildChecked = false; | |
675 | ||
676 | for (Object child : fContentProvider.getChildren(element)) { | |
677 | wasChildChecked |= uncheckInactive(child); | |
678 | } | |
679 | ||
680 | /* | |
681 | * Call isActive if this element is checked and none of its children | |
682 | * are checked. | |
683 | */ | |
684 | if (fTree.getChecked(element) && !wasChildChecked) { | |
685 | /* Uncheck this element if its inactive. */ | |
686 | if (element instanceof ITimeGraphEntry && | |
687 | !fActiveProvider.isActive((ITimeGraphEntry) element)) { | |
688 | fTree.setChecked(element, false); | |
689 | } | |
690 | } | |
691 | ||
692 | return fTree.getChecked(element); | |
693 | } | |
694 | } | |
40b7b614 | 695 | } |