1 51 52 package org.jfree.ui; 53 54 import java.awt.geom.Rectangle2D ; 55 import java.io.Serializable ; 56 57 67 public class Spacer implements Serializable { 68 69 70 private static final long serialVersionUID = 1464655984225158198L; 71 72 73 public static final int RELATIVE = 0; 74 75 76 public static final int ABSOLUTE = 1; 77 78 79 public static final Spacer NO_SPACE 80 = new Spacer(ABSOLUTE, 0.0, 0.0, 0.0, 0.0); 81 82 83 private int type; 84 85 86 private double left; 87 88 89 private double right; 90 91 92 private double top; 93 94 95 private double bottom; 96 97 112 public Spacer(final int type, 113 final double left, 114 final double top, 115 final double right, 116 final double bottom) { 117 118 this.type = type; 119 this.left = left; 120 this.top = top; 121 this.right = right; 122 this.bottom = bottom; 123 124 } 125 126 135 public double getLeftSpace(final double width) { 136 double result = 0.0; 137 if (this.type == ABSOLUTE) { 138 result = this.left; 139 } 140 else if (this.type == RELATIVE) { 141 result = this.left * width; 142 } 143 return result; 144 } 145 146 155 public double getRightSpace(final double width) { 156 double result = 0.0; 157 if (this.type == ABSOLUTE) { 158 result = this.right; 159 } 160 else if (this.type == RELATIVE) { 161 result = this.right * width; 162 } 163 return result; 164 } 165 166 174 public double getTopSpace(final double height) { 175 double result = 0.0; 176 if (this.type == ABSOLUTE) { 177 result = this.top; 178 } 179 else if (this.type == RELATIVE) { 180 result = this.top * height; 181 } 182 return result; 183 } 184 185 193 public double getBottomSpace(final double height) { 194 double result = 0.0; 195 if (this.type == ABSOLUTE) { 196 result = this.bottom; 197 } 198 else if (this.type == RELATIVE) { 199 result = this.bottom * height; 200 } 201 return result; 202 } 203 204 211 public double calculateExtendedWidth(final double width) { 212 double result = width; 213 if (this.type == ABSOLUTE) { 214 result = result + this.left + this.right; 215 } 216 else if (this.type == RELATIVE) { 217 result = result + (this.left * width) + (this.right * width); 218 } 219 return result; 220 } 221 222 230 public double calculateExtendedHeight(final double height) { 231 double result = height; 232 if (this.type == ABSOLUTE) { 233 result = result + this.top + this.bottom; 234 } 235 else if (this.type == RELATIVE) { 236 result = result + (this.top * height) + (this.bottom * height); 237 } 238 return result; 239 } 240 241 246 public void trim(final Rectangle2D area) { 247 if (area == null) { 248 throw new IllegalArgumentException ("Null 'area' argument."); 249 } 250 final double x = area.getX(); 251 final double y = area.getY(); 252 final double h = area.getHeight(); 253 final double w = area.getWidth(); 254 final double l = getLeftSpace(w); 255 final double r = getRightSpace(w); 256 final double t = getTopSpace(h); 257 final double b = getBottomSpace(h); 258 area.setRect(x + l, y + t, w - l - r, h - t - b); 259 } 260 261 269 public double trimWidth(final double w) { 270 return w - getLeftSpace(w) - getRightSpace(w); 271 } 272 273 280 public boolean equals(final Object obj) { 281 if (obj == this) { 282 return true; 283 } 284 if (!(obj instanceof Spacer)) { 285 return false; 286 } 287 final Spacer that = (Spacer) obj; 288 if (this.type != that.type) { 289 return false; 290 } 291 if (this.left != that.left) { 292 return false; 293 } 294 if (this.right != that.right) { 295 return false; 296 } 297 if (this.top != that.top) { 298 return false; 299 } 300 if (this.bottom != that.bottom) { 301 return false; 302 } 303 return true; 304 } 305 306 311 public int hashCode() { 312 int result; 313 long temp; 314 result = this.type; 315 temp = this.left != +0.0d ? Double.doubleToLongBits(this.left) : 0l; 316 result = 29 * result + (int) (temp ^ (temp >>> 32)); 317 temp = this.right != +0.0d ? Double.doubleToLongBits(this.right) : 0l; 318 result = 29 * result + (int) (temp ^ (temp >>> 32)); 319 temp = this.top != +0.0d ? Double.doubleToLongBits(this.top) : 0l; 320 result = 29 * result + (int) (temp ^ (temp >>> 32)); 321 temp = this.bottom != +0.0d ? Double.doubleToLongBits(this.bottom) : 0l; 322 result = 29 * result + (int) (temp ^ (temp >>> 32)); 323 return result; 324 } 325 326 } 327 | Popular Tags |