tmf: Use LostEventsOutputAnnotationProvider trace adapter

Replace the use of LostEventsMarkerEventSource with the use of the core
LostEventOutputAnnotationProvider trace adapter.

Fix fetching of lost event annotations when requested_items is absent.

Renamed provider to LostEventsOutputAnnotationProvider

Changed category name to 'Lost Events'

[Removed] LostEventsMarkerEventSource
[Changed] Register LostEventsOutputAnnotationProviderFactory adapter
[Fixed] Fetching of lost event annotations when requested_items absent

Change-Id: Ie086c216522f0c7cba1828f9c6a341cacf8ee5e4
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/182460
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java
index f64012e..7d6597b 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/Activator.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2017 Ericsson
+ * Copyright (c) 2009, 2021 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License 2.0 which
@@ -20,6 +20,7 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.internal.tmf.core.annotations.CustomDefinedOutputAnnotationProviderFactory;
+import org.eclipse.tracecompass.internal.tmf.core.annotations.LostEventsOutputAnnotationProviderFactory;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager;
 import org.eclipse.tracecompass.tmf.core.dataprovider.DataProviderManager;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
@@ -51,6 +52,7 @@
      * The shared instance
      */
     private static Activator fPlugin;
+    private static final LostEventsOutputAnnotationProviderFactory LOST_EVENTS_ANNOTATION_PROVIDER_FACTORY = new LostEventsOutputAnnotationProviderFactory();
     private static final CustomDefinedOutputAnnotationProviderFactory CUSTOM_DEFINED_OUTPUT_ANNOTATION_PROVIDER_FACTORY = new CustomDefinedOutputAnnotationProviderFactory();
 
     // ------------------------------------------------------------------------
@@ -99,12 +101,15 @@
         SymbolProviderManager.getInstance();
         /* Initialize the data provider manager */
         DataProviderManager.getInstance();
+        TmfTraceAdapterManager.registerFactory(LOST_EVENTS_ANNOTATION_PROVIDER_FACTORY, ITmfTrace.class);
         TmfTraceAdapterManager.registerFactory(CUSTOM_DEFINED_OUTPUT_ANNOTATION_PROVIDER_FACTORY, ITmfTrace.class);
     }
 
     @Override
     public void stop(BundleContext context) throws Exception {
+        TmfTraceAdapterManager.unregisterFactory(LOST_EVENTS_ANNOTATION_PROVIDER_FACTORY);
         TmfTraceAdapterManager.unregisterFactory(CUSTOM_DEFINED_OUTPUT_ANNOTATION_PROVIDER_FACTORY);
+        LOST_EVENTS_ANNOTATION_PROVIDER_FACTORY.dispose();
         CUSTOM_DEFINED_OUTPUT_ANNOTATION_PROVIDER_FACTORY.dispose();
         TmfCoreTracer.stop();
         TmfTraceManager.getInstance().dispose();
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventOutputAnnotationProvider.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProvider.java
similarity index 94%
rename from tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventOutputAnnotationProvider.java
rename to tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProvider.java
index 7a18c26..954579c 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventOutputAnnotationProvider.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProvider.java
@@ -53,9 +53,9 @@
 /**
  * Trace Annotation provider for providing lost events annotations.
  */
-public class LostEventOutputAnnotationProvider implements IOutputAnnotationProvider {
+public class LostEventsOutputAnnotationProvider implements IOutputAnnotationProvider {
 
-    private static final String LOST_EVENTS = checkNotNull(Messages.LostEventOutputAnnotationProvider_LostEventsCategory);
+    private static final String LOST_EVENTS = checkNotNull(Messages.LostEventsOutputAnnotationProvider_LostEventsCategory);
     private static final TmfModelResponse<AnnotationModel> NO_DATA = new TmfModelResponse<>(new AnnotationModel(Collections.emptyMap()), Status.COMPLETED, ""); //$NON-NLS-1$
 
     private final ITmfTrace fTrace;
@@ -68,7 +68,7 @@
      * @param trace
      *            the trace to provide lost events annotations.
      */
-    public LostEventOutputAnnotationProvider(ITmfTrace trace) {
+    public LostEventsOutputAnnotationProvider(ITmfTrace trace) {
         fTrace = trace;
     }
 
@@ -102,9 +102,8 @@
         }
 
         List<Long> timeRequested = DataProviderParameterUtils.extractTimeRequested(fetchParameters);
-        List<@NonNull Long> entries = DataProviderParameterUtils.extractSelectedItems(fetchParameters);
         @Nullable Set<@NonNull String> categories = DataProviderParameterUtils.extractSelectedCategories(fetchParameters);
-        if (timeRequested == null || timeRequested.size() < 2|| entries == null || (categories != null && !categories.contains(LOST_EVENTS))) {
+        if (timeRequested == null || timeRequested.size() < 2 || (categories != null && !categories.contains(LOST_EVENTS))) {
             return NO_DATA;
         }
 
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProviderFactory.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProviderFactory.java
index c960e5a..9d5796a 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProviderFactory.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/LostEventsOutputAnnotationProviderFactory.java
@@ -24,7 +24,7 @@
     @Override
     protected <T> @Nullable T getTraceAdapter(ITmfTrace trace, @Nullable Class<T> adapterType) {
         if (null != adapterType && IOutputAnnotationProvider.class.equals(adapterType)) {
-            IOutputAnnotationProvider adapter = new LostEventOutputAnnotationProvider(trace);
+            IOutputAnnotationProvider adapter = new LostEventsOutputAnnotationProvider(trace);
             return adapterType.cast(adapter);
         }
         return null;
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/Messages.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/Messages.java
index 4346f05..e0c436d 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/Messages.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/Messages.java
@@ -19,7 +19,7 @@
 @SuppressWarnings("javadoc")
 public class Messages extends NLS {
     private static final String BUNDLE_NAME = "org.eclipse.tracecompass.internal.tmf.core.annotations.messages"; //$NON-NLS-1$
-    public static @Nullable String LostEventOutputAnnotationProvider_LostEventsCategory;
+    public static @Nullable String LostEventsOutputAnnotationProvider_LostEventsCategory;
 
     static {
         // initialize resource bundle
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/messages.properties b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/messages.properties
index 3a4ba4e..7dfcba5 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/messages.properties
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/annotations/messages.properties
@@ -9,4 +9,4 @@
 # SPDX-License-Identifier: EPL-2.0
 ###############################################################################
 
-LostEventOutputAnnotationProvider_LostEventsCategory=Lost Event
+LostEventsOutputAnnotationProvider_LostEventsCategory=Lost Events
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Activator.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Activator.java
index 6779883..6255acc 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Activator.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Activator.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2018 Ericsson
+ * Copyright (c) 2009, 2021 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License 2.0 which
@@ -22,12 +22,9 @@
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.ImageRegistry;
 import org.eclipse.swt.graphics.Image;
-import org.eclipse.tracecompass.internal.tmf.ui.markers.LostEventsMarkerEventSourceFactory;
 import org.eclipse.tracecompass.internal.tmf.ui.perspectives.TmfPerspectiveManager;
 import org.eclipse.tracecompass.internal.tmf.ui.views.TmfAlignmentSynchronizer;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.core.trace.TmfTraceAdapterManager;
 import org.eclipse.tracecompass.tmf.ui.TmfUiRefreshHandler;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
@@ -61,7 +58,6 @@
     private static Activator plugin;
 
     private TmfEventAdapterFactory fTmfEventAdapterFactory;
-    private LostEventsMarkerEventSourceFactory fLostEventMarkerEventSourceFactory;
     private IPreferenceStore fCorePreferenceStore;
 
     // ------------------------------------------------------------------------
@@ -105,8 +101,6 @@
 
         fTmfEventAdapterFactory = new TmfEventAdapterFactory();
         Platform.getAdapterManager().registerAdapters(fTmfEventAdapterFactory, ITmfEvent.class);
-        fLostEventMarkerEventSourceFactory = new LostEventsMarkerEventSourceFactory();
-        TmfTraceAdapterManager.registerFactory(fLostEventMarkerEventSourceFactory, ITmfTrace.class);
     }
 
     @Override
@@ -119,8 +113,6 @@
         plugin = null;
 
         Platform.getAdapterManager().unregisterAdapters(fTmfEventAdapterFactory);
-        TmfTraceAdapterManager.unregisterFactory(fLostEventMarkerEventSourceFactory);
-        fLostEventMarkerEventSourceFactory.dispose();
         super.stop(context);
     }
 
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Messages.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Messages.java
index 8f00780..12d8654 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Messages.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/Messages.java
@@ -43,7 +43,6 @@
     public static String ManageCustomParsersDialog_TextButtonLabel;
 
     public static String MarkerEvent_Bookmarks;
-    public static String MarkerEvent_LostEvents;
 
     public static String TmfChartView_LockYAxis;
     public static String TmfCommonXLineChartTooltipProvider_time;
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/markers/LostEventsMarkerEventSource.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/markers/LostEventsMarkerEventSource.java
deleted file mode 100644
index b556bb4..0000000
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/markers/LostEventsMarkerEventSource.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015, 2018 Ericsson
- *
- * All rights reserved. This program and the accompanying materials are
- * made available under the terms of the Eclipse Public License 2.0 which
- * accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- *   Patrick Tasse - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.internal.tmf.ui.markers;
-
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.swt.graphics.RGBA;
-import org.eclipse.tracecompass.internal.tmf.ui.Messages;
-import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
-import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
-import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
-import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
-import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
-import org.eclipse.tracecompass.tmf.core.statistics.TmfStateStatistics.Attributes;
-import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsEventTypesModule;
-import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsModule;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
-import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent;
-import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEventSource;
-import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.MarkerEvent;
-
-/**
- * Marker event source for lost events.
- */
-public class LostEventsMarkerEventSource implements IMarkerEventSource {
-
-    private static final @NonNull String LOST_EVENTS = checkNotNull(Messages.MarkerEvent_LostEvents);
-
-    private static final RGBA COLOR = new RGBA(255, 0, 0, 50);
-
-    private final @NonNull ITmfTrace fTrace;
-    private long[] fLastRequest;
-    private @NonNull List<@NonNull IMarkerEvent> fLastMarkers = Collections.emptyList();
-
-    /**
-     * Constructor.
-     *
-     * @param trace
-     *            the trace
-     */
-    public LostEventsMarkerEventSource(@NonNull ITmfTrace trace) {
-        fTrace = trace;
-    }
-
-    @Override
-    public @NonNull List<@NonNull String> getMarkerCategories() {
-        return Arrays.asList(LOST_EVENTS);
-    }
-
-    @Override
-    public synchronized @NonNull List<@NonNull IMarkerEvent> getMarkerList(@NonNull String category, long startTime, long endTime, long resolution, @NonNull IProgressMonitor monitor) {
-        if (!category.equals(LOST_EVENTS)) {
-            return Collections.emptyList();
-        }
-        ITmfStateSystem ss = getStateSystem();
-        if (ss == null) {
-            return Collections.emptyList();
-        }
-        int lostEventsQuark = getLostEventsQuark(ss);
-        if (lostEventsQuark == -1) {
-            return Collections.emptyList();
-        }
-        long[] request = new long[] { startTime, endTime, resolution, ss.getCurrentEndTime() };
-        if (Arrays.equals(request, fLastRequest)) {
-            return fLastMarkers;
-        }
-        List<@NonNull IMarkerEvent> markers = new ArrayList<>();
-        try {
-            long start = Math.max(startTime, ss.getStartTime());
-            long end = Math.min(endTime, ss.getCurrentEndTime());
-            if (start <= end) {
-                /* Update start to ensure that the previous marker is included. */
-                start = Math.max(start - 1, ss.getStartTime());
-                /* Update end to ensure that the next marker is included. */
-                long nextStartTime = ss.querySingleState(end, lostEventsQuark).getEndTime() + 1;
-                end = Math.min(nextStartTime, ss.getCurrentEndTime());
-                List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, lostEventsQuark, start, end, resolution, monitor);
-                if (monitor.isCanceled()) {
-                    return Collections.emptyList();
-                }
-                for (ITmfStateInterval interval : intervals) {
-                    if (interval.getStateValue().isNull()) {
-                        continue;
-                    }
-                    long lostEventsStartTime = interval.getStartTime();
-                    /*
-                     * The end time of the lost events range is the value of the
-                     * attribute, not the end time of the interval.
-                     */
-                    long lostEventsEndTime = interval.getStateValue().unboxLong();
-                    long duration = lostEventsEndTime - lostEventsStartTime;
-                    IMarkerEvent marker = new MarkerEvent(null, lostEventsStartTime, duration, LOST_EVENTS, COLOR, null, false);
-                    markers.add(marker);
-                }
-            }
-        } catch (AttributeNotFoundException | StateSystemDisposedException e) {
-            /* ignored */
-        }
-        fLastRequest = request;
-        fLastMarkers = Collections.unmodifiableList(markers);
-        return fLastMarkers;
-    }
-
-    private ITmfStateSystem getStateSystem() {
-        TmfStatisticsModule module = TmfTraceUtils.getAnalysisModuleOfClass(fTrace, TmfStatisticsModule.class, TmfStatisticsModule.ID);
-        if (module == null) {
-            return null;
-        }
-        return module.getStateSystem(TmfStatisticsEventTypesModule.ID);
-    }
-
-    private static int getLostEventsQuark(ITmfStateSystem ss) {
-        try {
-            return ss.getQuarkAbsolute(Attributes.LOST_EVENTS);
-        } catch (AttributeNotFoundException e) {
-            return -1;
-        }
-    }
-}
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/markers/LostEventsMarkerEventSourceFactory.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/markers/LostEventsMarkerEventSourceFactory.java
deleted file mode 100644
index cfcd032..0000000
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/markers/LostEventsMarkerEventSourceFactory.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015 Ericsson
- *
- * All rights reserved. This program and the accompanying materials are
- * made available under the terms of the Eclipse Public License 2.0 which
- * accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- *   Patrick Tasse - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.tracecompass.internal.tmf.ui.markers;
-
-import org.eclipse.tracecompass.tmf.core.trace.AbstractTmfTraceAdapterFactory;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
-import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEventSource;
-
-/**
- * Marker event source factory for lost events.
- */
-public class LostEventsMarkerEventSourceFactory extends AbstractTmfTraceAdapterFactory {
-
-    @Override
-    protected <T> T getTraceAdapter(ITmfTrace trace, Class<T> adapterType) {
-        if (IMarkerEventSource.class.equals(adapterType)) {
-            IMarkerEventSource adapter = new LostEventsMarkerEventSource(trace);
-            return adapterType.cast(adapter);
-        }
-        return null;
-    }
-
-    @Override
-    public Class<?>[] getAdapterList() {
-        return new Class[] {
-                IMarkerEventSource.class
-        };
-    }
-}
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/messages.properties b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/messages.properties
index 9e5d5b6..4d0fdd2 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/messages.properties
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/messages.properties
@@ -90,7 +90,6 @@
 
 # org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model
 MarkerEvent_Bookmarks=Bookmarks
-MarkerEvent_LostEvents=Lost Events
 
 # org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets
 TmfTimeTipHandler_DURATION=Duration