From 7011de74a8fd125677b6a9261d6fbddd51081c99 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Sat, 21 Jul 2018 20:39:41 +0200 Subject: History table: adapt column widths when view size changes Use a TableColumnLayout; give columns minimum widths, and give the commit ID column a fixed size. Re-layout the table when the width changes, and if the table then doesn't fit the view, re-distribute all column widths afresh. This results in the table always using at least the available width, and getting a horizontal scroll bar only if the user makes columns so wide that they cannot fit the view. (Or makes the view that small.) Bug: 537181 Change-Id: Ic105acd0ffc7877e8834888b841e3f0aa96993cb Signed-off-by: Thomas Wolf --- .../egit/ui/internal/history/CommitGraphTable.java | 51 +++++++++++++++++----- 1 file changed, 40 insertions(+), 11 deletions(-) (limited to 'org.eclipse.egit.ui/src/org/eclipse/egit/ui') diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java index 4c24d641f0..7339c39b43 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitGraphTable.java @@ -48,7 +48,9 @@ import org.eclipse.egit.ui.internal.trace.GitTraceLocation; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; +import org.eclipse.jface.layout.TableColumnLayout; import org.eclipse.jface.resource.ResourceManager; +import org.eclipse.jface.viewers.ColumnPixelData; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IOpenListener; @@ -60,7 +62,6 @@ import org.eclipse.jface.viewers.LabelProviderChangedEvent; import org.eclipse.jface.viewers.OpenEvent; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.jface.viewers.TableLayout; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revplot.PlotCommit; @@ -161,7 +162,9 @@ class CommitGraphTable { hFont = highlightFont(); tableLoader = loader; - final Table rawTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL + Composite tableContainer = new Composite(parent, SWT.NONE); + final Table rawTable = new Table(tableContainer, + SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL); rawTable.setHeaderVisible(true); rawTable.setLinesVisible(false); @@ -181,12 +184,14 @@ class CommitGraphTable { } }); - final TableLayout layout = new TableLayout(); - rawTable.setLayout(layout); + TableColumnLayout layout = new TableColumnLayout(); + tableContainer.setLayout(layout); createColumns(rawTable, layout); createPaintListener(rawTable); + rawTable.addListener(SWT.Resize, event -> layout(rawTable, layout)); + table = new TableViewer(rawTable) { @Override protected Widget doFindItem(final Object element) { @@ -412,7 +417,8 @@ class CommitGraphTable { } } - private void createColumns(final Table rawTable, final TableLayout layout) { + private void createColumns(Table rawTable, + TableColumnLayout layout) { final TableColumn commitId = new TableColumn(rawTable, SWT.NONE); commitId.setResizable(true); commitId.setText(UIText.CommitGraphTable_CommitId); @@ -424,37 +430,37 @@ class CommitGraphTable { } finally { gc.dispose(); } - layout.addColumnData(new ColumnWeightData(1, minWidth, true)); + layout.setColumnData(commitId, new ColumnPixelData(minWidth, false)); final TableColumn graph = new TableColumn(rawTable, SWT.NONE); graph.setResizable(true); graph.setText(UIText.CommitGraphTable_messageColumn); graph.setWidth(400); - layout.addColumnData(new ColumnWeightData(20, true)); + layout.setColumnData(graph, new ColumnWeightData(20, 200, true)); final TableColumn author = new TableColumn(rawTable, SWT.NONE); author.setResizable(true); author.setText(UIText.HistoryPage_authorColumn); author.setWidth(100); - layout.addColumnData(new ColumnWeightData(5, true)); + layout.setColumnData(author, new ColumnWeightData(5, 80, true)); final TableColumn date = new TableColumn(rawTable, SWT.NONE); date.setResizable(true); date.setText(UIText.HistoryPage_authorDateColumn); date.setWidth(100); - layout.addColumnData(new ColumnWeightData(5, true)); + layout.setColumnData(date, new ColumnWeightData(5, 80, true)); final TableColumn committer = new TableColumn(rawTable, SWT.NONE); committer.setResizable(true); committer.setText(UIText.CommitGraphTable_Committer); committer.setWidth(100); - layout.addColumnData(new ColumnWeightData(5, true)); + layout.setColumnData(committer, new ColumnWeightData(5, 80, true)); final TableColumn committerDate = new TableColumn(rawTable, SWT.NONE); committerDate.setResizable(true); committerDate.setText(UIText.CommitGraphTable_committerDataColumn); committerDate.setWidth(100); - layout.addColumnData(new ColumnWeightData(5, true)); + layout.setColumnData(committerDate, new ColumnWeightData(5, 80, true)); } private void createPaintListener(final Table rawTable) { @@ -523,6 +529,29 @@ class CommitGraphTable { return table; } + private void layout(Table rawTable, TableColumnLayout layout) { + rawTable.getParent().layout(); + // Check that the table now fits + int tableWidth = rawTable.getSize().x; + int columnsWidth = 0; + TableColumn[] columns = rawTable.getColumns(); + for (TableColumn col : columns) { + columnsWidth += col.getWidth(); + } + if (columnsWidth > tableWidth) { + // Re-distribute the space again + layout.setColumnData(columns[1], + new ColumnWeightData(20, 200, true)); + for (int i = 2; i < columns.length; i++) { + layout.setColumnData(columns[i], + new ColumnWeightData(5, 80, true)); + } + rawTable.getParent().layout(); + } + // Sometimes observed cheese on Cocoa without this + rawTable.redraw(); + } + private final class CommitDragSourceListener extends DragSourceAdapter { @Override public void dragStart(DragSourceEvent event) { -- cgit v1.2.3