TMF: Add some non null annotations to TmfTimeRange
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / events / TmfEventsTable.java
index 5a16565b8d8852842cb69e7da0fc7d248a01a14d..a38ca9a7789c1270cca411f44fcb732edbb4e3f3 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2014 Ericsson
+ * Copyright (c) 2010, 2015 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -17,6 +17,8 @@
 
 package org.eclipse.tracecompass.tmf.ui.viewers.events;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -75,6 +77,8 @@ import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.SashForm;
 import org.eclipse.swt.custom.TableEditor;
+import org.eclipse.swt.events.ControlAdapter;
+import org.eclipse.swt.events.ControlEvent;
 import org.eclipse.swt.events.FocusAdapter;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.KeyAdapter;
@@ -102,6 +106,7 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.swt.widgets.Text;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.internal.tmf.core.filter.TmfCollapseFilter;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
@@ -110,6 +115,8 @@ import org.eclipse.tracecompass.internal.tmf.ui.dialogs.MultiLineInputDialog;
 import org.eclipse.tracecompass.tmf.core.component.ITmfEventProvider;
 import org.eclipse.tracecompass.tmf.core.component.TmfComponent;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
+import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfContentFieldAspect;
 import org.eclipse.tracecompass.tmf.core.event.collapse.ITmfCollapsibleEvent;
 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfCallsite;
 import org.eclipse.tracecompass.tmf.core.event.lookup.ITmfModelLookup;
@@ -132,11 +139,11 @@ import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
 import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsCache.CachedEvent;
 import org.eclipse.tracecompass.tmf.ui.viewers.events.columns.TmfEventTableColumn;
-import org.eclipse.tracecompass.tmf.ui.viewers.events.columns.TmfEventTableFieldColumn;
 import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSetting;
 import org.eclipse.tracecompass.tmf.ui.views.colors.ColorSettingsManager;
 import org.eclipse.tracecompass.tmf.ui.views.colors.IColorSettingsListener;
@@ -164,19 +171,16 @@ import com.google.common.collect.Multimap;
  *
  * @author Francois Chouinard
  * @author Patrick Tasse
- * @since 2.0
  */
 public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorSettingsListener, ISelectionProvider {
 
     /**
      * Empty string array, used by {@link #getItemStrings}.
-     * @since 3.0
      */
     protected static final @NonNull String[] EMPTY_STRING_ARRAY = new String[0];
 
     /**
      * Empty string
-     * @since 3.1
      */
     protected static final @NonNull String EMPTY_STRING = ""; //$NON-NLS-1$
 
@@ -200,19 +204,6 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
     private static final int FILTER_SUMMARY_INDEX = 1;
     private static final int EVENT_COLUMNS_START_INDEX = MARGIN_COLUMN_INDEX + 1;
 
-    /**
-     * Default set of columns to use for trace types that do not specify
-     * anything
-     * @since 3.2
-     */
-    public static final Collection<TmfEventTableColumn> DEFAULT_COLUMNS = ImmutableList.of(
-            TmfEventTableColumn.BaseColumns.TIMESTAMP,
-            TmfEventTableColumn.BaseColumns.SOURCE,
-            TmfEventTableColumn.BaseColumns.EVENT_TYPE,
-            TmfEventTableColumn.BaseColumns.REFERENCE,
-            TmfEventTableColumn.BaseColumns.CONTENTS
-            );
-
     /**
      * The events table search/filter keys
      *
@@ -238,11 +229,11 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         /** Rank */
         String RANK = "$rank"; //$NON-NLS-1$
 
-        /** Field ID */
-        String FIELD_ID = "$field_id"; //$NON-NLS-1$
-
         /** Bookmark indicator */
         String BOOKMARK = "$bookmark"; //$NON-NLS-1$
+
+        /** Event aspect represented by this column */
+        String ASPECT = "$aspect"; //$NON-NLS-1$
     }
 
     /**
@@ -332,7 +323,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      *            The size of the event table cache
      */
     public TmfEventsTable(final Composite parent, final int cacheSize) {
-        this(parent, cacheSize, DEFAULT_COLUMNS);
+        this(parent, cacheSize, TmfTrace.BASE_ASPECTS);
     }
 
     /**
@@ -343,7 +334,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      * @param cacheSize
      *            The size of the event table cache
      * @param columnData
-     *            Unused
+     *            The column data array
      * @deprecated Deprecated constructor, use
      *             {@link #TmfEventsTable(Composite, int, Collection)}
      */
@@ -352,24 +343,24 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
             final org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData[] columnData) {
         /*
          * We'll do a "best-effort" to keep trace types still using this API to
-         * keep working, by defining a TmfEventTableFieldColumn for each
+         * keep working, by defining a TmfEventTableColumn for each
          * ColumnData they passed.
          */
         this(parent, cacheSize, convertFromColumnData(columnData));
     }
 
     @Deprecated
-    private static Collection<TmfEventTableColumn> convertFromColumnData(
+    private static @NonNull Iterable<ITmfEventAspect> convertFromColumnData(
             org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData[] columnData) {
 
-        ImmutableList.Builder<TmfEventTableColumn> builder = new ImmutableList.Builder<>();
+        ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
         for (org.eclipse.tracecompass.tmf.ui.widgets.virtualtable.ColumnData col : columnData) {
-            String header = col.header;
-            if (header != null) {
-                builder.add(new TmfEventTableFieldColumn(header));
+            String fieldName = col.header;
+            if (fieldName != null) {
+                builder.add(new TmfContentFieldAspect(fieldName, fieldName));
             }
         }
-        return builder.build();
+        return checkNotNull(builder.build());
     }
 
     /**
@@ -379,16 +370,16 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      *            The parent composite UI object
      * @param cacheSize
      *            The size of the event table cache
-     * @param columns
-     *            The columns to use in this table.
+     * @param aspects
+     *            The event aspects to display in this table. One column per
+     *            aspect will be created.
      *            <p>
      *            The iteration order of this collection will correspond to the
-     *            initial ordering of this series of columns in the table.
+     *            initial ordering of the columns in the table.
      *            </p>
-     * @since 3.1
      */
     public TmfEventsTable(final Composite parent, int cacheSize,
-            Collection<? extends TmfEventTableColumn> columns) {
+            @NonNull Iterable<ITmfEventAspect> aspects) {
         super("TmfEventsTable"); //$NON-NLS-1$
 
         fComposite = new Composite(parent, SWT.NONE);
@@ -414,8 +405,10 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         fTable.setLinesVisible(true);
 
         // Setup the columns
-        if (columns != null) {
-            fColumns.addAll(columns);
+        for (ITmfEventAspect aspect : aspects) {
+            if (aspect != null) {
+                fColumns.add(new TmfEventTableColumn(aspect));
+            }
         }
 
         TmfMarginColumn collapseCol = new TmfMarginColumn();
@@ -426,10 +419,31 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
             TableColumn column = fTable.newTableColumn(SWT.LEFT);
             column.setText(col.getHeaderName());
             column.setToolTipText(col.getHeaderTooltip());
-            column.setData(Key.FIELD_ID, col.getFilterFieldId());
+            column.setData(Key.ASPECT, col.getEventAspect());
             column.pack();
             if (col instanceof TmfMarginColumn) {
                 column.setResizable(false);
+                column.addControlListener(new ControlAdapter() {
+                    /*
+                     * Make sure that the margin column is always first
+                     */
+                    @Override
+                    public void controlMoved(ControlEvent e) {
+                        int[] order = fTable.getColumnOrder();
+                        if (order[0] == MARGIN_COLUMN_INDEX) {
+                            return;
+                        }
+                        for (int i = order.length - 1; i > 0; i--) {
+                            if (order[i] == MARGIN_COLUMN_INDEX) {
+                                order[i] = order[i - 1];
+                                order[i - 1] = MARGIN_COLUMN_INDEX;
+                            }
+                        }
+                        fTable.setColumnOrder(order);
+                    }
+                });
+            } else {
+                column.setMoveable(true);
             }
         }
 
@@ -453,18 +467,19 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                         fRawViewer.selectAndReveal((Long) e.item.getData(Key.RANK));
                     }
                     if (e.item.getData(Key.TIMESTAMP) instanceof ITmfTimestamp) {
-                        final ITmfTimestamp ts = (ITmfTimestamp) e.item.getData(Key.TIMESTAMP);
+                        final ITmfTimestamp ts = NonNullUtils.checkNotNull((ITmfTimestamp) e.item.getData(Key.TIMESTAMP));
                         if (fTable.getSelectionIndices().length == 1) {
                             fSelectedBeginTimestamp = ts;
                         }
-                        if (fSelectedBeginTimestamp != null) {
-                            if (fSelectedBeginTimestamp.compareTo(ts) <= 0) {
-                                broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, fSelectedBeginTimestamp, ts));
+                        ITmfTimestamp selectedBeginTimestamp = fSelectedBeginTimestamp;
+                        if (selectedBeginTimestamp != null) {
+                            if (selectedBeginTimestamp.compareTo(ts) <= 0) {
+                                broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, selectedBeginTimestamp, ts));
                                 if (fTable.getSelectionIndices().length == 2) {
-                                    updateStatusLine(ts.getDelta(fSelectedBeginTimestamp));
+                                    updateStatusLine(ts.getDelta(selectedBeginTimestamp));
                                 }
                             } else {
-                                broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, ts, fSelectedBeginTimestamp));
+                                broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, checkNotNull(ts), checkNotNull(fSelectedBeginTimestamp)));
                                 updateStatusLine(fSelectedBeginTimestamp.getDelta(ts));
                             }
                         }
@@ -650,10 +665,17 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                 }
                 final TableItem[] selection = fTable.getSelection();
                 if ((selection != null) && (selection.length > 0)) {
-                    final TmfTimestamp ts = (TmfTimestamp) fTable.getSelection()[0].getData(Key.TIMESTAMP);
+                    TableItem item = fTable.getSelection()[0];
+                    final TmfTimestamp ts = (TmfTimestamp) item.getData(Key.TIMESTAMP);
                     if (ts != null) {
                         broadcast(new TmfTimeSynchSignal(TmfEventsTable.this, ts));
                     }
+                    if (item.getData() instanceof ITmfEvent) {
+                        broadcast(new TmfEventSelectedSignal(TmfEventsTable.this, (ITmfEvent) item.getData()));
+                        fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, new StructuredSelection(item.getData())));
+                    } else {
+                        fireSelectionChanged(new SelectionChangedEvent(TmfEventsTable.this, StructuredSelection.EMPTY));
+                    }
                 }
             }
         });
@@ -841,18 +863,17 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                     ParameterizedCommand cmd = ParameterizedCommand.generateCommand(command, parameters);
 
                     IEvaluationContext context = handlerService.getCurrentState();
-                    // Omit the margin column
-                    List<TmfEventTableColumn> exportColumns = fColumns.subList(EVENT_COLUMNS_START_INDEX, fColumns.size());
+                    List<TmfEventTableColumn> exportColumns = new ArrayList<>();
+                    for (int i : fTable.getColumnOrder()) {
+                        // Omit the margin column
+                        if (i >= EVENT_COLUMNS_START_INDEX) {
+                            exportColumns.add(fColumns.get(i));
+                        }
+                    }
                     context.addVariable(ExportToTextCommandHandler.TMF_EVENT_TABLE_COLUMNS_ID, exportColumns);
 
                     handlerService.executeCommandInContext(cmd, null, context);
-                } catch (ExecutionException e) {
-                    displayException(e);
-                } catch (NotDefinedException e) {
-                    displayException(e);
-                } catch (NotEnabledException e) {
-                    displayException(e);
-                } catch (NotHandledException e) {
+                } catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) {
                     displayException(e);
                 }
             }
@@ -976,8 +997,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                 // only show collapse filter if at least one trace can be collapsed
                 boolean isCollapsible = false;
                 if (fTrace != null) {
-                    ITmfTrace traces[] = TmfTraceManager.getTraceSet(fTrace);
-                    for (ITmfTrace trace : traces) {
+                    for (ITmfTrace trace : TmfTraceManager.getTraceSet(fTrace)) {
                         Class <? extends ITmfEvent> eventClass = trace.getEventType();
                         isCollapsible = ITmfCollapsibleEvent.class.isAssignableFrom(eventClass);
                         if (isCollapsible) {
@@ -1115,9 +1135,15 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      */
     protected void setItemData(final TableItem item, final ITmfEvent event, final long rank) {
         String[] itemStrings = getItemStrings(fColumns, event);
+
+        // Get the actual ITmfEvent from the CachedEvent
+        ITmfEvent tmfEvent = event;
+        if (event instanceof CachedEvent) {
+            tmfEvent = ((CachedEvent) event).event;
+        }
         item.setText(itemStrings);
-        item.setData(event);
-        item.setData(Key.TIMESTAMP, new TmfTimestamp(event.getTimestamp()));
+        item.setData(tmfEvent);
+        item.setData(Key.TIMESTAMP, new TmfTimestamp(tmfEvent.getTimestamp()));
         item.setData(Key.RANK, rank);
 
         final Collection<Long> markerIds = fBookmarksMap.get(rank);
@@ -1144,14 +1170,14 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         boolean searchNoMatch = false;
         final ITmfFilter searchFilter = (ITmfFilter) fTable.getData(Key.SEARCH_OBJ);
         if (searchFilter != null) {
-            if (searchFilter.matches(event)) {
+            if (searchFilter.matches(tmfEvent)) {
                 searchMatch = true;
             } else {
                 searchNoMatch = true;
             }
         }
 
-        final ColorSetting colorSetting = ColorSettingsManager.getColorSetting(event);
+        final ColorSetting colorSetting = ColorSettingsManager.getColorSetting(tmfEvent);
         if (searchNoMatch) {
             item.setForeground(colorSetting.getDimmedForegroundColor());
             item.setBackground(colorSetting.getDimmedBackgroundColor());
@@ -1362,11 +1388,8 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                             return false;
                         }
                         final TmfFilterMatchesNode filter = new TmfFilterMatchesNode(null);
-                        String fieldId = (String) column.getData(Key.FIELD_ID);
-                        if (fieldId == null) {
-                            fieldId = column.getText();
-                        }
-                        filter.setField(fieldId);
+                        ITmfEventAspect aspect = (ITmfEventAspect) column.getData(Key.ASPECT);
+                        filter.setEventAspect(aspect);
                         filter.setRegex(regex);
                         column.setData(objKey, filter);
                         column.setData(txtKey, regex);
@@ -1515,7 +1538,6 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      *
      * @param filter
      *            The filter to apply
-     * @since 1.1
      */
     protected void applyFilter(ITmfFilter filter) {
         stopFilterThread();
@@ -2037,10 +2059,13 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      * @param event
      *            The event printed in this row
      * @return The event row entries
-     * @since 3.0
      */
     public String[] getItemStrings(ITmfEvent event) {
-        return getItemStrings(fColumns, event);
+        List<TmfEventTableColumn> columns = new ArrayList<>();
+        for (int i : fTable.getColumnOrder()) {
+            columns.add(fColumns.get(i));
+        }
+        return getItemStrings(columns, event);
     }
 
     /**
@@ -2097,7 +2122,6 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      *
      * @param statusLineManager
      *            The status line manager, or null to disable status line messages
-     * @since 2.1
      */
     public void setStatusLineManager(IStatusLineManager statusLineManager) {
         if (fStatusLineManager != null && statusLineManager == null) {
@@ -2171,17 +2195,11 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
     // ISelectionProvider
     // ------------------------------------------------------------------------
 
-    /**
-     * @since 2.0
-     */
     @Override
     public void addSelectionChangedListener(ISelectionChangedListener listener) {
         selectionChangedListeners.add(listener);
     }
 
-    /**
-     * @since 2.0
-     */
     @Override
     public ISelection getSelection() {
         if (fTable == null || fTable.isDisposed()) {
@@ -2196,17 +2214,11 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         return new StructuredSelection(list);
     }
 
-    /**
-     * @since 2.0
-     */
     @Override
     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
         selectionChangedListeners.remove(listener);
     }
 
-    /**
-     * @since 2.0
-     */
     @Override
     public void setSelection(ISelection selection) {
         // not implemented
@@ -2219,7 +2231,6 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      * @param event a selection changed event
      *
      * @see ISelectionChangedListener#selectionChanged
-     * @since 2.0
      */
     protected void fireSelectionChanged(final SelectionChangedEvent event) {
         Object[] listeners = selectionChangedListeners.getListeners();
@@ -2437,8 +2448,8 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                 }
 
                 @Override
-                public void handleCompleted() {
-                    super.handleCompleted();
+                public void handleSuccess() {
+                    super.handleSuccess();
                     if (fTrace == null) {
                         return;
                     }
@@ -2502,7 +2513,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
     }
 
     /**
-     * @since 2.0
+     * Refresh the table
      */
     public void refresh() {
         fCache.clear();
@@ -2511,31 +2522,37 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
     }
 
     /**
-    * Margin column for images and special text (e.g. collapse count)
-    */
-   private static final class TmfMarginColumn extends TmfEventTableColumn {
-
-       private static final @NonNull String HEADER = EMPTY_STRING;
-
-       /**
-        * Constructor
-        */
-       public TmfMarginColumn() {
-           super(HEADER);
-       }
-
-       @Override
-       public String getItemString(ITmfEvent event) {
-           if (!(event instanceof CachedEvent) || ((CachedEvent) event).repeatCount == 0) {
-               return EMPTY_STRING;
-           }
-           return "+" + ((CachedEvent) event).repeatCount; //$NON-NLS-1$
-       }
-
-       @Override
-       public String getFilterFieldId() {
-           return null;
-       }
-   }
+     * Margin column for images and special text (e.g. collapse count)
+     */
+    private static final class TmfMarginColumn extends TmfEventTableColumn {
+
+        private static final @NonNull ITmfEventAspect MARGIN_ASPECT = new ITmfEventAspect() {
+
+            @Override
+            public String getName() {
+                return EMPTY_STRING;
+            }
+
+            @Override
+            public String resolve(ITmfEvent event) {
+                if (!(event instanceof CachedEvent) || ((CachedEvent) event).repeatCount == 0) {
+                    return EMPTY_STRING;
+                }
+                return "+" + ((CachedEvent) event).repeatCount; //$NON-NLS-1$
+            }
+
+            @Override
+            public String getHelpText() {
+                return EMPTY_STRING;
+            }
+        };
+
+        /**
+         * Constructor
+         */
+        public TmfMarginColumn() {
+            super(MARGIN_ASPECT);
+        }
+    }
 
 }
This page took 0.032617 seconds and 5 git commands to generate.