Label myLabel = ...;
Text myText = ...;
FormData data = null;
//error, don't do this !!!
data.left = new FormAttachment(myLabel, SWT.RIGHT, 5);
data.top = new FormAttachment(myLabel, SWT.CENTER, 0);
myText.setLayoutData(data);
You see the mistake ? Yes, you got it ! The constructor of FormAttachment is :
public FormAttachment(Control control, int offset, int alignment)
So I just have to invert offset and alignment to solve my problem. Here is the correct snippet :
Label myLabel = ...;
Text myText = ...;
FormData data = null;
//this time, it's ok
data.left = new FormAttachment(myLabel, 5, SWT.RIGHT);
data.top = new FormAttachment(myLabel, 0, SWT.CENTER);
myText.setLayoutData(data);
Yesterday, I read the excellent book of Joshua Bloch "Effective Java 2nd Ed.". This book explains how to take the most of the java language in 78 independent items. It's undoubtly a must-have for each java programmer.
Specifically, I read the item 2 :"Consider a builder when faced with many constructor parameters". And I thought I could have avoid this stupid bug if only FormData had its own builder class.
So I decided to write this builder class and I'm glad to publish this class in my blog:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Control;
/**
* Class FormDataBuilder
*
* Utility class used to avoid the verbosity/opacity of FormData
* and FormAttachment.
* See the example below for how to use it :
* <pre>
FormData data = new FormDataBuilder()
.topAt(10)
.leftAlignedToTheRightOf(myControl, offset)
.build();
</pre>
gives the sam result as:
<pre>
FormData data = new FormData();
data.top = new FormAttachment(10, 100, 0);
data.left = new FormAttachment(myControl, offset, SWT.RIGHT);
</pre>
*
* THIS CLASS IS NOT THREAD-SAFE.
*
* @author Sylvere Richard
*
*/
public class FormDataBuilder {
private final static int denominator = 100;
private int top = Integer.MIN_VALUE;
private int insetTop = Integer.MIN_VALUE;
private int bottom = Integer.MIN_VALUE;
private int insetBottom = Integer.MIN_VALUE;
private int left = Integer.MIN_VALUE;
private int insetLeft = Integer.MIN_VALUE;
private int right = Integer.MIN_VALUE;
private int insetRight = Integer.MIN_VALUE;
private int width = Integer.MIN_VALUE;
private int height = Integer.MIN_VALUE;
private Control rightAlignedToTheRightOf = null;
private Control rightAlignedToTheLeftOf = null;
private Control rightAlignedToTheCenterOf = null;
private Control leftAlignedToTheRightOf = null;
private Control leftAlignedToTheLeftOf = null;
private Control leftAlignedToTheCenterOf = null;
private Control topAlignedToTheTopOf = null;
private Control topAlignedToTheBottomOf = null;
private Control topAlignedToTheCenterOf = null;
private Control bottomAlignedToTheTopOf = null;
private Control bottomAlignedToTheBottomOf = null;
private Control bottomAlignedToTheCenterOf = null;
private int rightAlignedToTheRightOfInset = Integer.MIN_VALUE;
private int rightAlignedToTheLeftOfInset = Integer.MIN_VALUE;
private int rightAlignedToTheCenterOfInset = Integer.MIN_VALUE;
private int leftAlignedToTheRightOfInset = Integer.MIN_VALUE;
private int leftAlignedToTheLeftOfInset = Integer.MIN_VALUE;
private int leftAlignedToTheCenterOfInset = Integer.MIN_VALUE;
private int topAlignedToTheTopOfInset = Integer.MIN_VALUE;
private int topAlignedToTheBottomOfInset = Integer.MIN_VALUE;
private int topAlignedToTheCenterOfInset = Integer.MIN_VALUE;
private int bottomAlignedToTheTopOfInset = Integer.MIN_VALUE;
private int bottomAlignedToTheBottomOfInset = Integer.MIN_VALUE;
private int bottomAlignedToTheCenterOfInset = Integer.MIN_VALUE;
/**
*
*/
public FormDataBuilder() {
}
/**
* Used to get a FormData properly configured by previous calls
* of all the methods beginning with top, bottom, left or right.
* Successive calls of build() produces different identical FormData object.
* @return Returns a FormData object.
*/
public FormData build() {
final FormData data = new FormData();
if (top != Integer.MIN_VALUE) {
data.top = new FormAttachment(top, denominator,
insetTop == Integer.MIN_VALUE ? 0 : insetTop);
}
if (bottom != Integer.MIN_VALUE) {
data.bottom = new FormAttachment(bottom, denominator,
insetBottom == Integer.MIN_VALUE ? 0 : insetBottom);
}
if (right != Integer.MIN_VALUE) {
data.right = new FormAttachment(right, denominator,
insetRight == Integer.MIN_VALUE ? 0 : insetRight);
}
if (left != Integer.MIN_VALUE) {
data.left = new FormAttachment(left, denominator,
insetLeft == Integer.MIN_VALUE ? 0 : insetLeft);
}
if (width != Integer.MIN_VALUE) {
data.width = width;
}
if (height != Integer.MIN_VALUE) {
data.height = height;
}
if (rightAlignedToTheRightOf != null) {
data.right = new FormAttachment(rightAlignedToTheRightOf,
rightAlignedToTheRightOfInset == Integer.MIN_VALUE ? 0
: rightAlignedToTheRightOfInset, SWT.RIGHT);
}
if (rightAlignedToTheLeftOf != null) {
data.right = new FormAttachment(rightAlignedToTheLeftOf,
rightAlignedToTheLeftOfInset == Integer.MIN_VALUE ? 0
: rightAlignedToTheLeftOfInset, SWT.LEFT);
}
if (rightAlignedToTheCenterOf != null) {
data.right = new FormAttachment(rightAlignedToTheCenterOf,
rightAlignedToTheCenterOfInset == Integer.MIN_VALUE ? 0
: rightAlignedToTheCenterOfInset, SWT.CENTER);
}
if (leftAlignedToTheRightOf != null) {
data.left = new FormAttachment(leftAlignedToTheRightOf,
leftAlignedToTheRightOfInset == Integer.MIN_VALUE ? 0
: leftAlignedToTheRightOfInset, SWT.RIGHT);
}
if (leftAlignedToTheLeftOf != null) {
data.left = new FormAttachment(leftAlignedToTheLeftOf,
leftAlignedToTheLeftOfInset == Integer.MIN_VALUE ? 0
: leftAlignedToTheLeftOfInset, SWT.LEFT);
}
if (leftAlignedToTheCenterOf != null) {
data.left = new FormAttachment(leftAlignedToTheCenterOf,
leftAlignedToTheCenterOfInset == Integer.MIN_VALUE ? 0
: leftAlignedToTheCenterOfInset, SWT.CENTER);
}
if (topAlignedToTheTopOf != null) {
data.top = new FormAttachment(topAlignedToTheTopOf,
topAlignedToTheTopOfInset == Integer.MIN_VALUE ? 0
: topAlignedToTheTopOfInset, SWT.RIGHT);
}
if (topAlignedToTheBottomOf != null) {
data.top = new FormAttachment(topAlignedToTheBottomOf,
topAlignedToTheBottomOfInset == Integer.MIN_VALUE ? 0
: topAlignedToTheBottomOfInset, SWT.LEFT);
}
if (topAlignedToTheCenterOf != null) {
data.top = new FormAttachment(topAlignedToTheCenterOf,
topAlignedToTheCenterOfInset == Integer.MIN_VALUE ? 0
: topAlignedToTheCenterOfInset, SWT.CENTER);
}
if (bottomAlignedToTheTopOf != null) {
data.bottom = new FormAttachment(bottomAlignedToTheTopOf,
bottomAlignedToTheTopOfInset == Integer.MIN_VALUE ? 0
: bottomAlignedToTheTopOfInset, SWT.RIGHT);
}
if (bottomAlignedToTheBottomOf != null) {
data.bottom = new FormAttachment(bottomAlignedToTheBottomOf,
bottomAlignedToTheBottomOfInset == Integer.MIN_VALUE ? 0
: bottomAlignedToTheBottomOfInset, SWT.LEFT);
}
if (bottomAlignedToTheCenterOf != null) {
data.bottom = new FormAttachment(bottomAlignedToTheCenterOf,
bottomAlignedToTheCenterOfInset == Integer.MIN_VALUE ? 0
: bottomAlignedToTheCenterOfInset, SWT.CENTER);
}
return data;
}
/**
* do the same as FormData data = ... data.top = new
* FormAttachment(numerator, 100, 0);
*
* @param numerator
* @return Returns this.
*/
public FormDataBuilder topAt(int numerator) {
return topAt(numerator, 0);
}
/**
* do the same as FormData data = ... data.top = new
* FormAttachment(numerator, 100, offset);
*
* @param numerator
* @param offset
* @return Returns this.
*/
public FormDataBuilder topAt(int numerator, int offset) {
this.top = numerator;
this.insetTop = offset;
return this;
}
/**
* do the same as FormData data = ... data.bottom = new
* FormAttachment(numerator, 100, 0);
*
* @param numerator
* @return Returns this.
*/
public FormDataBuilder bottomAt(int numerator) {
return bottomAt(numerator, 0);
}
/**
* do the same as FormData data = ... data.bottom = new
* FormAttachment(numerator, 100, offset);
*
* @param numerator
* @param offset
* @return Returns this.
*/
public FormDataBuilder bottomAt(int numerator, int offset) {
this.bottom = numerator;
this.insetBottom = offset;
return this;
}
/**
* do the same as FormData data = ... data.left = new
* FormAttachment(numerator, 100, 0);
*
* @param numerator
* @return Returns this.
*/
public FormDataBuilder leftAt(int numerator) {
return leftAt(numerator, 0);
}
/**
* do the same as FormData data = ... data.left = new
* FormAttachment(numerator, 100, offset);
*
* @param numerator
* @param offset
* @return Returns this.
*/
public FormDataBuilder leftAt(int numerator, int offset) {
this.left = numerator;
this.insetLeft = offset;
return this;
}
/**
* do the same as FormData data = ... data.right = new
* FormAttachment(numerator, 100, 0);
*
* @param numerator
* @return Returns this.
*/
public FormDataBuilder rightAt(int numerator) {
return rightAt(numerator, 0);
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(numerator, 100, offset);
*
* @param numerator
* @param offset
* @return Returns this.
*/
public FormDataBuilder rightAt(int numerator, int offset) {
this.right = numerator;
this.insetRight = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(ctr, 0, SWT.LEFT);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder rightAlignedToTheLeftOf(Control ctr) {
return rightAlignedToTheLeftOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(ctr, offset, SWT.LEFT);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder rightAlignedToTheLeftOf(Control ctr, int offset) {
this.rightAlignedToTheLeftOf = ctr;
this.rightAlignedToTheLeftOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(ctr, 0, SWT.CENTER);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder rightAlignedToTheCenterOf(Control ctr) {
return rightAlignedToTheCenterOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(ctr, offset, SWT.CENTER);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder rightAlignedToTheCenterOf(Control ctr, int offset) {
this.rightAlignedToTheCenterOf = ctr;
this.rightAlignedToTheCenterOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(ctr, 0, SWT.RIGHT);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder rightAlignedToTheRightOf(Control ctr) {
return rightAlignedToTheRightOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.right = new FormAttachment(ctr, offset, SWT.RIGHT);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder rightAlignedToTheRightOf(Control ctr, int offset) {
this.rightAlignedToTheRightOf = ctr;
this.rightAlignedToTheRightOfInset = offset;
return this;
}
//LEFT
/**
* do the same as
* FormData data = ...
* data.left = new FormAttachment(ctr, 0, SWT.LEFT);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder leftAlignedToTheLeftOf(Control ctr) {
return leftAlignedToTheLeftOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.left = new FormAttachment(ctr, offset, SWT.LEFT);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder leftAlignedToTheLeftOf(Control ctr, int offset) {
this.leftAlignedToTheLeftOf = ctr;
this.leftAlignedToTheLeftOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.left = new FormAttachment(ctr, 0, SWT.CENTER);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder leftAlignedToTheCenterOf(Control ctr) {
return leftAlignedToTheCenterOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.left = new FormAttachment(ctr, offset, SWT.CENTER);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder leftAlignedToTheCenterOf(Control ctr, int offset) {
this.leftAlignedToTheCenterOf = ctr;
this.leftAlignedToTheCenterOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.left = new FormAttachment(ctr, 0, SWT.RIGHT);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder leftAlignedToTheRightOf(Control ctr) {
return leftAlignedToTheRightOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.left = new FormAttachment(ctr, offset, SWT.RIGHT);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder leftAlignedToTheRightOf(Control ctr, int offset) {
this.leftAlignedToTheRightOf = ctr;
this.leftAlignedToTheRightOfInset = offset;
return this;
}
//TOP
/**
* do the same as
* FormData data = ...
* data.top = new FormAttachment(ctr, 0, SWT.TOP);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder topAlignedToTheTopOf(Control ctr) {
return topAlignedToTheTopOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.top = new FormAttachment(ctr, offset, SWT.TOP);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder topAlignedToTheTopOf(Control ctr, int offset) {
this.topAlignedToTheTopOf = ctr;
this.topAlignedToTheTopOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.top = new FormAttachment(ctr, 0, SWT.CENTER);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder topAlignedToTheCenterOf(Control ctr) {
return topAlignedToTheCenterOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.top = new FormAttachment(ctr, offset, SWT.CENTER);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder topAlignedToTheCenterOf(Control ctr, int offset) {
this.topAlignedToTheCenterOf = ctr;
this.topAlignedToTheCenterOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.top = new FormAttachment(ctr, 0, SWT.BOTTOM);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder topAlignedToTheBottomOf(Control ctr) {
return topAlignedToTheBottomOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.top = new FormAttachment(ctr, offset, SWT.BOTTOM);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder topAlignedToTheBottomOf(Control ctr, int offset) {
this.topAlignedToTheBottomOf = ctr;
this.topAlignedToTheBottomOfInset = offset;
return this;
}
//BOTTOM
/**
* do the same as
* FormData data = ...
* data.bottom = new FormAttachment(ctr, 0, SWT.TOP);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder bottomAlignedToTheTopOf(Control ctr) {
return bottomAlignedToTheTopOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.bottom = new FormAttachment(ctr, offset, SWT.TOP);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder bottomAlignedToTheTopOf(Control ctr, int offset) {
this.bottomAlignedToTheTopOf = ctr;
this.bottomAlignedToTheTopOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.bottom = new FormAttachment(ctr, 0, SWT.CENTER);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder bottomAlignedToTheCenterOf(Control ctr) {
return bottomAlignedToTheCenterOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.bottom = new FormAttachment(ctr, offset, SWT.CENTER);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder bottomAlignedToTheCenterOf(Control ctr, int offset) {
this.bottomAlignedToTheCenterOf = ctr;
this.bottomAlignedToTheCenterOfInset = offset;
return this;
}
/**
* do the same as
* FormData data = ...
* data.bottom = new FormAttachment(ctr, 0, SWT.BOTTOM);
*
* @param ctr
* @return Returns this.
*/
public FormDataBuilder bottomAlignedToTheBottomOf(Control ctr) {
return bottomAlignedToTheBottomOf(ctr, 0);
}
/**
* do the same as
* FormData data = ...
* data.bottom = new FormAttachment(ctr, offset, SWT.BOTTOM);
*
* @param ctr
* @param offset
* @return Returns this.
*/
public FormDataBuilder bottomAlignedToTheBottomOf(Control ctr, int offset) {
this.bottomAlignedToTheBottomOf = ctr;
this.bottomAlignedToTheBottomOfInset = offset;
return this;
}
}
Now, the simple snippet above can be (safely) written like this:
Label myLabel = ...;
Text myText = ...;
FormData data = null;
//the old version
//data.left = new FormAttachment(myLabel, 5, SWT.RIGHT);
//data.top = new FormAttachment(myLabel, 0, SWT.CENTER);
//here is the version using FormDataBuilder
data = new FormDataBuilder().leftAlignedToTheRightOf(myLabel, 5)
.topAlignedToTheCenterOf(myLabel);
myText.setLayoutData(data);
IMHO, the FormDataBuilder solution is less error-prone.
Thanks, M. Bloch !!!!