tmf.ui: make table columns persist their size and visibility
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 30 Jun 2016 22:11:54 +0000 (18:11 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 28 Jul 2016 11:59:26 +0000 (07:59 -0400)
This allows the configuration to remain after closing and
re-opening a given trace.

Change-Id: I93dd11e8ccc5c8bf5078a95b45e47fc02b74a99c
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/76395
Reviewed-by: Hudson CI
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/parsers/CustomParserUtils.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/editors/TmfEventsEditor.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/events/TmfEventsTable.java

index 3379b6fe09eee5e9e38af04bbad3363e8326dfdf..5a30a8e9ec77019f6347ff98487a41fa842f2d5e 100644 (file)
@@ -21,9 +21,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.tracecompass.internal.tmf.ui.editors.TmfTableColumnUtils;
 import org.eclipse.tracecompass.internal.tmf.ui.project.operations.TmfWorkspaceModifyOperation;
 import org.eclipse.tracecompass.tmf.core.TmfProjectNature;
-import org.eclipse.tracecompass.tmf.ui.editors.TmfTraceColumnManager;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
@@ -74,9 +74,11 @@ public class CustomParserUtils {
                 }
 
                 /*
-                 * Clear the column order for this trace type. Must be done after closing the editors.
+                 * Clear the column settings for this trace type. Must be done after closing the editors.
                  */
-                TmfTraceColumnManager.clearColumnOrder(traceTypeId);
+                TmfTableColumnUtils.clearColumnOrder(traceTypeId);
+                TmfTableColumnUtils.clearColumnWidth(traceTypeId);
+                TmfTableColumnUtils.clearColumnResizable(traceTypeId);
             }
         };
         try {
index 3366cd0a2cb03f8da27158894288022266256fc7..dd66670d45ee184e61a0b4d88f8d0c64f1c5e640 100644 (file)
@@ -42,9 +42,10 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
 import org.eclipse.tracecompass.internal.tmf.ui.editors.ITmfEventsEditorConstants;
+import org.eclipse.tracecompass.internal.tmf.ui.editors.TmfTableColumnUtils;
 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
-import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
 import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
@@ -200,7 +201,7 @@ public class TmfEventsEditor extends TmfEditor implements ITmfTraceEditor, IReus
         if (propId == IEditorPart.PROP_INPUT && getEditorInput() instanceof TmfEditorInput) {
             if (fTrace != null) {
                 broadcast(new TmfTraceClosedSignal(this, fTrace));
-                TmfTraceColumnManager.saveColumnOrder(fTrace.getTraceTypeId(), fEventsTable.getColumnOrder());
+                saveState();
             }
             fEventsTable.dispose();
             fFile = ((TmfEditorInput) getEditorInput()).getFile();
@@ -218,6 +219,19 @@ public class TmfEventsEditor extends TmfEditor implements ITmfTraceEditor, IReus
         }
     }
 
+    private void loadState() {
+        final @Nullable String traceTypeId = fTrace.getTraceTypeId();
+        fEventsTable.setColumnOrder(TmfTableColumnUtils.loadColumnOrder(traceTypeId));
+        fEventsTable.setColumnWidth(TmfTableColumnUtils.loadColumnWidth(traceTypeId), TmfTableColumnUtils.loadColumnResizable(traceTypeId));
+    }
+
+    private void saveState() {
+        final @Nullable String traceTypeId = fTrace.getTraceTypeId();
+        TmfTableColumnUtils.saveColumnOrder(traceTypeId, fEventsTable.getColumnOrder());
+        TmfTableColumnUtils.saveColumnWidth(traceTypeId, fEventsTable.getColumnWidth());
+        TmfTableColumnUtils.saveColumnResizability(traceTypeId, fEventsTable.getColumnResizable());
+    }
+
     @Override
     public void createPartControl(final Composite parent) {
         fParent = parent;
@@ -236,7 +250,7 @@ public class TmfEventsEditor extends TmfEditor implements ITmfTraceEditor, IReus
             setPartName(fTrace.getName());
             fEventsTable = createEventsTable(fParent, fTrace.getCacheSize());
             fEventsTable.registerContextMenus(getSite());
-            fEventsTable.setColumnOrder(TmfTraceColumnManager.loadColumnOrder(fTrace.getTraceTypeId()));
+            loadState();
             fEventsTable.addSelectionChangedListener(this);
             fEventsTable.setTrace(fTrace, true);
             fEventsTable.refreshBookmarks(fFile);
@@ -274,7 +288,7 @@ public class TmfEventsEditor extends TmfEditor implements ITmfTraceEditor, IReus
         if (fTrace != null) {
             broadcast(new TmfTraceClosedSignal(this, fTrace));
             if (fEventsTable != null) {
-                TmfTraceColumnManager.saveColumnOrder(fTrace.getTraceTypeId(), fEventsTable.getColumnOrder());
+                saveState();
             }
         }
         super.dispose();
index f0d39861f385bf5797b060aeee8ef9497c358db8..92866dad9b42198abe36d660e2a9f3e64ea7a472 100644 (file)
@@ -251,6 +251,26 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
             fTable.setColumnOrder(order);
             fColumnOrder = fTable.getColumnOrder();
         }
+
+        @Override
+        public void controlResized(ControlEvent e) {
+            final int size = fTable.getColumns().length;
+            int[] width = new int[size];
+            boolean[] resizable = new boolean[size];
+            for (TableColumn column : fTable.getColumns()) {
+                int i = (int) column.getData(Key.INDEX);
+                resizable[i] = column.getResizable();
+                Object data = column.getData(Key.WIDTH);
+                if (data instanceof Integer) {
+                    width[i] = resizable[i] ? column.getWidth() : (Integer) data;
+                } else {
+                    width[i] = column.getWidth();
+                }
+            }
+            fColumnSize = width;
+            fColumnResizable = resizable;
+            super.controlResized(e);
+        }
     }
 
     private final class TableSelectionListener extends SelectionAdapter {
@@ -630,6 +650,13 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
          * @since 1.1
          */
         String WIDTH = "$width"; //$NON-NLS-1$
+
+        /**
+         * The position of the column
+         *
+         * @since 2.1
+         */
+        String INDEX = "$index"; //$NON-NLS-1$
     }
 
     /**
@@ -639,8 +666,11 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      * @author Patrick Tasse
      */
     public static enum HeaderState {
-        /** No search filter is applied
-         * @since 2.0*/
+        /**
+         * No search filter is applied
+         *
+         * @since 2.0
+         */
         NO_SEARCH,
 
         /** A search filter is applied */
@@ -730,6 +760,10 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
     private MenuManager fTablePopupMenuManager;
     private MenuManager fHeaderPopupMenuManager;
 
+    private boolean[] fColumnResizable;
+
+    private int[] fColumnSize;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -864,6 +898,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         for (ITmfEventAspect<?> aspect : aspects) {
             if (aspect != null) {
                 fColumns.add(new TmfEventTableColumn(aspect));
+
             }
         }
 
@@ -871,12 +906,15 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         fColumns.add(MARGIN_COLUMN_INDEX, collapseCol);
 
         fHeaderMenu = new Menu(fTable);
+        int i = 0;
         // Create the UI columns in the table
         for (TmfEventTableColumn col : fColumns) {
             TableColumn column = fTable.newTableColumn(SWT.LEFT);
             column.setText(col.getHeaderName());
             column.setToolTipText(col.getHeaderTooltip());
             column.setData(Key.ASPECT, col.getEventAspect());
+            column.setData(Key.INDEX, i);
+            i++;
             column.pack();
             if (col instanceof TmfMarginColumn) {
                 column.setResizable(false);
@@ -904,7 +942,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         // Handle the table item requests
         fTable.addListener(SWT.SetData, new SetDataListener());
 
-        fTable.addMenuDetectListener( event -> {
+        fTable.addMenuDetectListener(event -> {
             fLastMenuCursorLocation = new Point(event.x, event.y);
             Point pt = fTable.getDisplay().map(null, fTable, fLastMenuCursorLocation);
             Rectangle clientArea = fTable.getClientArea();
@@ -961,18 +999,25 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      * @param column
      *            the column
      */
-    private static IAction createHeaderAction(final TableColumn column) {
+    private IAction createHeaderAction(final TableColumn column) {
         final IAction columnMenuAction = new Action(column.getText(), IAction.AS_CHECK_BOX) {
             @Override
             public void run() {
-                if (isChecked()) {
-                    column.setWidth((int) column.getData(Key.WIDTH));
+                boolean isChecked = isChecked();
+                int width = column.getWidth();
+                if (isChecked) {
+                    int newWidth = (int) column.getData(Key.WIDTH);
+                    column.setWidth(newWidth);
                     column.setResizable(true);
+                    width = newWidth;
                 } else {
                     column.setData(Key.WIDTH, column.getWidth());
                     column.setWidth(0);
                     column.setResizable(false);
                 }
+                int pos = (int) column.getData(Key.INDEX);
+                fColumnSize[pos] = width;
+                fColumnResizable[pos] = isChecked;
             }
         };
         columnMenuAction.setChecked(column.getResizable());
@@ -986,21 +1031,21 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                 for (TableColumn column : fTable.getColumns()) {
                     final Object widthVal = column.getData(Key.WIDTH);
                     if (widthVal instanceof Integer) {
-                        Integer width = (Integer) widthVal;
-                        if (!column.getResizable()) {
-                            column.setWidth(width);
-                            column.setResizable(true);
-                            /*
-                             * This is because Linux always resizes the last
-                             * column to fill in the void, this means that
-                             * hiding a column resizes others and we can have 10
-                             * columns that are 1000 pixels wide by hiding the
-                             * last one progressively.
-                             */
-                            if (IS_LINUX) {
-                                column.pack();
-                            }
+                        column.setWidth((Integer) widthVal);
+                        column.setResizable(true);
+                        /*
+                         * This is because Linux always resizes the last column
+                         * to fill in the void, this means that hiding a column
+                         * resizes others and we can have 10 columns that are
+                         * 1000 pixels wide by hiding the last one
+                         * progressively.
+                         */
+                        if (IS_LINUX) {
+                            column.pack();
                         }
+                        int index = (int) column.getData(Key.INDEX);
+                        fColumnSize[index] = column.getWidth();
+                        fColumnResizable[index] = true;
                     }
                 }
             }
@@ -2480,6 +2525,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
             final int selection = index + 1 + (eventFilter != null ? +1 : 0);
 
             display.asyncExec(new Runnable() {
+
                 @Override
                 public void run() {
                     if (monitor.isCanceled()) {
@@ -2503,6 +2549,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                 }
             });
             return Status.OK_STATUS;
+
         }
 
         @Override
@@ -2514,6 +2561,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
                 fSearchThread = null;
             }
         }
+
     }
 
     /**
@@ -2563,9 +2611,8 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
     /**
      * Pack the columns.
      *
-     * @return
-     *            Whether or not a pack was done in this call. Otherwise, it was already done by a
-     *            previous call
+     * @return Whether or not a pack was done in this call. Otherwise, it was
+     *         already done by a previous call
      *
      * @since 2.0
      */
@@ -2598,6 +2645,9 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
 
     private void packSingleColumn(int i, final TableColumn column) {
         final int headerWidth = column.getWidth();
+        if (column.getWidth() <= 0) {
+            return;
+        }
         column.pack();
         /*
          * Workaround for Linux which doesn't consider the image width of
@@ -2702,6 +2752,26 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         return fColumnOrder;
     }
 
+    /**
+     * Get column widths
+     *
+     * @return the current visual widths of the receiver's columns
+     * @since 2.1
+     */
+    public int[] getColumnWidth() {
+        return fColumnSize;
+    }
+
+    /**
+     * Get whether the columns are resizable
+     *
+     * @return an array stating if each column is resizable
+     * @since 2.1
+     */
+    public boolean[] getColumnResizable() {
+        return fColumnResizable;
+    }
+
     /**
      * Sets the order that the columns in the receiver should be displayed in to
      * the given argument which is described in terms of the zero-relative
@@ -2722,6 +2792,39 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
         fColumnOrder = fTable.getColumnOrder();
     }
 
+    /**
+     * Sets the column width and resizability
+     *
+     * @param width
+     *            an array of widths
+     * @param resizable
+     *            an array of bools saying if a column is resizable or not
+     * @since 2.1
+     */
+    public void setColumnWidth(int[] width, boolean[] resizable) {
+        int length = fTable.getColumns().length;
+        if (width == null || resizable == null || resizable.length != length || width.length != length) {
+            fColumnSize = null;
+            fColumnResizable = null;
+            return;
+        }
+        int i = 0;
+        for (TableColumn column : fTable.getColumns()) {
+            if (i != 0) {
+                column.setData(Key.WIDTH, width[i]);
+                column.setResizable(resizable[i]);
+                if (column.getResizable()) {
+                    column.setWidth((int) column.getData(Key.WIDTH));
+                } else {
+                    column.setWidth(0);
+                }
+            }
+            i++;
+        }
+        fColumnSize = width;
+        fColumnResizable = resizable;
+    }
+
     /**
      * Notify this table that is got the UI focus.
      */
@@ -2757,11 +2860,27 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      *            disposed
      */
     public void setTrace(final ITmfTrace trace, final boolean disposeOnClose) {
+        setTrace(trace, disposeOnClose, null);
+    }
+
+    /**
+     * Assign a new trace to this event table.
+     *
+     * @param trace
+     *            The trace to assign to this event table
+     * @param disposeOnClose
+     *            true if the trace should be disposed when the table is
+     *            disposed
+     * @param sizes
+     *            the column widths
+     * @since 2.1
+     */
+    public void setTrace(final ITmfTrace trace, final boolean disposeOnClose, final int[] sizes) {
         if ((fTrace != null) && fDisposeOnClose) {
             fTrace.dispose();
         }
         fTrace = trace;
-        fPackDone = false;
+        fPackDone = sizes != null;
         fDisposeOnClose = disposeOnClose;
 
         // Perform the updates on the UI thread
@@ -3358,8 +3477,7 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
      */
     private static final class TmfMarginColumn extends TmfEventTableColumn {
 
-        private static final @NonNull ITmfEventAspect<String> MARGIN_ASPECT =
-                new ITmfEventAspect<String>() {
+        private static final @NonNull ITmfEventAspect<String> MARGIN_ASPECT = new ITmfEventAspect<String>() {
 
             @Override
             public String getName() {
@@ -3387,5 +3505,4 @@ public class TmfEventsTable extends TmfComponent implements IGotoMarker, IColorS
             super(MARGIN_ASPECT);
         }
     }
-
-}
+}
\ No newline at end of file
This page took 0.030316 seconds and 5 git commands to generate.