001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------------
028: * CenterArrangement.java
029: * ----------------------
030: * (C) Copyright 2005, 2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CenterArrangement.java,v 1.3.2.2 2006/07/20 16:21:58 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 08-Mar-2005 : Version 1 (DG);
040: * ------------- JFREECHART 1.0.0 ---------------------------------------------
041: * 20-Jul-2006 : Set bounds of contained block when arranging (DG);
042: *
043: */
044:
045: package org.jfree.chart.block;
046:
047: import java.awt.Graphics2D;
048: import java.awt.geom.Rectangle2D;
049: import java.io.Serializable;
050: import java.util.List;
051:
052: import org.jfree.ui.Size2D;
053:
054: /**
055: * Arranges a block in the center of its container. This class is immutable.
056: */
057: public class CenterArrangement implements Arrangement, Serializable {
058:
059: /** For serialization. */
060: private static final long serialVersionUID = -353308149220382047L;
061:
062: /**
063: * Creates a new instance.
064: */
065: public CenterArrangement() {
066: }
067:
068: /**
069: * Adds a block to be managed by this instance. This method is usually
070: * called by the {@link BlockContainer}, you shouldn't need to call it
071: * directly.
072: *
073: * @param block the block.
074: * @param key a key that controls the position of the block.
075: */
076: public void add(Block block, Object key) {
077: // since the flow layout is relatively straightforward,
078: // no information needs to be recorded here
079: }
080:
081: /**
082: * Calculates and sets the bounds of all the items in the specified
083: * container, subject to the given constraint. The <code>Graphics2D</code>
084: * can be used by some items (particularly items containing text) to
085: * calculate sizing parameters.
086: *
087: * @param container the container whose items are being arranged.
088: * @param g2 the graphics device.
089: * @param constraint the size constraint.
090: *
091: * @return The size of the container after arrangement of the contents.
092: */
093: public Size2D arrange(BlockContainer container, Graphics2D g2,
094: RectangleConstraint constraint) {
095:
096: LengthConstraintType w = constraint.getWidthConstraintType();
097: LengthConstraintType h = constraint.getHeightConstraintType();
098: if (w == LengthConstraintType.NONE) {
099: if (h == LengthConstraintType.NONE) {
100: return arrangeNN(container, g2);
101: } else if (h == LengthConstraintType.FIXED) {
102: throw new RuntimeException("Not implemented.");
103: } else if (h == LengthConstraintType.RANGE) {
104: throw new RuntimeException("Not implemented.");
105: }
106: } else if (w == LengthConstraintType.FIXED) {
107: if (h == LengthConstraintType.NONE) {
108: return arrangeFN(container, g2, constraint);
109: } else if (h == LengthConstraintType.FIXED) {
110: throw new RuntimeException("Not implemented.");
111: } else if (h == LengthConstraintType.RANGE) {
112: throw new RuntimeException("Not implemented.");
113: }
114: } else if (w == LengthConstraintType.RANGE) {
115: if (h == LengthConstraintType.NONE) {
116: return arrangeRN(container, g2, constraint);
117: } else if (h == LengthConstraintType.FIXED) {
118: return arrangeRF(container, g2, constraint);
119: } else if (h == LengthConstraintType.RANGE) {
120: return arrangeRR(container, g2, constraint);
121: }
122: }
123: throw new IllegalArgumentException(
124: "Unknown LengthConstraintType.");
125:
126: }
127:
128: /**
129: * Arranges the blocks in the container with a fixed width and no height
130: * constraint.
131: *
132: * @param container the container.
133: * @param g2 the graphics device.
134: * @param constraint the constraint.
135: *
136: * @return The size.
137: */
138: protected Size2D arrangeFN(BlockContainer container, Graphics2D g2,
139: RectangleConstraint constraint) {
140:
141: List blocks = container.getBlocks();
142: Block b = (Block) blocks.get(0);
143: Size2D s = b.arrange(g2, RectangleConstraint.NONE);
144: double width = constraint.getWidth();
145: Rectangle2D bounds = new Rectangle2D.Double(
146: (width - s.width) / 2.0, 0.0, s.width, s.height);
147: b.setBounds(bounds);
148: return new Size2D((width - s.width) / 2.0, s.height);
149: }
150:
151: /**
152: * Arranges the blocks in the container with a fixed with and a range
153: * constraint on the height.
154: *
155: * @param container the container.
156: * @param g2 the graphics device.
157: * @param constraint the constraint.
158: *
159: * @return The size following the arrangement.
160: */
161: protected Size2D arrangeFR(BlockContainer container, Graphics2D g2,
162: RectangleConstraint constraint) {
163:
164: Size2D s = arrangeFN(container, g2, constraint);
165: if (constraint.getHeightRange().contains(s.height)) {
166: return s;
167: } else {
168: RectangleConstraint c = constraint.toFixedHeight(constraint
169: .getHeightRange().constrain(s.getHeight()));
170: return arrangeFF(container, g2, c);
171: }
172: }
173:
174: /**
175: * Arranges the blocks in the container with the overall height and width
176: * specified as fixed constraints.
177: *
178: * @param container the container.
179: * @param g2 the graphics device.
180: * @param constraint the constraint.
181: *
182: * @return The size following the arrangement.
183: */
184: protected Size2D arrangeFF(BlockContainer container, Graphics2D g2,
185: RectangleConstraint constraint) {
186:
187: // TODO: implement this properly
188: return arrangeFN(container, g2, constraint);
189: }
190:
191: /**
192: * Arranges the blocks with the overall width and height to fit within
193: * specified ranges.
194: *
195: * @param container the container.
196: * @param g2 the graphics device.
197: * @param constraint the constraint.
198: *
199: * @return The size after the arrangement.
200: */
201: protected Size2D arrangeRR(BlockContainer container, Graphics2D g2,
202: RectangleConstraint constraint) {
203:
204: // first arrange without constraints, and see if this fits within
205: // the required ranges...
206: Size2D s1 = arrangeNN(container, g2);
207: if (constraint.getWidthRange().contains(s1.width)) {
208: return s1; // TODO: we didn't check the height yet
209: } else {
210: RectangleConstraint c = constraint.toFixedWidth(constraint
211: .getWidthRange().getUpperBound());
212: return arrangeFR(container, g2, c);
213: }
214: }
215:
216: /**
217: * Arranges the blocks in the container with a range constraint on the
218: * width and a fixed height.
219: *
220: * @param container the container.
221: * @param g2 the graphics device.
222: * @param constraint the constraint.
223: *
224: * @return The size following the arrangement.
225: */
226: protected Size2D arrangeRF(BlockContainer container, Graphics2D g2,
227: RectangleConstraint constraint) {
228:
229: Size2D s = arrangeNF(container, g2, constraint);
230: if (constraint.getWidthRange().contains(s.width)) {
231: return s;
232: } else {
233: RectangleConstraint c = constraint.toFixedWidth(constraint
234: .getWidthRange().constrain(s.getWidth()));
235: return arrangeFF(container, g2, c);
236: }
237: }
238:
239: /**
240: * Arranges the block with a range constraint on the width, and no
241: * constraint on the height.
242: *
243: * @param container the container.
244: * @param g2 the graphics device.
245: * @param constraint the constraint.
246: *
247: * @return The size following the arrangement.
248: */
249: protected Size2D arrangeRN(BlockContainer container, Graphics2D g2,
250: RectangleConstraint constraint) {
251: // first arrange without constraints, then see if the width fits
252: // within the required range...if not, call arrangeFN() at max width
253: Size2D s1 = arrangeNN(container, g2);
254: if (constraint.getWidthRange().contains(s1.width)) {
255: return s1;
256: } else {
257: RectangleConstraint c = constraint.toFixedWidth(constraint
258: .getWidthRange().getUpperBound());
259: return arrangeFN(container, g2, c);
260: }
261: }
262:
263: /**
264: * Arranges the blocks without any constraints. This puts all blocks
265: * into a single row.
266: *
267: * @param container the container.
268: * @param g2 the graphics device.
269: *
270: * @return The size after the arrangement.
271: */
272: protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) {
273: List blocks = container.getBlocks();
274: Block b = (Block) blocks.get(0);
275: Size2D s = b.arrange(g2, RectangleConstraint.NONE);
276: b
277: .setBounds(new Rectangle2D.Double(0.0, 0.0, s.width,
278: s.height));
279: return new Size2D(s.width, s.height);
280: }
281:
282: /**
283: * Arranges the blocks with no width constraint and a fixed height
284: * constraint. This puts all blocks into a single row.
285: *
286: * @param container the container.
287: * @param g2 the graphics device.
288: * @param constraint the constraint.
289: *
290: * @return The size after the arrangement.
291: */
292: protected Size2D arrangeNF(BlockContainer container, Graphics2D g2,
293: RectangleConstraint constraint) {
294: // TODO: for now we are ignoring the height constraint
295: return arrangeNN(container, g2);
296: }
297:
298: /**
299: * Clears any cached information.
300: */
301: public void clear() {
302: // no action required.
303: }
304:
305: /**
306: * Tests this instance for equality with an arbitrary object.
307: *
308: * @param obj the object (<code>null</code> permitted).
309: *
310: * @return A boolean.
311: */
312: public boolean equals(Object obj) {
313: if (obj == this ) {
314: return true;
315: }
316: if (!(obj instanceof CenterArrangement)) {
317: return false;
318: }
319: return true;
320: }
321:
322: }
|