001: package net.xoetrope.xui.helper;
002:
003: import java.awt.Container;
004:
005: /**
006: * <p>A helper class to assist in positioning rows and columns of
007: * components within a container. The containers width is divided into columns
008: * and the height is divided into rows. Each cell is padded on either side.<br>
009: * No attempt is made to constrain the measurements. Therefore if an attempt is
010: * made to call nextX or nextY more times than there are rows or columns
011: * respectively then dimensions beyond the bounds on the container will be
012: * returned.</p>
013: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
014: * License: see license.txt
015: * @version $Revision: 1.22 $
016: */
017: public class Measurer {
018: private int width;
019: private int height;
020: private int x;
021: private int y;
022: private int padX;
023: private int padY;
024: private int cellWidth;
025: private int cellHeight;
026:
027: /**
028: * Constructs a new Measurer.
029: * @param cont the container to be dividied
030: * @param rows the number of rows into which the container's height is divided
031: * @param cols the number of columns into which the container's width is divided
032: * @param horzPadding the amount of padding on the left and right of the cell
033: * @param vertPadding the amount of padding on the top and bottom of the cell
034: */
035: public Measurer(Container cont, int rows, int cols,
036: int horzPadding, int vertPadding) {
037: width = cont.getSize().width;
038: height = cont.getSize().height;
039:
040: padX = horzPadding;
041: padY = vertPadding;
042:
043: // Calculate padding on either side of the cell object.
044: cellWidth = width / cols;
045: cellHeight = height / rows;
046:
047: // Simplify the nextX, nextY methods by allowing uniform incrementing, without
048: // a special initail case.
049: x = -cellWidth;
050: y = -cellHeight;
051: }
052:
053: /**
054: * Get the left hand coordinate. The coordinate includes the cell padding
055: * @return the new coordinate
056: */
057: public int getNextX() {
058: x += cellWidth;
059: return x + padX;
060: }
061:
062: /**
063: * Get the top coordinate. The coordinate includes the cell padding
064: * @return the new coordinate
065: */
066: public int getNextY() {
067: y += cellHeight;
068: return y + padY;
069: }
070:
071: /**
072: * Get the cell width including padding
073: * @return the cell width
074: */
075: public int getCellWidth() {
076: return cellWidth;
077: }
078:
079: /**
080: * Get the cell width including padding
081: * @return the cell height
082: */
083: public int getCellHeight() {
084: return cellHeight;
085: }
086:
087: /**
088: * Get the cell width including padding (the cell width less the padding)
089: * @return the cell width
090: */
091: public int getWidth() {
092: return cellWidth - 2 * padX;
093: }
094:
095: /**
096: * Get the cell content's height (the cell height less the padding)
097: * @return the cell height
098: */
099: public int getHeight() {
100: return cellHeight - 2 * padY;
101: }
102:
103: /**
104: * Get the remaining width available for a component, taking account of the
105: * required padding
106: * @return the remaining width
107: */
108: public int getRemainingWidth() {
109: return width - x - cellWidth - 2 * padX;
110: }
111:
112: /**
113: * Get the remaining height available for a component, taking account of the
114: * required padding
115: * @return the remaining height
116: */
117: public int getRemainingHeight() {
118: return height - y - cellHeight - 2 * padY;
119: }
120: }
|