KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SDimension


1 /*
2  * $Id: SDimension.java,v 1.11 2005/03/03 16:46:38 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.ParsePosition JavaDoc;
22
23 public class SDimension
24         implements Serializable JavaDoc {
25     private final transient static Log log = LogFactory.getLog(SDimension.class);
26
27     public String JavaDoc width = null;
28     public String JavaDoc height = null;
29     private int iwidth = -1;
30     private int iheight = -1;
31
32     public SDimension() {}
33
34     public SDimension(String JavaDoc width, String JavaDoc height) {
35         setWidth(width);
36         setHeight(height);
37     }
38
39     /**
40      * Construct a new dimension.
41      * The value is converted to "<value>px".
42      *
43      * @param width the width. If value is less than <code>0</code> width
44      * is set to <code>null</code>.
45      * @param height the height. If value is less than <code>0</code> height
46      * is set to <code>null</code>.
47      * @see #setSize(int,int)
48      */

49     public SDimension(int width, int height) {
50         setSize(width, height);
51     }
52
53     public void setWidth(String JavaDoc width) {
54         this.width = width;
55         iwidth = -1;
56     }
57
58     public void setHeight(String JavaDoc height) {
59         this.height = height;
60         iheight = -1;
61     }
62
63     /**
64      * Set width in pixel.
65      * This appends "px" to the integer value.
66      *
67      * @param width if -1 set {@link SDimension#width} to <code>null</code>
68      */

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     /**
78      * Set height in pixel.
79      * This appends "px" to the integer value.
80      *
81      * @param height if -1 set {@link SDimension#height} to <code>null</code>
82      */

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     /**
92      * Extract number from string.
93      *
94      * @return extracted integer. f.e.: "120px" becomes 120
95      */

96     protected int getInt(String JavaDoc size) {
97         try {
98             return new DecimalFormat JavaDoc().parse(size, new ParsePosition JavaDoc(0)).intValue();
99         } catch (Exception JavaDoc e) {
100             log.warn("Can not parse [" + size + "]", e);
101             return -1;
102         }
103     }
104
105     public String JavaDoc getWidth() { return width; }
106
107     public String JavaDoc getHeight() { return height; }
108
109     /**
110      * Get just the width as number without trailing
111      * unit.
112      */

113     public int getIntWidth() {
114         if (iwidth == -1)
115             iwidth = getInt(width);
116         return iwidth;
117     }
118
119     /**
120      * Get just the height as number without trailing
121      * unit.
122      */

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 JavaDoc 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     /**
154      * @see java.lang.Object#hashCode()
155      */

156     public int hashCode() {
157         return width != null ? width.hashCode() : 0;
158     }
159
160     /**
161      * Set the size of this Dimension object to the specified width and height
162      * and append "px" to both values.
163      *
164      * @see #setHeight(int)
165      * @see #setWidth(int)
166      */

167     public void setSize(int width, int height) {
168         setWidth(width);
169         setHeight(height);
170     }
171
172     public String JavaDoc toString() {
173         return "width: " + width + "; height: " + height;
174     }
175 }
176
Popular Tags