[WIP] CFV Refactor
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / provisional / tmf / ui / views / timegraph2 / TimeGraphModelViewer.java
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/provisional/tmf/ui/views/timegraph2/TimeGraphModelViewer.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/provisional/tmf/ui/views/timegraph2/TimeGraphModelViewer.java
new file mode 100644 (file)
index 0000000..1a11860
--- /dev/null
@@ -0,0 +1,178 @@
+/*******************************************************************************
+ * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
+ *
+ * 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.provisional.tmf.ui.views.timegraph2;
+
+import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.ITimeGraphModelRenderProvider;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+
+public abstract class TimeGraphModelViewer {
+
+    private static final long UNINITIALIZED = -1;
+
+    private final ITimeGraphModelRenderProvider fModelRenderProvider;
+    private final SignallingContext fSignallingContext;
+
+    private long fFullTimeGraphStartTime = 1;
+    private long fFullTimeGraphEndTime = 1;
+
+    private long fLatestVisibleRangeStartTime = UNINITIALIZED;
+    private long fLatestVisibleRangeEndTime = UNINITIALIZED;
+
+    protected TimeGraphModelViewer(ITimeGraphModelRenderProvider modelRenderProvider) {
+        fModelRenderProvider = modelRenderProvider;
+        fSignallingContext = new SignallingContext(this);
+    }
+
+    public void dispose() {
+        fSignallingContext.dispose();
+    }
+
+    // ------------------------------------------------------------------------
+    // Accessors
+    // ------------------------------------------------------------------------
+
+    public ITimeGraphModelRenderProvider getModelRenderProvider() {
+        return fModelRenderProvider;
+    }
+
+    protected SignallingContext getSignalingContext() {
+        return fSignallingContext;
+    }
+
+    protected long getFullTimeGraphStartTime() {
+        return fFullTimeGraphStartTime;
+    }
+
+    protected long getFullTimeGraphEndTime() {
+        return fFullTimeGraphEndTime;
+    }
+
+    // ------------------------------------------------------------------------
+    // Operations
+    // ------------------------------------------------------------------------
+
+    public final void repaintCurrentArea() {
+        ITmfTrace trace = fSignallingContext.getCurrentTrace();
+        long start = fLatestVisibleRangeStartTime;
+        long end = fLatestVisibleRangeEndTime;
+        if (trace == null || start == UNINITIALIZED || end == UNINITIALIZED) {
+            return;
+        }
+        paintArea(trace, fLatestVisibleRangeStartTime, fLatestVisibleRangeEndTime);
+    }
+
+    public final void seekVisibleRange(long visibleWindowStartTime, long visibleWindowEndTime) {
+        checkWindowTimeRange(visibleWindowStartTime, visibleWindowEndTime);
+
+        seekVisibleRangeImpl(visibleWindowStartTime, visibleWindowEndTime);
+        updateLatestVisibleRange(visibleWindowStartTime, visibleWindowEndTime);
+    }
+
+    protected final void paintArea(ITmfTrace trace, long windowStartTime, long windowEndTime) {
+        checkWindowTimeRange(windowStartTime, windowEndTime);
+
+        paintAreaImpl(trace, windowStartTime, windowEndTime);
+    }
+
+    protected final void drawSelection(long selectionStartTime, long selectionEndTime) {
+        checkWindowTimeRange(selectionStartTime, selectionEndTime);
+
+        drawSelectionImpl(selectionStartTime, selectionEndTime);
+    }
+
+    void updateLatestVisibleRange(long visibleWindowStartTime, long visibleWindowEndTime) {
+        fLatestVisibleRangeStartTime = visibleWindowStartTime;
+        fLatestVisibleRangeEndTime = visibleWindowEndTime;
+    }
+
+    /**
+     * Recompute the total virtual size of the time graph area, and assigns the
+     * given timestamps as the start and end positions.
+     *
+     * All subsquent operations (seek, paint, etc.) that use timestamp expect
+     * these timestamps to be within the range passed here!
+     *
+     * Should be called when the trace changes, or the trace's total time range
+     * is updated (while indexing, or in live cases).
+     */
+    protected void setTimeGraphAreaRange(long fullAreaStartTime, long fullAreaEndTime) {
+        checkTimeRange(fullAreaStartTime, fullAreaEndTime);
+
+        if (fFullTimeGraphStartTime == fullAreaStartTime &&
+                fFullTimeGraphEndTime == fullAreaEndTime) {
+            /* No need to update */
+            return;
+        }
+
+        fFullTimeGraphStartTime = fullAreaStartTime;
+        fFullTimeGraphEndTime = fullAreaEndTime;
+    }
+
+    // ------------------------------------------------------------------------
+    // Template methods
+    // ------------------------------------------------------------------------
+
+    /**
+     * This should be called whenever the visible window moves, including zoom
+     * level changes.
+     */
+    protected abstract void seekVisibleRangeImpl(long visibleWindowStartTime, long visibleWindowEndTime);
+
+    /**
+     * Paint an area of the viewer. This will also update the tree pane
+     * accordingly.
+     *
+     * It is encouraged to actually paint a bit more of the area on both sides,
+     * so that small scrolling and resizing operations do not require fetching
+     * new data to display the area.
+     */
+    protected abstract void paintAreaImpl(ITmfTrace trace, long windowStartTime, long windowEndTime);
+
+    /**
+     * Draw a new selection rectangle. The previous one, if any, will be
+     * removed.
+     */
+    protected abstract void drawSelectionImpl(long selectionStartTime, long selectionEndTime);
+
+    // ------------------------------------------------------------------------
+    // Operations
+    // ------------------------------------------------------------------------
+
+    private static void checkTimeRange(long rangeStart, long rangeEnd) {
+        if (rangeStart > rangeEnd) {
+            throw new IllegalArgumentException("Time range start " + rangeStart + "is after its end time " + rangeEnd); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+        if (rangeStart < 0 || rangeEnd < 0) {
+            throw new IllegalArgumentException("One of the time range bounds is negative"); //$NON-NLS-1$
+        }
+        if (rangeStart == Long.MAX_VALUE) {
+            throw new IllegalArgumentException("You are trying to make me believe the range starts at " + //$NON-NLS-1$
+                    rangeStart + ". I do not believe you."); //$NON-NLS-1$
+        }
+        if (rangeEnd == Long.MAX_VALUE) {
+            throw new IllegalArgumentException("You are trying to make me believe the range ends at " + //$NON-NLS-1$
+                    rangeEnd + ". I do not believe you."); //$NON-NLS-1$
+        }
+    }
+
+    private void checkWindowTimeRange(long windowStartTime, long windowEndTime) {
+        checkTimeRange(windowStartTime, windowEndTime);
+
+        if (windowStartTime < fFullTimeGraphStartTime) {
+            throw new IllegalArgumentException("Requested window start time: " + windowStartTime +
+                    " is smaller than trace start time " + fFullTimeGraphStartTime);
+        }
+        if (windowEndTime > fFullTimeGraphEndTime) {
+            throw new IllegalArgumentException("Requested window end time: " + windowEndTime +
+                    " is greater than trace end time " + fFullTimeGraphEndTime);
+        }
+    }
+
+}
This page took 0.033035 seconds and 5 git commands to generate.