tmf: Replace Color resource with RGBA descriptor in marker events
authorPatrick Tasse <patrick.tasse@gmail.com>
Fri, 5 Feb 2016 21:54:31 +0000 (16:54 -0500)
committerPatrick Tasse <patrick.tasse@gmail.com>
Tue, 9 Feb 2016 18:53:38 +0000 (13:53 -0500)
This avoids the marker event sources from having to manage, create and
dispose color resources.

Also adds a RGBA to Color map in the time graph color scheme object
which is shared between the time graph control and scale.

Change-Id: I168c949e2aa63c0f5e958385490dc2543ecde7a5
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/66046
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/internal/tmf/ui/markers/LostEventsMarkerEventSource.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/timegraph/AbstractTimeGraphView.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/model/IMarkerEvent.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/model/MarkerEvent.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/widgets/TimeGraphColorScheme.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/widgets/TimeGraphControl.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/widgets/timegraph/widgets/TimeGraphScale.java

index ae9488295c05bfd1e49e8b633342a5a2b1598703..7e30793bc8eef5add4b7548dcd4ad39212c98fe0 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015 Ericsson
+ * Copyright (c) 2015, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -21,9 +21,7 @@ import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.RGBA;
-import org.eclipse.swt.widgets.Display;
 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
@@ -46,7 +44,7 @@ public class LostEventsMarkerEventSource implements IMarkerEventSource {
 
     private static final String LOST_EVENTS = Messages.MarkerEvent_LostEvents;
 
-    private static final Color COLOR = new Color(Display.getDefault(), new RGBA(255, 0, 0, 50));
+    private static final RGBA COLOR = new RGBA(255, 0, 0, 50);
 
     private final @NonNull ITmfTrace fTrace;
     private long[] fLastRequest;
index 0b9f3717b0243daf114654d104cbe74e551096a4..9d50149b569241211f4391cccc0e25e5bd976c62 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
@@ -62,8 +62,8 @@ import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGBA;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Tree;
@@ -222,9 +222,6 @@ public abstract class AbstractTimeGraphView extends TmfView implements ITmfTimeA
 
     private int fAutoExpandLevel = ALL_LEVELS;
 
-    /** The list of color resources created by this view */
-    private final List<Color> fColors = new ArrayList<>();
-
     /** The default column index for sorting */
     private int fInitialSortColumn = 0;
 
@@ -1209,7 +1206,7 @@ public abstract class AbstractTimeGraphView extends TmfView implements ITmfTimeA
                                         NLS.bind(org.eclipse.tracecompass.internal.tmf.ui.Messages.TmfMarker_LocationTime,
                                                 new TmfNanoTimestamp(bookmark.getTime())));
                             }
-                            marker.setAttribute(ITmfMarker.MARKER_COLOR, bookmark.getColor().getRGBA().toString());
+                            marker.setAttribute(ITmfMarker.MARKER_COLOR, bookmark.getColor().toString());
                         }
                     }, null);
                 } catch (CoreException e) {
@@ -1226,7 +1223,7 @@ public abstract class AbstractTimeGraphView extends TmfView implements ITmfTimeA
                         if (bookmark.getLabel().equals(marker.getAttribute(IMarker.MESSAGE)) &&
                                 Long.toString(bookmark.getTime()).equals(marker.getAttribute(ITmfMarker.MARKER_TIME, (String) null)) &&
                                 Long.toString(bookmark.getDuration()).equals(marker.getAttribute(ITmfMarker.MARKER_DURATION, Long.toString(0))) &&
-                                bookmark.getColor().getRGBA().toString().equals(marker.getAttribute(ITmfMarker.MARKER_COLOR))) {
+                                bookmark.getColor().toString().equals(marker.getAttribute(ITmfMarker.MARKER_COLOR))) {
                             marker.delete();
                             break;
                         }
@@ -1282,12 +1279,8 @@ public abstract class AbstractTimeGraphView extends TmfView implements ITmfTimeA
         }
     }
 
-    private List<IMarkerEvent> refreshBookmarks(final IFile editorFile) {
+    private static List<IMarkerEvent> refreshBookmarks(final IFile editorFile) {
         List<IMarkerEvent> bookmarks = new ArrayList<>();
-        for (Color color : fColors) {
-            color.dispose();
-        }
-        fColors.clear();
         if (editorFile == null || !editorFile.exists()) {
             return bookmarks;
         }
@@ -1306,8 +1299,7 @@ public abstract class AbstractTimeGraphView extends TmfView implements ITmfTimeA
                             int green = Integer.valueOf(matcher.group(2));
                             int blue = Integer.valueOf(matcher.group(3));
                             int alpha = Integer.valueOf(matcher.group(4));
-                            Color color = new Color(Display.getDefault(), red, green, blue, alpha);
-                            fColors.add(color);
+                            RGBA color = new RGBA(red, green, blue, alpha);
                             bookmarks.add(new MarkerEvent(null, Long.valueOf(time), Long.valueOf(duration), IMarkerEvent.BOOKMARKS, color, label, true));
                         } catch (NumberFormatException e) {
                             Activator.getDefault().logError(e.getMessage());
index 3fd9d3c36d7b95fa831fd3feea99ebfa316fc04f..6cb17d16571fc8dfc31a656e491a2be9f927e8d3 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;
@@ -185,9 +184,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;
@@ -469,9 +465,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
         fDataViewer.addDisposeListener(new DisposeListener() {
             @Override
             public void widgetDisposed(DisposeEvent e) {
-                for (Color color : fColors) {
-                    color.dispose();
-                }
                 if (fMarkersMenu != null) {
                     fMarkersMenu.dispose();
                 }
@@ -1243,9 +1236,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);
@@ -1306,23 +1296,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 +2053,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 +2061,6 @@ public class TimeGraphViewer implements ITimeDataProvider, SelectionListener {
                             fireBookmarkAdded(bookmark);
                         }
                     } else {
-                        checkDisposeColor(selectedBookmark.getColor());
                         fBookmarks.remove(selectedBookmark);
                         updateMarkerList();
                         updateMarkerActions();
index 087a8f0ef5af3c01f6b6d6d8f79a4e74b4ae3920..51120ef975771ee7436a1bc2e5c58e7d4d523504 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015 Ericsson
+ * Copyright (c) 2015, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -15,7 +15,7 @@ package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model;
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.RGBA;
 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
 
 /**
@@ -48,7 +48,7 @@ public interface IMarkerEvent extends ITimeEvent {
      *
      * @return The color
      */
-    Color getColor();
+    RGBA getColor();
 
     /**
      * Returns true if the marker is drawn in foreground, and false otherwise.
index 0490c48d9dfe1e1290af74bb4ee4b210db5ea37e..3b9e00b5468f3d73376edc864d47666b8842d33a 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015 Ericsson
+ * Copyright (c) 2015, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -12,7 +12,7 @@
 
 package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model;
 
-import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.RGBA;
 
 /**
  * TimeEvent implementation for marker events
@@ -22,7 +22,7 @@ import org.eclipse.swt.graphics.Color;
 public class MarkerEvent extends TimeEvent implements IMarkerEvent {
 
     private final String fCategory;
-    private final Color fColor;
+    private final RGBA fColor;
     private final String fLabel;
     private final boolean fForeground;
 
@@ -44,7 +44,7 @@ public class MarkerEvent extends TimeEvent implements IMarkerEvent {
      * @param foreground
      *            true if the marker is drawn in foreground, and false otherwise
      */
-    public MarkerEvent(ITimeGraphEntry entry, long time, long duration, String category, Color color, String label, boolean foreground) {
+    public MarkerEvent(ITimeGraphEntry entry, long time, long duration, String category, RGBA color, String label, boolean foreground) {
         super(entry, time, duration);
         fCategory = category;
         fColor = color;
@@ -72,7 +72,7 @@ public class MarkerEvent extends TimeEvent implements IMarkerEvent {
      * @param value
      *            The value of the marker
      */
-    public MarkerEvent(ITimeGraphEntry entry, long time, long duration, String category, Color color, String label, boolean foreground, int value) {
+    public MarkerEvent(ITimeGraphEntry entry, long time, long duration, String category, RGBA color, String label, boolean foreground, int value) {
         super(entry, time, duration, value);
         fCategory = category;
         fColor = color;
@@ -86,7 +86,7 @@ public class MarkerEvent extends TimeEvent implements IMarkerEvent {
     }
 
     @Override
-    public Color getColor() {
+    public RGBA getColor() {
         return fColor;
     }
 
index 2c44c26157f5074c30309dea05ce44aecf5db0de..f4e0ec7f5ebd397bef4c674be0ae1950c2643cf4 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2008, 2014 Intel Corporation, Ericsson
+ * Copyright (c) 2008, 2016 Intel Corporation, Ericsson
  * 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
 
 package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.RGBA;
+import org.eclipse.swt.widgets.Display;
 
 /**
  * Color theme used by the timegraph view
@@ -300,13 +305,17 @@ public class TimeGraphColorScheme {
         new Mix(new SysCol(SWT.COLOR_GRAY), new SysCol(SWT.COLOR_WIDGET_BACKGROUND), 1, 6), // BACKGROUND_NAME_SEL_NOFOCUS
     };
 
+    /** Fixed scheme colors */
     private final Color fColors[];
+    /** Dynamic RGBA to Color map */
+    private final Map<RGBA, Color> fColorMap;
 
     /**
      * Default constructor
      */
     public TimeGraphColorScheme() {
         fColors = new Color[PROVIDERS_MAP.length];
+        fColorMap = new HashMap<>();
     }
 
     /**
@@ -317,6 +326,9 @@ public class TimeGraphColorScheme {
             Utils.dispose(fColors[i]);
             fColors[i] = null;
         }
+        for (Color color : fColorMap.values()) {
+            color.dispose();
+        }
     }
 
     /**
@@ -425,4 +437,22 @@ public class TimeGraphColorScheme {
         }
         return getColor(GR_FOREGROUND);
     }
+
+    /**
+     * Get a color resource for the specified RGBA color descriptor
+     *
+     * @param rgba
+     *            the color descriptor
+     * @return a color resource
+     * @since 2.0
+     */
+    public Color getColor(RGBA rgba) {
+        Color color = fColorMap.get(rgba);
+        if (color == null) {
+            color = new Color(Display.getDefault(), rgba);
+            fColorMap.put(rgba, color);
+        }
+        return color;
+    }
+
 }
index bb991f6bafba59548bb112002d22e8ea5ac7f3ef..78cae3833ea35ab666a96083ecbf428b5d32072c 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2007, 2015 Intel Corporation and others
+ * Copyright (c) 2007, 2016 Intel Corporation and others
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -1734,14 +1734,15 @@ public class TimeGraphControl extends TimeGraphBaseControl
         rect.x = Math.max(nameSpace, Math.min(bounds.width, x0));
         rect.width = Math.max(1, Math.min(bounds.width, x1) - rect.x);
 
-        gc.setBackground(marker.getColor());
-        gc.setAlpha(marker.getColor().getAlpha());
+        Color color = getColorScheme().getColor(marker.getColor());
+        gc.setBackground(color);
+        gc.setAlpha(color.getAlpha());
         gc.fillRectangle(rect);
         gc.setAlpha(255);
         String label = marker.getLabel();
         if (label != null && marker.getEntry() != null) {
             label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH));
-            gc.setForeground(marker.getColor());
+            gc.setForeground(color);
             Utils.drawText(gc, label, rect.x - gc.textExtent(label).x, rect.y, true);
         }
     }
index 743baaa610f81126e68fa35dc436ab1e664081d2..398f21ae129f9698ef06c36273d0609992310d7a 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2007, 2015 Intel Corporation, Ericsson
+ * Copyright (c) 2007, 2016 Intel Corporation, Ericsson
  * 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
@@ -28,6 +28,7 @@ import org.eclipse.swt.events.MouseEvent;
 import org.eclipse.swt.events.MouseListener;
 import org.eclipse.swt.events.MouseMoveListener;
 import org.eclipse.swt.events.PaintEvent;
+import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
@@ -477,7 +478,7 @@ public class TimeGraphScale extends TimeGraphBaseControl implements
         }
     }
 
-    private static void drawMarkerLabels(List<IMarkerEvent> markerEvents, Rectangle rect, GC gc, long time0, int leftSpace, double pixelsPerNanoSec) {
+    private void drawMarkerLabels(List<IMarkerEvent> markerEvents, Rectangle rect, GC gc, long time0, int leftSpace, double pixelsPerNanoSec) {
         if (markerEvents == null) {
             return;
         }
@@ -487,7 +488,8 @@ public class TimeGraphScale extends TimeGraphBaseControl implements
                 label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH));
                 int x = rect.x + leftSpace + (int) (Math.floor((markerEvent.getTime() - time0) * pixelsPerNanoSec));
                 int y = rect.y + rect.height - gc.stringExtent(" ").y + 2; //$NON-NLS-1$
-                gc.setForeground(markerEvent.getColor());
+                Color color = getColorScheme().getColor(markerEvent.getColor());
+                gc.setForeground(color);
                 Utils.drawText(gc, label, x, y, true);
             }
         }
This page took 0.032755 seconds and 5 git commands to generate.