KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > propertyrender > ExtentRender


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webcontainer.propertyrender;
31
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import nextapp.echo2.app.Extent;
37 import nextapp.echo2.webrender.output.CssStyle;
38
39 /**
40  * Utility class for rendering <code>nextapp.echo2.app.Extent</code>
41  * properties to CSS.
42  */

43 public class ExtentRender {
44     
45     private static final Map JavaDoc UNIT_NAMES_TO_VALUES;
46     static {
47         Map JavaDoc m = new HashMap JavaDoc();
48         m.put("cm", new Integer JavaDoc(Extent.CM));
49         m.put("em", new Integer JavaDoc(Extent.EM));
50         m.put("ex", new Integer JavaDoc(Extent.EX));
51         m.put("in", new Integer JavaDoc(Extent.IN));
52         m.put("mm", new Integer JavaDoc(Extent.MM));
53         m.put("pc", new Integer JavaDoc(Extent.PC));
54         m.put("%", new Integer JavaDoc(Extent.PERCENT));
55         m.put("pt", new Integer JavaDoc(Extent.PT));
56         m.put("px", new Integer JavaDoc(Extent.PX));
57         UNIT_NAMES_TO_VALUES = Collections.unmodifiableMap(m);
58     }
59     
60     /**
61      * Determines if the given <code>Extent</code> is of zero length.
62      * This method interprets null <code>Extent</code>s to be of zero
63      * length.
64      *
65      * @param extent the extent
66      * @return true if the extent is of zero length
67      */

68     public static boolean isZeroLength(Extent extent) {
69         return extent == null || extent.getValue() == 0;
70     }
71     
72     /**
73      * Attempts to render a given <code>Extent</code> to a pixel CSS attribute
74      * value. Returns null if the the extent can not be represented by a pixel
75      * value.
76      *
77      * @param extent the property value
78      * @return the CSS attribute value
79      */

80     public static final String JavaDoc renderCssAttributePixelValue(Extent extent) {
81         return renderCssAttributePixelValue(extent, null);
82     }
83     
84     /**
85      * Attempts to render a given <code>Extent</code> to a pixel CSS attribute
86      * value. Returns the specified <code>invalidValue</code> if the specified
87      * <code>Extent</code> is not valid.
88      *
89      * @param extent the property value
90      * @return the CSS attribute value to return if the provided value is not a valid
91      * pixel value
92      * @return the CSS attribute value
93      */

94     public static final String JavaDoc renderCssAttributePixelValue(Extent extent, String JavaDoc invalidValue) {
95         if (extent != null && extent.getUnits() == Extent.PX) {
96             return extent.getValue() + "px";
97         } else {
98             return invalidValue;
99         }
100     }
101     
102     /**
103      * Renders an <code>Extent</code> property value to a CSS dimensioned
104      * attribute value.
105      *
106      * @param extent the property value
107      * @return the CSS attribute value
108      */

109     public static final String JavaDoc renderCssAttributeValue(Extent extent) {
110         return extent.getValue() + renderUnits(extent.getUnits());
111     }
112     
113     /**
114      * Renders 1/2 the distance specified by an <code>Extent</code> property
115      * value to a CSS dimensioned attribute.
116      * For example, an extent that would normally render as "3px" would be
117      * rendered as "1.5px" by this method.
118      *
119      * @param extent the property value
120      * @return the CSS attribute value
121      */

122     public static final String JavaDoc renderCssAttributeValueHalf(Extent extent) {
123         if (extent.getValue() % 2 == 0) {
124             return (extent.getValue() / 2) + renderUnits(extent.getUnits());
125         } else {
126             return (extent.getValue() / 2) + ".5" + renderUnits(extent.getUnits());
127         }
128     }
129     
130     /**
131      * Renders an <code>Extent</code> property to the given CSS style.
132      * Null property values are ignored.
133      *
134      * @param cssStyle the target <code>CssStyle</code>
135      * @param cssAttribute the CSS attribute name, e.g., "width" or "height".
136      * @param extent the property value
137      */

138     public static final void renderToStyle(CssStyle cssStyle, String JavaDoc cssAttribute, Extent extent) {
139         if (extent == null) {
140             return;
141         }
142         cssStyle.setAttribute(cssAttribute, renderCssAttributeValue(extent));
143     }
144      
145     /**
146      * Renders the given <code>Extent</code> units constant into a CSS
147      * unit suffix.
148      *
149      * @param units the <code>Extent</code> units constant value
150      * @return the CSS unit suffix
151      */

152     public static final String JavaDoc renderUnits(int units) {
153         switch (units) {
154         case Extent.CM: return "cm";
155         case Extent.EM: return "em";
156         case Extent.EX: return "ex";
157         case Extent.IN: return "in";
158         case Extent.MM: return "mm";
159         case Extent.PC: return "pc";
160         case Extent.PERCENT: return "%";
161         case Extent.PT: return "pt";
162         case Extent.PX: return "px";
163         default:
164             throw new IllegalArgumentException JavaDoc("Invalid extent.");
165         }
166     }
167     
168     private static int getUnitPosition(String JavaDoc s) {
169         int length = s.length();
170         for (int i = 0; i < length; ++i) {
171             char ch = s.charAt(i);
172             if (ch != '-' && (ch < '0' || ch > '9')) {
173                 return i;
174             }
175         }
176         return -1;
177     }
178     
179     /**
180      * Creates an <code>Extent</code> from the given CSS dimensioned attribute
181      * value.
182      *
183      * @param extentString the CSS dimensioned attribute value
184      * @return an equivalent <code>Extent</code>, or null if the input
185      * is not valid.
186      */

187     public static final Extent toExtent(String JavaDoc extentString) {
188         try {
189             if (extentString == null) {
190                 return null;
191             }
192             int unitPosition = getUnitPosition(extentString);
193             if (unitPosition == -1) {
194                 return null;
195             }
196             String JavaDoc unitString = extentString.substring(unitPosition);
197             Integer JavaDoc unitInteger = (Integer JavaDoc) UNIT_NAMES_TO_VALUES.get(unitString);
198             if (unitInteger == null) {
199                 return null;
200             }
201             Extent extent = new Extent(Integer.parseInt(extentString.substring(0, unitPosition)), unitInteger.intValue());
202             return extent;
203         } catch (NumberFormatException JavaDoc ex) {
204             return null;
205         }
206     }
207     
208     /**
209      * Attempts to convert a given <code>Extent</code> to a pixel value.
210      * Returns <code>defaultPixels</code> if the conversion is not possible.
211      *
212      * @param extent the <code>Extent</code> to convert
213      * @param defaultPixels the pixel value to return if conversion is
214      * impossible.
215      * @return the value of the <code>extent</code> in pixels
216      */

217     public static final int toPixels(Extent extent, int defaultPixels) {
218         if (extent != null && extent.getUnits() == Extent.PX) {
219             return extent.getValue();
220         } else {
221             return defaultPixels;
222         }
223     }
224     
225     /** Non-instantiable class. */
226     private ExtentRender() { }
227 }
228
Popular Tags