/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* --------------------
* GridArrangement.java
* --------------------
* (C) Copyright 2005, 2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes:
* --------
* 08-Feb-2005 : Version 1 (DG);
*
*/
package com.positive.charts.block;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.graphics.GC;
import com.positive.charts.util.Size2D;
/**
* Arranges blocks in a grid within their container.
*/
public class GridArrangement implements Arrangement, Serializable {
/** For serialization. */
private static final long serialVersionUID = -2563758090144655938L;
/** The rows. */
private final int rows;
/** The columns. */
private final int columns;
/**
* Creates a new grid arrangement.
*
* @param rows
* the row count.
* @param columns
* the column count.
*/
public GridArrangement(final int rows, final int columns) {
this.rows = rows;
this.columns = columns;
}
/**
* Adds a block and a key which can be used to determine the position of the
* block in the arrangement. This method is called by the container (you
* don't need to call this method directly) and gives the arrangement an
* opportunity to record the details if they are required.
*
* @param block
* the block.
* @param key
* the key (<code>null</code> permitted).
*/
public void add(final Block block, final Object key) {
// can safely ignore
}
/**
* Arranges the blocks within the specified container, subject to the given
* constraint.
*
* @param container
* the container.
* @param constraint
* the constraint.
* @param g2
* the graphics device.
*
* @return The size following the arrangement.
*/
public Size2D arrange(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
final LengthConstraintType w = constraint.getWidthConstraintType();
final LengthConstraintType h = constraint.getHeightConstraintType();
if (w == LengthConstraintType.NONE) {
if (h == LengthConstraintType.NONE) {
return this.arrangeNN(container, g2);
} else if (h == LengthConstraintType.FIXED) {
throw new RuntimeException("Not yet implemented.");
} else if (h == LengthConstraintType.RANGE) {
// find optimum height, then map to range
throw new RuntimeException("Not yet implemented.");
}
} else if (w == LengthConstraintType.FIXED) {
if (h == LengthConstraintType.NONE) {
// find optimum height
return this.arrangeFN(container, g2, constraint);
} else if (h == LengthConstraintType.FIXED) {
return this.arrangeFF(container, g2, constraint);
} else if (h == LengthConstraintType.RANGE) {
// find optimum height and map to range
return this.arrangeFR(container, g2, constraint);
}
} else if (w == LengthConstraintType.RANGE) {
// find optimum width and map to range
if (h == LengthConstraintType.NONE) {
// find optimum height
throw new RuntimeException("Not yet implemented.");
} else if (h == LengthConstraintType.FIXED) {
// fixed width
throw new RuntimeException("Not yet implemented.");
} else if (h == LengthConstraintType.RANGE) {
throw new RuntimeException("Not yet implemented.");
}
}
return new Size2D(); // TODO: complete this
}
/**
* Arranges the container with a fixed overall width and height.
*
* @param container
* the container.
* @param g2
* the graphics device.
* @param constraint
* the constraint.
*
* @return The size following the arrangement.
*/
protected Size2D arrangeFF(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
final double width = constraint.getWidth() / this.columns;
final double height = constraint.getHeight() / this.rows;
final List blocks = container.getBlocks();
for (int c = 0; c < this.columns; c++) {
for (int r = 0; r < this.rows; r++) {
final int index = r * this.columns + c;
if (index == blocks.size()) {
break;
}
final Block b = (Block) blocks.get(index);
b.setBounds(RectUtils.doubleRect(c * width, r * height, width,
height));
}
}
return new Size2D(this.columns * width, this.rows * height);
}
/**
* Arrange with a fixed width and a height within a given range.
*
* @param container
* the container.
* @param g2
* the graphics device.
* @param constraint
* the constraint.
*
* @return The size of the arrangement.
*/
protected Size2D arrangeFN(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
final double width = constraint.getWidth() / this.columns;
final RectangleConstraint constraint2 = constraint.toFixedWidth(width);
final List blocks = container.getBlocks();
double maxH = 0.0;
for (int r = 0; r < this.rows; r++) {
for (int c = 0; c < this.columns; c++) {
final int index = r * this.columns + c;
if (index == blocks.size()) {
break;
}
final Block b = (Block) blocks.get(index);
final Size2D s = b.arrange(g2, constraint2);
maxH = Math.max(maxH, s.getHeight());
}
}
final RectangleConstraint constraint3 = constraint.toFixedHeight(maxH
* this.rows);
return this.arrange(container, g2, constraint3);
}
/**
* Arrange with a fixed width and a height within a given range.
*
* @param container
* the container.
* @param constraint
* the constraint.
* @param g2
* the graphics device.
*
* @return The size of the arrangement.
*/
protected Size2D arrangeFR(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
final RectangleConstraint c1 = constraint.toUnconstrainedHeight();
final Size2D size1 = this.arrange(container, g2, c1);
if (constraint.getHeightRange().contains(size1.getHeight())) {
return size1;
} else {
final double h = constraint.getHeightRange().constrain(
size1.getHeight());
final RectangleConstraint c2 = constraint.toFixedHeight(h);
return this.arrange(container, g2, c2);
}
}
/**
* Arranges the container with no constraint on the width or height.
*
* @param container
* the container.
* @param g2
* the graphics device.
*
* @return The size.
*/
protected Size2D arrangeNN(final BlockContainer container, final GC g2) {
double maxW = 0.0;
double maxH = 0.0;
final List blocks = container.getBlocks();
final Iterator iterator = blocks.iterator();
while (iterator.hasNext()) {
final Block b = (Block) iterator.next();
final Size2D s = b.arrange(g2, RectangleConstraint.NONE);
maxW = Math.max(maxW, s.width);
maxH = Math.max(maxH, s.height);
}
final double width = this.columns * maxW;
final double height = this.rows * maxH;
final RectangleConstraint c = new RectangleConstraint(width, height);
return this.arrangeFF(container, g2, c);
}
/**
* Clears any cached layout information retained by the arrangement.
*/
public void clear() {
// nothing to clear
}
/**
* Compares this layout manager for equality with an arbitrary object.
*
* @param obj
* the object.
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof GridArrangement)) {
return false;
}
final GridArrangement that = (GridArrangement) obj;
if (this.columns != that.columns) {
return false;
}
if (this.rows != that.rows) {
return false;
}
return true;
}
}