Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created April 20, 2015 17:14
Show Gist options
  • Save thomasnield/aa0e33b67ff50672b356 to your computer and use it in GitHub Desktop.
Save thomasnield/aa0e33b67ff50672b356 to your computer and use it in GitHub Desktop.
GridBagBuilder
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.function.Consumer;
/**
* This greatly streamlines using the GridBagLayout
* Instantiate with the targeted container as an argument through the static factory forContainer()
* Then use the "addItem" method to add components. After calling addItem, start chaining the GridBag parameters and then call "finalizeConstraints".
* @author [email protected]
*
*/
public final class GridBagBuilder<T extends Container> {
private final T container;
private final GridBagLayout layout;
private DefaultConstraint defaultConstraint = null;
public DefaultConstraint setDefaultConstraints() {
defaultConstraint = new DefaultConstraint();
return defaultConstraint;
}
private DefaultConstraint getDefaultConstraint() {
return defaultConstraint;
}
public static <T extends Container> GridBagBuilder<T> forContainer(T container) {
return new GridBagBuilder<T>(container);
}
private GridBagBuilder(T container) {
this.container = container;
this.layout = new GridBagLayout();
container.setLayout(this.layout);
}
private void addComponent(Component comp) {
container.add(comp);
}
public <C extends Component> GridItem<C,T> addItem(C comp) {
return new GridItem<C,T>(comp, this);
}
public T buildContainer() {
return container;
}
@SuppressWarnings("serial")
public class DefaultConstraint extends GridBagConstraints {
public DefaultConstraint setFill(FillType fillType) {
this.fill = fillType.getCode();
return this;
}
public DefaultConstraint setIPadX(int padding) {
this.ipadx = padding;
return this;
}
public DefaultConstraint setIPadY(int padding) {
this.ipady = padding;
return this;
}
public DefaultConstraint setInsets(int top, int left, int bottom, int right){
this.insets = new Insets(top,left,bottom,right);
return this;
}
public DefaultConstraint setInsets(int allSidesLength) {
this.insets = new Insets(allSidesLength,allSidesLength,allSidesLength,allSidesLength);
return this;
}
public DefaultConstraint setAnchor(AnchorType anchorType) {
this.anchor = anchorType.getCode();
return this;
}
}
public static final class GridItem<C extends Component, T extends Container> {
private final GridBagBuilder<T> parent;
private final C comp;
private final GridBagConstraints constraint;
private GridItem(C comp, GridBagBuilder<T> parent) {
this.comp = comp;
this.constraint = new GridBagConstraints();
this.parent = parent;
if (parent.getDefaultConstraint() != null) {
GridBagConstraints defaultConstraint = parent.getDefaultConstraint();
this.constraint.fill = defaultConstraint.fill;
this.constraint.insets = defaultConstraint.insets;
this.constraint.anchor = defaultConstraint.anchor;
this.constraint.gridwidth = defaultConstraint.gridwidth;
this.constraint.gridheight = defaultConstraint.gridheight;
this.constraint.gridx = defaultConstraint.gridx;
this.constraint.gridy = defaultConstraint.gridy;
this.constraint.weightx = defaultConstraint.weightx;
this.constraint.weighty = defaultConstraint.weighty;
}
parent.addComponent(comp);
}
public Component getComponent() {
return comp;
}
public GridItem<C,T> setGridX(int x) {
constraint.gridx = x;
return this;
}
public GridItem<C,T> setGridY(int y) {
constraint.gridy = y;
return this;
}
public GridItem<C,T> setGridXY(int x, int y) {
setGridX(x);
setGridY(y);
return this;
}
public GridItem<C,T> setGridWidth(int xWidth) {
constraint.gridwidth = xWidth;
return this;
}
public GridItem<C,T> setGridHeight(int yHeight) {
constraint.gridheight = yHeight;
return this;
}
public GridItem<C,T> setFill(FillType fillType) {
constraint.fill = fillType.getCode();
return this;
}
public GridItem<C,T> setIPadX(int padding) {
constraint.ipadx = padding;
return this;
}
public GridItem<C,T>setIPadY(int padding) {
constraint.ipady = padding;
return this;
}
public GridItem<C,T> setInsets(int top, int left, int bottom, int right) {
constraint.insets = new Insets(top,left,bottom,right);
return this;
}
public GridItem<C,T> setInsetTop(int top) {
constraint.insets = getInset();
constraint.insets.top = top;
return this;
}
public GridItem<C,T> setInsetTopBottom(int value) {
constraint.insets = getInset();
constraint.insets.top = value;
constraint.insets.bottom = value;
return this;
}
public GridItem<C,T> setInsetLeftRight(int value) {
constraint.insets = getInset();
constraint.insets.left = value;
constraint.insets.right = value;
return this;
}
public GridItem<C,T> setInsetBottom(int bottom) {
constraint.insets = getInset();
constraint.insets.bottom = bottom;
return this;
}
public GridItem<C,T> setInsetLeft(int left) {
constraint.insets = getInset();
constraint.insets.left = left;
return this;
}
public GridItem<C,T> setInsetRight(int right) {
constraint.insets = getInset();
constraint.insets.right = right;
return this;
}
private Insets getInset() {
return constraint.insets == null ? new Insets(0,0,0,0) : constraint.insets;
}
public GridItem<C,T> setInsets(int allSidesLength) {
constraint.insets = new Insets(allSidesLength,allSidesLength,allSidesLength,allSidesLength);
return this;
}
public GridItem<C,T> setAnchor(AnchorType anchorType) {
constraint.anchor = anchorType.getCode();
return this;
}
public GridItem<C,T> setWeightX(int x) {
constraint.weightx = x;
return this;
}
public GridItem<C,T> setWeightY(int y) {
constraint.weighty = y;
return this;
}
public GridItem<C,T> setWeightXY(int x, int y) {
setWeightX(x);
setWeightY(y);
return this;
}
/**Perform an operation on the component using a Consumer, handy for adding listeners or formatting*/
public GridItem<C,T> handle(Consumer<C> consumer) {
consumer.accept(comp);
return this;
}
public C build() {
parent.layout.setConstraints(comp, constraint);
return comp;
}
}
public static enum FillType {
NONE(GridBagConstraints.NONE),
HORIZONTAL(GridBagConstraints.HORIZONTAL),
VERTICAL(GridBagConstraints.VERTICAL),
BOTH(GridBagConstraints.BOTH);
private final int code;
public int getCode() { return code; }
private FillType(int code) {
this.code = code;
}
}
public static enum AnchorType {
NORTHWEST(GridBagConstraints.NORTHWEST),
WEST(GridBagConstraints.WEST),
EAST(GridBagConstraints.EAST),
SOUTHEAST(GridBagConstraints.SOUTHEAST),
NORTH(GridBagConstraints.NORTH),
CENTER(GridBagConstraints.CENTER),
SOUTH(GridBagConstraints.SOUTH),
NORTHEAST(GridBagConstraints.NORTHEAST),
SOUTHWEST(GridBagConstraints.SOUTHWEST);
private final int code;
public int getCode() {return code; }
private AnchorType(int code) {
this.code = code;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment