001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * FlowArrangement.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 * 04-Feb-2005 : Implemented equals() and made serializable (DG);
039 * 08-Feb-2005 : Updated for changes in RectangleConstraint (DG);
040 *
041 */
042
043package org.jfree.chart.block;
044
045import java.awt.Graphics2D;
046import java.awt.geom.Rectangle2D;
047import java.io.Serializable;
048import java.util.ArrayList;
049import java.util.List;
050import org.jfree.chart.ui.HorizontalAlignment;
051import org.jfree.chart.ui.Size2D;
052import org.jfree.chart.ui.VerticalAlignment;
053
054/**
055 * Arranges blocks in a flow layout.  This class is immutable.
056 */
057public class FlowArrangement implements Arrangement, Serializable {
058
059    /** For serialization. */
060    private static final long serialVersionUID = 4543632485478613800L;
061
062    /** The horizontal alignment of blocks. */
063    private HorizontalAlignment horizontalAlignment;
064
065    /** The vertical alignment of blocks within each row. */
066    private VerticalAlignment verticalAlignment;
067
068    /** The horizontal gap between items within rows. */
069    private double horizontalGap;
070
071    /** The vertical gap between rows. */
072    private double verticalGap;
073
074    /**
075     * Creates a new instance.
076     */
077    public FlowArrangement() {
078        this(HorizontalAlignment.CENTER, VerticalAlignment.CENTER, 2.0, 2.0);
079    }
080
081    /**
082     * Creates a new instance.
083     *
084     * @param hAlign  the horizontal alignment (currently ignored).
085     * @param vAlign  the vertical alignment (currently ignored).
086     * @param hGap  the horizontal gap.
087     * @param vGap  the vertical gap.
088     */
089    public FlowArrangement(HorizontalAlignment hAlign, VerticalAlignment vAlign,
090                           double hGap, double vGap) {
091        this.horizontalAlignment = hAlign;
092        this.verticalAlignment = vAlign;
093        this.horizontalGap = hGap;
094        this.verticalGap = vGap;
095    }
096
097    /**
098     * Adds a block to be managed by this instance.  This method is usually
099     * called by the {@link BlockContainer}, you shouldn't need to call it
100     * directly.
101     *
102     * @param block  the block.
103     * @param key  a key that controls the position of the block.
104     */
105    @Override
106    public void add(Block block, Object key) {
107        // since the flow layout is relatively straightforward,
108        // no information needs to be recorded here
109    }
110
111    /**
112     * Calculates and sets the bounds of all the items in the specified
113     * container, subject to the given constraint.  The {@code Graphics2D}
114     * can be used by some items (particularly items containing text) to
115     * calculate sizing parameters.
116     *
117     * @param container  the container whose items are being arranged.
118     * @param constraint  the size constraint.
119     * @param g2  the graphics device.
120     *
121     * @return The size of the container after arrangement of the contents.
122     */
123    @Override
124    public Size2D arrange(BlockContainer container, Graphics2D g2,
125                          RectangleConstraint constraint) {
126
127        LengthConstraintType w = constraint.getWidthConstraintType();
128        LengthConstraintType h = constraint.getHeightConstraintType();
129        if (w == LengthConstraintType.NONE) {
130            if (h == LengthConstraintType.NONE) {
131                return arrangeNN(container, g2);
132            }
133            else if (h == LengthConstraintType.FIXED) {
134                return arrangeNF(container, g2, constraint);
135            }
136            else if (h == LengthConstraintType.RANGE) {
137                throw new RuntimeException("Not implemented.");
138            }
139        }
140        else if (w == LengthConstraintType.FIXED) {
141            if (h == LengthConstraintType.NONE) {
142                return arrangeFN(container, g2, constraint);
143            }
144            else if (h == LengthConstraintType.FIXED) {
145                return arrangeFF(container, g2, constraint);
146            }
147            else if (h == LengthConstraintType.RANGE) {
148                return arrangeFR(container, g2, constraint);
149            }
150        }
151        else if (w == LengthConstraintType.RANGE) {
152            if (h == LengthConstraintType.NONE) {
153                return arrangeRN(container, g2, constraint);
154            }
155            else if (h == LengthConstraintType.FIXED) {
156                return arrangeRF(container, g2, constraint);
157            }
158            else if (h == LengthConstraintType.RANGE) {
159                return arrangeRR(container, g2, constraint);
160            }
161        }
162        throw new RuntimeException("Unrecognised constraint type.");
163
164    }
165
166    /**
167     * Arranges the blocks in the container with a fixed width and no height
168     * constraint.
169     *
170     * @param container  the container.
171     * @param constraint  the constraint.
172     * @param g2  the graphics device.
173     *
174     * @return The size.
175     */
176    protected Size2D arrangeFN(BlockContainer container, Graphics2D g2,
177                               RectangleConstraint constraint) {
178
179        List blocks = container.getBlocks();
180        double width = constraint.getWidth();
181
182        double x = 0.0;
183        double y = 0.0;
184        double maxHeight = 0.0;
185        List itemsInRow = new ArrayList();
186        for (int i = 0; i < blocks.size(); i++) {
187            Block block = (Block) blocks.get(i);
188            Size2D size = block.arrange(g2, RectangleConstraint.NONE);
189            if (x + size.width <= width) {
190                itemsInRow.add(block);
191                block.setBounds(
192                    new Rectangle2D.Double(x, y, size.width, size.height)
193                );
194                x = x + size.width + this.horizontalGap;
195                maxHeight = Math.max(maxHeight, size.height);
196            }
197            else {
198                if (itemsInRow.isEmpty()) {
199                    // place in this row (truncated) anyway
200                    block.setBounds(
201                        new Rectangle2D.Double(
202                            x, y, Math.min(size.width, width - x), size.height
203                        )
204                    );
205                    x = 0.0;
206                    y = y + size.height + this.verticalGap;
207                }
208                else {
209                    // start new row
210                    itemsInRow.clear();
211                    x = 0.0;
212                    y = y + maxHeight + this.verticalGap;
213                    maxHeight = size.height;
214                    block.setBounds(
215                        new Rectangle2D.Double(
216                            x, y, Math.min(size.width, width), size.height
217                        )
218                    );
219                    x = size.width + this.horizontalGap;
220                    itemsInRow.add(block);
221                }
222            }
223        }
224        return new Size2D(constraint.getWidth(), y + maxHeight);
225    }
226
227    /**
228     * Arranges the blocks in the container with a fixed width and a range
229     * constraint on the height.
230     *
231     * @param container  the container.
232     * @param constraint  the constraint.
233     * @param g2  the graphics device.
234     *
235     * @return The size following the arrangement.
236     */
237    protected Size2D arrangeFR(BlockContainer container, Graphics2D g2,
238                               RectangleConstraint constraint) {
239
240        Size2D s = arrangeFN(container, g2, constraint);
241        if (constraint.getHeightRange().contains(s.height)) {
242            return s;
243        }
244        else {
245            RectangleConstraint c = constraint.toFixedHeight(
246                constraint.getHeightRange().constrain(s.getHeight())
247            );
248            return arrangeFF(container, g2, c);
249        }
250    }
251
252    /**
253     * Arranges the blocks in the container with the overall height and width
254     * specified as fixed constraints.
255     *
256     * @param container  the container.
257     * @param constraint  the constraint.
258     * @param g2  the graphics device.
259     *
260     * @return The size following the arrangement.
261     */
262    protected Size2D arrangeFF(BlockContainer container, Graphics2D g2,
263                               RectangleConstraint constraint) {
264
265        // TODO: implement this properly
266        return arrangeFN(container, g2, constraint);
267    }
268
269    /**
270     * Arranges the blocks with the overall width and height to fit within
271     * specified ranges.
272     *
273     * @param container  the container.
274     * @param constraint  the constraint.
275     * @param g2  the graphics device.
276     *
277     * @return The size after the arrangement.
278     */
279    protected Size2D arrangeRR(BlockContainer container, Graphics2D g2,
280                               RectangleConstraint constraint) {
281
282        // first arrange without constraints, and see if this fits within
283        // the required ranges...
284        Size2D s1 = arrangeNN(container, g2);
285        if (constraint.getWidthRange().contains(s1.width)) {
286            return s1;  // TODO: we didn't check the height yet
287        }
288        else {
289            RectangleConstraint c = constraint.toFixedWidth(
290                constraint.getWidthRange().getUpperBound()
291            );
292            return arrangeFR(container, g2, c);
293        }
294    }
295
296    /**
297     * Arranges the blocks in the container with a range constraint on the
298     * width and a fixed height.
299     *
300     * @param container  the container.
301     * @param constraint  the constraint.
302     * @param g2  the graphics device.
303     *
304     * @return The size following the arrangement.
305     */
306    protected Size2D arrangeRF(BlockContainer container, Graphics2D g2,
307                               RectangleConstraint constraint) {
308
309        Size2D s = arrangeNF(container, g2, constraint);
310        if (constraint.getWidthRange().contains(s.width)) {
311            return s;
312        }
313        else {
314            RectangleConstraint c = constraint.toFixedWidth(
315                constraint.getWidthRange().constrain(s.getWidth())
316            );
317            return arrangeFF(container, g2, c);
318        }
319    }
320
321    /**
322     * Arranges the block with a range constraint on the width, and no
323     * constraint on the height.
324     *
325     * @param container  the container.
326     * @param constraint  the constraint.
327     * @param g2  the graphics device.
328     *
329     * @return The size following the arrangement.
330     */
331    protected Size2D arrangeRN(BlockContainer container, Graphics2D g2,
332                               RectangleConstraint constraint) {
333        // first arrange without constraints, then see if the width fits
334        // within the required range...if not, call arrangeFN() at max width
335        Size2D s1 = arrangeNN(container, g2);
336        if (constraint.getWidthRange().contains(s1.width)) {
337            return s1;
338        }
339        else {
340            RectangleConstraint c = constraint.toFixedWidth(
341                constraint.getWidthRange().getUpperBound()
342            );
343            return arrangeFN(container, g2, c);
344        }
345    }
346
347    /**
348     * Arranges the blocks without any constraints.  This puts all blocks
349     * into a single row.
350     *
351     * @param container  the container.
352     * @param g2  the graphics device.
353     *
354     * @return The size after the arrangement.
355     */
356    protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) {
357        double x = 0.0;
358        double width = 0.0;
359        double maxHeight = 0.0;
360        List blocks = container.getBlocks();
361        int blockCount = blocks.size();
362        if (blockCount > 0) {
363            Size2D[] sizes = new Size2D[blocks.size()];
364            for (int i = 0; i < blocks.size(); i++) {
365                Block block = (Block) blocks.get(i);
366                sizes[i] = block.arrange(g2, RectangleConstraint.NONE);
367                width = width + sizes[i].getWidth();
368                maxHeight = Math.max(sizes[i].height, maxHeight);
369                block.setBounds(
370                    new Rectangle2D.Double(
371                        x, 0.0, sizes[i].width, sizes[i].height
372                    )
373                );
374                x = x + sizes[i].width + this.horizontalGap;
375            }
376            if (blockCount > 1) {
377                width = width + this.horizontalGap * (blockCount - 1);
378            }
379            if (this.verticalAlignment != VerticalAlignment.TOP) {
380                for (int i = 0; i < blocks.size(); i++) {
381                    //Block b = (Block) blocks.get(i);
382                    if (this.verticalAlignment == VerticalAlignment.CENTER) {
383                        //TODO: shift block down by half
384                    }
385                    else if (this.verticalAlignment
386                            == VerticalAlignment.BOTTOM) {
387                        //TODO: shift block down to bottom
388                    }
389                }
390            }
391        }
392        return new Size2D(width, maxHeight);
393    }
394
395    /**
396     * Arranges the blocks with no width constraint and a fixed height
397     * constraint.  This puts all blocks into a single row.
398     *
399     * @param container  the container.
400     * @param constraint  the constraint.
401     * @param g2  the graphics device.
402     *
403     * @return The size after the arrangement.
404     */
405    protected Size2D arrangeNF(BlockContainer container, Graphics2D g2,
406                               RectangleConstraint constraint) {
407        // TODO: for now we are ignoring the height constraint
408        return arrangeNN(container, g2);
409    }
410
411    /**
412     * Clears any cached information.
413     */
414    @Override
415    public void clear() {
416        // no action required.
417    }
418
419    /**
420     * Tests this instance for equality with an arbitrary object.
421     *
422     * @param obj  the object ({@code null} permitted).
423     *
424     * @return A boolean.
425     */
426    @Override
427    public boolean equals(Object obj) {
428        if (obj == this) {
429            return true;
430        }
431        if (!(obj instanceof FlowArrangement)) {
432            return false;
433        }
434        FlowArrangement that = (FlowArrangement) obj;
435        if (this.horizontalAlignment != that.horizontalAlignment) {
436            return false;
437        }
438        if (this.verticalAlignment != that.verticalAlignment) {
439            return false;
440        }
441        if (this.horizontalGap != that.horizontalGap) {
442            return false;
443        }
444        if (this.verticalGap != that.verticalGap) {
445            return false;
446        }
447        return true;
448    }
449
450}