Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » WindowBuilder » Customizer changes more attributes than it should
Customizer changes more attributes than it should [message #760473] Fri, 02 December 2011 13:59 Go to next message
Rayleigh Missing name is currently offline Rayleigh Missing nameFriend
Messages: 20
Registered: July 2011
Junior Member
Whenever I call one of the bean setters inside the customizer, not only the setter I called will end up in the generated code but also subsequently called methods.

For example, if I call Textfield3270.setModified(true), both, setModified(true) and setInputState(MODIFIED) will end up in the generated code but I only want setModified() in the code.

class Textfield3270 extends JTextField{

	public void setModified(boolean modified) {
		this.setInputState(modified ? InputState.MODIFIED : InputState.NOT_TOUCHED);
	}
	
	public void setInputState(InputState inputState) {
		this.inputState = inputState;
	}

}


This is even worse when I call one of the JTextField methods like setText() and setBounds().
Also, calling setBounds(x,y,width,height) will generate setBounds(Rectangle) instead of overriding the existing setBounds(x,y,width,height) which is generated by default from window builder.

Is it possible to get the behaviour I want?
Re: Customizer changes more attributes than it should [message #760551 is a reply to message #760473] Fri, 02 December 2011 19:47 Go to previous messageGo to next message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
WindowBuilder runs your Customizer and when you click OK, it compares values of all properties and generate code for changed properties.

You could use EXPLICIT_PROPERTY_CHANGE flag in bean descriptor.


  public void test_customizer_EXPLICIT_PROPERTY_CHANGE() throws Exception {
    setFileContentSrc(
        "test/MyButton.java",
        getTestSource(
            "public class MyButton extends JButton {",
            "  private String m_title;",
            "  public String getTitle() {",
            "    return m_title;",
            "  }",
            "  public void setTitle(String title) {",
            "    m_title = title;",
            "  }",
            "  private boolean m_freeze;",
            "  public boolean isFreeze() {",
            "    return m_freeze;",
            "  }",
            "  public void setFreeze(boolean freeze) {",
            "    m_freeze = freeze;",
            "  }",
            "  public Object customizer;",
            "}"));
    setFileContentSrc(
        "test/MyCustomizer.java",
        getTestSource(
            "import java.beans.Customizer;",
            "import java.beans.PropertyChangeListener;",
            "public class MyCustomizer extends JPanel implements Customizer {",
            "  public void setObject(Object bean) {",
            "    MyButton button = (MyButton)bean;",
            "    button.customizer = this;",
            "  }",
            "}"));
    setFileContentSrc(
        "test/MyButtonBeanInfo.java",
        getTestSource(
            "import java.beans.BeanInfo;",
            "import java.beans.BeanDescriptor;",
            "import java.beans.Introspector;",
            "import java.beans.SimpleBeanInfo;",
            "import java.beans.PropertyDescriptor;",
            "public class MyButtonBeanInfo extends SimpleBeanInfo {",
            "  private BeanDescriptor m_descriptor;",
            "  private PropertyDescriptor[] m_properties;",
            "  public MyButtonBeanInfo() {",
            "    m_descriptor = new BeanDescriptor(MyButton.class, MyCustomizer.class);",
            "    m_descriptor.setValue('EXPLICIT_PROPERTY_CHANGE', Boolean.TRUE);",
            "    try {",
            "      m_properties = new PropertyDescriptor[2];",
            "      m_properties[0] = new PropertyDescriptor('title', MyButton.class, 'getTitle', 'setTitle');",
            "      m_properties[1] = new PropertyDescriptor('freeze', MyButton.class, 'isFreeze', 'setFreeze');",
            "    } catch(Throwable e) {",
            "    }",
            "  }",
            "  public BeanDescriptor getBeanDescriptor() {",
            "    return m_descriptor;",
            "  }",
            "  public PropertyDescriptor[] getPropertyDescriptors() {",
            "    return m_properties;",
            "  }",
            "  public BeanInfo[] getAdditionalBeanInfo() {",
            "    try {",
            "      BeanInfo info = Introspector.getBeanInfo(JButton.class);",
            "      return new BeanInfo[] {info};",
            "    } catch (Throwable e) {",
            "    }",
            "    return null;",
            "  }",
            "}"));
    waitForAutoBuild();
    // create panel
    ContainerInfo panel =
        parseContainer(
            "public class Test extends JPanel {",
            "  public Test() {",
            "    MyButton button = new MyButton();",
            "    add(button);",
            "  }",
            "}");
    panel.refresh();
    final ComponentInfo button = panel.getChildrenComponents().get(0);
    // check action
    IMenuManager manager = getContextMenu(button);
    final IAction action = findChildAction(manager, "&Customize...");
    assertNotNull(action);
    // open customize dialog
    new UiContext().executeAndCheck(new UIRunnable() {
      public void run(UiContext context) throws Exception {
        action.run();
      }
    }, new UIRunnable() {
      public void run(UiContext context) throws Exception {
        context.useShell("Customize");
        // change properties
        Object object = button.getObject();
        ReflectionUtils.invokeMethod(object, "setTitle(java.lang.String)", "test");
        ReflectionUtils.invokeMethod(object, "setFreeze(boolean)", true);
        // fire property changes
        Object customizer = ReflectionUtils.getFieldObject(object, "customizer");
        ReflectionUtils.invokeMethod(
            customizer,
            "firePropertyChange(java.lang.String,java.lang.Object,java.lang.Object)",
            "title",
            null,
            "test");
        // press "OK" button
        Button okButton = context.getButtonByText("OK");
        assertNotNull(okButton);
        context.click(okButton);
      }
    });
    // check source
    assertEditor(
        "public class Test extends JPanel {",
        "  public Test() {",
        "    MyButton button = new MyButton();",
        "    button.setTitle('test');",
        "    add(button);",
        "  }",
        "}");
  }


Konstantin Scheglov,
Google, Inc.
Re: Customizer changes more attributes than it should [message #760814 is a reply to message #760551] Mon, 05 December 2011 10:51 Go to previous messageGo to next message
Rayleigh Missing name is currently offline Rayleigh Missing nameFriend
Messages: 20
Registered: July 2011
Junior Member
Thank you.
This works fine for everything except setBounds().
I've switched from calling 'bean.setXXX()' to 'Customizer.this.firePropertyChange("xxx", bean.getXXX(), newXXX)'.
For the bounds attribute, I'm calling firePropertyChange with a Rectangle as parameter
firePropertyChange("bounds", bean.getBounds(), bounds);

The generated code now has 2 calls to setBounds. One call to setBounds(Rectangle) and another to setBounds(int, int, int, int).
private Textfield3270 getTxtfld3270Asdgr() {
	if (this.txtfld3270Asdgr == null) {
		this.txtfld3270Asdgr = new Textfield3270();
		txtfld3270Asdgr.setBounds(new Rectangle(140, 325, 270, 25));
		this.txtfld3270Asdgr.setBounds(140, 325, 258, 25);
	}
	return this.txtfld3270Asdgr;
}


Is it possible to override the existing setBounds(int, int, int, int) call?
Re: Customizer changes more attributes than it should [message #761019 is a reply to message #760814] Mon, 05 December 2011 19:29 Go to previous message
Konstantin Scheglov is currently offline Konstantin ScheglovFriend
Messages: 555
Registered: July 2009
Senior Member
Please, provide complete example with bean, customizer, initial code and result.


Konstantin Scheglov,
Google, Inc.
Previous Topic:Store preferences permanently
Next Topic:how to increase height of windowbuilder's editor?
Goto Forum:
  


Current Time: Wed Apr 24 19:53:59 GMT 2024

Powered by FUDForum. Page generated in 0.02612 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top