Created
March 30, 2014 05:15
-
-
Save fappel/9867945 to your computer and use it in GitHub Desktop.
A utility class and to ease SWT FormData handling (Dependencies: SWT & JUnit + DisplayHelper gist for test).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.layout.FormAttachment; | |
import org.eclipse.swt.layout.FormData; | |
import org.eclipse.swt.widgets.Control; | |
public class FormDatas { | |
static final int DENOMINATOR = 100; | |
private final FormData formData; | |
public static FormDatas attach( Control control ) { | |
return new FormDatas( control ); | |
} | |
public FormDatas atLeft() { | |
return atLeft( 0 ); | |
} | |
public FormDatas atLeft( int margin ) { | |
formData.left = new FormAttachment( 0, margin ); | |
return this; | |
} | |
public FormDatas atLeftTo( Control control ) { | |
return atLeftTo( control, 0 ); | |
} | |
public FormDatas atLeftTo( Control control, int margin ) { | |
return atLeftTo( control, margin, SWT.DEFAULT ); | |
} | |
public FormDatas atLeftTo( Control control, int margin, int alignment ) { | |
formData.left = new FormAttachment( control, margin, alignment ); | |
return this; | |
} | |
public FormDatas fromLeft( int numerator ) { | |
return fromLeft( numerator, 0 ); | |
} | |
public FormDatas fromLeft( int numerator, int margin ) { | |
formData.left = new FormAttachment( numerator, margin ); | |
return this; | |
} | |
public FormDatas atRight() { | |
return atRight( 0 ); | |
} | |
public FormDatas atRight( int margin ) { | |
formData.right = new FormAttachment( DENOMINATOR, -margin ); | |
return this; | |
} | |
public FormDatas atRightTo( Control control ) { | |
atRightTo( control, 0 ); | |
return this; | |
} | |
public FormDatas atRightTo( Control control, int margin ) { | |
return atRightTo( control, margin, SWT.DEFAULT ); | |
} | |
public FormDatas atRightTo( Control control, int margin, int alignment ) { | |
formData.right = new FormAttachment( control, -margin, alignment ); | |
return this; | |
} | |
public FormDatas fromRight( int numerator ) { | |
return fromRight( numerator, 0 ); | |
} | |
public FormDatas fromRight( int numerator, int margin ) { | |
formData.right = new FormAttachment( DENOMINATOR - numerator, -margin ); | |
return this; | |
} | |
public FormDatas atTop() { | |
return atTop( 0 ); | |
} | |
public FormDatas atTop( int margin ) { | |
formData.top = new FormAttachment( 0, margin ); | |
return this; | |
} | |
public FormDatas atTopTo( Control control ) { | |
return atTopTo( control, 0 ); | |
} | |
public FormDatas atTopTo( Control control, int margin ) { | |
return atTopTo( control, margin, SWT.DEFAULT ); | |
} | |
public FormDatas atTopTo( Control control, int margin, int alignment ) { | |
formData.top = new FormAttachment( control, margin, alignment ); | |
return this; | |
} | |
public FormDatas fromTop( int numerator ) { | |
return fromTop( numerator, 0 ); | |
} | |
public FormDatas fromTop( int numerator, int margin ) { | |
formData.top = new FormAttachment( numerator, margin ); | |
return this; | |
} | |
public FormDatas atBottom() { | |
return atBottom( 0 ); | |
} | |
public FormDatas atBottom( int margin ) { | |
formData.bottom = new FormAttachment( DENOMINATOR, -margin ); | |
return this; | |
} | |
public FormDatas atBottomTo( Control control ) { | |
return atBottomTo( control, 0 ); | |
} | |
public FormDatas atBottomTo( Control control, int margin ) { | |
return atBottomTo( control, margin, SWT.DEFAULT ); | |
} | |
public FormDatas atBottomTo( Control control, int margin, int alignment ) { | |
formData.bottom = new FormAttachment( control, -margin, alignment ); | |
return this; | |
} | |
public FormDatas fromBottom( int numerator ) { | |
return fromBottom( numerator, 0 ); | |
} | |
public FormDatas fromBottom( int numerator, int margin ) { | |
formData.bottom = new FormAttachment( DENOMINATOR - numerator, -margin ); | |
return this; | |
} | |
public FormDatas withWidth( int width ) { | |
formData.width = width; | |
return this; | |
} | |
public FormDatas withHeight( int height ) { | |
formData.height = height; | |
return this; | |
} | |
public FormData getFormData() { | |
return formData; | |
} | |
private FormDatas( Control control ) { | |
formData = new FormData(); | |
control.setLayoutData( formData ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static com.codeaffine.gitplus.ui.internal.util.FormDatas.DENOMINATOR; | |
import static com.codeaffine.gitplus.ui.internal.util.FormDatas.attach; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNull; | |
import static org.junit.Assert.assertSame; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.layout.FormAttachment; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Control; | |
import org.eclipse.swt.widgets.Label; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import com.codeaffine.gitplus.ui.test.DisplayHelper; | |
public class FormDatasTest { | |
private static final int NUMERATOR = 10; | |
private static final int MARGIN = 20; | |
private static final int WIDTH = 300; | |
private static final int HEIGHT = 200; | |
@Rule | |
public final DisplayHelper displayHelper = new DisplayHelper(); | |
private Control otherControl; | |
private Composite parent; | |
private Control control; | |
@Test | |
public void testAttach() { | |
FormDatas actual = attach( control ); | |
assertSame( control.getLayoutData(), actual.getFormData() ); | |
} | |
@Test | |
public void testAtLeft() { | |
FormDatas actual = attach( control ).atLeft(); | |
verifyAttachmentValues( 0, 0, actual.getFormData().left ); | |
} | |
@Test | |
public void testAtLeftWithMargin() { | |
FormDatas actual = attach( control ).atLeft( MARGIN ); | |
verifyAttachmentValues( 0, MARGIN, actual.getFormData().left ); | |
} | |
@Test | |
public void testAtRight() { | |
FormDatas actual = attach( control ).atRight(); | |
verifyAttachmentValues( DENOMINATOR, 0, actual.getFormData().right ); | |
} | |
@Test | |
public void testAtRightWithMargin() { | |
FormDatas actual = attach( control ).atRight( MARGIN ); | |
verifyAttachmentValues( DENOMINATOR, -MARGIN, actual.getFormData().right ); | |
} | |
@Test | |
public void testAtTop() { | |
FormDatas actual = attach( control ).atTop(); | |
verifyAttachmentValues( 0, 0, actual.getFormData().top ); | |
} | |
@Test | |
public void testAtTopWithMargin() { | |
FormDatas actual = attach( control ).atTop( MARGIN ); | |
verifyAttachmentValues( 0, MARGIN, actual.getFormData().top ); | |
} | |
@Test | |
public void testAtBottom() { | |
FormDatas actual = attach( control ).atBottom(); | |
verifyAttachmentValues( DENOMINATOR, 0, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testAtBottomWithMargin() { | |
FormDatas actual = attach( control ).atBottom( MARGIN ); | |
verifyAttachmentValues( DENOMINATOR, -MARGIN, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testAtLeftTo() { | |
FormDatas actual = attach( control ).atLeftTo( otherControl ); | |
verifyAttachmentValues( otherControl, 0, SWT.DEFAULT, actual.getFormData().left ); | |
} | |
@Test | |
public void testAtLeftToWithMargin() { | |
int margin = 10; | |
FormDatas actual = attach( control ).atLeftTo( otherControl, margin ); | |
verifyAttachmentValues( otherControl, margin, SWT.DEFAULT, actual.getFormData().left ); | |
} | |
@Test | |
public void testAtLeftToWithAligment() { | |
FormDatas actual = attach( control ).atLeftTo( otherControl, 0, SWT.CENTER ); | |
verifyAttachmentValues( otherControl, 0, SWT.CENTER, actual.getFormData().left ); | |
} | |
@Test | |
public void testAtRightTo() { | |
FormDatas actual = attach( control ).atRightTo( otherControl ); | |
verifyAttachmentValues( otherControl, 0, SWT.DEFAULT, actual.getFormData().right ); | |
} | |
@Test | |
public void testAtRightToWithMargin() { | |
int margin = 10; | |
FormDatas actual = attach( control ).atRightTo( otherControl, margin ); | |
verifyAttachmentValues( otherControl, -margin, SWT.DEFAULT, actual.getFormData().right ); | |
} | |
@Test | |
public void testAtRightToWithAligment() { | |
FormDatas actual = attach( control ).atRightTo( otherControl, 0, SWT.CENTER ); | |
verifyAttachmentValues( otherControl, 0, SWT.CENTER, actual.getFormData().right ); | |
} | |
@Test | |
public void testAtTopTo() { | |
FormDatas actual = attach( control ).atTopTo( otherControl ); | |
verifyAttachmentValues( otherControl, 0, SWT.DEFAULT, actual.getFormData().top ); | |
} | |
@Test | |
public void testAtTopToWithMargin() { | |
int margin = 10; | |
FormDatas actual = attach( control ).atTopTo( otherControl, margin ); | |
verifyAttachmentValues( otherControl, margin, SWT.DEFAULT, actual.getFormData().top ); | |
} | |
@Test | |
public void testAtTopToWithAligment() { | |
FormDatas actual = attach( control ).atTopTo( otherControl, 0, SWT.CENTER ); | |
verifyAttachmentValues( otherControl, 0, SWT.CENTER, actual.getFormData().top ); | |
} | |
@Test | |
public void testAtBottomTo() { | |
FormDatas actual = attach( control ).atBottomTo( otherControl ); | |
verifyAttachmentValues( otherControl, 0, SWT.DEFAULT, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testAtBottomToWithMargin() { | |
int margin = 10; | |
FormDatas actual = attach( control ).atBottomTo( otherControl, margin ); | |
verifyAttachmentValues( otherControl, -margin, SWT.DEFAULT, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testAtBottomToWithAligment() { | |
FormDatas actual = attach( control ).atBottomTo( otherControl, 0, SWT.CENTER ); | |
verifyAttachmentValues( otherControl, 0, SWT.CENTER, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testFromLeft() { | |
int numerator = 10; | |
FormDatas actual = attach( control ).fromLeft( numerator ); | |
verifyAttachmentValues( numerator, 0, actual.getFormData().left ); | |
} | |
@Test | |
public void testFromLeftWithMargin() { | |
FormDatas actual = attach( control ).fromLeft( NUMERATOR, MARGIN ); | |
verifyAttachmentValues( NUMERATOR, MARGIN, actual.getFormData().left ); | |
} | |
@Test | |
public void testFromRight() { | |
FormDatas actual = attach( control ).fromRight( NUMERATOR ); | |
verifyAttachmentValues( DENOMINATOR - NUMERATOR, 0, actual.getFormData().right ); | |
} | |
@Test | |
public void testFromRightWithMargin() { | |
FormDatas actual = attach( control ).fromRight( NUMERATOR, MARGIN ); | |
verifyAttachmentValues( DENOMINATOR - NUMERATOR, -MARGIN, actual.getFormData().right ); | |
} | |
@Test | |
public void testFromTop() { | |
FormDatas actual = attach( control ).fromTop( NUMERATOR ); | |
verifyAttachmentValues( NUMERATOR, 0, actual.getFormData().top ); | |
} | |
@Test | |
public void testFromTopWithMargin() { | |
FormDatas actual = attach( control ).fromTop( NUMERATOR, MARGIN ); | |
verifyAttachmentValues( NUMERATOR, MARGIN, actual.getFormData().top ); | |
} | |
@Test | |
public void testFromBottom() { | |
FormDatas actual = attach( control ).fromBottom( NUMERATOR ); | |
verifyAttachmentValues( DENOMINATOR - NUMERATOR, 0, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testFromBottomWithMargin() { | |
FormDatas actual = attach( control ).fromBottom( NUMERATOR, MARGIN ); | |
verifyAttachmentValues( DENOMINATOR - NUMERATOR, -MARGIN, actual.getFormData().bottom ); | |
} | |
@Test | |
public void testWithWidth() { | |
FormDatas actual = attach( control ).withWidth( WIDTH ); | |
assertEquals( WIDTH, actual.getFormData().width ); | |
} | |
@Test | |
public void testWithHeight() { | |
FormDatas actual = attach( control ).withHeight( HEIGHT ); | |
assertEquals( HEIGHT, actual.getFormData().height ); | |
} | |
@Before | |
public void setUp() { | |
parent = displayHelper.createShell(); | |
control = new Label( parent, SWT.NONE ); | |
otherControl = new Label( parent, SWT.NONE ); | |
} | |
private static void verifyAttachmentValues( | |
Control control, int margin, int alignment, FormAttachment actual ) | |
{ | |
assertSame( control, actual.control ); | |
assertEquals( margin, actual.offset ); | |
assertEquals( alignment, actual.alignment ); | |
assertEquals( 0, actual.numerator ); | |
} | |
private static void verifyAttachmentValues( int numerator, int margin, FormAttachment actual ) { | |
assertEquals( numerator, actual.numerator ); | |
assertEquals( margin, actual.offset ); | |
assertEquals( 0, actual.alignment ); | |
assertNull( actual.control ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment