tmf: Bug 496504: Fix duplicate child entries in Control Flow view
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / model / TimeGraphEntry.java
index f5289745149daece368768d6b2cc786fdafc0783..5d06e922121d4da7b2e07eda1199da8dea7fdb77 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
+ * Copyright (c) 2012, 2016 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -19,6 +19,7 @@ import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.regex.Pattern;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.swt.SWT;
@@ -29,10 +30,10 @@ import org.eclipse.swt.SWT;
 public class TimeGraphEntry implements ITimeGraphEntry {
 
     /** Entry's parent */
-    private ITimeGraphEntry fParent = null;
+    private TimeGraphEntry fParent = null;
 
     /** List of child entries */
-    private final List<@NonNull ITimeGraphEntry> fChildren = new CopyOnWriteArrayList<>();
+    private final List<@NonNull TimeGraphEntry> fChildren = new CopyOnWriteArrayList<>();
 
     /** Name of this entry (text to show) */
     private String fName;
@@ -62,32 +63,21 @@ public class TimeGraphEntry implements ITimeGraphEntry {
     // Getters and setters
     // ---------------------------------------------
 
-    @Override
-    public ITimeGraphEntry getParent() {
-        return fParent;
-    }
-
     /**
-     * Sets the entry's parent
-     *
-     * @param entry The new parent entry
-     */
-    /*
-     * TODO: This method can be removed in the next major API version.
+     * @since 2.0
      */
-    protected void setParent(TimeGraphEntry entry) {
-        fParent = entry;
+    @Override
+    public TimeGraphEntry getParent() {
+        return fParent;
     }
 
     /**
      * Sets the entry's parent
      *
      * @param entry The new parent entry
+     * @since 2.0
      */
-    /*
-     * TODO: This method should be added to the interface in the next major API version.
-     */
-    protected void setParent(ITimeGraphEntry entry) {
+    public void setParent(TimeGraphEntry entry) {
         fParent = entry;
     }
 
@@ -97,10 +87,19 @@ public class TimeGraphEntry implements ITimeGraphEntry {
     }
 
     @Override
-    public synchronized List<@NonNull ? extends ITimeGraphEntry> getChildren() {
+    public synchronized List<@NonNull TimeGraphEntry> getChildren() {
         return fChildren;
     }
 
+    /**
+     * Clear the children of the entry
+     *
+     * @since 2.0
+     */
+    public synchronized void clearChildren() {
+        fChildren.clear();
+    }
+
     @Override
     public String getName() {
         return fName;
@@ -222,6 +221,8 @@ public class TimeGraphEntry implements ITimeGraphEntry {
      * starts at the same time as the event to add, it is replaced by the new
      * event. If the new event starts before the zoomed event list's last event,
      * the new event is ignored and is assumed to be already part of the list.
+     * If the new event starts before the zoomed event list's first event, the
+     * list is assumed to be incomplete and is cleared, and the event is added.
      *
      * @param event
      *            The time event to add
@@ -236,6 +237,9 @@ public class TimeGraphEntry implements ITimeGraphEntry {
             fZoomedEventList.add(event);
         } else if (start == lastStart) {
             fZoomedEventList.set(lastIndex, event);
+        } else if (start < fZoomedEventList.get(0).getTime()) {
+            fZoomedEventList.clear();
+            fZoomedEventList.add(event);
         }
         if (event instanceof NullTimeEvent) {
             /* A NullTimeEvent should not affect the entry bounds */
@@ -249,19 +253,6 @@ public class TimeGraphEntry implements ITimeGraphEntry {
         }
     }
 
-    /**
-     * Add a child entry to this one
-     *
-     * @param child
-     *            The child entry
-     */
-    /*
-     * TODO: This method can be removed in the next major API version.
-     */
-    public synchronized void addChild(@NonNull TimeGraphEntry child) {
-        addChild((ITimeGraphEntry) child);
-    }
-
     /**
      * Add a child entry to this one. If a comparator was previously set with
      * {@link #sortChildren(Comparator)}, the entry will be inserted in its
@@ -270,15 +261,9 @@ public class TimeGraphEntry implements ITimeGraphEntry {
      * @param child
      *            The child entry
      */
-    public synchronized void addChild(@NonNull ITimeGraphEntry child) {
-        /*
-         * TODO: Use setParent() once it is added to the interface.
-         */
-        if (child instanceof TimeGraphEntry) {
-            ((TimeGraphEntry) child).fParent = this;
-        }
+    public synchronized void addChild(@NonNull TimeGraphEntry child) {
         if (fComparator == null) {
-            fChildren.add(child);
+            addChild(fChildren.size(), child);
         } else {
             int i;
             for (i = 0; i < fChildren.size(); i++) {
@@ -287,7 +272,7 @@ public class TimeGraphEntry implements ITimeGraphEntry {
                     break;
                 }
             }
-            fChildren.add(i, child);
+            addChild(i, child);
         }
     }
 
@@ -298,20 +283,36 @@ public class TimeGraphEntry implements ITimeGraphEntry {
      *            Index at which the specified entry is to be inserted
      * @param child
      *            The child entry
+     * @since 2.0
      */
-    public synchronized void addChild(int index, @NonNull ITimeGraphEntry child) {
-        /*
-         * TODO: Use setParent() once it is added to the interface.
-         */
-        if (child instanceof TimeGraphEntry) {
-            ((TimeGraphEntry) child).fParent = this;
+    public synchronized void addChild(int index, @NonNull TimeGraphEntry child) {
+        if (child.getParent() == this) {
+            return;
+        }
+        if (child.getParent() != null) {
+            child.getParent().removeChild(child);
         }
+        child.setParent(this);
         fChildren.add(index, child);
     }
 
+    /**
+     * Remove a child entry from this one.
+     *
+     * @param child
+     *            The child entry
+     * @since 2.0
+     */
+    public synchronized void removeChild(@NonNull TimeGraphEntry child) {
+        if (child.getParent() == this) {
+            child.setParent(null);
+        }
+        fChildren.remove(child);
+    }
+
     /**
      * Sort the children of this entry using the provided comparator. Subsequent
-     * calls to {@link #addChild(ITimeGraphEntry)} will use this comparator to
+     * calls to {@link #addChild(TimeGraphEntry)} will use this comparator to
      * maintain the sort order.
      *
      * @param comparator
@@ -322,7 +323,7 @@ public class TimeGraphEntry implements ITimeGraphEntry {
         if (comparator == null) {
             return;
         }
-        @NonNull ITimeGraphEntry[] array = fChildren.toArray(new @NonNull ITimeGraphEntry[0]);
+        @NonNull TimeGraphEntry[] array = fChildren.toArray(new @NonNull TimeGraphEntry[0]);
         Arrays.sort(array, comparator);
         fChildren.clear();
         fChildren.addAll(Arrays.asList(array));
@@ -333,4 +334,13 @@ public class TimeGraphEntry implements ITimeGraphEntry {
         return getClass().getSimpleName() + '(' + fName + ')';
     }
 
+    /**
+     * @since 2.0
+     */
+    @Override
+    public boolean matches(@NonNull Pattern pattern) {
+        // Default implementation
+        return pattern.matcher(fName).find();
+    }
+
 }
This page took 0.028351 seconds and 5 git commands to generate.