Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ && getArbitraryValue(JavaInfo.FLAG_MANUAL_COMPONENT) == Boolean.TRUE
// this is first, and last time when we should do processing
putArbitraryValue(KEY_LAYOUT_ALREADY_PROCESSED, Boolean.TRUE);
}
// check if a layout is already set in the AST (which might not be known
// to the model). This can happen when the layout is set during e.g.
// creation in the *.wbp-components.xml file.
if (getMethodInvocation("setLayout(java.awt.LayoutManager)") != null) {
return;
}
// check for inheritance from parent
if (preferences.getBoolean(IPreferenceConstants.P_LAYOUT_OF_PARENT)
&& getParent() instanceof ContainerInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ && getArbitraryValue(KEY_LAYOUT_ALREADY_PROCESSED) == null
// this is first, and last time when we should do processing
putArbitraryValue(KEY_LAYOUT_ALREADY_PROCESSED, Boolean.TRUE);
}
// check if a layout is already set in the AST (which might not be known
// to the model). This can happen when the layout is set during e.g.
// creation in the *.wbp-components.xml file.
if (getMethodInvocation("setLayout(org.eclipse.swt.widgets.Layout)") != null) {
return;
}
// check for inheritance from parent
if (preferences.getBoolean(IPreferenceConstants.P_LAYOUT_OF_PARENT)
&& getParent() instanceof CompositeInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,42 @@ public Test() {
*/
@Test
public void test_setDefaultLayout() throws Exception {
test_setDefaultLayout("javax.swing.JPanel", """
// filler filler filler
public class Test extends JPanel {
public Test() {
{
JPanel panel = new JPanel();
add(panel);
panel.setLayout(new BorderLayout(0, 0));
}
}
}""");
}

/**
* Avoid duplicate layouts if one is already set during creation.
*
* @see <a href="https://github.com/eclipse-windowbuilder/windowbuilder/issues/1671">here</a>
*/
@Test
public void test_setDefaultLayout_skip() throws Exception {
prepareMyPanel_withFixedLayout();
test_setDefaultLayout("test.MyPanel", """
// filler filler filler
public class Test extends JPanel {
public Test() {
{
MyPanel myPanel = new MyPanel();
add(myPanel);
myPanel.setLayout(new GridBagLayout());
}
}
}""");

}

private void test_setDefaultLayout(String name, String source) throws Exception {
ContainerInfo panel = parseContainer("""
// filler filler filler
public class Test extends JPanel {
Expand All @@ -379,7 +415,7 @@ public Test() {
// add new JPanel
ContainerInfo newPanel;
try {
newPanel = createJavaInfo("javax.swing.JPanel");
newPanel = createJavaInfo(name);
// do add
try {
panel.startEdit();
Expand All @@ -393,17 +429,7 @@ public Test() {
preferencesRepairer.restore();
}
// check result
assertEditor("""
// filler filler filler
public class Test extends JPanel {
public Test() {
{
JPanel panel = new JPanel();
add(panel);
panel.setLayout(new BorderLayout(0, 0));
}
}
}""");
assertEditor(source);
assertNotSame(flowLayout, newPanel.getLayout());
}

Expand Down Expand Up @@ -697,4 +723,29 @@ public MyPanel() {
</component>""");
waitForAutoBuild();
}

/**
* Prepares <code>test.MyPanel</code> class which does not accept new
* {@link LayoutManager}.
*/
public static void prepareMyPanel_withFixedLayout() throws Exception {
setFileContentSrc("test/MyPanel.java", getSource("""
// filler filler filler filler filler
// filler filler filler filler filler
package test;
import javax.swing.JPanel;
public class MyPanel extends JPanel {
public MyPanel() {
}
}"""));
setFileContentSrc("test/MyPanel.wbp-component.xml", """
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns="http://www.eclipse.org/wb/WBPComponent">
<creation>
<source><![CDATA[new test.MyPanel()]]></source>
<invocation signature="setLayout(java.awt.LayoutManager)"><![CDATA[new java.awt.GridBagLayout()]]></invocation>
</creation>
</component>""");
waitForAutoBuild();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,40 @@ public Test() {
*/
@Test
public void test_setDefaultLayout() throws Exception {
test_setDefaultLayout("org.eclipse.swt.widgets.Composite", """
public class Test extends Shell {
public Test() {
setLayout(new FillLayout());
{
Composite composite = new Composite(this, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
}
}
}""");
}

/**
* Avoid duplicate layouts if one is already set during creation.
*
* @see <a href="https://github.com/eclipse-windowbuilder/windowbuilder/issues/1671">here</a>
*/
@Test
public void test_setDefaultLayout_skip() throws Exception {
prepareMyComposite_withFixedLayout();
test_setDefaultLayout("test.MyComposite", """
public class Test extends Shell {
public Test() {
setLayout(new FillLayout());
{
MyComposite myComposite = new MyComposite(this, SWT.NONE);
myComposite.setLayout(new BorderLayout());
}
}
}""");

}

private void test_setDefaultLayout(String name, String source) throws Exception {
CompositeInfo shell = parseComposite("""
public class Test extends Shell {
public Test() {
Expand All @@ -749,7 +783,7 @@ public Test() {
// add new Composite
CompositeInfo composite;
try {
composite = (CompositeInfo) BTestUtils.createControl("org.eclipse.swt.widgets.Composite");
composite = (CompositeInfo) BTestUtils.createControl(name);
composite.putArbitraryValue(JavaInfo.FLAG_MANUAL_COMPONENT, Boolean.TRUE);
fillLayout.command_CREATE(composite, null);
// execute pending async's
Expand All @@ -758,16 +792,7 @@ public Test() {
preferencesRepairer.restore();
}
// check result
assertEditor("""
public class Test extends Shell {
public Test() {
setLayout(new FillLayout());
{
Composite composite = new Composite(this, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
}
}
}""");
assertEditor(source);
assertNotSame(fillLayout, composite.getLayout());
}

Expand Down Expand Up @@ -1259,4 +1284,30 @@ public Test(Composite parent, int style) {
assertTrue(m_shouldNotBeSharedShell.isDisposed());
m_shouldNotBeSharedShell = null;
}

/**
* Prepares <code>test.MyComposite</code> class which does not accept new
* {@link Layout}.
*/
public static void prepareMyComposite_withFixedLayout() throws Exception {
setFileContentSrc("test/MyComposite.java", getSource("""
// filler filler filler filler filler
// filler filler filler filler filler
package test;
import org.eclipse.swt.widgets.Composite;
public class MyComposite extends Composite {
public MyComposite(Composite parent, int style) {
super(parent, style);
}
}"""));
setFileContentSrc("test/MyComposite.wbp-component.xml", """
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns="http://www.eclipse.org/wb/WBPComponent">
<creation>
<source><![CDATA[new test.MyComposite(%parent%, org.eclipse.swt.SWT.NONE)]]></source>
<invocation signature="setLayout(org.eclipse.swt.widgets.Layout)"><![CDATA[new org.eclipse.swt.layout.BorderLayout()]]></invocation>
</creation>
</component>""");
waitForAutoBuild();
}
}
Loading