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