PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/util/ConstraintFactory.java

#
Java | 199 lines | 123 code | 20 blank | 56 comment | 8 complexity | 402e8e856fd6c81c6ace01fb3d9e2f94 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package common.gui.util;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. /**
  5. * Provides easier creation of GridBagConstraints
  6. *
  7. * @author mace
  8. * @created October 22, 2002
  9. */
  10. public class ConstraintFactory {
  11. public final static int NONE = GridBagConstraints.NONE;
  12. public final static int BOTH = GridBagConstraints.BOTH;
  13. public final static int HORIZONTAL = GridBagConstraints.HORIZONTAL;
  14. public final static int H = HORIZONTAL;
  15. public final static int VERTICAL = GridBagConstraints.VERTICAL;
  16. public final static int V = VERTICAL;
  17. public final static int RELATIVE = GridBagConstraints.RELATIVE;
  18. public final static int REMAINDER = GridBagConstraints.REMAINDER;
  19. public final static int CENTER = GridBagConstraints.CENTER;
  20. public final static int N = GridBagConstraints.NORTH;
  21. public final static int E = GridBagConstraints.EAST;
  22. public final static int S = GridBagConstraints.SOUTH;
  23. public final static int W = GridBagConstraints.WEST;
  24. public final static int NW = GridBagConstraints.NORTHWEST;
  25. public final static int NE = GridBagConstraints.NORTHEAST;
  26. public final static int SW = GridBagConstraints.SOUTHWEST;
  27. public final static int SE = GridBagConstraints.SOUTHEAST;
  28. protected int DEFAULT_WEIGHTX = 100;
  29. protected int DEFAULT_WEIGHTY = 100;
  30. protected int DEFAULT_ANCHOR = NW;
  31. protected int DEFAULT_FILL = BOTH;
  32. protected Insets DEFAULT_INSETS = new Insets(0, 0, 0, 0);
  33. protected int DEFAULT_IPADX = 0;
  34. protected int DEFAULT_IPADY = 0;
  35. protected int LAST_X = 0;
  36. protected int LAST_Y = 0;
  37. protected int LAST_WIDTH = 0;
  38. protected int LAST_HEIGHT = 0;
  39. public ConstraintFactory() { }
  40. private GridBagConstraints buildConstraints() {
  41. GridBagConstraints constraints = new GridBagConstraints();
  42. constraints.weightx = DEFAULT_WEIGHTX;
  43. constraints.weighty = DEFAULT_WEIGHTY;
  44. constraints.anchor = DEFAULT_ANCHOR;
  45. constraints.fill = DEFAULT_FILL;
  46. constraints.insets = DEFAULT_INSETS;
  47. constraints.ipadx = DEFAULT_IPADX;
  48. constraints.ipady = DEFAULT_IPADY;
  49. return constraints;
  50. }
  51. /**
  52. * Build basic constraints. weights default to 100, anchor defaults to NW, fill
  53. * defaults to BOTH. If a negative value is given, the last non-negative value
  54. * will be re-used
  55. *
  56. * @param x an int specifying the object's x grid position
  57. * @param y an int specifying the object's y grid position
  58. * @param width an int specifying the object's width
  59. * @param height an int specifying the object's height
  60. * @return a GridBagConstraints object
  61. */
  62. public GridBagConstraints buildConstraints
  63. (int x, int y, int width, int height) {
  64. GridBagConstraints constraints = buildConstraints();
  65. if (x < 0) {
  66. constraints.gridx = LAST_X;
  67. } else {
  68. constraints.gridx = LAST_X = x;
  69. }
  70. if (y < 0) {
  71. constraints.gridx = LAST_Y;
  72. } else {
  73. constraints.gridy = LAST_Y = y;
  74. }
  75. if (width < 0) {
  76. constraints.gridwidth = LAST_WIDTH;
  77. } else {
  78. constraints.gridwidth = LAST_WIDTH = width;
  79. }
  80. if (height < 0) {
  81. constraints.gridheight = LAST_HEIGHT;
  82. } else {
  83. constraints.gridheight = LAST_HEIGHT = height;
  84. }
  85. return constraints;
  86. }
  87. /**
  88. * Builds basic constraints + anchor and fill constraints. Weights default to
  89. * 100
  90. *
  91. * @param x an int specifying the object's x grid position
  92. * @param y an int specifying the object's y grid position
  93. * @param width an int specifying the object's width
  94. * @param height an int specifying the object's height
  95. * @param anchor an int specifying the position to place the object
  96. * @param fill an int specifying which directions to fill
  97. * @return a GridBagConstraints object
  98. */
  99. public GridBagConstraints buildConstraints
  100. (int x, int y, int width, int height, int anchor, int fill) {
  101. GridBagConstraints constraints = buildConstraints(x, y, width, height);
  102. constraints.anchor = anchor;
  103. constraints.fill = fill;
  104. return constraints;
  105. }
  106. /**
  107. * Builds basic constraints + anchor,fill, and weights.
  108. *
  109. * @param x an int specifying the object's x grid position
  110. * @param y an int specifying the object's y grid position
  111. * @param width an int specifying the object's width
  112. * @param height an int specifying the object's height
  113. * @param anchor an int specifying the position to place the object
  114. * @param fill an int specifying which directions to fill
  115. * @param xweight an int specifying the object's x weight
  116. * @param yweight an int specifying the object's y weight
  117. * @return a GridBagConstraints object
  118. */
  119. public GridBagConstraints buildConstraints
  120. (int x, int y, int width, int height, int anchor, int fill, int xweight,
  121. int yweight) {
  122. GridBagConstraints constraints
  123. = buildConstraints(x, y, width, height, anchor, fill);
  124. constraints.weightx = xweight;
  125. constraints.weighty = yweight;
  126. return constraints;
  127. }
  128. /**
  129. * Builds basic constraints + anchor,fill, weights, and insets.
  130. *
  131. * @param x an int specifying the object's x grid position
  132. * @param y an int specifying the object's y grid position
  133. * @param width an int specifying the object's width
  134. * @param height an int specifying the object's height
  135. * @param anchor an int specifying the position to place the object
  136. * @param fill an int specifying which directions to fill
  137. * @param weightx an int specifying the object's x weight
  138. * @param weighty an int specifying the object's y weight
  139. * @param i <TT>Insets</TT> to use for this object
  140. * @return a GridBagConstraints object
  141. */
  142. public GridBagConstraints buildConstraints
  143. (int x, int y, int width, int height, int anchor, int fill, int weightx,
  144. int weighty, Insets i) {
  145. GridBagConstraints constraints
  146. = buildConstraints(x, y, width, height, anchor, fill, weightx, weighty);
  147. constraints.insets = i;
  148. return constraints;
  149. }
  150. public void setDefaultAnchor(int i) {
  151. DEFAULT_ANCHOR = i;
  152. }
  153. public void setDefaultFill(int i) {
  154. DEFAULT_FILL = i;
  155. }
  156. public void setDefaultWeights(int x, int y) {
  157. setDefaultWeightX(x);
  158. setDefaultWeightY(y);
  159. }
  160. public void setDefaultWeightX(int i) {
  161. DEFAULT_WEIGHTX = i;
  162. }
  163. public void setDefaultWeightY(int i) {
  164. DEFAULT_WEIGHTY = i;
  165. }
  166. public void setDefaultInsets(Insets i) {
  167. DEFAULT_INSETS = i;
  168. }
  169. public void setDefaultInternalPaddingX(int i) {
  170. DEFAULT_IPADX = i;
  171. }
  172. public void setDefaultInternalPaddingY(int i) {
  173. DEFAULT_IPADY = i;
  174. }
  175. public void setDefaultInternalPadding(int i) {
  176. setDefaultInternalPaddingX(i);
  177. setDefaultInternalPaddingY(i);
  178. }
  179. }