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 * GridArrangement.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-Feb-2005 : Version 1 (DG); 038 * 03-Dec-2008 : Implemented missing methods, and fixed bugs reported in 039 * patch 2370487 (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.Iterator; 049import java.util.List; 050import org.jfree.chart.ui.Size2D; 051 052/** 053 * Arranges blocks in a grid within their container. 054 */ 055public class GridArrangement implements Arrangement, Serializable { 056 057 /** For serialization. */ 058 private static final long serialVersionUID = -2563758090144655938L; 059 060 /** The rows. */ 061 private int rows; 062 063 /** The columns. */ 064 private int columns; 065 066 /** 067 * Creates a new grid arrangement. 068 * 069 * @param rows the row count. 070 * @param columns the column count. 071 */ 072 public GridArrangement(int rows, int columns) { 073 this.rows = rows; 074 this.columns = columns; 075 } 076 077 /** 078 * Adds a block and a key which can be used to determine the position of 079 * the block in the arrangement. This method is called by the container 080 * (you don't need to call this method directly) and gives the arrangement 081 * an opportunity to record the details if they are required. 082 * 083 * @param block the block. 084 * @param key the key ({@code null} permitted). 085 */ 086 @Override 087 public void add(Block block, Object key) { 088 // can safely ignore 089 } 090 091 /** 092 * Arranges the blocks within the specified container, subject to the given 093 * constraint. 094 * 095 * @param container the container ({@code null} not permitted). 096 * @param constraint the constraint. 097 * @param g2 the graphics device. 098 * 099 * @return The size following the arrangement. 100 */ 101 @Override 102 public Size2D arrange(BlockContainer container, Graphics2D g2, 103 RectangleConstraint constraint) { 104 LengthConstraintType w = constraint.getWidthConstraintType(); 105 LengthConstraintType h = constraint.getHeightConstraintType(); 106 if (w == LengthConstraintType.NONE) { 107 if (h == LengthConstraintType.NONE) { 108 return arrangeNN(container, g2); 109 } 110 else if (h == LengthConstraintType.FIXED) { 111 return arrangeNF(container, g2, constraint); 112 } 113 else if (h == LengthConstraintType.RANGE) { 114 // find optimum height, then map to range 115 return arrangeNR(container, g2, constraint); 116 } 117 } 118 else if (w == LengthConstraintType.FIXED) { 119 if (h == LengthConstraintType.NONE) { 120 // find optimum height 121 return arrangeFN(container, g2, constraint); 122 } 123 else if (h == LengthConstraintType.FIXED) { 124 return arrangeFF(container, g2, constraint); 125 } 126 else if (h == LengthConstraintType.RANGE) { 127 // find optimum height and map to range 128 return arrangeFR(container, g2, constraint); 129 } 130 } 131 else if (w == LengthConstraintType.RANGE) { 132 // find optimum width and map to range 133 if (h == LengthConstraintType.NONE) { 134 // find optimum height 135 return arrangeRN(container, g2, constraint); 136 } 137 else if (h == LengthConstraintType.FIXED) { 138 // fixed width 139 return arrangeRF(container, g2, constraint); 140 } 141 else if (h == LengthConstraintType.RANGE) { 142 return arrangeRR(container, g2, constraint); 143 } 144 } 145 throw new RuntimeException("Should never get to here!"); 146 } 147 148 /** 149 * Arranges the container with no constraint on the width or height. 150 * 151 * @param container the container ({@code null} not permitted). 152 * @param g2 the graphics device. 153 * 154 * @return The size. 155 */ 156 protected Size2D arrangeNN(BlockContainer container, Graphics2D g2) { 157 double maxW = 0.0; 158 double maxH = 0.0; 159 List blocks = container.getBlocks(); 160 Iterator iterator = blocks.iterator(); 161 while (iterator.hasNext()) { 162 Block b = (Block) iterator.next(); 163 if (b != null) { 164 Size2D s = b.arrange(g2, RectangleConstraint.NONE); 165 maxW = Math.max(maxW, s.width); 166 maxH = Math.max(maxH, s.height); 167 } 168 } 169 double width = this.columns * maxW; 170 double height = this.rows * maxH; 171 RectangleConstraint c = new RectangleConstraint(width, height); 172 return arrangeFF(container, g2, c); 173 } 174 175 /** 176 * Arranges the container with a fixed overall width and height. 177 * 178 * @param container the container ({@code null} not permitted). 179 * @param g2 the graphics device. 180 * @param constraint the constraint ({@code null} not permitted). 181 * 182 * @return The size following the arrangement. 183 */ 184 protected Size2D arrangeFF(BlockContainer container, Graphics2D g2, 185 RectangleConstraint constraint) { 186 double width = constraint.getWidth() / this.columns; 187 double height = constraint.getHeight() / this.rows; 188 List blocks = container.getBlocks(); 189 for (int c = 0; c < this.columns; c++) { 190 for (int r = 0; r < this.rows; r++) { 191 int index = r * this.columns + c; 192 if (index >= blocks.size()) { 193 break; 194 } 195 Block b = (Block) blocks.get(index); 196 if (b != null) { 197 b.setBounds(new Rectangle2D.Double(c * width, r * height, 198 width, height)); 199 } 200 } 201 } 202 return new Size2D(this.columns * width, this.rows * height); 203 } 204 205 /** 206 * Arrange with a fixed width and a height within a given range. 207 * 208 * @param container the container. 209 * @param constraint the constraint. 210 * @param g2 the graphics device. 211 * 212 * @return The size of the arrangement. 213 */ 214 protected Size2D arrangeFR(BlockContainer container, Graphics2D g2, 215 RectangleConstraint constraint) { 216 217 RectangleConstraint c1 = constraint.toUnconstrainedHeight(); 218 Size2D size1 = arrange(container, g2, c1); 219 220 if (constraint.getHeightRange().contains(size1.getHeight())) { 221 return size1; 222 } 223 else { 224 double h = constraint.getHeightRange().constrain(size1.getHeight()); 225 RectangleConstraint c2 = constraint.toFixedHeight(h); 226 return arrange(container, g2, c2); 227 } 228 } 229 230 /** 231 * Arrange with a fixed height and a width within a given range. 232 * 233 * @param container the container. 234 * @param constraint the constraint. 235 * @param g2 the graphics device. 236 * 237 * @return The size of the arrangement. 238 */ 239 protected Size2D arrangeRF(BlockContainer container, Graphics2D g2, 240 RectangleConstraint constraint) { 241 242 RectangleConstraint c1 = constraint.toUnconstrainedWidth(); 243 Size2D size1 = arrange(container, g2, c1); 244 245 if (constraint.getWidthRange().contains(size1.getWidth())) { 246 return size1; 247 } 248 else { 249 double w = constraint.getWidthRange().constrain(size1.getWidth()); 250 RectangleConstraint c2 = constraint.toFixedWidth(w); 251 return arrange(container, g2, c2); 252 } 253 } 254 255 /** 256 * Arrange with a fixed width and no height constraint. 257 * 258 * @param container the container. 259 * @param constraint the constraint. 260 * @param g2 the graphics device. 261 * 262 * @return The size of the arrangement. 263 */ 264 protected Size2D arrangeRN(BlockContainer container, Graphics2D g2, 265 RectangleConstraint constraint) { 266 267 RectangleConstraint c1 = constraint.toUnconstrainedWidth(); 268 Size2D size1 = arrange(container, g2, c1); 269 270 if (constraint.getWidthRange().contains(size1.getWidth())) { 271 return size1; 272 } 273 else { 274 double w = constraint.getWidthRange().constrain(size1.getWidth()); 275 RectangleConstraint c2 = constraint.toFixedWidth(w); 276 return arrange(container, g2, c2); 277 } 278 } 279 280 /** 281 * Arrange with a fixed height and no width constraint. 282 * 283 * @param container the container. 284 * @param constraint the constraint. 285 * @param g2 the graphics device. 286 * 287 * @return The size of the arrangement. 288 */ 289 protected Size2D arrangeNR(BlockContainer container, Graphics2D g2, 290 RectangleConstraint constraint) { 291 292 RectangleConstraint c1 = constraint.toUnconstrainedHeight(); 293 Size2D size1 = arrange(container, g2, c1); 294 295 if (constraint.getHeightRange().contains(size1.getHeight())) { 296 return size1; 297 } 298 else { 299 double h = constraint.getHeightRange().constrain(size1.getHeight()); 300 RectangleConstraint c2 = constraint.toFixedHeight(h); 301 return arrange(container, g2, c2); 302 } 303 } 304 305 /** 306 * Arrange with ranges for both the width and height constraints. 307 * 308 * @param container the container. 309 * @param constraint the constraint. 310 * @param g2 the graphics device. 311 * 312 * @return The size of the arrangement. 313 */ 314 protected Size2D arrangeRR(BlockContainer container, Graphics2D g2, 315 RectangleConstraint constraint) { 316 317 Size2D size1 = arrange(container, g2, RectangleConstraint.NONE); 318 319 if (constraint.getWidthRange().contains(size1.getWidth())) { 320 if (constraint.getHeightRange().contains(size1.getHeight())) { 321 return size1; 322 } 323 else { 324 // width is OK, but height must be constrained 325 double h = constraint.getHeightRange().constrain( 326 size1.getHeight()); 327 RectangleConstraint cc = new RectangleConstraint( 328 size1.getWidth(), h); 329 return arrangeFF(container, g2, cc); 330 } 331 } 332 else { 333 if (constraint.getHeightRange().contains(size1.getHeight())) { 334 // height is OK, but width must be constrained 335 double w = constraint.getWidthRange().constrain( 336 size1.getWidth()); 337 RectangleConstraint cc = new RectangleConstraint(w, 338 size1.getHeight()); 339 return arrangeFF(container, g2, cc); 340 341 } 342 else { 343 double w = constraint.getWidthRange().constrain( 344 size1.getWidth()); 345 double h = constraint.getHeightRange().constrain( 346 size1.getHeight()); 347 RectangleConstraint cc = new RectangleConstraint(w, h); 348 return arrangeFF(container, g2, cc); 349 } 350 } 351 } 352 353 /** 354 * Arrange with a fixed width and a height within a given range. 355 * 356 * @param container the container. 357 * @param g2 the graphics device. 358 * @param constraint the constraint. 359 * 360 * @return The size of the arrangement. 361 */ 362 protected Size2D arrangeFN(BlockContainer container, Graphics2D g2, 363 RectangleConstraint constraint) { 364 365 double width = constraint.getWidth() / this.columns; 366 RectangleConstraint bc = constraint.toFixedWidth(width); 367 List blocks = container.getBlocks(); 368 double maxH = 0.0; 369 for (int r = 0; r < this.rows; r++) { 370 for (int c = 0; c < this.columns; c++) { 371 int index = r * this.columns + c; 372 if (index >= blocks.size()) { 373 break; 374 } 375 Block b = (Block) blocks.get(index); 376 if (b != null) { 377 Size2D s = b.arrange(g2, bc); 378 maxH = Math.max(maxH, s.getHeight()); 379 } 380 } 381 } 382 RectangleConstraint cc = constraint.toFixedHeight(maxH * this.rows); 383 return arrange(container, g2, cc); 384 } 385 386 /** 387 * Arrange with a fixed height and no constraint for the width. 388 * 389 * @param container the container. 390 * @param g2 the graphics device. 391 * @param constraint the constraint. 392 * 393 * @return The size of the arrangement. 394 */ 395 protected Size2D arrangeNF(BlockContainer container, Graphics2D g2, 396 RectangleConstraint constraint) { 397 398 double height = constraint.getHeight() / this.rows; 399 RectangleConstraint bc = constraint.toFixedHeight(height); 400 List blocks = container.getBlocks(); 401 double maxW = 0.0; 402 for (int r = 0; r < this.rows; r++) { 403 for (int c = 0; c < this.columns; c++) { 404 int index = r * this.columns + c; 405 if (index >= blocks.size()) { 406 break; 407 } 408 Block b = (Block) blocks.get(index); 409 if (b != null) { 410 Size2D s = b.arrange(g2, bc); 411 maxW = Math.max(maxW, s.getWidth()); 412 } 413 } 414 } 415 RectangleConstraint cc = constraint.toFixedWidth(maxW * this.columns); 416 return arrange(container, g2, cc); 417 } 418 419 /** 420 * Clears any cached layout information retained by the arrangement. 421 */ 422 @Override 423 public void clear() { 424 // nothing to clear 425 } 426 427 /** 428 * Compares this layout manager for equality with an arbitrary object. 429 * 430 * @param obj the object. 431 * 432 * @return A boolean. 433 */ 434 @Override 435 public boolean equals(Object obj) { 436 if (obj == this) { 437 return true; 438 } 439 if (!(obj instanceof GridArrangement)) { 440 return false; 441 } 442 GridArrangement that = (GridArrangement) obj; 443 if (this.columns != that.columns) { 444 return false; 445 } 446 if (this.rows != that.rows) { 447 return false; 448 } 449 return true; 450 } 451 452}