KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > layout > BoundedSize


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.layout;
32
33 import java.awt.Container JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * Describes sizes as used by the {@link com.jgoodies.forms.layout.FormLayout}
38  * that provide lower and upper bounds.
39  *
40  * @author Karsten Lentzsch
41  * @version $Revision: 1.3 $
42  * @see Sizes
43  * @see ConstantSize
44  * @see Sizes.ComponentSize
45  */

46
47 final class BoundedSize implements Size {
48     
49     /**
50      * Holds the base size.
51      */

52     private final Size basis;
53     
54     /**
55      * Holds an optional lower bound.
56      */

57     private Size lowerBound;
58
59     /**
60      * Holds an optional upper bound.
61      */

62     private Size upperBound;
63     
64     
65     // Instance Creation ****************************************************
66

67     /**
68      * Constructs a <code>BoundedSize</code> for the given basis using the
69      * specified lower and upper bounds.
70      *
71      * @param basis the base size
72      * @param lowerBound the lower bound size
73      * @param upperBound the upper bound size
74      * @throws NullPointerException if the basis is null
75      */

76     BoundedSize(Size basis, Size lowerBound, Size upperBound) {
77         if (basis == null)
78             throw new NullPointerException JavaDoc("The basis of a bounded size must not be null.");
79         this.basis = basis;
80         this.lowerBound = lowerBound;
81         this.upperBound = upperBound;
82     }
83     
84     /**
85      * Constructs a <code>BoundedSize</code> for the given basis having
86      * no lower and upper bounds.
87      *
88      * @param basis the base size
89      * @throws NullPointerException if the basis is null
90      */

91     BoundedSize(Size basis) {
92         this(basis, null, null);
93     }
94     
95     
96     // Accessors ************************************************************
97

98     /**
99      * Sets the lower bound.
100      *
101      * @param lowerBound the new lower bound
102      */

103     void setLowerBound(Size lowerBound) {
104         this.lowerBound = lowerBound;
105     }
106     
107     /**
108      * Sets the upper bound.
109      *
110      * @param upperBound the new upper bound
111      */

112     void setUpperBound(Size upperBound) {
113         this.upperBound = upperBound;
114     }
115     
116     // Implementation of the Size Interface *********************************
117

118     /**
119      * Returns this size as pixel size. Neither requires the component
120      * list nor the specified measures. Honors the lower and upper bound.
121      * <p>
122      * Invoked by <code>FormSpec</code> to determine the size of a column or
123      * row.
124      *
125      * @see FormSpec#maximumSize
126      */

127     public int maximumSize(Container JavaDoc container,
128                     List JavaDoc components,
129                     FormLayout.Measure minMeasure,
130                     FormLayout.Measure prefMeasure,
131                     FormLayout.Measure defaultMeasure) {
132         int size = basis.maximumSize(container,
133                                      components,
134                                      minMeasure,
135                                      prefMeasure,
136                                      defaultMeasure);
137         if (lowerBound != null) {
138             size = Math.max(size, lowerBound.maximumSize(
139                                      container,
140                                      components,
141                                      minMeasure,
142                                      prefMeasure,
143                                      defaultMeasure));
144         }
145         if (upperBound != null) {
146             size = Math.min(size, upperBound.maximumSize(
147                                      container,
148                                      components,
149                                      minMeasure,
150                                      prefMeasure,
151                                      defaultMeasure));
152         }
153         return size;
154     }
155
156
157     // Overriding Object Behavior *******************************************
158

159     /**
160      * Indicates whether some other BoundedSize is "equal to" this one.
161      *
162      * @param size the BoundedSize with which to compare
163      * @return <code>true</code> if this object is the same as the obj
164      * argument; <code>false</code> otherwise.
165      * @see java.lang.Object#hashCode()
166      * @see java.util.Hashtable
167      */

168     public boolean equals(Object JavaDoc o) {
169         if (!(o instanceof BoundedSize))
170             return false;
171         BoundedSize size = (BoundedSize) o;
172         return basis.equals(size.basis)
173              && ( (lowerBound == null && size.lowerBound == null)
174                  || (lowerBound != null && lowerBound.equals(size.lowerBound)))
175              && ( (upperBound == null && size.upperBound == null)
176                  || (upperBound != null && upperBound.equals(size.upperBound)));
177     }
178     
179     /**
180      * Returns a hash code value for the object. This method is
181      * supported for the benefit of hashtables such as those provided by
182      * <code>java.util.Hashtable</code>.
183      *
184      * @return a hash code value for this object.
185      * @see java.lang.Object#equals(java.lang.Object)
186      * @see java.util.Hashtable
187      */

188     public int hashCode() {
189         int hashValue = basis.hashCode();
190         if (lowerBound != null) {
191             hashValue = hashValue * 37 + lowerBound.hashCode();
192         }
193         if (upperBound != null) {
194             hashValue = hashValue * 37 + upperBound.hashCode();
195         }
196         return hashValue;
197     }
198     
199     /**
200      * Returns a string representation of this size object.
201      *
202      * @return a string representation of the constant size
203      */

204     public String JavaDoc toString() {
205         if (lowerBound != null) {
206             if (upperBound == null) {
207                 return "max(" + basis + ';' + lowerBound + ')';
208             } else {
209                 return "max(" + basis + ';'
210                       + "min(" + lowerBound + ';' + upperBound + "))";
211             }
212         } else if (upperBound != null) {
213             return "min(" + basis + ';' + upperBound + ')';
214         } else {
215             return "bounded(" + basis + ')';
216         }
217     }
218
219     
220 }
Popular Tags