TMF: added support for process filter in the TimeGraphFilterDialog.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / dialogs / TimeGraphFilterDialog.java
index b4f3bb476ca1519e86d1f5ef3875c95a04369b8e..76465cd0710690bcbcebb3c2353b8878100ee3bb 100644 (file)
@@ -13,6 +13,8 @@
  *          CheckedTreeSelectionDialog#createSelectionButtons(Composite) fails to
  *          align the selection buttons to the right
  *      François Rajotte - Support for multiple columns + selection control
+ *      Patrick Tasse - Fix Sonar warnings
+ *      Generoso Pagano - Add tree filter
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.dialogs;
@@ -49,25 +51,28 @@ import org.eclipse.swt.widgets.Tree;
 import org.eclipse.swt.widgets.TreeColumn;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
+import org.eclipse.ui.dialogs.PatternFilter;
 import org.eclipse.ui.dialogs.SelectionStatusDialog;
 
 /**
- * Filter dialog for the time graphs
- * This class is derived from the CheckedTreeSelectionDialog
- * It was necessary to develop this similar dialog to allow multiple columns
+ * Filter dialog for the time graphs This class is derived from the
+ * CheckedTreeSelectionDialog It was necessary to develop this similar dialog to
+ * allow multiple columns
  *
  * @version 1.0
  * @since 2.0
  * @author François Rajotte
  */
 public class TimeGraphFilterDialog extends SelectionStatusDialog {
-    private final static int BUTTON_CHECK_SELECTED_ID = IDialogConstants.CLIENT_ID;
-    private final static int BUTTON_UNCHECK_SELECTED_ID = IDialogConstants.CLIENT_ID + 1;
-    private final static int BUTTON_CHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 2;
-    private final static int BUTTON_UNCHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 3;
+    private static final int BUTTON_CHECK_SELECTED_ID = IDialogConstants.CLIENT_ID;
+    private static final int BUTTON_UNCHECK_SELECTED_ID = IDialogConstants.CLIENT_ID + 1;
+    private static final int BUTTON_CHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 2;
+    private static final int BUTTON_UNCHECK_SUBTREE_ID = IDialogConstants.CLIENT_ID + 3;
 
+    private static final int DEFAULT_WIDTH = 60;
+    private static final int DEFAULT_HEIGHT = 18;
 
-    private CheckboxTreeViewer fViewer;
+    private FilteredCheckboxTree fTree;
 
     private IBaseLabelProvider fLabelProvider;
 
@@ -90,9 +95,9 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
 
     private boolean fIsEmpty;
 
-    private int fWidth = 60;
+    private int fWidth = DEFAULT_WIDTH;
 
-    private int fHeight = 18;
+    private int fHeight = DEFAULT_HEIGHT;
 
     private Object[] fExpandedElements;
 
@@ -104,7 +109,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      */
     public TimeGraphFilterDialog(Shell parent) {
         super(parent);
-        setResult(new ArrayList<Object>(0));
+        setResult(new ArrayList<>(0));
         setStatusLineAboveButtons(true);
         setHelpAvailable(false);
         fExpandedElements = null;
@@ -148,7 +153,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      */
     public void addFilter(ViewerFilter filter) {
         if (fFilters == null) {
-            fFilters = new ArrayList<ViewerFilter>(4);
+            fFilters = new ArrayList<>();
         }
         fFilters.add(filter);
     }
@@ -181,7 +186,11 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      *            The elements that will be expanded.
      */
     public void setExpandedElements(Object[] elements) {
-        fExpandedElements = elements;
+        if (elements != null) {
+            fExpandedElements = Arrays.copyOf(elements, elements.length);
+        } else {
+            fExpandedElements = null;
+        }
     }
 
     /**
@@ -198,24 +207,31 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
     }
 
     /**
-     * @param contentProvider The content provider for the table
+     * @param contentProvider
+     *            The content provider for the table
      */
     public void setContentProvider(ITreeContentProvider contentProvider) {
         fContentProvider = contentProvider;
     }
 
     /**
-     * @param labelProvider The label provider for the table
+     * @param labelProvider
+     *            The label provider for the table
      */
     public void setLabelProvider(IBaseLabelProvider labelProvider) {
         fLabelProvider = labelProvider;
     }
 
     /**
-     * @param columnNames An array of column names to display
+     * @param columnNames
+     *            An array of column names to display
      */
     public void setColumnNames(String[] columnNames) {
-        fColumnNames = columnNames;
+        if (columnNames != null) {
+            fColumnNames = Arrays.copyOf(columnNames, columnNames.length);
+        } else {
+            fColumnNames = null;
+        }
     }
 
     /**
@@ -225,7 +241,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
     protected void updateOKStatus() {
         if (!fIsEmpty) {
             if (fValidator != null) {
-                fCurrStatus = fValidator.validate(fViewer.getCheckedElements());
+                fCurrStatus = fValidator.validate(fTree.getCheckedElements());
                 updateStatus(fCurrStatus);
             } else if (!fCurrStatus.isOK()) {
                 fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
@@ -246,10 +262,6 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         return getReturnCode();
     }
 
-    private void access$superCreate() {
-        super.create();
-    }
-
     @Override
     protected void cancelPressed() {
         setResult(null);
@@ -258,7 +270,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
 
     @Override
     protected void computeResult() {
-        setResult(Arrays.asList(fViewer.getCheckedElements()));
+        setResult(Arrays.asList(fTree.getCheckedElements()));
     }
 
     @Override
@@ -266,11 +278,11 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         BusyIndicator.showWhile(null, new Runnable() {
             @Override
             public void run() {
-                access$superCreate();
-                fViewer.setCheckedElements(getInitialElementSelections()
+                TimeGraphFilterDialog.super.create();
+                fTree.setCheckedElements(getInitialElementSelections()
                         .toArray());
                 if (fExpandedElements != null) {
-                    fViewer.setExpandedElements(fExpandedElements);
+                    fTree.getViewer().setExpandedElements(fExpandedElements);
                 }
                 updateOKStatus();
             }
@@ -305,9 +317,11 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      * @return the tree viewer
      */
     protected CheckboxTreeViewer createTreeViewer(Composite parent) {
-        fViewer = new CheckboxTreeViewer(parent, SWT.BORDER | SWT.MULTI);
+        PatternFilter filter = new TreePatternFilter();
+        filter.setIncludeLeadingWildcard(true);
+        fTree = new FilteredCheckboxTree(parent, SWT.BORDER | SWT.MULTI, filter, true);
 
-        Tree tree = fViewer.getTree();
+        Tree tree = fTree.getViewer().getTree();
         tree.setHeaderVisible(true);
         for (String columnName : fColumnNames) {
             TreeColumn column = new TreeColumn(tree, SWT.LEFT);
@@ -315,28 +329,22 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
             column.pack();
         }
 
-        fViewer.setContentProvider(fContentProvider);
-        fViewer.setLabelProvider(fLabelProvider);
-        fViewer.addCheckStateListener(new CheckStateListener());
-        fViewer.addCheckStateListener(new ICheckStateListener() {
-            @Override
-            public void checkStateChanged(CheckStateChangedEvent event) {
-                updateOKStatus();
-            }
-        });
-        fViewer.setComparator(fComparator);
+        fTree.getViewer().setContentProvider(fContentProvider);
+        fTree.getViewer().setLabelProvider(fLabelProvider);
+        fTree.addCheckStateListener(new CheckStateListener());
+        fTree.getViewer().setComparator(fComparator);
         if (fFilters != null) {
             for (int i = 0; i != fFilters.size(); i++) {
-                fViewer.addFilter(fFilters.get(i));
+                fTree.getViewer().addFilter(fFilters.get(i));
             }
         }
-        fViewer.setInput(fInput);
+        fTree.getViewer().setInput(fInput);
 
-        //pack the columns again for a nice view...
+        // pack the columns again for a nice view...
         for (TreeColumn column : tree.getColumns()) {
             column.pack();
         }
-        return fViewer;
+        return (CheckboxTreeViewer) fTree.getViewer();
     }
 
     /**
@@ -345,7 +353,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      * @return the tree viewer
      */
     protected CheckboxTreeViewer getTreeViewer() {
-        return fViewer;
+        return (CheckboxTreeViewer) fTree.getViewer();
     }
 
     /**
@@ -388,7 +396,6 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
                 IDialogConstants.DESELECT_ALL_ID, Messages.TmfTimeFilterDialog_UNCHECK_ALL,
                 false);
 
-
         /*
          * Apply the layout again after creating the buttons to override
          * createButton messing with the columns
@@ -400,7 +407,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         checkSelectedButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                TreeSelection selection = (TreeSelection) fViewer.getSelection();
+                TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection();
 
                 for (Object element : selection.toArray()) {
                     checkElement(element);
@@ -413,7 +420,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         checkSubtreeButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                TreeSelection selection = (TreeSelection) fViewer.getSelection();
+                TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection();
 
                 for (Object element : selection.toArray()) {
                     checkElementAndSubtree(element);
@@ -427,7 +434,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
                 Object[] viewerElements = fContentProvider.getElements(fInput);
 
                 for (int i = 0; i < viewerElements.length; i++) {
-                    fViewer.setSubtreeChecked(viewerElements[i], true);
+                    fTree.setSubtreeChecked(viewerElements[i], true);
                 }
 
                 updateOKStatus();
@@ -437,7 +444,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         uncheckSelectedButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                TreeSelection selection = (TreeSelection) fViewer.getSelection();
+                TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection();
 
                 for (Object element : selection.toArray()) {
                     uncheckElement(element);
@@ -450,7 +457,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         uncheckSubtreeButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                TreeSelection selection = (TreeSelection) fViewer.getSelection();
+                TreeSelection selection = (TreeSelection) fTree.getViewer().getSelection();
 
                 for (Object element : selection.toArray()) {
                     uncheckElement(element);
@@ -463,7 +470,13 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
         uncheckAllButton.addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
-                fViewer.setCheckedElements(new Object[0]);
+                Object[] viewerElements = fContentProvider.getElements(fInput);
+                for (Object element : viewerElements) {
+                    if (fTree.getViewer().testFindItem(element) != null) {
+                        // uncheck only visible roots and their children
+                        uncheckElement(element);
+                    }
+                }
                 updateOKStatus();
             }
         });
@@ -478,11 +491,11 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      *            The element to check.
      */
     private void checkElement(Object element) {
-        fViewer.setChecked(element, true);
+        fTree.setChecked(element, true);
 
         Object parent = fContentProvider.getParent(element);
 
-        if (parent != null) {
+        if (parent != null && !fTree.getChecked(parent)) {
             checkElement(parent);
         }
     }
@@ -508,7 +521,7 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
      *            The element to uncheck.
      */
     private void uncheckElement(Object element) {
-        fViewer.setChecked(element, false);
+        fTree.setChecked(element, false);
 
         for (Object child : fContentProvider.getChildren(element)) {
             uncheckElement(child);
@@ -517,12 +530,10 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
 
     private boolean evaluateIfTreeEmpty(Object input) {
         Object[] elements = fContentProvider.getElements(input);
-        if (elements.length > 0) {
-            if (fFilters != null) {
-                for (int i = 0; i < fFilters.size(); i++) {
-                    ViewerFilter curr = fFilters.get(i);
-                    elements = curr.filter(fViewer, input, elements);
-                }
+        if (elements.length > 0 && fFilters != null) {
+            for (int i = 0; i < fFilters.size(); i++) {
+                ViewerFilter curr = fFilters.get(i);
+                elements = curr.filter(fTree.getViewer(), input, elements);
             }
         }
         return elements.length == 0;
@@ -549,7 +560,10 @@ public class TimeGraphFilterDialog extends SelectionStatusDialog {
                 }
             } catch (ClassCastException e) {
                 return;
+            } finally {
+                updateOKStatus();
             }
         }
+
     }
-}
+}
\ No newline at end of file
This page took 0.030517 seconds and 5 git commands to generate.