Bug 513185 - FillLayout throws ClassCastExecption for children with
wrong layout data
Change-Id: I2c169c63102112a42d82e995d01c6c5a0bb5d795
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java
index f66f280..9fc9f69 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FillLayout.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Christoph Läubrich - Bug 513185
*******************************************************************************/
package org.eclipse.swt.layout;
@@ -145,14 +146,19 @@
}
Point computeChildSize (Control control, int wHint, int hHint, boolean flushCache) {
- FillData data = (FillData)control.getLayoutData ();
- if (data == null) {
- data = new FillData ();
- control.setLayoutData (data);
+ Object data = control.getLayoutData ();
+ FillData fillData;
+ if (data instanceof FillData) {
+ fillData = (FillData) data;
+ } else {
+ fillData = new FillData ();
+ if (data == null) {
+ control.setLayoutData(fillData);
+ }
}
Point size = null;
if (wHint == SWT.DEFAULT && hHint == SWT.DEFAULT) {
- size = data.computeSize (control, wHint, hHint, flushCache);
+ size = fillData.computeSize (control, wHint, hHint, flushCache);
} else {
// TEMPORARY CODE
int trimX, trimY;
@@ -165,7 +171,7 @@
}
int w = wHint == SWT.DEFAULT ? wHint : Math.max (0, wHint - trimX);
int h = hHint == SWT.DEFAULT ? hHint : Math.max (0, hHint - trimY);
- size = data.computeSize (control, w, h, flushCache);
+ size = fillData.computeSize (control, w, h, flushCache);
}
return size;
}
@@ -173,8 +179,11 @@
@Override
protected boolean flushCache (Control control) {
Object data = control.getLayoutData();
- if (data != null) ((FillData)data).flushCache();
- return true;
+ if (data instanceof FillData) {
+ ((FillData)data).flushCache();
+ return true;
+ }
+ return false;
}
String getName () {
diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Control.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Control.java
index d08ad5a..652c434 100644
--- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Control.java
+++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Control.java
@@ -47,10 +47,12 @@
import org.eclipse.swt.graphics.GC;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
@@ -1183,4 +1185,16 @@
}
}
+@Test
+public void test_NoExceptionsThrownInFillLayout () {
+ shell.setLayout(new FillLayout());
+ Label label = new Label(shell, SWT.NONE);
+ Label label2 = new Label(shell, SWT.NONE);
+ String layoutData = new String("not a fill layout");
+ label.setLayoutData(layoutData);
+ shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
+ assertEquals(layoutData, label.getLayoutData());
+ assertNotNull(label2.getLayoutData());
+}
+
}