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 * CenterArrangement.java
029 * ----------------------
030 * (C) Copyright 2005-2008, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 08-Mar-2005 : Version 1 (DG);
038 * ------------- JFREECHART 1.0.x ---------------------------------------------
039 * 20-Jul-2006 : Set bounds of contained block when arranging (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.List;
049import org.jfree.chart.ui.Size2D;
050
051/**
052 * Arranges a block in the center of its container.  This class is immutable.
053 */
054public class CenterArrangement implements Arrangement, Serializable {
055
056    /** For serialization. */
057    private static final long serialVersionUID = -353308149220382047L;
058
059    /**
060     * Creates a new instance.
061     */
062    public CenterArrangement() {
063    }
064
065    /**
066     * Adds a block to be managed by this instance.  This method is usually
067     * called by the {@link BlockContainer}, you shouldn't need to call it
068     * directly.
069     *
070     * @param block  the block.
071     * @param key  a key that controls the position of the block.
072     */
073    @Override
074    public void add(Block block, Object key) {
075        // since the flow layout is relatively straightforward,
076        // no information needs to be recorded here
077    }
078
079    /**
080     * Calculates and sets the bounds of all the items in the specified
081     * container, subject to the given constraint.  The {@code Graphics2D}
082     * can be used by some items (particularly items containing text) to
083     * calculate sizing parameters.
084     *
085     * @param container  the container whose items are being arranged.
086     * @param g2  the graphics device.
087     * @param constraint  the size constraint.
088     *
089     * @return The size of the container after arrangement of the contents.
090     */
091    @Override
092    public Size2D arrange(BlockContainer container, Graphics2D g2,
093                          RectangleConstraint constraint) {
094
095        LengthConstraintType w = constraint.getWidthConstraintType();
096        LengthConstraintType h = constraint.getHeightConstraintType();
097        if (w == LengthConstraintType.NONE) {
098            if (h == LengthConstraintType.NONE) {
099                return arrangeNN(container, g2);
100            }
101            else if (h == LengthConstraintType.FIXED) {
102                throw new RuntimeException("Not implemented.");
103            }
104            else if (h == LengthConstraintType.RANGE) {
105                throw new RuntimeException("Not implemented.");
106            }
107        }
108        else if (w == LengthConstraintType.FIXED) {
109            if (h == LengthConstraintType.NONE) {
110                return arrangeFN(container, g2, constraint);
111            }
112            else if (h == LengthConstraintType.FIXED) {
113                throw new RuntimeException("Not implemented.");
114            }
115            else if (h == LengthConstraintType.RANGE) {
116                throw new RuntimeException("Not implemented.");
117            }
118        }
119        else if (w == LengthConstraintType.RANGE) {
120            if (h == LengthConstraintType.NONE) {
121                return arrangeRN(container, g2, constraint);
122            }
123            else if (h == LengthConstraintType.FIXED) {
124                return arrangeRF(container, g2, constraint);
125            }
126            else if (h == LengthConstraintType.RANGE) {
127                return arrangeRR(container, g2, constraint);
128            }
129        }
130        throw new IllegalArgumentException("Unknown LengthConstraintType.");
131
132    }
133
134    /**
135     * Arranges the blocks in the container with a fixed width and no height
136     * constraint.
137     *
138     * @param container  the container.
139     * @param g2  the graphics device.
140     * @param constraint  the constraint.
141     *
142     * @return The size.
143     */
144    protected Size2D arrangeFN(BlockContainer container, Graphics2D g2,
145                               RectangleConstraint constraint) {
146
147        List blocks = container.getBlocks();
148        Block b = (Block) blocks.get(0);
149        Size2D s = b.arrange(g2, RectangleConstraint.NONE);
150        double width = constraint.getWidth();
151        Rectangle2D bounds = new Rectangle2D.Double((width - s.width) / 2.0,
152                0.0, s.width, s.height);
153        b.setBounds(bounds);
154        return new Size2D((width - s.width) / 2.0, s.height);
155    }
156
157    /**
158     * Arranges the blocks in the container with a fixed with and a range
159     * constraint on the height.
160     *
161     * @param container  the container.
162     * @param g2  the graphics device.
163     * @param constraint  the constraint.
164     *
165     * @return The size following the arrangement.
166     */
167    protected Size2D arrangeFR(BlockContainer container, Graphics2D g2,
168                               RectangleConstraint constraint) {
169
170        Size2D s = arrangeFN(container, g2, constraint);
171        if (constraint.getHeightRange().contains(s.height)) {
172            return s;
173        }
174        else {
175            RectangleConstraint c = constraint.toFixedHeight(
176                    constraint.getHeightRange().constrain(s.getHeight()));
177            return arrangeFF(container, g2, c);
178        }
179    }
180
181    /**
182     * Arranges the blocks in the container with the overall height and width
183     * specified as fixed constraints.
184     *
185     * @param container  the container.
186     * @param g2  the graphics device.
187     * @param constraint  the constraint.
188     *
189     * @return The size following the arrangement.
190     */
191    protected Size2D arrangeFF(BlockContainer container, Graphics2D g2,
192                               RectangleConstraint constraint) {
193
194        // TODO: implement this properly
195        return arrangeFN(container, g2, constraint);
196    }
197
198    /**
199     * Arranges the blocks with the overall width and height to fit within
200     * specified ranges.
201     *
202     * @param container  the container.
203     * @param g2  the graphics device.
204     * @param constraint  the constraint.
205     *
206     * @return The size after the arrangement.
207     */
208    protected Size2D arrangeRR(BlockContainer container, Graphics2D g2,
209                               RectangleConstraint constraint) {
210
211        // first arrange without constraints, and see if this fits within
212        // the required ranges...
213        Size2D s1 = arrangeNN(container, g2);
214        if (constraint.getWidthRange().contains(s1.width)) {
215            return s1;  // TODO: we didn't check the height yet
216        }
217        else {
218            RectangleConstraint c = constraint.toFixedWidth(
219                    constraint.getWidthRange().getUpperBound());
220            return arrangeFR(container, g2, c);
221        }
222    }
223
224    /**
225     * Arranges the blocks in the container with a range constraint on the
226     * width and a fixed height.
227     *
228     * @param container  the container.
229     * @param g2  the graphics device.
230     * @param constraint  the constraint.
231     *
232     * @return The size following the arrangement.
233     */
234    protected Size2D arrangeRF(BlockContainer container, Graphics2D g2,
235                               RectangleConstraint constraint) {
236
237        Size2D s = arrangeNF(container, g2, constraint);
238        if (constraint.getWidthRange().contains(s.width)) {
239            return s;
240        }
241        else {
242            RectangleConstraint c = constraint.toFixedWidth(
243                    constraint.getWidthRange().constrain(s.getWidth()));
244            return arrangeFF(container, g2, c);
245        }
246    }
247
248    /**
249     * Arranges the block with a range constraint on the width, and no
250     * constraint on the height.
251     *
252     * @param container  the container.
253     * @param g2  the graphics device.
254     * @param constraint  the constraint.
255     *
256     * @return The size following the arrangement.
257     */
258    protected Size2D arrangeRN(BlockContainer container, Graphics2D g2,
259                               RectangleConstraint constraint) {
260        // first arrange without constraints, then see if the width fits
261        // within the required range...if not, call arrangeFN() at max width
262        Size2D s1 = arrangeNN(container, g2);
263        if (constraint.getWidthRange().contains(s1.width)) {
264            return s1;
265        }
266        else {
267            RectangleConstraint c = constraint.toFixedWidth(
268                    constraint.getWidthRange().getUpperBound());
269            return arrangeFN(container, g2, c);
270        }
271    }
272
273    /**
274     * Arranges the blocks without any constraints.  This puts all blocks
275     * into a single row.
276     *
277     * @param container  the container.
278     * @param g2  the graphics device.
279     *
280     * @return The size after the arrangement.
281     */
282    protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) {
283        List blocks = container.getBlocks();
284        Block b = (Block) blocks.get(0);
285        Size2D s = b.arrange(g2, RectangleConstraint.NONE);
286        b.setBounds(new Rectangle2D.Double(0.0, 0.0, s.width, s.height));
287        return new Size2D(s.width, s.height);
288    }
289
290    /**
291     * Arranges the blocks with no width constraint and a fixed height
292     * constraint.  This puts all blocks into a single row.
293     *
294     * @param container  the container.
295     * @param g2  the graphics device.
296     * @param constraint  the constraint.
297     *
298     * @return The size after the arrangement.
299     */
300    protected Size2D arrangeNF(BlockContainer container, Graphics2D g2,
301                               RectangleConstraint constraint) {
302        // TODO: for now we are ignoring the height constraint
303        return arrangeNN(container, g2);
304    }
305
306    /**
307     * Clears any cached information.
308     */
309    @Override
310    public void clear() {
311        // no action required.
312    }
313
314    /**
315     * Tests this instance for equality with an arbitrary object.
316     *
317     * @param obj  the object ({@code null} permitted).
318     *
319     * @return A boolean.
320     */
321    @Override
322    public boolean equals(Object obj) {
323        if (obj == this) {
324            return true;
325        }
326        if (!(obj instanceof CenterArrangement)) {
327            return false;
328        }
329        return true;
330    }
331
332}