1 42 43 package org.jfree.chart.block; 44 45 import java.awt.Graphics2D ; 46 import java.awt.geom.Rectangle2D ; 47 import java.io.Serializable ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 51 import org.jfree.ui.Size2D; 52 53 56 public class GridArrangement implements Arrangement, Serializable { 57 58 59 private static final long serialVersionUID = -2563758090144655938L; 60 61 62 private int rows; 63 64 65 private int columns; 66 67 73 public GridArrangement(int rows, int columns) { 74 this.rows = rows; 75 this.columns = columns; 76 } 77 78 87 public void add(Block block, Object key) { 88 } 90 91 101 public Size2D arrange(BlockContainer container, Graphics2D g2, 102 RectangleConstraint constraint) { 103 LengthConstraintType w = constraint.getWidthConstraintType(); 104 LengthConstraintType h = constraint.getHeightConstraintType(); 105 if (w == LengthConstraintType.NONE) { 106 if (h == LengthConstraintType.NONE) { 107 return arrangeNN(container, g2); 108 } 109 else if (h == LengthConstraintType.FIXED) { 110 111 throw new RuntimeException ("Not yet implemented."); 112 } 113 else if (h == LengthConstraintType.RANGE) { 114 throw new RuntimeException ("Not yet implemented."); 116 } 117 } 118 else if (w == LengthConstraintType.FIXED) { 119 if (h == LengthConstraintType.NONE) { 120 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 return arrangeFR(container, g2, constraint); 129 } 130 } 131 else if (w == LengthConstraintType.RANGE) { 132 if (h == LengthConstraintType.NONE) { 134 throw new RuntimeException ("Not yet implemented."); 136 } 137 else if (h == LengthConstraintType.FIXED) { 138 throw new RuntimeException ("Not yet implemented."); 140 } 141 else if (h == LengthConstraintType.RANGE) { 142 throw new RuntimeException ("Not yet implemented."); 143 } 144 } 145 return new Size2D(); } 147 148 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 Size2D s = b.arrange(g2, RectangleConstraint.NONE); 164 maxW = Math.max(maxW, s.width); 165 maxH = Math.max(maxH, s.height); 166 } 167 double width = this.columns * maxW; 168 double height = this.rows * maxH; 169 RectangleConstraint c = new RectangleConstraint(width, height); 170 return arrangeFF(container, g2, c); 171 } 172 173 182 protected Size2D arrangeFF(BlockContainer container, Graphics2D g2, 183 RectangleConstraint constraint) { 184 double width = constraint.getWidth() / this.columns; 185 double height = constraint.getHeight() / this.rows; 186 List blocks = container.getBlocks(); 187 for (int c = 0; c < this.columns; c++) { 188 for (int r = 0; r < this.rows; r++) { 189 int index = r * this.columns + c; 190 if (index == blocks.size()) { 191 break; 192 } 193 Block b = (Block) blocks.get(index); 194 b.setBounds(new Rectangle2D.Double ( 195 c * width, r * height, width, height 196 )); 197 } 198 } 199 return new Size2D(this.columns * width, this.rows * height); 200 } 201 202 211 protected Size2D arrangeFR(BlockContainer container, Graphics2D g2, 212 RectangleConstraint constraint) { 213 214 RectangleConstraint c1 = constraint.toUnconstrainedHeight(); 215 Size2D size1 = arrange(container, g2, c1); 216 217 if (constraint.getHeightRange().contains(size1.getHeight())) { 218 return size1; 219 } 220 else { 221 double h = constraint.getHeightRange().constrain(size1.getHeight()); 222 RectangleConstraint c2 = constraint.toFixedHeight(h); 223 return arrange(container, g2, c2); 224 } 225 } 226 227 236 protected Size2D arrangeFN(BlockContainer container, Graphics2D g2, 237 RectangleConstraint constraint) { 238 239 double width = constraint.getWidth() / this.columns; 240 RectangleConstraint constraint2 = constraint.toFixedWidth(width); 241 List blocks = container.getBlocks(); 242 double maxH = 0.0; 243 for (int r = 0; r < this.rows; r++) { 244 for (int c = 0; c < this.columns; c++) { 245 int index = r * this.columns + c; 246 if (index == blocks.size()) { 247 break; 248 } 249 Block b = (Block) blocks.get(index); 250 Size2D s = b.arrange(g2, constraint2); 251 maxH = Math.max(maxH, s.getHeight()); 252 } 253 } 254 RectangleConstraint constraint3 = constraint.toFixedHeight( 255 maxH * this.rows 256 ); 257 return arrange(container, g2, constraint3); 258 } 259 260 263 public void clear() { 264 } 266 267 274 public boolean equals(Object obj) { 275 if (obj == this) { 276 return true; 277 } 278 if (!(obj instanceof GridArrangement)) { 279 return false; 280 } 281 GridArrangement that = (GridArrangement) obj; 282 if (this.columns != that.columns) { 283 return false; 284 } 285 if (this.rows != that.rows) { 286 return false; 287 } 288 return true; 289 } 290 291 } 292 | Popular Tags |