Reorganizing code, moving controls, and FormAttachments

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Reorganizing code, moving controls, and FormAttachments

Postby mdonovan » Wed Feb 14, 2007 1:36 pm

I am encountering a situation where Designer is generating code that has compile errors. I have a simple window with a label, text and button using FormLayout. The createContents() method looks like this -

protected void createContents() {
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");

final Label label = new Label(shell, SWT.NONE);
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 144);
formData.top = new FormAttachment(0, 131);
formData.left = new FormAttachment(0, 200);
formData.right = new FormAttachment(0, 225);
label.setLayoutData(formData);
label.setText("Label");

final Button button = new Button(shell, SWT.NONE);
final FormData formData_2 = new FormData();
formData_2.top = new FormAttachment(0, 290);
formData_2.left = new FormAttachment(0, 240);
button.setLayoutData(formData_2);
button.setText("button");

text = new Text(shell, SWT.BORDER);
final FormData formData_1 = new FormData();
formData_1.top = new FormAttachment(label, 0, SWT.TOP);
formData_1.left = new FormAttachment(0, 265);
text.setLayoutData(formData_1);
//
}

I have moved some code around - all of the form data code to the end of the createContents() method -

protected void createContents() {
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");

final Label label = new Label(shell, SWT.NONE);

final Button button = new Button(shell, SWT.NONE);

text = new Text(shell, SWT.BORDER);

//
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 144);
formData.top = new FormAttachment(0, 131);
formData.left = new FormAttachment(0, 200);
formData.right = new FormAttachment(0, 225);
label.setLayoutData(formData);
label.setText("Label");

final FormData formData_2 = new FormData();
formData_2.top = new FormAttachment(0, 290);
formData_2.left = new FormAttachment(0, 240);
button.setLayoutData(formData_2);
button.setText("button");

final FormData formData_1 = new FormData();
formData_1.top = new FormAttachment(label, 0, SWT.TOP);
formData_1.left = new FormAttachment(0, 265);
text.setLayoutData(formData_1);
}

Then I move the button up closer to the text field, which causes it to become attached to that text field. The generated code now looks like -

protected void createContents() {
shell = new Shell();
shell.setLayout(new FormLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");

final Label label = new Label(shell, SWT.NONE);

final Button button = new Button(shell, SWT.NONE);

text = new Text(shell, SWT.BORDER);
formData_2.left = new FormAttachment(text, -49, SWT.LEFT);
formData_2.right = new FormAttachment(text, -5, SWT.LEFT);

//
final FormData formData = new FormData();
formData.bottom = new FormAttachment(0, 144);
formData.top = new FormAttachment(0, 131);
formData.left = new FormAttachment(0, 200);
formData.right = new FormAttachment(0, 225);
label.setLayoutData(formData);
label.setText("Label");

FormData formData_2;
formData_2 = new FormData();
formData_2.top = new FormAttachment(0, 157);
formData_2.bottom = new FormAttachment(0, 180);
button.setLayoutData(formData_2);
button.setText("button");

final FormData formData_1 = new FormData();
formData_1.top = new FormAttachment(label, 0, SWT.TOP);
formData_1.left = new FormAttachment(0, 265);
text.setLayoutData(formData_1);
}

formData_2.left and formData_2.right are now being referenced before formData_2 is defined. Is this a problem with how Designer works or is it invalid to move the form data code after all of the widgets have been defined?

Thanks for any help or suggestions.
Mark
mdonovan
 
Posts: 2
Joined: Wed Feb 14, 2007 12:51 pm

Re: Reorganizing code, moving controls, and FormAttachments

Postby Eric Clayberg » Thu Feb 15, 2007 12:24 pm

mdonovan wrote:I have moved some code around - all of the form data code to the end of the createContents() method

Moving those lines out of their expected sequence, definitely confused Designer. It expects the constraints to be near the widgets that use them.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby mdonovan » Thu Feb 15, 2007 3:37 pm

Eric,

Would you say this is limitation of Designer or is this something that Designer should (and possibly will) be able to handle?

Thanks,
Mark
mdonovan
 
Posts: 2
Joined: Wed Feb 14, 2007 12:51 pm

Postby Eric Clayberg » Fri Feb 16, 2007 1:52 pm

mdonovan wrote:Would you say this is limitation of Designer or is this something that Designer should (and possibly will) be able to handle?

This may be something we can add support for in the future.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 2 guests