Guest User

Untitled

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