Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An efficient way to deal with SWT GridLayout and GridData configurations. Allows you to apply SWT GridLayout and GridData to Composites and Controls in a very compact form, while being more powerful at the same time. To apply a GridLayout and configure it to your use case the following expression can be used: GridLayoutUtil.applyGridLayout( comp…
package com.eclipsesource.swt.util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Control;
public class GridDataUtil {
private final GridData gridData;
private GridDataUtil( GridData gridData ) {
this.gridData = gridData;
}
public static GridDataUtil applyGridData( Control control ) {
GridData gridData = new GridData();
control.setLayoutData( gridData );
return new GridDataUtil( gridData );
}
public static GridDataUtil onGridData( Control control ) {
Object layoutData = control.getLayoutData();
if( layoutData instanceof GridData ) {
return new GridDataUtil( ( GridData )layoutData );
}
throw new IllegalStateException( "Control must have GridData layout data. Has " + layoutData );
}
public GridDataUtil withHorizontalFill() {
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.CENTER;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = false;
return this;
}
public GridDataUtil withVerticalFill() {
gridData.horizontalAlignment = SWT.TOP;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = false;
gridData.grabExcessVerticalSpace = true;
return this;
}
public GridDataUtil withFill() {
gridData.horizontalAlignment = SWT.FILL;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
return this;
}
public GridDataUtil withCenterCollapse() {
gridData.horizontalAlignment = SWT.CENTER;
gridData.verticalAlignment = SWT.CENTER;
gridData.grabExcessHorizontalSpace = false;
gridData.grabExcessVerticalSpace = false;
return this;
}
public GridDataUtil grabExcessHorizontalSpace( boolean grabExcessHorizontalSpace ) {
gridData.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
return this;
}
public GridDataUtil grabExcessVerticalSpace( boolean grabExcessVerticalSpace ) {
gridData.grabExcessVerticalSpace = grabExcessVerticalSpace;
return this;
}
public GridDataUtil horizontalSpan( int horizontalSpan ) {
gridData.horizontalSpan = horizontalSpan;
return this;
}
public GridDataUtil verticalSpan( int verticalSpan ) {
gridData.verticalSpan = verticalSpan;
return this;
}
public GridDataUtil minimumHeight( int minimumHeight ) {
gridData.minimumHeight = minimumHeight;
return this;
}
public GridDataUtil minimumWidth( int minimumWidth ) {
gridData.minimumWidth = minimumWidth;
return this;
}
public GridDataUtil verticalIndent( int verticalIndent ) {
gridData.verticalIndent = verticalIndent;
return this;
}
public GridDataUtil horizontalIndent( int horizontalIndent ) {
gridData.horizontalIndent = horizontalIndent;
return this;
}
public GridDataUtil heightHint( int heightHint ) {
gridData.heightHint = heightHint;
return this;
}
public GridDataUtil widthHint( int widthHint ) {
gridData.widthHint = widthHint;
return this;
}
public GridDataUtil verticalAlignment( int verticalAlignment ) {
gridData.verticalAlignment = verticalAlignment;
return this;
}
public GridDataUtil horizontalAlignment( int horizontalAlignment ) {
gridData.horizontalAlignment = horizontalAlignment;
return this;
}
public GridDataUtil exclude( boolean exclude ) {
gridData.exclude = exclude;
return this;
}
}
package com.eclipsesource.swt.util;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Layout;
public class GridLayoutUtil {
private final GridLayout gridLayout;
private GridLayoutUtil( GridLayout gridLayout ) {
this.gridLayout = gridLayout;
}
public static GridLayoutUtil applyGridLayout( Composite composite ) {
GridLayout gridLayout = new GridLayout();
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
gridLayout.verticalSpacing = 0;
gridLayout.horizontalSpacing = 0;
composite.setLayout( gridLayout );
return new GridLayoutUtil( gridLayout );
}
public static GridLayoutUtil onGridLayout( Composite composite ) {
Layout layout = composite.getLayout();
if( layout instanceof GridLayout ) {
return new GridLayoutUtil( ( GridLayout )layout );
}
throw new IllegalStateException( "Composite has to have a GridLayout. Has " + layout );
}
public GridLayoutUtil numColumns( int numColumns ) {
gridLayout.numColumns = numColumns;
return this;
}
public GridLayoutUtil columnsEqualWidth( boolean columnsEqualWidth ) {
gridLayout.makeColumnsEqualWidth = columnsEqualWidth;
return this;
}
public GridLayoutUtil horizontalSpacing( int horizontalSpacing ) {
gridLayout.horizontalSpacing = horizontalSpacing;
return this;
}
public GridLayoutUtil verticalSpacing( int verticalSpacing ) {
gridLayout.verticalSpacing = verticalSpacing;
return this;
}
public GridLayoutUtil marginWidth( int marginWidth ) {
gridLayout.marginWidth = marginWidth;
return this;
}
public GridLayoutUtil marginHeight( int marginHeight ) {
gridLayout.marginHeight = marginHeight;
return this;
}
public GridLayoutUtil marginTop( int marginTop ) {
gridLayout.marginTop = marginTop;
return this;
}
public GridLayoutUtil marginBottom( int marginBottom ) {
gridLayout.marginBottom = marginBottom;
return this;
}
public GridLayoutUtil marginLeft( int marginLeft ) {
gridLayout.marginLeft = marginLeft;
return this;
}
public GridLayoutUtil marginRight( int marginRight ) {
gridLayout.marginRight = marginRight;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment