tmf: Add/update '-', '+' and '*' key handling in TimeGraphViewer
authorBernd Hufmann <Bernd.Hufmann@ericsson.com>
Wed, 13 Apr 2016 01:40:10 +0000 (21:40 -0400)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Thu, 21 Apr 2016 17:43:12 +0000 (13:43 -0400)
The handling of '-'and '+' key have been updated when the mouse is over
the namespace. Before they were used for time zooming regardless of the
mouse position. The handling of '*' key has been added when the mouse
is over the namespace.

When the mouse is over the namespace, pressing the
- key '-' will collapse recursively selected entry
- key '+' will expand selected entry
- key '*' will expand selected entry to the level with at least one
  collapsed entry

This patch is to align the keystroke handling to the TimeGraphCombo.

Change-Id: I964168bd1aa388430775055a39f625beaffb5985
Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/70583
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/TimeGraphViewer.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/widgets/TimeGraphControl.java

index ab5fd9b1643628e5e28000af43911c06823987a9..96462542fb3db66691db372aba8b9d35077743a5 100644 (file)
@@ -548,11 +548,7 @@ public class TimeGraphViewer implements ITimeDataProvider, IMarkerAxisListener,
         fTimeGraphCtrl.addKeyListener(new KeyAdapter() {
             @Override
             public void keyPressed(KeyEvent e) {
-                if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) {
-                    zoomIn();
-                } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) {
-                    zoomOut();
-                } else if (e.keyCode == '.') {
+                if (e.keyCode == '.') {
                     boolean extend = (e.stateMask & SWT.SHIFT) != 0;
                     if (extend) {
                         extendToNextMarker();
index 067f6ece0ea8127639a1dfb6cd2180dcb5701107..712da69de470d15b8336a629653578fdec97f0d1 100644 (file)
@@ -27,9 +27,11 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Queue;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.action.IStatusLineManager;
@@ -155,6 +157,7 @@ public class TimeGraphControl extends TimeGraphBaseControl
     private int fDragButton;
     private int fDragX0 = 0;
     private int fDragX = 0;
+    private boolean fHasNamespaceFocus = false;
     private long fDragTime0 = 0; // used to preserve accuracy of modified selection
     private int fIdealNameSpace = 0;
     private long fTime0bak;
@@ -583,6 +586,108 @@ public class TimeGraphControl extends TimeGraphBaseControl
         }
     }
 
+    /**
+     * Set the expanded state of a given entry to certain relative level.
+     * It will call fireTreeEvent() for each changed entry. At the end
+     * it will call redraw().
+     *
+     * @param entry
+     *            The entry
+     * @param level
+     *            level to expand to or negative for all levels
+     * @param expanded
+     *            True if expanded, false if collapsed
+     */
+    private void setExpandedState(ITimeGraphEntry entry, int level, boolean expanded) {
+        setExpandedStateInt(entry, level, expanded);
+        redraw();
+    }
+
+    /**
+     * Set the expanded state of a given entry and its children to the first
+     * level that has one collapsed entry.
+     *
+     * @param entry
+     *            The entry
+     */
+    private void setExpandedStateLevel(ITimeGraphEntry entry) {
+        int level = findExpandedLevel(entry);
+        if (level >= 0) {
+            setExpandedStateInt(entry, level, true);
+            redraw();
+        }
+    }
+
+    /*
+     * Inner class for finding relative level with at least one
+     * collapsed entry.
+     */
+    private class SearchNode {
+        SearchNode(ITimeGraphEntry e, int l) {
+            entry = e;
+            level = l;
+        }
+        ITimeGraphEntry entry;
+        int level;
+    }
+
+    /**
+     * Finds the relative level with at least one collapsed entry.
+     *
+     * @param entry
+     *            the start entry
+     * @return the found level or -1 if all levels are already expanded.
+     */
+    private int findExpandedLevel(ITimeGraphEntry entry) {
+        Queue<SearchNode> queue = new LinkedList<>();
+        SearchNode root = new SearchNode(entry, 0);
+        SearchNode node = root;
+        queue.add(root);
+
+        while (!queue.isEmpty()) {
+            node = queue.remove();
+            if (node.entry.hasChildren() && !getExpandedState(node.entry)) {
+                return node.level;
+            }
+            for (ITimeGraphEntry e : node.entry.getChildren()) {
+                if (e.hasChildren()) {
+                    SearchNode n = new SearchNode(e, node.level + 1);
+                    queue.add(n);
+                }
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * Set the expanded state of a given entry to certain relative level.
+     * It will call fireTreeEvent() for each changed entry. No redraw is done.
+     *
+     * @param entry
+     *            The entry
+     * @param level
+     *            level to expand to or negative for all levels
+     * @param expanded
+     *            True if expanded, false if collapsed
+     */
+    private void setExpandedStateInt(ITimeGraphEntry entry, int aLevel, boolean expanded) {
+        int level = aLevel;
+        if ((level > 0) || (level < 0)) {
+            level--;
+            if (entry.hasChildren()) {
+                for (ITimeGraphEntry e : entry.getChildren()) {
+                    setExpandedStateInt(e, level, expanded);
+                }
+            }
+        }
+        Item item = fItemData.findItem(entry);
+        if (item != null && item.fExpanded != expanded) {
+            item.fExpanded = expanded;
+            fItemData.updateExpandedItems();
+            fireTreeEvent(item.fEntry, item.fExpanded);
+        }
+    }
+
     /**
      * Collapses all nodes of the viewer's tree, starting with the root.
      */
@@ -720,6 +825,14 @@ public class TimeGraphControl extends TimeGraphBaseControl
         }
     }
 
+    @Override
+    public boolean setFocus() {
+        if ((fTimeProvider != null) && fTimeProvider.getNameSpace() > 0) {
+            fHasNamespaceFocus = true;
+        }
+        return super.setFocus();
+    }
+
     @Override
     public ISelection getSelection() {
         TimeGraphSelection sel = new TimeGraphSelection();
@@ -2299,6 +2412,29 @@ public class TimeGraphControl extends TimeGraphBaseControl
             if (fVerticalZoomAlignEntry != null) {
                 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
             }
+        } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) {
+            if (fHasNamespaceFocus) {
+                ITimeGraphEntry entry = getSelectedTrace();
+                setExpandedState(entry, 0, true);
+            } else {
+                zoomIn();
+            }
+        } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) {
+            if (fHasNamespaceFocus) {
+                ITimeGraphEntry entry = getSelectedTrace();
+                if ((entry != null) && entry.hasChildren()) {
+                    setExpandedState(entry, -1, false);
+                }
+            } else {
+                zoomOut();
+            }
+        } else if ((e.character == '*') && ((e.stateMask & SWT.CTRL) == 0)) {
+            if (fHasNamespaceFocus) {
+                ITimeGraphEntry entry = getSelectedTrace();
+                if ((entry != null) && entry.hasChildren()) {
+                    setExpandedStateLevel(entry);
+                }
+            }
         }
         if (idx >= 0) {
             selectItem(idx, false);
@@ -2500,6 +2636,12 @@ public class TimeGraphControl extends TimeGraphBaseControl
             }
             fMouseOverSplitLine = mouseOverSplitLine;
         }
+
+        if (e.x >= fTimeProvider.getNameSpace()) {
+            fHasNamespaceFocus = false;
+        } else {
+            fHasNamespaceFocus = true;
+        }
         updateCursor(e.x, e.stateMask);
         updateStatusLine(e.x);
     }
This page took 0.028377 seconds and 5 git commands to generate.