tmf: Bug 474710: Time graph combo expanded state is not synchronized
authorPatrick Tasse <patrick.tasse@gmail.com>
Tue, 11 Aug 2015 14:34:22 +0000 (10:34 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Tue, 18 Aug 2015 17:58:16 +0000 (13:58 -0400)
When a time graph combo is refreshed, newly discovered items are
expanded to the auto-expand level in the time graph viewer, and the tree
viewer item expanded states are synchronized with those of the time
graph viewer. The expanded state of existing items is no longer affected
by the call to refresh().

Change-Id: I709681ef488e7d22991d5b7384c667532afd3410
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/53558
Reviewed-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/TimeGraphCombo.java
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 c0cc704471d226fff01f52fa184ce891f7c57f7b..95156b56e2d206db343d6de53f580c1b3cd7c28e 100644 (file)
@@ -952,7 +952,6 @@ public class TimeGraphCombo extends Composite {
         try {
             tree.setRedraw(false);
             fTreeViewer.refresh();
-            fTreeViewer.expandToLevel(fTreeViewer.getAutoExpandLevel());
         } finally {
             tree.setRedraw(true);
         }
@@ -1003,14 +1002,15 @@ public class TimeGraphCombo extends Composite {
     }
 
     /**
-     * Sets the auto-expand level to be used when the input of the viewer is set
-     * using {@link #setInput(Object)}. The value 0 means that there is no
-     * auto-expand; 1 means that top-level elements are expanded, but not their
-     * children; 2 means that top-level elements are expanded, and their
-     * children, but not grand-children; and so on.
+     * Sets the auto-expand level to be used for new entries discovered when
+     * calling {@link #setInput(Object)} or {@link #refresh()}. The value 0
+     * means that there is no auto-expand; 1 means that top-level entries are
+     * expanded, but not their children; 2 means that top-level entries are
+     * expanded, and their children, but not grand-children; and so on.
      * <p>
      * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
      * </p>
+     *
      * @param level
      *            non-negative level, or <code>ALL_LEVELS</code> to expand all
      *            levels of the tree
@@ -1073,26 +1073,27 @@ public class TimeGraphCombo extends Composite {
 
     private List<TreeItem> getVisibleExpandedItems(Tree tree, boolean refresh) {
         if (fVisibleExpandedItems == null || refresh) {
-            ArrayList<TreeItem> items = new ArrayList<>();
-            for (TreeItem item : tree.getItems()) {
-                if (item.getData() == FILLER) {
-                    break;
-                }
-                items.add(item);
-                if (item.getExpanded()) {
-                    addVisibleExpandedItems(items, item);
-                }
-            }
-            fVisibleExpandedItems = items;
+            List<TreeItem> visibleExpandedItems = new ArrayList<>();
+            addVisibleExpandedItems(visibleExpandedItems, tree.getItems());
+            fVisibleExpandedItems = visibleExpandedItems;
         }
         return fVisibleExpandedItems;
     }
 
-    private void addVisibleExpandedItems(List<TreeItem> items, TreeItem treeItem) {
-        for (TreeItem item : treeItem.getItems()) {
-            items.add(item);
-            if (item.getExpanded()) {
-                addVisibleExpandedItems(items, item);
+    private void addVisibleExpandedItems(List<TreeItem> visibleExpandedItems, TreeItem[] items) {
+        for (TreeItem item : items) {
+            Object data = item.getData();
+            if (data == FILLER) {
+                break;
+            }
+            visibleExpandedItems.add(item);
+            boolean expandedState = fTimeGraphViewer.getExpandedState((ITimeGraphEntry) data);
+            if (item.getExpanded() != expandedState) {
+                /* synchronize the expanded state of both viewers */
+                fTreeViewer.setExpandedState(data, expandedState);
+            }
+            if (expandedState) {
+                addVisibleExpandedItems(visibleExpandedItems, item.getItems());
             }
         }
     }
index bd6fed84756ac9af394e212ffe437d246cfc67ea..51057f639ec28c6c3a13ed2448a709be5343a62e 100644 (file)
@@ -1427,14 +1427,15 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
     }
 
     /**
-     * Sets the auto-expand level to be used when the input of the viewer is set
-     * using {@link #setInput(Object)}. The value 0 means that there is no
-     * auto-expand; 1 means that top-level elements are expanded, but not their
-     * children; 2 means that top-level elements are expanded, and their
-     * children, but not grand-children; and so on.
+     * Sets the auto-expand level to be used for new entries discovered when
+     * calling {@link #setInput(Object)} or {@link #refresh()}. The value 0
+     * means that there is no auto-expand; 1 means that top-level entries are
+     * expanded, but not their children; 2 means that top-level entries are
+     * expanded, and their children, but not grand-children; and so on.
      * <p>
      * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
      * </p>
+     *
      * @param level
      *            non-negative level, or <code>ALL_LEVELS</code> to expand all
      *            levels of the tree
@@ -1454,6 +1455,18 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         return fTimeGraphCtrl.getAutoExpandLevel();
     }
 
+    /**
+     * Get the expanded state of an entry.
+     *
+     * @param entry
+     *            The entry
+     * @return true if the entry is expanded, false if collapsed
+     * @since 2.0
+     */
+    public boolean getExpandedState(ITimeGraphEntry entry) {
+        return fTimeGraphCtrl.getExpandedState(entry);
+    }
+
     /**
      * Set the expanded state of an entry
      *
@@ -1476,7 +1489,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
     }
 
     /**
-     * Expands all nodes of the viewer's tree, starting with the root.
+     * Expands all entries of the viewer's tree, starting with the root.
      */
     public void expandAll() {
         fTimeGraphCtrl.expandAll();
@@ -1484,18 +1497,20 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
     }
 
     /**
-     * Get the number of sub-elements when expanded
+     * Get the number of expanded (visible) time graph entries. This includes
+     * leafs and does not include filtered-out entries.
      *
-     * @return The element count
+     * @return The number of expanded (visible) time graph entries
      */
     public int getExpandedElementCount() {
         return fTimeGraphCtrl.getExpandedElementCount();
     }
 
     /**
-     * Get the sub-elements
+     * Get the expanded (visible) time graph entries. This includes leafs and
+     * does not include filtered-out entries.
      *
-     * @return The array of entries that are below this one
+     * @return The array of expanded (visible) time graph entries
      */
     public ITimeGraphEntry[] getExpandedElements() {
         return fTimeGraphCtrl.getExpandedElements();
index 925540e37381351ab21e1640a4d832804c4228ab..32c8142539e3f583f12634585945a30979b69509 100644 (file)
@@ -472,15 +472,16 @@ public class TimeGraphControl extends TimeGraphBaseControl
     }
 
     /**
-     * Sets the auto-expand level to be used when the entries are refreshed
-     * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
-     * The value 0 means that there is no auto-expand; 1 means that top-level
+     * Sets the auto-expand level to be used for new entries discovered when
+     * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
+     * The value 0 means that there is no auto-expand; 1 means that top-level
      * entries are expanded, but not their children; 2 means that top-level
      * entries are expanded, and their children, but not grand-children; and so
      * on.
      * <p>
      * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
      * </p>
+     *
      * @param level
      *            non-negative level, or <code>ALL_LEVELS</code> to expand all
      *            levels of the tree
@@ -500,6 +501,19 @@ public class TimeGraphControl extends TimeGraphBaseControl
         return fAutoExpandLevel;
     }
 
+    /**
+     * Get the expanded state of a given entry.
+     *
+     * @param entry
+     *            The entry
+     * @return true if the entry is expanded, false if collapsed
+     * @since 2.0
+     */
+    public boolean getExpandedState(ITimeGraphEntry entry) {
+        Item item = fItemData.fItemMap.get(entry);
+        return (item != null ? item.fExpanded : false);
+    }
+
     /**
      * Set the expanded state of a given entry
      *
@@ -1249,18 +1263,18 @@ public class TimeGraphControl extends TimeGraphBaseControl
     }
 
     /**
-     * Get the number of expanded items
+     * Get the number of expanded (visible) items
      *
-     * @return The count of expanded items
+     * @return The count of expanded (visible) items
      */
     public int getExpandedElementCount() {
         return fItemData.fExpandedItems.length;
     }
 
     /**
-     * Get an array of all expanded elements
+     * Get an array of all expanded (visible) elements
      *
-     * @return The expanded elements
+     * @return The expanded (visible) elements
      */
     public ITimeGraphEntry[] getExpandedElements() {
         ArrayList<ITimeGraphEntry> elements = new ArrayList<>();
@@ -2523,7 +2537,7 @@ public class TimeGraphControl extends TimeGraphBaseControl
     }
 
     private class ItemData {
-        private final Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>();
+        private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>();
         private Item[] fExpandedItems = new Item[0];
         private Item[] fItems = new Item[0];
         private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0];
@@ -2547,13 +2561,14 @@ public class TimeGraphControl extends TimeGraphBaseControl
         }
 
         public void refreshData() {
-            fItemMap.clear();
             fFilteredOut.clear();
             ITimeGraphEntry selection = getSelectedTrace();
+            Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>();
             for (int i = 0; i < fRootEntries.length; i++) {
                 ITimeGraphEntry entry = fRootEntries[i];
-                refreshData(fItemMap, null, 0, entry);
+                refreshData(itemMap, null, 0, entry);
             }
+            fItemMap = itemMap;
             fItems = fItemMap.values().toArray(new Item[0]);
             updateExpandedItems();
             if (selection != null) {
@@ -2578,7 +2593,14 @@ public class TimeGraphControl extends TimeGraphBaseControl
             }
             itemMap.put(entry, item);
             if (entry.hasChildren()) {
-                item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel;
+                Item oldItem = fItemMap.get(entry);
+                if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) {
+                    /* existing items keep their old expanded state */
+                    item.fExpanded = oldItem.fExpanded;
+                } else {
+                    /* new items set the expanded state according to auto-expand level */
+                    item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel;
+                }
                 item.fHasChildren = true;
                 for (ITimeGraphEntry child : entry.getChildren()) {
                     refreshData(itemMap, item, level + 1, child);
This page took 0.030411 seconds and 5 git commands to generate.