001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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-2013, 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 * 02-Jul-2013 : Use ParamChecks (DG);
046 *
047 */
048
049package org.jfree.chart.block;
050
051import java.awt.Graphics2D;
052import java.awt.geom.Rectangle2D;
053import java.io.Serializable;
054import java.util.ArrayList;
055import java.util.Collections;
056import java.util.Iterator;
057import java.util.List;
058
059import org.jfree.chart.entity.EntityCollection;
060import org.jfree.chart.entity.StandardEntityCollection;
061import org.jfree.chart.util.ParamChecks;
062import org.jfree.ui.Size2D;
063import org.jfree.util.PublicCloneable;
064
065/**
066 * A container for a collection of {@link Block} objects.  The container uses
067 * an {@link Arrangement} object to handle the position of each block.
068 */
069public class BlockContainer extends AbstractBlock
070        implements Block, Cloneable, PublicCloneable, Serializable {
071
072    /** For serialization. */
073    private static final long serialVersionUID = 8199508075695195293L;
074
075    /** The blocks within the container. */
076    private List blocks;
077
078    /** The object responsible for laying out the blocks. */
079    private Arrangement arrangement;
080
081    /**
082     * Creates a new instance with default settings.
083     */
084    public BlockContainer() {
085        this(new BorderArrangement());
086    }
087
088    /**
089     * Creates a new instance with the specified arrangement.
090     *
091     * @param arrangement  the arrangement manager (<code>null</code> not
092     *                     permitted).
093     */
094    public BlockContainer(Arrangement arrangement) {
095        ParamChecks.nullNotPermitted(arrangement, "arrangement");
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        ParamChecks.nullNotPermitted(arrangement, "arrangement");
116        this.arrangement = arrangement;
117    }
118
119    /**
120     * Returns <code>true</code> if there are no blocks in the container, and
121     * <code>false</code> otherwise.
122     *
123     * @return A boolean.
124     */
125    public boolean isEmpty() {
126        return this.blocks.isEmpty();
127    }
128
129    /**
130     * Returns an unmodifiable list of the {@link Block} objects managed by
131     * this arrangement.
132     *
133     * @return A list of blocks.
134     */
135    public List getBlocks() {
136        return Collections.unmodifiableList(this.blocks);
137    }
138
139    /**
140     * Adds a block to the container.
141     *
142     * @param block  the block (<code>null</code> permitted).
143     */
144    public void add(Block block) {
145        add(block, null);
146    }
147
148    /**
149     * Adds a block to the container.
150     *
151     * @param block  the block (<code>null</code> permitted).
152     * @param key  the key (<code>null</code> permitted).
153     */
154    public void add(Block block, Object key) {
155        this.blocks.add(block);
156        this.arrangement.add(block, key);
157    }
158
159    /**
160     * Clears all the blocks from the container.
161     */
162    public void clear() {
163        this.blocks.clear();
164        this.arrangement.clear();
165    }
166
167    /**
168     * Arranges the contents of the block, within the given constraints, and
169     * returns the block size.
170     *
171     * @param g2  the graphics device.
172     * @param constraint  the constraint (<code>null</code> not permitted).
173     *
174     * @return The block size (in Java2D units, never <code>null</code>).
175     */
176    @Override
177    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
178        return this.arrangement.arrange(this, g2, constraint);
179    }
180
181    /**
182     * Draws the container and all the blocks within it.
183     *
184     * @param g2  the graphics device.
185     * @param area  the area.
186     */
187    @Override
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    @Override
203    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
204        // check if we need to collect chart entities from the container
205        EntityBlockParams ebp;
206        StandardEntityCollection sec = null;
207        if (params instanceof EntityBlockParams) {
208            ebp = (EntityBlockParams) params;
209            if (ebp.getGenerateEntities()) {
210                sec = new StandardEntityCollection();
211            }
212        }
213        Rectangle2D contentArea = (Rectangle2D) area.clone();
214        contentArea = trimMargin(contentArea);
215        drawBorder(g2, contentArea);
216        contentArea = trimBorder(contentArea);
217        contentArea = trimPadding(contentArea);
218        Iterator iterator = this.blocks.iterator();
219        while (iterator.hasNext()) {
220            Block block = (Block) iterator.next();
221            Rectangle2D bounds = block.getBounds();
222            Rectangle2D drawArea = new Rectangle2D.Double(bounds.getX()
223                    + area.getX(), bounds.getY() + area.getY(),
224                    bounds.getWidth(), bounds.getHeight());
225            Object r = block.draw(g2, drawArea, params);
226            if (sec != null) {
227                if (r instanceof EntityBlockResult) {
228                    EntityBlockResult ebr = (EntityBlockResult) r;
229                    EntityCollection ec = ebr.getEntityCollection();
230                    sec.addAll(ec);
231                }
232            }
233        }
234        BlockResult result = null;
235        if (sec != null) {
236            result = new BlockResult();
237            result.setEntityCollection(sec);
238        }
239        return result;
240    }
241
242    /**
243     * Tests this container for equality with an arbitrary object.
244     *
245     * @param obj  the object (<code>null</code> permitted).
246     *
247     * @return A boolean.
248     */
249    @Override
250    public boolean equals(Object obj) {
251        if (obj == this) {
252            return true;
253        }
254        if (!(obj instanceof BlockContainer)) {
255            return false;
256        }
257        if (!super.equals(obj)) {
258            return false;
259        }
260        BlockContainer that = (BlockContainer) obj;
261        if (!this.arrangement.equals(that.arrangement)) {
262            return false;
263        }
264        if (!this.blocks.equals(that.blocks)) {
265            return false;
266        }
267        return true;
268    }
269
270    /**
271     * Returns a clone of the container.
272     *
273     * @return A clone.
274     *
275     * @throws CloneNotSupportedException if there is a problem cloning.
276     */
277    @Override
278    public Object clone() throws CloneNotSupportedException {
279        BlockContainer clone = (BlockContainer) super.clone();
280        // TODO : complete this
281        return clone;
282    }
283
284}