tmf: Bug 488806: Mouse scroll with count of zero not handled correctly
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / TimeGraphViewer.java
index 5bdb90dd1ca504fcd3f8a9b5175e1a720a4be61d..76de9c8963aee9f21c358b2260052b31c84bf607 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
@@ -52,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;
@@ -78,10 +77,12 @@ import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.MarkerEvent;
+import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.IMarkerAxisListener;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.ITimeDataProvider;
 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;
@@ -93,7 +94,7 @@ import org.eclipse.ui.PlatformUI;
  *
  * @author Patrick Tasse, and others
  */
-public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
+public class TimeGraphViewer implements ITimeDataProvider, IMarkerAxisListener, SelectionListener {
 
     /** Constant indicating that all levels of the time graph should be expanded */
     public static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS;
@@ -131,13 +132,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<>();
@@ -185,9 +187,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;
@@ -457,21 +456,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();
                 }
@@ -506,6 +502,9 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         fTimeScaleCtrl.addMouseWheelListener(new MouseWheelListener() {
             @Override
             public void mouseScrolled(MouseEvent e) {
+                if (e.count == 0) {
+                    return;
+                }
                 fTimeGraphCtrl.zoom(e.count > 0);
             }
         });
@@ -519,15 +518,18 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         fTimeGraphCtrl.addMouseWheelListener(new MouseWheelListener() {
             @Override
             public void mouseScrolled(MouseEvent e) {
+                if (e.count == 0) {
+                    return;
+                }
                 adjustVerticalScrollBar();
             }
         });
         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;
@@ -548,6 +550,19 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
             }
         });
 
+        fMarkerAxisCtrl = createTimeGraphMarkerAxis(fTimeAlignedComposite, fColorScheme, this);
+        fMarkerAxisCtrl.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));
+        fMarkerAxisCtrl.addMarkerAxisListener(this);
+        fMarkerAxisCtrl.addMouseWheelListener(new MouseWheelListener() {
+            @Override
+            public void mouseScrolled(MouseEvent e) {
+                if (e.count == 0) {
+                    return;
+                }
+                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() {
@@ -562,13 +577,16 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         fHorizontalScrollBar.addListener(SWT.MouseWheel, new Listener() {
             @Override
             public void handleEvent(Event event) {
+                // don't handle the immediately following SWT.Selection event
+                event.doit = false;
+                if (event.count == 0) {
+                    return;
+                }
                 if ((event.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
                     getTimeGraphControl().zoom(event.count > 0);
                 } else {
                     getTimeGraphControl().horizontalScroll(event.count > 0);
                 }
-                // don't handle the immediately following SWT.Selection event
-                event.doit = false;
             }
         });
         fHorizontalScrollBar.addListener(SWT.Selection, new Listener() {
@@ -632,6 +650,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
      */
@@ -748,6 +783,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         }
         fTimeGraphCtrl.refreshData(traces);
         fTimeScaleCtrl.redraw();
+        fMarkerAxisCtrl.redraw();
         updateMarkerActions();
         adjustVerticalScrollBar();
     }
@@ -820,6 +856,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
@@ -901,6 +942,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
@@ -962,6 +1008,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
 
         fTimeGraphCtrl.redraw();
         fTimeScaleCtrl.redraw();
+        fMarkerAxisCtrl.redraw();
         updateMarkerActions();
 
         if ((time0 != fTime0) || (time1 != fTime1)) {
@@ -1243,9 +1290,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);
@@ -1273,11 +1317,29 @@ 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);
+    }
+
+    /**
+     * @since 2.0
+     */
+    @Override
+    public void setMarkerCategoryVisible(String category, boolean visible) {
+        boolean changed = false;
+        if (visible) {
+            changed = fHiddenMarkerCategories.remove(category);
+        } else {
+            changed = fHiddenMarkerCategories.add(category);
+        }
+        if (changed) {
+            updateMarkerList();
+            updateMarkerActions();
+            getControl().redraw();
+        }
     }
 
     /**
@@ -1306,23 +1368,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
      *
@@ -2080,9 +2125,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();
@@ -2090,7 +2133,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
                             fireBookmarkAdded(bookmark);
                         }
                     } else {
-                        checkDisposeColor(selectedBookmark.getColor());
                         fBookmarks.remove(selectedBookmark);
                         updateMarkerList();
                         updateMarkerActions();
@@ -2214,14 +2256,7 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
                         final Action action = new Action(category, IAction.AS_CHECK_BOX) {
                             @Override
                             public void runWithEvent(Event event) {
-                                if (isChecked()) {
-                                    fHiddenMarkerCategories.remove(getText());
-                                } else {
-                                    fHiddenMarkerCategories.add(getText());
-                                }
-                                updateMarkerList();
-                                updateMarkerActions();
-                                getControl().redraw();
+                                setMarkerCategoryVisible(getText(), isChecked());
                             }
                         };
                         action.setChecked(!fHiddenMarkerCategories.contains(category));
@@ -2367,7 +2402,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() {
This page took 0.028263 seconds and 5 git commands to generate.