/* ===========================================================
* 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.]
*
* ----------------------
* ColumnArrangement.java
* ----------------------
* (C) Copyright 2004-2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes:
* --------
* 22-Oct-2004 : Version 1 (DG);
* 04-Feb-2005 : Added equals() and implemented Serializable (DG);
*
*/
package com.positive.charts.block;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.graphics.GC;
import com.positive.charts.util.HorizontalAlignment;
import com.positive.charts.util.Size2D;
import com.positive.charts.util.VerticalAlignment;
/**
* Arranges blocks in a column layout. This class is immutable.
*/
public class ColumnArrangement implements Arrangement, Serializable {
/** For serialization. */
private static final long serialVersionUID = -5315388482898581555L;
/** The horizontal alignment of blocks. */
private HorizontalAlignment horizontalAlignment;
/** The vertical alignment of blocks within each row. */
private VerticalAlignment verticalAlignment;
/** The horizontal gap between columns. */
private double horizontalGap;
/** The vertical gap between items in a column. */
private double verticalGap;
/**
* Creates a new instance.
*/
public ColumnArrangement() {
}
/**
* Creates a new instance.
*
* @param hAlign
* the horizontal alignment (currently ignored).
* @param vAlign
* the vertical alignment (currently ignored).
* @param hGap
* the horizontal gap.
* @param vGap
* the vertical gap.
*/
public ColumnArrangement(final HorizontalAlignment hAlign,
final VerticalAlignment vAlign, final double hGap, final double vGap) {
this.horizontalAlignment = hAlign;
this.verticalAlignment = vAlign;
this.horizontalGap = hGap;
this.verticalGap = vGap;
}
/**
* Adds a block to be managed by this instance. This method is usually
* called by the {@link BlockContainer}, you shouldn't need to call it
* directly.
*
* @param block
* the block.
* @param key
* a key that controls the position of the block.
*/
public void add(final Block block, final Object key) {
// since the flow layout is relatively straightforward, no information
// needs to be recorded here
}
/**
* Calculates and sets the bounds of all the items in the specified
* container, subject to the given constraint. The <code>GC</code> can be
* used by some items (particularly items containing text) to calculate
* sizing parameters.
*
* @param container
* the container whose items are being arranged.
* @param g2
* the graphics device.
* @param constraint
* the size constraint.
*
* @return The size of the container after arrangement of the contents.
*/
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 implemented.");
} else if (h == LengthConstraintType.RANGE) {
throw new RuntimeException("Not implemented.");
}
} else if (w == LengthConstraintType.FIXED) {
if (h == LengthConstraintType.NONE) {
throw new RuntimeException("Not implemented.");
} else if (h == LengthConstraintType.FIXED) {
return this.arrangeFF(container, g2, constraint);
} else if (h == LengthConstraintType.RANGE) {
throw new RuntimeException("Not implemented.");
}
} else if (w == LengthConstraintType.RANGE) {
if (h == LengthConstraintType.NONE) {
throw new RuntimeException("Not implemented.");
} else if (h == LengthConstraintType.FIXED) {
return this.arrangeRF(container, g2, constraint);
} else if (h == LengthConstraintType.RANGE) {
return this.arrangeRR(container, g2, constraint);
}
}
return new Size2D(); // TODO: complete this
}
/**
* Calculates and sets the bounds of all the items in the specified
* container, subject to the given constraint. The <code>GC</code> can be
* used by some items (particularly items containing text) to calculate
* sizing parameters.
*
* @param container
* the container whose items are being arranged.
* @param g2
* the graphics device.
* @param constraint
* the size constraint.
*
* @return The container size after the arrangement.
*/
protected Size2D arrangeFF(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
// TODO: implement properly
return this.arrangeNF(container, g2, constraint);
}
/**
* Calculates and sets the bounds of all the items in the specified
* container, subject to the given constraint. The <code>GC</code> can be
* used by some items (particularly items containing text) to calculate
* sizing parameters.
*
* @param container
* the container whose items are being arranged.
* @param constraint
* the size constraint.
* @param g2
* the graphics device.
*
* @return The container size after the arrangement.
*/
protected Size2D arrangeNF(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
final List blocks = container.getBlocks();
double height = constraint.getHeight();
if (height <= 0.0) {
height = Double.POSITIVE_INFINITY;
}
double x = 0.0;
double y = 0.0;
double maxWidth = 0.0;
final List itemsInColumn = new ArrayList();
for (int i = 0; i < blocks.size(); i++) {
final Block block = (Block) blocks.get(i);
final Size2D size = block.arrange(g2, RectangleConstraint.NONE);
if (y + size.height <= height) {
itemsInColumn.add(block);
block.setBounds(RectUtils.doubleRect(x, y, size.width,
size.height));
y = y + size.height + this.verticalGap;
maxWidth = Math.max(maxWidth, size.width);
} else {
if (itemsInColumn.isEmpty()) {
// place in this column (truncated) anyway
block.setBounds(RectUtils.doubleRect(x, y, size.width, Math
.min(size.height, height - y)));
y = 0.0;
x = x + size.width + this.horizontalGap;
} else {
// start new column
itemsInColumn.clear();
x = x + maxWidth + this.horizontalGap;
y = 0.0;
maxWidth = size.width;
block.setBounds(RectUtils.doubleRect(x, y, size.width, Math
.min(size.height, height)));
y = size.height + this.verticalGap;
itemsInColumn.add(block);
}
}
}
return new Size2D(x + maxWidth, constraint.getHeight());
}
/**
* Arranges the blocks without any constraints. This puts all blocks into a
* single column.
*
* @param container
* the container.
* @param g2
* the graphics device.
*
* @return The size after the arrangement.
*/
protected Size2D arrangeNN(final BlockContainer container, final GC g2) {
double y = 0.0;
double height = 0.0;
double maxWidth = 0.0;
final List blocks = container.getBlocks();
final int blockCount = blocks.size();
if (blockCount > 0) {
final Size2D[] sizes = new Size2D[blocks.size()];
for (int i = 0; i < blocks.size(); i++) {
final Block block = (Block) blocks.get(i);
sizes[i] = block.arrange(g2, RectangleConstraint.NONE);
height = height + sizes[i].getHeight();
maxWidth = Math.max(sizes[i].width, maxWidth);
block.setBounds(RectUtils.doubleRect(0.0, y, sizes[i].width,
sizes[i].height));
y = y + sizes[i].height + this.verticalGap;
}
if (blockCount > 1) {
height = height + this.verticalGap * (blockCount - 1);
}
if (this.horizontalAlignment != HorizontalAlignment.LEFT) {
for (int i = 0; i < blocks.size(); i++) {
// Block b = (Block) blocks.get(i);
if (this.horizontalAlignment == HorizontalAlignment.CENTER) {
// TODO: shift block right by half
} else if (this.horizontalAlignment == HorizontalAlignment.RIGHT) {
// TODO: shift block over to right
}
}
}
}
return new Size2D(maxWidth, height);
}
/**
* Arranges the blocks in the container using a fixed height and a range for
* the width.
*
* @param container
* the container.
* @param g2
* the graphics device.
* @param constraint
* the constraint.
*
* @return The size of the container after arrangement.
*/
protected Size2D arrangeRF(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
final Size2D s = this.arrangeNF(container, g2, constraint);
if (constraint.getWidthRange().contains(s.width)) {
return s;
} else {
final RectangleConstraint c = constraint.toFixedWidth(constraint
.getWidthRange().constrain(s.getWidth()));
return this.arrangeFF(container, g2, c);
}
}
protected Size2D arrangeRR(final BlockContainer container, final GC g2,
final RectangleConstraint constraint) {
// first arrange without constraints, and see if this fits within
// the required ranges...
final Size2D s1 = this.arrangeNN(container, g2);
if (constraint.getHeightRange().contains(s1.height)) {
return s1; // TODO: we didn't check the width yet
} else {
final RectangleConstraint c = constraint.toFixedHeight(constraint
.getHeightRange().getUpperBound());
return this.arrangeRF(container, g2, c);
}
}
/**
* Clears any cached information.
*/
public void clear() {
// no action required.
}
/**
* Tests this instance for equality with an arbitrary object.
*
* @param obj
* the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ColumnArrangement)) {
return false;
}
final ColumnArrangement that = (ColumnArrangement) obj;
if (this.horizontalAlignment != that.horizontalAlignment) {
return false;
}
if (this.verticalAlignment != that.verticalAlignment) {
return false;
}
if (this.horizontalGap != that.horizontalGap) {
return false;
}
if (this.verticalGap != that.verticalGap) {
return false;
}
return true;
}
}