1:
40:
41: package ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50:
51:
54: public class GridArrangement implements Arrangement, Serializable {
55:
56:
57: private static final long serialVersionUID = -2563758090144655938L;
58:
59:
60: private int rows;
61:
62:
63: private int columns;
64:
65:
71: public GridArrangement(int rows, int columns) {
72: this.rows = rows;
73: this.columns = columns;
74: }
75:
76:
85: public void add(Block block, Object key) {
86:
87: }
88:
89:
99: public Size2D arrange(BlockContainer container, Graphics2D g2,
100: RectangleConstraint constraint) {
101: LengthConstraintType w = constraint.getWidthConstraintType();
102: LengthConstraintType h = constraint.getHeightConstraintType();
103: if (w == LengthConstraintType.NONE) {
104: if (h == LengthConstraintType.NONE) {
105: return arrangeNN(container, g2);
106: }
107: else if (h == LengthConstraintType.FIXED) {
108:
109: throw new RuntimeException("Not yet implemented.");
110: }
111: else if (h == LengthConstraintType.RANGE) {
112:
113: throw new RuntimeException("Not yet implemented.");
114: }
115: }
116: else if (w == LengthConstraintType.FIXED) {
117: if (h == LengthConstraintType.NONE) {
118:
119: return arrangeFN(container, g2, constraint);
120: }
121: else if (h == LengthConstraintType.FIXED) {
122: return arrangeFF(container, g2, constraint);
123: }
124: else if (h == LengthConstraintType.RANGE) {
125:
126: return arrangeFR(container, g2, constraint);
127: }
128: }
129: else if (w == LengthConstraintType.RANGE) {
130:
131: if (h == LengthConstraintType.NONE) {
132:
133: throw new RuntimeException("Not yet implemented.");
134: }
135: else if (h == LengthConstraintType.FIXED) {
136:
137: throw new RuntimeException("Not yet implemented.");
138: }
139: else if (h == LengthConstraintType.RANGE) {
140: throw new RuntimeException("Not yet implemented.");
141: }
142: }
143: return new Size2D();
144: }
145:
146:
154: protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) {
155: double maxW = 0.0;
156: double maxH = 0.0;
157: List blocks = container.getBlocks();
158: Iterator iterator = blocks.iterator();
159: while (iterator.hasNext()) {
160: Block b = (Block) iterator.next();
161: Size2D s = b.arrange(g2, RectangleConstraint.NONE);
162: maxW = Math.max(maxW, s.width);
163: maxH = Math.max(maxH, s.height);
164: }
165: double width = this.columns * maxW;
166: double height = this.rows * maxH;
167: RectangleConstraint c = new RectangleConstraint(width, height);
168: return arrangeFF(container, g2, c);
169: }
170:
171:
180: protected Size2D arrangeFF(BlockContainer container, Graphics2D g2,
181: RectangleConstraint constraint) {
182: double width = constraint.getWidth() / this.columns;
183: double height = constraint.getHeight() / this.rows;
184: List blocks = container.getBlocks();
185: for (int c = 0; c < this.columns; c++) {
186: for (int r = 0; r < this.rows; r++) {
187: int index = r * this.columns + c;
188: if (index == blocks.size()) {
189: break;
190: }
191: Block b = (Block) blocks.get(index);
192: b.setBounds(new Rectangle2D.Double(
193: c * width, r * height, width, height
194: ));
195: }
196: }
197: return new Size2D(this.columns * width, this.rows * height);
198: }
199:
200:
209: protected Size2D arrangeFR(BlockContainer container, Graphics2D g2,
210: RectangleConstraint constraint) {
211:
212: RectangleConstraint c1 = constraint.toUnconstrainedHeight();
213: Size2D size1 = arrange(container, g2, c1);
214:
215: if (constraint.getHeightRange().contains(size1.getHeight())) {
216: return size1;
217: }
218: else {
219: double h = constraint.getHeightRange().constrain(size1.getHeight());
220: RectangleConstraint c2 = constraint.toFixedHeight(h);
221: return arrange(container, g2, c2);
222: }
223: }
224:
225:
234: protected Size2D arrangeFN(BlockContainer container, Graphics2D g2,
235: RectangleConstraint constraint) {
236:
237: double width = constraint.getWidth() / this.columns;
238: RectangleConstraint constraint2 = constraint.toFixedWidth(width);
239: List blocks = container.getBlocks();
240: double maxH = 0.0;
241: for (int r = 0; r < this.rows; r++) {
242: for (int c = 0; c < this.columns; c++) {
243: int index = r * this.columns + c;
244: if (index == blocks.size()) {
245: break;
246: }
247: Block b = (Block) blocks.get(index);
248: Size2D s = b.arrange(g2, constraint2);
249: maxH = Math.max(maxH, s.getHeight());
250: }
251: }
252: RectangleConstraint constraint3 = constraint.toFixedHeight(
253: maxH * this.rows
254: );
255: return arrange(container, g2, constraint3);
256: }
257:
258:
261: public void clear() {
262:
263: }
264:
265:
272: public boolean equals(Object obj) {
273: if (obj == this) {
274: return true;
275: }
276: if (!(obj instanceof GridArrangement)) {
277: return false;
278: }
279: GridArrangement that = (GridArrangement) obj;
280: if (this.columns != that.columns) {
281: return false;
282: }
283: if (this.rows != that.rows) {
284: return false;
285: }
286: return true;
287: }
288:
289: }