Advertisement
Guest User

Untitled

a guest
May 29th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. import java.awt.Component;
  2. import java.awt.Container;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.util.function.Consumer;
  7. /**
  8. * This greatly streamlines using the GridBagLayout
  9. * Instantiate with the targeted container as an argument through the static factory forContainer()
  10. * Then use the "addItem" method to add components. After calling addItem, start chaining the GridBag parameters and then call "finalizeConstraints".
  11. * @author thomasnield@live.com
  12. *
  13. */
  14. public final class GridBagBuilder<T extends Container> {
  15.  
  16. private final T container;
  17. private final GridBagLayout layout;
  18. private DefaultConstraint defaultConstraint = null;
  19.  
  20. public DefaultConstraint setDefaultConstraints() {
  21. defaultConstraint = new DefaultConstraint();
  22. return defaultConstraint;
  23. }
  24.  
  25. private DefaultConstraint getDefaultConstraint() {
  26. return defaultConstraint;
  27. }
  28.  
  29. public static <T extends Container> GridBagBuilder<T> forContainer(T container) {
  30. return new GridBagBuilder<T>(container);
  31. }
  32. private GridBagBuilder(T container) {
  33. this.container = container;
  34. this.layout = new GridBagLayout();
  35. container.setLayout(this.layout);
  36. }
  37.  
  38. private void addComponent(Component comp) {
  39. container.add(comp);
  40. }
  41.  
  42. public <C extends Component> GridItem<C,T> addItem(C comp) {
  43. return new GridItem<C,T>(comp, this);
  44. }
  45.  
  46. public T buildContainer() {
  47. return container;
  48. }
  49.  
  50.  
  51. @SuppressWarnings("serial")
  52. public class DefaultConstraint extends GridBagConstraints {
  53. public DefaultConstraint setFill(FillType fillType) {
  54. this.fill = fillType.getCode();
  55. return this;
  56. }
  57. public DefaultConstraint setIPadX(int padding) {
  58. this.ipadx = padding;
  59. return this;
  60. }
  61. public DefaultConstraint setIPadY(int padding) {
  62. this.ipady = padding;
  63. return this;
  64. }
  65.  
  66. public DefaultConstraint setInsets(int top, int left, int bottom, int right){
  67. this.insets = new Insets(top,left,bottom,right);
  68. return this;
  69. }
  70. public DefaultConstraint setInsets(int allSidesLength) {
  71. this.insets = new Insets(allSidesLength,allSidesLength,allSidesLength,allSidesLength);
  72. return this;
  73. }
  74. public DefaultConstraint setAnchor(AnchorType anchorType) {
  75. this.anchor = anchorType.getCode();
  76. return this;
  77. }
  78. }
  79.  
  80. public static final class GridItem<C extends Component, T extends Container> {
  81. private final GridBagBuilder<T> parent;
  82. private final C comp;
  83. private final GridBagConstraints constraint;
  84.  
  85. private GridItem(C comp, GridBagBuilder<T> parent) {
  86. this.comp = comp;
  87. this.constraint = new GridBagConstraints();
  88. this.parent = parent;
  89.  
  90. if (parent.getDefaultConstraint() != null) {
  91. GridBagConstraints defaultConstraint = parent.getDefaultConstraint();
  92.  
  93. this.constraint.fill = defaultConstraint.fill;
  94. this.constraint.insets = defaultConstraint.insets;
  95. this.constraint.anchor = defaultConstraint.anchor;
  96. this.constraint.gridwidth = defaultConstraint.gridwidth;
  97. this.constraint.gridheight = defaultConstraint.gridheight;
  98. this.constraint.gridx = defaultConstraint.gridx;
  99. this.constraint.gridy = defaultConstraint.gridy;
  100. this.constraint.weightx = defaultConstraint.weightx;
  101. this.constraint.weighty = defaultConstraint.weighty;
  102.  
  103. }
  104.  
  105. parent.addComponent(comp);
  106. }
  107.  
  108. public Component getComponent() {
  109. return comp;
  110. }
  111.  
  112.  
  113. public GridItem<C,T> setGridX(int x) {
  114. constraint.gridx = x;
  115. return this;
  116. }
  117. public GridItem<C,T> setGridY(int y) {
  118. constraint.gridy = y;
  119. return this;
  120. }
  121. public GridItem<C,T> setGridXY(int x, int y) {
  122. setGridX(x);
  123. setGridY(y);
  124. return this;
  125. }
  126.  
  127. public GridItem<C,T> setGridWidth(int xWidth) {
  128. constraint.gridwidth = xWidth;
  129. return this;
  130. }
  131. public GridItem<C,T> setGridHeight(int yHeight) {
  132. constraint.gridheight = yHeight;
  133. return this;
  134. }
  135.  
  136. public GridItem<C,T> setFill(FillType fillType) {
  137. constraint.fill = fillType.getCode();
  138. return this;
  139. }
  140.  
  141. public GridItem<C,T> setIPadX(int padding) {
  142. constraint.ipadx = padding;
  143. return this;
  144. }
  145.  
  146. public GridItem<C,T>setIPadY(int padding) {
  147. constraint.ipady = padding;
  148. return this;
  149. }
  150.  
  151. public GridItem<C,T> setInsets(int top, int left, int bottom, int right) {
  152. constraint.insets = new Insets(top,left,bottom,right);
  153. return this;
  154. }
  155. public GridItem<C,T> setInsetTop(int top) {
  156. constraint.insets = getInset();
  157. constraint.insets.top = top;
  158. return this;
  159. }
  160. public GridItem<C,T> setInsetTopBottom(int value) {
  161. constraint.insets = getInset();
  162. constraint.insets.top = value;
  163. constraint.insets.bottom = value;
  164. return this;
  165. }
  166. public GridItem<C,T> setInsetLeftRight(int value) {
  167. constraint.insets = getInset();
  168. constraint.insets.left = value;
  169. constraint.insets.right = value;
  170. return this;
  171. }
  172. public GridItem<C,T> setInsetBottom(int bottom) {
  173. constraint.insets = getInset();
  174. constraint.insets.bottom = bottom;
  175. return this;
  176. }
  177. public GridItem<C,T> setInsetLeft(int left) {
  178. constraint.insets = getInset();
  179. constraint.insets.left = left;
  180. return this;
  181. }
  182. public GridItem<C,T> setInsetRight(int right) {
  183. constraint.insets = getInset();
  184. constraint.insets.right = right;
  185. return this;
  186. }
  187. private Insets getInset() {
  188. return constraint.insets == null ? new Insets(0,0,0,0) : constraint.insets;
  189. }
  190. public GridItem<C,T> setInsets(int allSidesLength) {
  191. constraint.insets = new Insets(allSidesLength,allSidesLength,allSidesLength,allSidesLength);
  192. return this;
  193. }
  194. public GridItem<C,T> setAnchor(AnchorType anchorType) {
  195. constraint.anchor = anchorType.getCode();
  196. return this;
  197. }
  198.  
  199. public GridItem<C,T> setWeightX(int x) {
  200. constraint.weightx = x;
  201. return this;
  202. }
  203.  
  204. public GridItem<C,T> setWeightY(int y) {
  205. constraint.weighty = y;
  206. return this;
  207. }
  208.  
  209. public GridItem<C,T> setWeightXY(int x, int y) {
  210. setWeightX(x);
  211. setWeightY(y);
  212. return this;
  213. }
  214. /**Perform an operation on the component using a Consumer, handy for adding listeners or formatting*/
  215. public GridItem<C,T> handle(Consumer<C> consumer) {
  216. consumer.accept(comp);
  217. return this;
  218. }
  219. public C build() {
  220. parent.layout.setConstraints(comp, constraint);
  221. return comp;
  222. }
  223.  
  224. }
  225.  
  226. public static enum FillType {
  227. NONE(GridBagConstraints.NONE),
  228. HORIZONTAL(GridBagConstraints.HORIZONTAL),
  229. VERTICAL(GridBagConstraints.VERTICAL),
  230. BOTH(GridBagConstraints.BOTH);
  231.  
  232. private final int code;
  233. public int getCode() { return code; }
  234.  
  235. private FillType(int code) {
  236. this.code = code;
  237. }
  238. }
  239.  
  240. public static enum AnchorType {
  241. NORTHWEST(GridBagConstraints.NORTHWEST),
  242. WEST(GridBagConstraints.WEST),
  243. EAST(GridBagConstraints.EAST),
  244. SOUTHEAST(GridBagConstraints.SOUTHEAST),
  245. NORTH(GridBagConstraints.NORTH),
  246. CENTER(GridBagConstraints.CENTER),
  247. SOUTH(GridBagConstraints.SOUTH),
  248. NORTHEAST(GridBagConstraints.NORTHEAST),
  249. SOUTHWEST(GridBagConstraints.SOUTHWEST);
  250.  
  251. private final int code;
  252. public int getCode() {return code; }
  253.  
  254. private AnchorType(int code) {
  255. this.code = code;
  256. }
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement