View Javadoc

1   /*
2    * ProportionConstraints.java
3    *
4    * Created on 4 december 2002, 17:56
5    */
6   
7   package net.sourceforge.simplegamenet.util.proportionlayout;
8   
9   import java.awt.*;
10  
11  /***
12   * The <code>ProportionConstraints</code> class specifies constraints for components that are laid
13   * out using the <code>ProportionLayout</code> class.
14   *
15   * @author Geoffrey and Jeroen
16   */
17  public class ProportionConstraints implements java.io.Serializable {
18  
19      /***
20       * Do not resize the component.
21       */
22      public static final int NONE = 0;
23  
24      /***
25       * Resize the component both horizontally and vertically.
26       */
27      public static final int BOTH = 1;
28  
29      /***
30       * Resize the component horizontally but not vertically.
31       */
32      public static final int HORIZONTAL = 2;
33  
34      /***
35       * Resize the component vertically but not horizontally.
36       */
37      public static final int VERTICAL = 3;
38  
39      /***
40       * Put the component in the center of its display area.
41       */
42      public static final int CENTER = 10;
43  
44      /***
45       * Put the component at the top of its display area, centered horizontally.
46       */
47      public static final int NORTH = 11;
48  
49      /***
50       * Put the component at the top-right corner of its display area.
51       */
52      public static final int NORTH_EAST = 12;
53  
54      /***
55       * Put the component on the right side of its display area, centered vertically.
56       */
57      public static final int EAST = 13;
58  
59      /***
60       * Put the component at the bottom-right corner of its display area.
61       */
62      public static final int SOUTH_EAST = 14;
63  
64      /***
65       * Put the component at the bottom of its display area, centered horizontally.
66       */
67      public static final int SOUTH = 15;
68  
69      /***
70       * Put the component at the bottom-left corner of its display area.
71       */
72      public static final int SOUTH_WEST = 16;
73  
74      /***
75       * Put the component on the left side of its display area, centered vertically.
76       */
77      public static final int WEST = 17;
78  
79      /***
80       * Put the component at the top-left corner of its display area.
81       */
82      public static final int NORTH_WEST = 18;
83  
84      private int gridX;
85      private int gridY;
86  
87      private int gridWidth;
88      private int gridHeight;
89  
90      private int extendGridX;
91      private int extendGridY;
92  
93      private int anchor;
94      private int fill;
95  
96      private int insetNorth;
97      private int insetEast;
98      private int insetSouth;
99      private int insetWest;
100 
101     private int iPadX;
102     private int iPadY;
103 
104     /***
105      * Creates a new instance of ProportionConstraints
106      *
107      * @param gridX specifies the column containing the left edge of the component's display area.
108      *              The first column has gridX = 0.
109      * @param gridY specifies the row containing the upper edge of the component's display area. The
110      *              first row has gridY = 0.
111      * @throws IllegalArgumentException if gridX or GridY < 0.
112      */
113     public ProportionConstraints(int gridX, int gridY) throws IllegalArgumentException {
114         this(gridX, gridY, 1, 1, gridX, gridY, CENTER, BOTH, 0, 0, 0, 0, 0, 0);
115     }
116 
117     /***
118      * Creates a new instance of ProportionConstraints
119      *
120      * @param gridX      specifies the column containing the left edge of the component's display
121      *                   area. The first column has gridX = 0.
122      * @param gridY      specifies the row containing the upper edge of the component's display
123      *                   area. The first row has gridY = 0.
124      * @param gridWidth  Specifies the width of the cell (gridX,gridY).
125      * @param gridHeight Specifies the height of the cell (gridX, gridY).
126      * @throws IllegalArgumentException if gridX or GridY < 0.
127      */
128     public ProportionConstraints(int gridX, int gridY, int gridWidth, int gridHeight)
129             throws IllegalArgumentException {
130         this(gridX, gridY, gridWidth, gridHeight,
131                 gridX + gridWidth - 1, gridY + gridHeight - 1,
132                 CENTER, BOTH, 0, 0, 0, 0, 0, 0);
133     }
134 
135     /***
136      * Creates a new instance of ProportionConstraints
137      *
138      * @param gridX       specifies the column containing the left edge of the component's display
139      *                    area. The first column has gridX = 0.
140      * @param gridY       specifies the row containing the left edge of the component's display
141      *                    area. The first row has gridY = 0.
142      * @param gridWidth   specifies the width of the cell (gridX,gridY).
143      * @param gridHeight  specifies the height of the cell (gridX,gridY).
144      * @param extendGridX specifies the number of cells to extend the cell (gridX, gridY)
145      *                    horizontally when a component needs more space then the cells combined.
146      * @param extendGridY specifies the number of cells to extend the cell (gridX, gridY) vertically
147      *                    when a component needs more space then the cells combined.
148      * @throws IllegalArgumentException if gridX or GridY < 0.
149      */
150     public ProportionConstraints(int gridX, int gridY, int gridWidth, int gridHeight,
151                                  int extendGridX, int extendGridY)
152             throws IllegalArgumentException {
153         this(gridX, gridY, gridWidth, gridHeight, extendGridX, extendGridY,
154                 CENTER, BOTH, 0, 0, 0, 0, 0, 0);
155     }
156 
157     /***
158      * Creates a new instance of ProportionConstraints
159      *
160      * @param gridX       specifies the column containing the left edge of the component's display
161      *                    area. The first column has gridX = 0.
162      * @param gridY       specifies the row containing the left edge of the component's display
163      *                    area. The first row has gridY = 0.
164      * @param gridWidth   specifies the width of the cell (gridX,gridY).
165      * @param gridHeight  specifies the height of the cell (gridX,gridY).
166      * @param extendGridX specifies the number of cells to extend the cell (gridX, gridY)
167      *                    horizontally when a component needs more space then the cells combined.
168      * @param extendGridY specifies the number of cells to extend the cell (gridX, gridY) vertically
169      *                    when a component needs more space then the cells combined.
170      * @param anchor      this field is used when the component is smaller than its display area. It
171      *                    determines where, within the display area, to place the component. The
172      *                    possible values are: <code>CENTER</code>, <code>NORTH</code>,
173      *                    <code>NORTHEAST</code>, <code>EAST</code>, <code>SOUTHEAST</code>,
174      *                    <code>SOUTH</code>, <code>SOUTHWEST</code>, <code>WEST</code> and
175      *                    <code>NORTHWEST</code>. The default value is <code>CENTER</code>.
176      * @param fill        this field is used when the component's display area is larger than the
177      *                    component's requested size. It determines whether to resize the component,
178      *                    and if so, how.
179      *                    <p/>
180      *                    The following values are valid for <code>fill</code>:
181      *                    <p/>
182      *                    <ul> <li> <code>NONE</code>: Do not resize the component. <li>
183      *                    <code>HORIZONTAL</code>: Make the component wide enough to fill its
184      *                    display area horizontally, but do not change its height. <li>
185      *                    <code>VERTICAL</code>: Make the component tall enough to fill its display
186      *                    area vertically, but do not change its width. <li> <code>BOTH</code>: Make
187      *                    the component fill its display area entirely. </ul>
188      *                    <p/>
189      *                    The default value is <code>NONE</code>.
190      * @throws IllegalArgumentException if gridX or GridY < 0.
191      */
192     public ProportionConstraints(int gridX, int gridY, int gridWidth, int gridHeight,
193                                  int extendGridX, int extendGridY, int anchor, int fill)
194             throws IllegalArgumentException {
195         this(gridX, gridY, gridWidth, gridHeight, extendGridX, extendGridY,
196                 anchor, fill, 0, 0, 0, 0, 0, 0);
197     }
198 
199     /***
200      * Creates a new instance of ProportionConstraints
201      *
202      * @param gridX       specifies the column containing the left edge of the component's display
203      *                    area. The first column has gridX = 0.
204      * @param gridY       specifies the row containing the left edge of the component's display
205      *                    area. The first row has gridY = 0.
206      * @param gridWidth   specifies the width of the cell (gridX,gridY).
207      * @param gridHeight  specifies the height of the cell (gridX,gridY).
208      * @param extendGridX specifies the number of cells to extend the cell (gridX, gridY)
209      *                    horizontally when a component needs more space then the cells combined.
210      * @param extendGridY specifies the number of cells to extend the cell (gridX, gridY) vertically
211      *                    when a component needs more space then the cells combined.
212      * @param anchor      this field is used when the component is smaller than its display area. It
213      *                    determines where, within the display area, to place the component. The
214      *                    possible values are: <code>CENTER</code>, <code>NORTH</code>,
215      *                    <code>NORTHEAST</code>, <code>EAST</code>, <code>SOUTHEAST</code>,
216      *                    <code>SOUTH</code>, <code>SOUTHWEST</code>, <code>WEST</code>, and
217      *                    <code>NORTHWEST</code>. The default value is <code>CENTER</code>.
218      * @param fill        this field is used when the component's display area is larger than the
219      *                    component's requested size. It determines whether to resize the component,
220      *                    and if so, how.
221      *                    <p/>
222      *                    The following values are valid for <code>fill</code>:
223      *                    <p/>
224      *                    <ul> <li> <code>NONE</code>: Do not resize the component. <li>
225      *                    <code>HORIZONTAL</code>: Make the component wide enough to fill its
226      *                    display area horizontally, but do not change its height. <li>
227      *                    <code>VERTICAL</code>: Make the component tall enough to fill its display
228      *                    area vertically, but do not change its width. <li> <code>BOTH</code>: Make
229      *                    the component fill its display area entirely. </ul>
230      *                    <p/>
231      *                    The default value is <code>NONE</code>.
232      * @param insets      this field specifies the external padding of the component, the minimum
233      *                    amount of space between the component and the edges of its display area.
234      * @param iPadX       this field specifies the internal padding of the component, how much space
235      *                    to add to the minimum width of the component. The width of the component
236      *                    is at least its minimum width plus (ipadx * 2) pixels.
237      * @param iPadY       this field specifies the internal padding, that is, how much space to add
238      *                    to the minimum height of the component. The height of the component is at
239      *                    least its minimum height plus (ipady * 2) pixels.
240      * @throws IllegalArgumentException if gridX or GridY < 0.
241      */
242     public ProportionConstraints(int gridX, int gridY, int gridWidth, int gridHeight,
243                                  int extendGridX, int extendGridY, int anchor, int fill,
244                                  Insets insets, int iPadX, int iPadY)
245             throws IllegalArgumentException {
246         this(gridX, gridY, gridWidth, gridHeight, extendGridX, extendGridY, anchor, fill,
247                 insets.top, insets.right, insets.bottom, insets.left, iPadX, iPadY);
248     }
249 
250     /***
251      * Creates a new instance of ProportionConstraints
252      *
253      * @param gridX       specifies the column containing the left edge of the component's display
254      *                    area. The first column has gridX = 0.
255      * @param gridY       specifies the row containing the left edge of the component's display
256      *                    area. The first row has gridY = 0.
257      * @param gridWidth   specifies the width of the cell (gridX,gridY).
258      * @param gridHeight  specifies the height of the cell (gridX,gridY).
259      * @param extendGridX specifies the number of cells to extend the cell (gridX, gridY)
260      *                    horizontally when a component needs more space then the cells combined.
261      * @param extendGridY specifies the number of cells to extend the cell (gridX, gridY) vertically
262      *                    when a component needs more space then the cells combined.
263      * @param anchor      this field is used when the component is smaller than its display area. It
264      *                    determines where, within the display area, to place the component. The
265      *                    possible values are: <code>CENTER</code>, <code>NORTH</code>,
266      *                    <code>NORTHEAST</code>, <code>EAST</code>, <code>SOUTHEAST</code>,
267      *                    <code>SOUTH</code>, <code>SOUTHWEST</code>, <code>WEST</code>, and
268      *                    <code>NORTHWEST</code>. The default value is <code>CENTER</code>.
269      * @param fill        this field is used when the component's display area is larger than the
270      *                    component's requested size. It determines whether to resize the component,
271      *                    and if so, how.
272      *                    <p/>
273      *                    The following values are valid for <code>fill</code>:
274      *                    <p/>
275      *                    <ul> <li> <code>NONE</code>: Do not resize the component. <li>
276      *                    <code>HORIZONTAL</code>: Make the component wide enough to fill its
277      *                    display area horizontally, but do not change its height. <li>
278      *                    <code>VERTICAL</code>: Make the component tall enough to fill its display
279      *                    area vertically, but do not change its width. <li> <code>BOTH</code>: Make
280      *                    the component fill its display area entirely. </ul>
281      *                    <p/>
282      *                    The default value is <code>NONE</code>.
283      * @param insetNorth  This field specifies the external padding of the component, the minimum
284      *                    amount of space between the component and the northern edge of its display
285      *                    area.
286      * @param insetEast   This field specifies the external padding of the component, the minimum
287      *                    amount of space between the component and the eastern edge of its display
288      *                    area.
289      * @param insetSouth  This field specifies the external padding of the component, the minimum
290      *                    amount of space between the component and the southern edge of its display
291      *                    area.
292      * @param insetWest   This field specifies the external padding of the component, the minimum
293      *                    amount of space between the component and the western edge of its display
294      *                    area.
295      * @param iPadX       This field specifies the internal padding of the component, how much space
296      *                    to add to the minimum width of the component. The width of the component
297      *                    is at least its minimum width plus (ipadx * 2) pixels.
298      * @param iPadY       This field specifies the internal padding, that is, how much space to add
299      *                    to the minimum height of the component. The height of the component is at
300      *                    least its minimum height plus (ipady * 2) pixels.
301      * @throws IllegalArgumentException if gridX or GridY < 0.
302      */
303     public ProportionConstraints(int gridX, int gridY, int gridWidth, int gridHeight,
304                                  int extendGridX, int extendGridY, int anchor, int fill,
305                                  int insetNorth, int insetEast, int insetSouth,
306                                  int insetWest, int iPadX, int iPadY)
307             throws IllegalArgumentException {
308         if (gridX < 0 || gridY < 0 || gridWidth < 1 || gridHeight < 1 || insetNorth < 0 ||
309                 insetEast < 0 || insetSouth < 0 || insetWest < 0 || iPadX < 0 || iPadY < 0) {
310             throw new IllegalArgumentException("Negative arguments or gridWidth/Height 0");
311         }
312         if (extendGridX < gridX || extendGridX >= gridX + gridWidth ||
313                 extendGridY < gridY || extendGridY >= gridY + gridHeight) {
314             throw new IllegalArgumentException("extendGridX/Y not between gridX/Y and"
315                     + " gridX/Y + gridWidth/Height");
316         }
317         this.gridX = gridX;
318         this.gridY = gridY;
319         this.gridWidth = gridWidth;
320         this.gridHeight = gridHeight;
321         this.extendGridX = extendGridX;
322         this.extendGridY = extendGridY;
323         this.anchor = anchor;
324         this.fill = fill;
325         this.insetNorth = insetNorth;
326         this.insetEast = insetEast;
327         this.insetSouth = insetSouth;
328         this.insetWest = insetWest;
329         this.iPadX = iPadX;
330         this.iPadY = iPadY;
331     }
332 
333     /***
334      * Represents the x-dimension of a specific cell made with ProportionConstraints.
335      *
336      * @param component specifies the component to which this column's ProportionLinePart belongs.
337      * @param linesSize specifies the number of columns in the grid made with addColumn.
338      * @return the linepart of this specific component based on it's constraints.
339      * @throws IndexOutOfBoundsException if gridX + gridWidth > the number of columns that are added
340      *                                   to this grid addColumn.
341      */
342     public ProportionLinePart getColumnLinePart(Component component, int linesSize)
343             throws IndexOutOfBoundsException {
344         int columnAnchor = ProportionLinePart.CENTER;
345         int columnFill = ProportionLinePart.FILL;
346 
347         if (gridX + gridWidth > linesSize) {
348             throw new IndexOutOfBoundsException("Index column from " + gridX + " to "
349                     + (gridX + gridWidth)
350                     + ", Size " + linesSize);
351         }
352         switch (anchor) {
353             case CENTER:
354             case NORTH:
355             case SOUTH:
356                 columnAnchor = ProportionLinePart.CENTER;
357                 break;
358             case WEST:
359             case NORTH_WEST:
360             case SOUTH_WEST:
361                 columnAnchor = ProportionLinePart.LEFT;
362                 break;
363             case EAST:
364             case NORTH_EAST:
365             case SOUTH_EAST:
366                 columnAnchor = ProportionLinePart.RIGHT;
367                 break;
368         }
369         switch (fill) {
370             case NONE:
371             case VERTICAL:
372                 columnFill = ProportionLinePart.NONE;
373                 break;
374             case BOTH:
375             case HORIZONTAL:
376                 columnFill = ProportionLinePart.FILL;
377                 break;
378         }
379         return new ProportionLinePart(ProportionLinePart.COLUMN, component,
380                 (iPadX * 2) + insetWest + insetEast,
381                 gridX, gridWidth, extendGridX,
382                 columnAnchor, columnFill, insetWest, insetEast);
383     }
384 
385     /***
386      * Represents the y-dimension of a specific cell made with ProportionConstraints.
387      *
388      * @param component specifies the component to which this row's ProportionLinePart belongs.
389      * @param linesSize specifies the number of rows in the grid made with addRow.
390      * @return the linepart of this specific component based on it's constraints.
391      * @throws IndexOutOfBoundsException if gridY + gridHeigth > the number of rows that are added
392      *                                   to this grid with addRow.
393      */
394     public ProportionLinePart getRowLinePart(Component component, int linesSize)
395             throws IndexOutOfBoundsException {
396         int rowAnchor = ProportionLinePart.CENTER;
397         int rowFill = ProportionLinePart.FILL;
398 
399         if (gridY + gridHeight > linesSize) {
400             throw new IndexOutOfBoundsException("Index row from " + gridY + " to "
401                     + (gridY + gridHeight)
402                     + ", Size " + linesSize);
403         }
404         switch (anchor) {
405             case CENTER:
406             case EAST:
407             case WEST:
408                 rowAnchor = ProportionLinePart.CENTER;
409                 break;
410             case NORTH:
411             case NORTH_EAST:
412             case NORTH_WEST:
413                 rowAnchor = ProportionLinePart.LEFT;
414                 break;
415             case SOUTH:
416             case SOUTH_EAST:
417             case SOUTH_WEST:
418                 rowAnchor = ProportionLinePart.RIGHT;
419                 break;
420         }
421         switch (fill) {
422             case NONE:
423             case HORIZONTAL:
424                 rowFill = ProportionLinePart.NONE;
425                 break;
426             case BOTH:
427             case VERTICAL:
428                 rowFill = ProportionLinePart.FILL;
429                 break;
430         }
431         return new ProportionLinePart(ProportionLinePart.ROW, component,
432                 (iPadY * 2) + insetNorth + insetSouth,
433                 gridY, gridHeight, extendGridY,
434                 rowAnchor, rowFill, insetNorth, insetSouth);
435     }
436 
437 }