1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import java.io.Serializable ; 20 import java.text.DecimalFormat ; 21 import java.text.ParsePosition ; 22 23 public class SDimension 24 implements Serializable { 25 private final transient static Log log = LogFactory.getLog(SDimension.class); 26 27 public String width = null; 28 public String height = null; 29 private int iwidth = -1; 30 private int iheight = -1; 31 32 public SDimension() {} 33 34 public SDimension(String width, String height) { 35 setWidth(width); 36 setHeight(height); 37 } 38 39 49 public SDimension(int width, int height) { 50 setSize(width, height); 51 } 52 53 public void setWidth(String width) { 54 this.width = width; 55 iwidth = -1; 56 } 57 58 public void setHeight(String height) { 59 this.height = height; 60 iheight = -1; 61 } 62 63 69 public void setWidth(int width) { 70 this.iwidth = width; 71 if (width > -1) 72 this.width = width + "px"; 73 else 74 this.width = null; 75 } 76 77 83 public void setHeight(int height) { 84 this.iheight = height; 85 if (height > -1) 86 this.height = height + "px"; 87 else 88 this.height = null; 89 } 90 91 96 protected int getInt(String size) { 97 try { 98 return new DecimalFormat ().parse(size, new ParsePosition (0)).intValue(); 99 } catch (Exception e) { 100 log.warn("Can not parse [" + size + "]", e); 101 return -1; 102 } 103 } 104 105 public String getWidth() { return width; } 106 107 public String getHeight() { return height; } 108 109 113 public int getIntWidth() { 114 if (iwidth == -1) 115 iwidth = getInt(width); 116 return iwidth; 117 } 118 119 123 public int getIntHeight() { 124 if (iheight == -1) 125 iheight = getInt(height); 126 return iheight; 127 } 128 129 public boolean isWidthDefined() { 130 return width != null; 131 } 132 133 public boolean isHeightDefined() { 134 return height != null; 135 } 136 137 public boolean isWidthOrHeightDefined() { 138 return (width != null || height != null); 139 } 140 141 public boolean equals(Object o) { 142 if (this == o) return true; 143 if (!(o instanceof SDimension)) return false; 144 145 final SDimension sDimension = (SDimension) o; 146 147 if (height != null ? !height.equals(sDimension.height) : sDimension.height != null) return false; 148 if (width != null ? !width.equals(sDimension.width) : sDimension.width != null) return false; 149 150 return true; 151 } 152 153 156 public int hashCode() { 157 return width != null ? width.hashCode() : 0; 158 } 159 160 167 public void setSize(int width, int height) { 168 setWidth(width); 169 setHeight(height); 170 } 171 172 public String toString() { 173 return "width: " + width + "; height: " + height; 174 } 175 } 176 | Popular Tags |