tmf: Preserve order of marker categories
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / TimeGraphViewer.java
index 79c3453591c50ac66636ba697784aec2609120b5..f5dc386c5286540c3867294cce8367469e926f1f 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2007, 2015 Intel Corporation, Ericsson, others
+ * Copyright (c) 2007, 2016 Intel Corporation, Ericsson, others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -23,6 +23,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.ActionContributionItem;
 import org.eclipse.jface.action.IAction;
@@ -51,7 +52,6 @@ import org.eclipse.swt.events.MouseWheelListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.RGBA;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.layout.FillLayout;
@@ -81,6 +81,7 @@ import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.ITimeDataProvid
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeDataProviderCyclesConverter;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphColorScheme;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
+import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphMarkerAxis;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphScale;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphTooltipHandler;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
@@ -105,10 +106,10 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
     private static final long DEFAULT_FREQUENCY = 1000000000L;
     private static final int H_SCROLLBAR_MAX = Integer.MAX_VALUE - 1;
 
-    private static ImageDescriptor ADD_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ADD_BOOKMARK);
-    private static ImageDescriptor NEXT_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_NEXT_BOOKMARK);
-    private static ImageDescriptor PREVIOUS_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_PREVIOUS_BOOKMARK);
-    private static ImageDescriptor REMOVE_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_REMOVE_BOOKMARK);
+    private static final ImageDescriptor ADD_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_ADD_BOOKMARK);
+    private static final ImageDescriptor NEXT_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_NEXT_BOOKMARK);
+    private static final ImageDescriptor PREVIOUS_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_PREVIOUS_BOOKMARK);
+    private static final ImageDescriptor REMOVE_BOOKMARK = Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_REMOVE_BOOKMARK);
 
     private long fMinTimeInterval;
     private ITimeGraphEntry fSelectedEntry;
@@ -130,13 +131,14 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
 
     private TimeGraphControl fTimeGraphCtrl;
     private TimeGraphScale fTimeScaleCtrl;
+    private TimeGraphMarkerAxis fMarkerAxisCtrl;
     private Slider fHorizontalScrollBar;
     private Slider fVerticalScrollBar;
-    private TimeGraphColorScheme fColorScheme;
+    private @NonNull TimeGraphColorScheme fColorScheme = new TimeGraphColorScheme();
     private Object fInputElement;
     private ITimeGraphContentProvider fTimeGraphContentProvider;
     private ITimeGraphPresentationProvider fTimeGraphProvider;
-    private ITimeDataProvider fTimeDataProvider = this;
+    private @NonNull ITimeDataProvider fTimeDataProvider = this;
     private TimeGraphTooltipHandler fToolTipHandler;
 
     private List<ITimeGraphSelectionListener> fSelectionListeners = new ArrayList<>();
@@ -184,9 +186,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
     /** The list of markers */
     private final List<IMarkerEvent> fMarkers = new ArrayList<>();
 
-    /** The list of color resources created by this viewer */
-    private final List<Color> fColors = new ArrayList<>();
-
     private ListenerNotifier fListenerNotifier;
 
     private Composite fTimeAlignedComposite;
@@ -456,21 +455,18 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      */
     protected Control createDataViewer(Composite parent, int style) {
         loadOptions();
-        fColorScheme = new TimeGraphColorScheme();
         fDataViewer = new Composite(parent, style) {
             @Override
             public void redraw() {
                 fTimeScaleCtrl.redraw();
                 fTimeGraphCtrl.redraw();
+                fMarkerAxisCtrl.redraw();
                 super.redraw();
             }
         };
         fDataViewer.addDisposeListener(new DisposeListener() {
             @Override
             public void widgetDisposed(DisposeEvent e) {
-                for (Color color : fColors) {
-                    color.dispose();
-                }
                 if (fMarkersMenu != null) {
                     fMarkersMenu.dispose();
                 }
@@ -524,9 +520,9 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         fTimeGraphCtrl.addKeyListener(new KeyAdapter() {
             @Override
             public void keyPressed(KeyEvent e) {
-                if (e.character == '+') {
+                if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) {
                     zoomIn();
-                } else if (e.character == '-') {
+                } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) {
                     zoomOut();
                 } else if (e.keyCode == '.') {
                     boolean extend = (e.stateMask & SWT.SHIFT) != 0;
@@ -547,6 +543,15 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             }
         });
 
+        fMarkerAxisCtrl = createTimeGraphMarkerAxis(fTimeAlignedComposite, fColorScheme, this);
+        fMarkerAxisCtrl.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));
+        fMarkerAxisCtrl.addMouseWheelListener(new MouseWheelListener() {
+            @Override
+            public void mouseScrolled(MouseEvent e) {
+                fTimeGraphCtrl.zoom(e.count > 0);
+            }
+        });
+
         fVerticalScrollBar = new Slider(fDataViewer, SWT.VERTICAL | SWT.NO_FOCUS);
         fVerticalScrollBar.setLayoutData(new GridData(SWT.DEFAULT, SWT.FILL, false, true, 1, 1));
         fVerticalScrollBar.addSelectionListener(new SelectionAdapter() {
@@ -631,6 +636,23 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         return new TimeGraphControl(parent, colors);
     }
 
+    /**
+     * Create a new time graph marker axis.
+     *
+     * @param parent
+     *            The parent composite object
+     * @param colorScheme
+     *            The color scheme to use
+     * @param timeProvider
+     *            The time data provider
+     * @return The new TimeGraphMarkerAxis
+     * @since 2.0
+     */
+    protected TimeGraphMarkerAxis createTimeGraphMarkerAxis(Composite parent,
+            @NonNull TimeGraphColorScheme colorScheme, @NonNull ITimeDataProvider timeProvider) {
+        return new TimeGraphMarkerAxis(parent, colorScheme, timeProvider);
+    }
+
     /**
      * Resize the controls
      */
@@ -747,6 +769,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         }
         fTimeGraphCtrl.refreshData(traces);
         fTimeScaleCtrl.redraw();
+        fMarkerAxisCtrl.redraw();
         updateMarkerActions();
         adjustVerticalScrollBar();
     }
@@ -819,6 +842,11 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         }
         fTimeGraphCtrl.redraw();
         fTimeScaleCtrl.redraw();
+        fMarkerAxisCtrl.redraw();
+        /* force update the controls to keep them aligned */
+        fTimeScaleCtrl.update();
+        fMarkerAxisCtrl.update();
+        fTimeGraphCtrl.update();
     }
 
     @Override
@@ -900,6 +928,11 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         adjustHorizontalScrollBar();
         fTimeGraphCtrl.redraw();
         fTimeScaleCtrl.redraw();
+        fMarkerAxisCtrl.redraw();
+        /* force update the controls to keep them aligned */
+        fTimeScaleCtrl.update();
+        fMarkerAxisCtrl.update();
+        fTimeGraphCtrl.update();
     }
 
     @Override
@@ -922,60 +955,53 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         setSelectedTimeInt(time, ensureVisible, false);
     }
 
+    private void setSelectedTimeInt(long time, boolean ensureVisible, boolean doNotify) {
+        setSelectionRangeInt(time, time, ensureVisible, doNotify);
+    }
+
+    /**
+     * @since 2.0
+     */
     @Override
-    public void setSelectionRangeNotify(long beginTime, long endTime) {
-        long time0 = fTime0;
-        long time1 = fTime1;
-        long selectionBegin = fSelectionBegin;
-        long selectionEnd = fSelectionEnd;
-        fSelectionBegin = Math.max(fTime0Bound, Math.min(fTime1Bound, beginTime));
-        fSelectionEnd = Math.max(fTime0Bound, Math.min(fTime1Bound, endTime));
-        boolean changed = (selectionBegin != fSelectionBegin || selectionEnd != fSelectionEnd);
-        ensureVisible(fSelectionEnd);
-        fTimeGraphCtrl.redraw();
-        fTimeScaleCtrl.redraw();
-        updateMarkerActions();
-        if ((time0 != fTime0) || (time1 != fTime1)) {
-            notifyRangeListeners();
-        }
-        if (changed) {
-            notifyTimeListeners();
-        }
+    public void setSelectionRangeNotify(long beginTime, long endTime, boolean ensureVisible) {
+        setSelectionRangeInt(beginTime, endTime, ensureVisible, true);
     }
 
+    /**
+     * @since 2.0
+     */
     @Override
-    public void setSelectionRange(long beginTime, long endTime) {
+    public void setSelectionRange(long beginTime, long endTime, boolean ensureVisible) {
         /* if there is a pending time selection, ignore this one */
         if (fListenerNotifier != null && fListenerNotifier.hasTimeSelected()) {
             return;
         }
-        fSelectionBegin = Math.max(fTime0Bound, Math.min(fTime1Bound, beginTime));
-        fSelectionEnd = Math.max(fTime0Bound, Math.min(fTime1Bound, endTime));
-        fTimeGraphCtrl.redraw();
-        fTimeScaleCtrl.redraw();
-        updateMarkerActions();
+        setSelectionRangeInt(beginTime, endTime, ensureVisible, false);
     }
 
-    private void setSelectedTimeInt(long time, boolean ensureVisible, boolean doNotify) {
-        long selection = Math.max(fTime0Bound, Math.min(fTime1Bound, time));
+    private void setSelectionRangeInt(long beginTime, long endTime, boolean ensureVisible, boolean doNotify) {
         long time0 = fTime0;
         long time1 = fTime1;
+        long selectionBegin = fSelectionBegin;
+        long selectionEnd = fSelectionEnd;
+        fSelectionBegin = Math.max(fTime0Bound, Math.min(fTime1Bound, beginTime));
+        fSelectionEnd = Math.max(fTime0Bound, Math.min(fTime1Bound, endTime));
+        boolean changed = (selectionBegin != fSelectionBegin || selectionEnd != fSelectionEnd);
+
         if (ensureVisible) {
-            ensureVisible(selection);
+            ensureVisible(selectionBegin != fSelectionBegin ? fSelectionBegin : fSelectionEnd);
         }
+
         fTimeGraphCtrl.redraw();
         fTimeScaleCtrl.redraw();
-
-        boolean notifySelectedTime = (selection != fSelectionBegin || selection != fSelectionEnd);
-        fSelectionBegin = selection;
-        fSelectionEnd = selection;
+        fMarkerAxisCtrl.redraw();
         updateMarkerActions();
 
         if ((time0 != fTime0) || (time1 != fTime1)) {
             notifyRangeListeners();
         }
 
-        if (doNotify && notifySelectedTime) {
+        if (doNotify && changed) {
             notifyTimeListeners();
         }
     }
@@ -1250,9 +1276,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      * @since 2.0
      */
     public void setBookmarks(List<IMarkerEvent> bookmarks) {
-        for (IMarkerEvent bookmark : fBookmarks) {
-            checkDisposeColor(bookmark.getColor());
-        }
         fBookmarks.clear();
         if (bookmarks != null) {
             fBookmarks.addAll(bookmarks);
@@ -1280,11 +1303,11 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      */
     public void setMarkerCategories(List<String> categories) {
         fMarkerCategories.clear();
-        fMarkerCategories.add(IMarkerEvent.BOOKMARKS);
         if (categories != null) {
             fMarkerCategories.addAll(categories);
         }
-        Collections.sort(fMarkerCategories);
+        fMarkerCategories.add(IMarkerEvent.BOOKMARKS);
+        fMarkerAxisCtrl.setMarkerCategories(fMarkerCategories);
     }
 
     /**
@@ -1313,23 +1336,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         return Collections.unmodifiableList(fMarkers);
     }
 
-    /**
-     * Dispose the color resource if and only if it was created by this viewer.
-     *
-     * @param color
-     *            the color
-     */
-    private void checkDisposeColor(Color color) {
-        for (int i = 0; i < fColors.size(); i++) {
-            /* check for identity, not equality */
-            if (fColors.get(i) == color) {
-                color.dispose();
-                fColors.remove(i);
-                break;
-            }
-        }
-    }
-
     /**
      * Callback to set a selected event in the view
      *
@@ -2087,9 +2093,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
                         if (dialog.open() == Window.OK) {
                             final String label = dialog.getValue();
                             final RGBA rgba = dialog.getColorValue();
-                            Color color = new Color(Display.getDefault(), rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha);
-                            fColors.add(color);
-                            IMarkerEvent bookmark = new MarkerEvent(null, time, duration, IMarkerEvent.BOOKMARKS, color, label, true);
+                            IMarkerEvent bookmark = new MarkerEvent(null, time, duration, IMarkerEvent.BOOKMARKS, rgba, label, true);
                             fBookmarks.add(bookmark);
                             updateMarkerList();
                             updateMarkerActions();
@@ -2097,7 +2101,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
                             fireBookmarkAdded(bookmark);
                         }
                     } else {
-                        checkDisposeColor(selectedBookmark.getColor());
                         fBookmarks.remove(selectedBookmark);
                         updateMarkerList();
                         updateMarkerActions();
@@ -2255,7 +2258,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             if ((marker.getTime() > time ||
                     (marker.getTime() == time && marker.getDuration() > duration))
                     && !fSkippedMarkerCategories.contains(marker.getCategory())) {
-                setSelectionRangeNotify(marker.getTime(), marker.getTime() + marker.getDuration());
+                setSelectionRangeNotify(marker.getTime(), marker.getTime() + marker.getDuration(), true);
                 fTimeGraphCtrl.updateStatusLine();
                 return;
             }
@@ -2278,7 +2281,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             if ((marker.getTime() < time ||
                     (marker.getTime() == time && marker.getDuration() < duration))
                     && !fSkippedMarkerCategories.contains(marker.getCategory())) {
-                setSelectionRangeNotify(marker.getTime(), marker.getTime() + marker.getDuration());
+                setSelectionRangeNotify(marker.getTime(), marker.getTime() + marker.getDuration(), true);
                 fTimeGraphCtrl.updateStatusLine();
                 return;
             }
@@ -2302,7 +2305,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             }
         }
         if (nextMarker != null) {
-            setSelectionRangeNotify(fSelectionBegin, nextMarker.getTime() + nextMarker.getDuration());
+            setSelectionRangeNotify(fSelectionBegin, nextMarker.getTime() + nextMarker.getDuration(), true);
             fTimeGraphCtrl.updateStatusLine();
         }
     }
@@ -2319,7 +2322,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             IMarkerEvent marker = markers.get(i);
             if (marker.getTime() < fSelectionEnd
                     && !fSkippedMarkerCategories.contains(marker.getCategory())) {
-                setSelectionRangeNotify(fSelectionBegin, marker.getTime());
+                setSelectionRangeNotify(fSelectionBegin, marker.getTime(), true);
                 fTimeGraphCtrl.updateStatusLine();
                 return;
             }
@@ -2374,7 +2377,8 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             markers.addAll(fBookmarks);
         }
         Collections.sort(markers, new MarkerComparator());
-        getTimeGraphControl().setMarkers(markers);
+        fTimeGraphCtrl.setMarkers(markers);
+        fMarkerAxisCtrl.setMarkers(markers);
     }
 
     private void adjustHorizontalScrollBar() {
@@ -2451,7 +2455,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      * @param filter
      *            The filter object to be attached to the view
      */
-    public void addFilter(ViewerFilter filter) {
+    public void addFilter(@NonNull ViewerFilter filter) {
         fTimeGraphCtrl.addFilter(filter);
         refresh();
     }
@@ -2460,7 +2464,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      * @param filter
      *            The filter object to be attached to the view
      */
-    public void removeFilter(ViewerFilter filter) {
+    public void removeFilter(@NonNull ViewerFilter filter) {
         fTimeGraphCtrl.removeFilter(filter);
         refresh();
     }
@@ -2471,7 +2475,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      * @return an array of viewer filters
      * @since 2.0
      */
-    public ViewerFilter[] getFilters() {
+    public @NonNull ViewerFilter[] getFilters() {
         return fTimeGraphCtrl.getFilters();
     }
 
@@ -2483,7 +2487,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
      *            an array of viewer filters, or null
      * @since 2.0
      */
-    public void setFilters(ViewerFilter[] filters) {
+    public void setFilters(@NonNull ViewerFilter[] filters) {
         fTimeGraphCtrl.setFilters(filters);
         refresh();
     }
This page took 0.031178 seconds and 5 git commands to generate.