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 7a84a05c6c4f047615d424ff5ed8425ad6b45151..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;
@@ -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
@@ -961,6 +994,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
 
         fTimeGraphCtrl.redraw();
         fTimeScaleCtrl.redraw();
+        fMarkerAxisCtrl.redraw();
         updateMarkerActions();
 
         if ((time0 != fTime0) || (time1 != fTime1)) {
@@ -1242,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);
@@ -1272,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);
     }
 
     /**
@@ -1305,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
      *
@@ -2079,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();
@@ -2089,7 +2101,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
                             fireBookmarkAdded(bookmark);
                         }
                     } else {
-                        checkDisposeColor(selectedBookmark.getColor());
                         fBookmarks.remove(selectedBookmark);
                         updateMarkerList();
                         updateMarkerActions();
@@ -2366,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() {
@@ -2443,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();
     }
@@ -2452,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();
     }
@@ -2463,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();
     }
 
@@ -2475,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.030633 seconds and 5 git commands to generate.