Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXi Yan2018-07-13 11:16:39 -0400
committerXi Yan2018-07-13 13:02:28 -0400
commit110467fbdd4dac0efc1995fef03b4ab92d527232 (patch)
tree65255eeb545638ba77d87b2ea2d0ad87fca99028
parente5cb446c7db273beadcd33f7bffe7c82cd6fe524 (diff)
downloadeclipse.platform.swt-110467fbdd4dac0efc1995fef03b4ab92d527232.tar.gz
eclipse.platform.swt-110467fbdd4dac0efc1995fef03b4ab92d527232.tar.xz
eclipse.platform.swt-110467fbdd4dac0efc1995fef03b4ab92d527232.zip
Bug 127132 - [GTK] TableColumn#pack does not work for virtual table
Fixed NPE when invoking pack on virtual table caused by previous patch. Change-Id: I3f1f6a995294d20a86c964d3b697888ebd427aac Signed-off-by: Xi Yan <xixiyan@redhat.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableColumn.java4
-rw-r--r--tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug127132_VirtualTablePackNPE.java136
2 files changed, 138 insertions, 2 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableColumn.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableColumn.java
index bb5ac91a57..b4ee6d0b47 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableColumn.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableColumn.java
@@ -462,9 +462,9 @@ public void pack () {
}
for (int i=0; i<parent.items.length; i++) {
TableItem item = parent.items [i];
- if (itemBounds == null) itemBounds = item.getBounds();
+ if (itemBounds == null && item != null) itemBounds = item.getBounds();
boolean isVisible = false;
- if (!calcWidth) {
+ if (!calcWidth && itemBounds != null) {
int itemTopBound = itemBounds.y + itemBounds.height * i + i;
int itemBottomBound = itemTopBound + itemBounds.height;
isVisible = (itemTopBound > 0 && itemBottomBound < tableHeight);
diff --git a/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug127132_VirtualTablePackNPE.java b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug127132_VirtualTablePackNPE.java
new file mode 100644
index 0000000000..31e793841a
--- /dev/null
+++ b/tests/org.eclipse.swt.tests.gtk/ManualTests/org/eclipse/swt/tests/gtk/snippets/Bug127132_VirtualTablePackNPE.java
@@ -0,0 +1,136 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Red Hat and others.
+ * 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
+ *
+ * Contributors:
+ * Thomas Singer - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.tests.gtk.snippets;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
+import org.eclipse.swt.widgets.TableItem;
+
+public class Bug127132_VirtualTablePackNPE {
+
+ // Constants ==============================================================
+
+ static final int COUNT1 = 1000;
+ static final int COUNT2 = 100;
+
+ private static boolean packPending = true;
+
+ // Static =================================================================
+
+ public static void main(String[] args) {
+ final Display display = new Display();
+ final Shell shell = new Shell(display);
+ shell.setLayout(new FillLayout(SWT.VERTICAL));
+
+ final Button button = new Button(shell, SWT.NONE);
+ final Button packButton = new Button(shell, SWT.NONE);
+
+ final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
+ button.setText("Change content");
+ button.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ table.setItemCount(table.getItemCount() == COUNT1 ? COUNT2 : COUNT1);
+ }
+ });
+
+ packButton.setText("Pack");
+ packButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ for (TableColumn c : table.getColumns()) {
+ c.pack();
+ }
+ }
+ });
+
+ table.setHeaderVisible(true);
+ final Listener listener = event -> {
+ final TableItem item = (TableItem)event.item;
+ if (event.type == SWT.EraseItem) {
+ if (event.index < 2) {
+ event.detail &= ~SWT.FOREGROUND;
+ }
+ }
+ else if (event.type == SWT.PaintItem) {
+ if (event.index == 0) {
+ event.gc.drawText((String)item.getData(), event.x, event.y, true);
+ }
+ else if (event.index == 1) {
+ final String text1 = (String)item.getData();
+ final Point size = event.gc.textExtent(text1);
+ final Rectangle bounds = item.getBounds(event.index);
+ event.gc.drawText(text1, bounds.x + bounds.width - size.x, bounds.y + bounds.height - size.y, true);
+ }
+ }
+ else if (event.type == SWT.MeasureItem) {
+ final String text2 = (String)item.getData();
+ final Point point = event.gc.textExtent(text2);
+ event.height = point.y * 2;
+ event.width = Math.max(event.width, point.x);
+ }
+ else if (event.type == SWT.SetData) {
+ final int index = table.indexOf(item);
+ final String data = "Item " + index;
+ item.setText(2, data);
+ item.setData(data);
+
+ if (packPending) {
+ packPending = false;
+ display.asyncExec(() -> {
+ table.setRedraw(false);
+ for (TableColumn column : table.getColumns()) {
+ column.pack();
+ }
+ table.setRedraw(true);
+ });
+ }
+ }
+ };
+ table.addListener(SWT.EraseItem, listener);
+ table.addListener(SWT.SetData, listener);
+ table.addListener(SWT.MeasureItem, listener);
+ table.addListener(SWT.PaintItem, listener);
+
+ createColumn(SWT.LEFT, "OD Left", table);
+ createColumn(SWT.RIGHT, "OD Right", table);
+ createColumn(SWT.LEFT, "Primitive", table);
+
+ table.setItemCount(COUNT1);
+ table.setTopIndex(50000);
+
+ shell.setSize(400, 500);
+ shell.open();
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch()) {
+ display.sleep();
+ }
+ }
+ display.dispose();
+ }
+
+ private static void createColumn(int style, String text, Table table) {
+ final TableColumn tableColumn = new TableColumn(table, style);
+ tableColumn.setText(text);
+ tableColumn.setMoveable(true);
+ tableColumn.setWidth(20);
+ }
+} \ No newline at end of file

Back to the top