From cc7c032c6a35b40426b84232f90c8d5c7255a8b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 1 May 2022 19:31:58 +0200 Subject: [PATCH] NullPointerException in BorderLayout when Layout Data is null Fix https://github.com/eclipse-platform/eclipse.platform.swt/issues/32 --- .../org/eclipse/swt/layout/BorderLayout.java | 21 ++++++++++++------- .../org/eclipse/swt/snippets/Snippet379.java | 1 + 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/BorderLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/BorderLayout.java index 4f6b1769719..bc79a32cb2a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/BorderLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/BorderLayout.java @@ -61,6 +61,7 @@ */ public class BorderLayout extends Layout { + private static final String LAYOUT_KEY = BorderLayout.class.getName() + ".layoutData"; private static final ToIntFunction WIDTH = p -> p.x; private static final ToIntFunction HEIGHT = p -> p.y; @@ -119,9 +120,9 @@ public class BorderLayout extends Layout { */ public double widthDistributionFactor = 0.5; /** - * If the height of the {@link SWT#TOP} and {@link SWT#BOTTOM} region exceeds the - * available space this factor is used to distribute the size to the controls, - * valid values range between [0 ... 1] + * If the height of the {@link SWT#TOP} and {@link SWT#BOTTOM} region exceeds + * the available space this factor is used to distribute the size to the + * controls, valid values range between [0 ... 1] * * The default value is 0.5 (equal distribution of available space) * @@ -134,7 +135,7 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f return new Point(wHint, hHint); } Stream> children = Arrays.stream(composite.getChildren())// - .map(control-> borderDataControl(control, flushCache)); + .map(control -> borderDataControl(control, flushCache)); Map>> regionMap = children .collect(Collectors.groupingBy(BorderLayout::region)); int width; @@ -253,7 +254,7 @@ protected void layout(Composite composite, boolean flushCache) { int clientWidth = clientArea.width - 2 * marginWidth; int clientHeight = clientArea.height - 2 * marginHeight; Stream> children = Arrays.stream(composite.getChildren())// - .map(control-> borderDataControl(control, flushCache)); + .map(control -> borderDataControl(control, flushCache)); Map>> regionMap = children .collect(Collectors.groupingBy(BorderLayout::region)); regionMap.getOrDefault(SWT.NONE, Collections.emptyList()) @@ -383,7 +384,7 @@ protected void layout(Composite composite, boolean flushCache) { } } - private static Entry borderDataControl(C control, boolean flushCache) { + private Entry borderDataControl(C control, boolean flushCache) { Object layoutData = control.getLayoutData(); if (layoutData instanceof BorderData) { BorderData borderData = (BorderData) layoutData; @@ -392,14 +393,18 @@ private static Entry borderDataControl(C cont } return new SimpleEntry<>(control, borderData); } else { - return new SimpleEntry<>(control, null); + BorderData borderData = flushCache ? null : (BorderData) control.getData(LAYOUT_KEY); + if (borderData == null) { + control.setData(LAYOUT_KEY, borderData = new BorderData()); + } + return new SimpleEntry<>(control, borderData); } } private static int region(Entry entry) { BorderData borderData = entry.getValue(); if (borderData == null) { - //we assume all controls without explicit data to be placed in the center area + // we assume all controls without explicit data to be placed in the center area return SWT.CENTER; } return borderData.getRegion(); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet379.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet379.java index 1a865ce39b0..2341e2f5d2a 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet379.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet379.java @@ -46,6 +46,7 @@ public static void main(String[] args) { region(new Button(shell, SWT.PUSH), SWT.RIGHT).setText("East 2"); new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL).setText("Center 1"); new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL).setText("Center 2"); + shell.pack(true); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) {