    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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     * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    025     * Other names may be trademarks of their respective owners.]
    026     *
    027     * -------------------
    028     * BlockContainer.java
    029     * -------------------
    030     * (C) Copyright 2004-2008, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * Changes:
    036     * --------
    037     * 22-Oct-2004 : Version 1 (DG);
    038     * 02-Feb-2005 : Added isEmpty() method (DG);
    039     * 04-Feb-2005 : Added equals(), clone() and implemented Serializable (DG);
    040     * 08-Feb-2005 : Updated for changes in RectangleConstraint (DG);
    041     * 20-Apr-2005 : Added new draw() method (DG);
    042     * ------------- JFREECHART 1.0.x ---------------------------------------------
    043     * 20-Jul-2006 : Perform translation directly on drawing area, not via
    044     *               Graphics2D (DG);
    045     *
    046     */
    047
    048    package org.jfree.chart.block;
    049
    050    import java.awt.Graphics2D;
    051    import java.awt.geom.Rectangle2D;
    052    import java.io.Serializable;
    053    import java.util.ArrayList;
    054    import java.util.Collections;
    055    import java.util.Iterator;
    056    import java.util.List;
    057
    058    import org.jfree.chart.entity.EntityCollection;
    059    import org.jfree.chart.entity.StandardEntityCollection;
    060    import org.jfree.ui.Size2D;
    061    import org.jfree.util.PublicCloneable;
    062
    063    /**
    064     * A container for a collection of {@link Block} objects.  The container uses
    065     * an {@link Arrangement} object to handle the position of each block.
    066     */
    067    public class BlockContainer extends AbstractBlock
    068            implements Block, Cloneable, PublicCloneable, Serializable {
    069
    070        /** For serialization. */
    071        private static final long serialVersionUID = 8199508075695195293L;
    072
    073        /** The blocks within the container. */
    074        private List blocks;
    075
    076        /** The object responsible for laying out the blocks. */
    077        private Arrangement arrangement;
    078
    079        /**
    080         * Creates a new instance with default settings.
    081         */
    082        public BlockContainer() {
    083            this(new BorderArrangement());
    084        }
    085
    086        /**
    087         * Creates a new instance with the specified arrangement.
    088         *
    089         * @param arrangement  the arrangement manager (<code>null</code> not
    090         *                     permitted).
    091         */
    092        public BlockContainer(Arrangement arrangement) {
    093            if (arrangement == null) {
    094                throw new IllegalArgumentException("Null 'arrangement' argument.");
    095            }
    096            this.arrangement = arrangement;
    097            this.blocks = new ArrayList();
    098        }
    099
    100        /**
    101         * Returns the arrangement (layout) manager for the container.
    102         *
    103         * @return The arrangement manager (never <code>null</code>).
    104         */
    105        public Arrangement getArrangement() {
    106            return this.arrangement;
    107        }
    108
    109        /**
    110         * Sets the arrangement (layout) manager.
    111         *
    112         * @param arrangement  the arrangement (<code>null</code> not permitted).
    113         */
    114        public void setArrangement(Arrangement arrangement) {
    115            if (arrangement == null) {
    116                throw new IllegalArgumentException("Null 'arrangement' argument.");
    117            }
    118            this.arrangement = arrangement;
    119        }
    120
    121        /**
    122         * Returns <code>true</code> if there are no blocks in the container, and
    123         * <code>false</code> otherwise.
    124         *
    125         * @return A boolean.
    126         */
    127        public boolean isEmpty() {
    128            return this.blocks.isEmpty();
    129        }
    130
    131        /**
    132         * Returns an unmodifiable list of the {@link Block} objects managed by
    133         * this arrangement.
    134         *
    135         * @return A list of blocks.
    136         */
    137        public List getBlocks() {
    138            return Collections.unmodifiableList(this.blocks);
    139        }
    140
    141        /**
    142         * Adds a block to the container.
    143         *
    144         * @param block  the block (<code>null</code> permitted).
    145         */
    146        public void add(Block block) {
    147            add(block, null);
    148        }
    149
    150        /**
    151         * Adds a block to the container.
    152         *
    153         * @param block  the block (<code>null</code> permitted).
    154         * @param key  the key (<code>null</code> permitted).
    155         */
    156        public void add(Block block, Object key) {
    157            this.blocks.add(block);
    158            this.arrangement.add(block, key);
    159        }
    160
    161        /**
    162         * Clears all the blocks from the container.
    163         */
    164        public void clear() {
    165            this.blocks.clear();
    166            this.arrangement.clear();
    167        }
    168
    169        /**
    170         * Arranges the contents of the block, within the given constraints, and
    171         * returns the block size.
    172         *
    173         * @param g2  the graphics device.
    174         * @param constraint  the constraint (<code>null</code> not permitted).
    175         *
    176         * @return The block size (in Java2D units, never <code>null</code>).
    177         */
    178        public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
    179            return this.arrangement.arrange(this, g2, constraint);
    180        }
    181
    182        /**
    183         * Draws the container and all the blocks within it.
    184         *
    185         * @param g2  the graphics device.
    186         * @param area  the area.
    187         */
    188        public void draw(Graphics2D g2, Rectangle2D area) {
    189            draw(g2, area, null);
    190        }
    191
    192        /**
    193         * Draws the block within the specified area.
    194         *
    195         * @param g2  the graphics device.
    196         * @param area  the area.
    197         * @param params  passed on to blocks within the container
    198         *                (<code>null</code> permitted).
    199         *
    200         * @return An instance of {@link EntityBlockResult}, or <code>null</code>.
    201         */
    202        public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
    203            // check if we need to collect chart entities from the container
    204            EntityBlockParams ebp = null;
    205            StandardEntityCollection sec = null;
    206            if (params instanceof EntityBlockParams) {
    207                ebp = (EntityBlockParams) params;
    208                if (ebp.getGenerateEntities()) {
    209                    sec = new StandardEntityCollection();
    210                }
    211            }
    212            Rectangle2D contentArea = (Rectangle2D) area.clone();
    213            contentArea = trimMargin(contentArea);
    214            drawBorder(g2, contentArea);
    215            contentArea = trimBorder(contentArea);
    216            contentArea = trimPadding(contentArea);
    217            Iterator iterator = this.blocks.iterator();
    218            while (iterator.hasNext()) {
    219                Block block = (Block) iterator.next();
    220                Rectangle2D bounds = block.getBounds();
    221                Rectangle2D drawArea = new Rectangle2D.Double(bounds.getX()
    222                        + area.getX(), bounds.getY() + area.getY(),
    223                        bounds.getWidth(), bounds.getHeight());
    224                Object r = block.draw(g2, drawArea, params);
    225                if (sec != null) {
    226                    if (r instanceof EntityBlockResult) {
    227                        EntityBlockResult ebr = (EntityBlockResult) r;
    228                        EntityCollection ec = ebr.getEntityCollection();
    229                        sec.addAll(ec);
    230                    }
    231                }
    232            }
    233            BlockResult result = null;
    234            if (sec != null) {
    235                result = new BlockResult();
    236                result.setEntityCollection(sec);
    237            }
    238            return result;
    239        }
    240
    241        /**
    242         * Tests this container for equality with an arbitrary object.
    243         *
    244         * @param obj  the object (<code>null</code> permitted).
    245         *
    246         * @return A boolean.
    247         */
    248        public boolean equals(Object obj) {
    249            if (obj == this) {
    250                return true;
    251            }
    252            if (!(obj instanceof BlockContainer)) {
    253                return false;
    254            }
    255            if (!super.equals(obj)) {
    256                return false;
    257            }
    258            BlockContainer that = (BlockContainer) obj;
    259            if (!this.arrangement.equals(that.arrangement)) {
    260                return false;
    261            }
    262            if (!this.blocks.equals(that.blocks)) {
    263                return false;
    264            }
    265            return true;
    266        }
    267
    268        /**
    269         * Returns a clone of the container.
    270         *
    271         * @return A clone.
    272         *
    273         * @throws CloneNotSupportedException if there is a problem cloning.
    274         */
    275        public Object clone() throws CloneNotSupportedException {
    276            BlockContainer clone = (BlockContainer) super.clone();
    277            // TODO : complete this
    278            return clone;
    279        }
    280
    281    }