KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.w3c.dom.Element JavaDoc;
33
34 import nextapp.echo2.app.Alignment;
35 import nextapp.echo2.app.Component;
36 import nextapp.echo2.app.LayoutDirection;
37 import nextapp.echo2.webrender.output.CssStyle;
38
39 /**
40  * Utility class for rendering <code>nextapp.echo2.app.Alignment</code>
41  * properties to CSS.
42  */

43 public class AlignmentRender {
44     
45     /**
46      * Returns the horizontal property of an <code>Alignment</code> object,
47      * with <code>Alignment.LEADING</code> and <code>Alignment.TRAILING</code>
48      * automatically translated based on the layout direction of the provided
49      * <code>Component</code>. If the provided component is null, a
50      * left-to-right layout direction will be assumed.
51      *
52      * @param alignment the <code>Alignment</code> to analyze
53      * @param component the <code>Component</code> to analyze
54      * @return the horizontal alignment constant
55      */

56     public static int getRenderedHorizontal(Alignment alignment, Component component) {
57         LayoutDirection layoutDirection;
58         if (component == null) {
59             layoutDirection = LayoutDirection.LTR;
60         } else {
61             layoutDirection = component.getRenderLayoutDirection();
62         }
63         switch (alignment.getHorizontal()) {
64         case Alignment.LEADING:
65             return layoutDirection.isLeftToRight() ? Alignment.LEFT : Alignment.RIGHT;
66         case Alignment.TRAILING:
67             return layoutDirection.isLeftToRight() ? Alignment.RIGHT : Alignment.LEFT;
68         default:
69             return alignment.getHorizontal();
70         }
71     }
72     
73     /**
74      * Renders an <code>Alignment</code> property to the given element.
75      * The 'align' and 'valign' attributes will be set if they
76      * can be derived from the provided <code>Alignment</code>.
77      * Null property values are ignored.
78      *
79      * @param element the target <code>Element</code>
80      * @param alignment the property value
81      */

82     public static void renderToElement(Element JavaDoc element, Alignment alignment) {
83         renderToElement(element, alignment, null);
84     }
85     
86     /**
87      * Renders an <code>Alignment</code> property to the given element.
88      * The 'align' and 'valign' attributes will be set if they
89      * can be derived from the provided <code>Alignment</code>.
90      * Null property values are ignored.
91      *
92      * @param element the target <code>Element</code>
93      * @param alignment the property value
94      * @param component The <code>Component</code> for which the style is being
95      * rendered (necessary for property translation of leading/trailing
96      * alignment settings).
97      */

98     public static void renderToElement(Element JavaDoc element, Alignment alignment, Component component) {
99         if (alignment == null) {
100             return;
101         }
102         
103         String JavaDoc horizontal = getHorizontalCssAttributeValue(alignment, component);
104         if (horizontal != null) {
105             element.setAttribute("align", horizontal);
106         }
107         String JavaDoc vertical = getVerticalCssAttributeValue(alignment);
108         if (vertical != null) {
109             element.setAttribute("valign", vertical);
110         }
111     }
112
113     /**
114      * Renders an <code>Alignment</code> property to the given CSS style.
115      * The 'text-align' and 'vertical-align' properties will be set if they
116      * can be derived from the provided <code>Alignment</code>.
117      * Null property values are ignored.
118      *
119      * @param cssStyle the target <code>CssStyle</code>
120      * @param alignment the property value
121      */

122     public static void renderToStyle(CssStyle cssStyle, Alignment alignment) {
123         renderToStyle(cssStyle, alignment, null);
124     }
125     
126     /**
127      * Renders an <code>Alignment</code> property to the given CSS style.
128      * The 'text-align' and 'vertical-align' properties will be set if they
129      * can be derived from the provided <code>Alignment</code>.
130      * Null property values are ignored.
131      *
132      * @param cssStyle the target <code>CssStyle</code>
133      * @param component The <code>Component</code> for which the style is being
134      * rendered (necessary for property translation of leading/trailing
135      * alignment settings).
136      * @param alignment the property value
137      */

138     public static void renderToStyle(CssStyle cssStyle, Alignment alignment, Component component) {
139         if (alignment == null) {
140             return;
141         }
142         
143         String JavaDoc horizontal = getHorizontalCssAttributeValue(alignment, component);
144         if (horizontal != null) {
145             cssStyle.setAttribute("text-align", horizontal);
146         }
147         String JavaDoc vertical = getVerticalCssAttributeValue(alignment);
148         if (vertical != null) {
149             cssStyle.setAttribute("vertical-align", vertical);
150         }
151     }
152     
153     /**
154      * Determines the CSS attribute value of the horizontal property of the
155      * specified <code>Alignment</code>. The provided <code>Component</code>
156      * is used to determine the rendered alignment setting for
157      * <code>Alignment.LEADING</code> and <code>alignment.TRAILING</code>
158      * values.
159      *
160      * @param alignment the <code>Alignment</code> property value
161      * @param component the <code>Component</code> to use in order to determine
162      * appropriate rendered values for <code>Alignment.LEADING</code> /
163      * <code>Alignment.TRAILING</code> (this value may be null, in
164      * which case an LTR layout direction will be assumed)
165      * @return the horizontal CSS attribute value, or null if it is not
166      * specified by the <code>Alignment</code>
167      */

168     private static String JavaDoc getHorizontalCssAttributeValue(Alignment alignment, Component component) {
169         switch (getRenderedHorizontal(alignment, component)) {
170         case Alignment.LEFT:
171             return "left";
172         case Alignment.CENTER:
173             return "center";
174         case Alignment.RIGHT:
175             return "right";
176         default:
177             return null;
178         }
179     }
180     
181     /**
182      * Determines the CSS attribute value of the vertical property of the
183      * specified <code>Alignment</code>.
184      *
185      * @param alignment the <code>Alignment</code> property value
186      * @return the CSS attribute value, or null if it is not specified by the
187      * <code>Alignment</code>
188      */

189     private static String JavaDoc getVerticalCssAttributeValue(Alignment alignment) {
190         switch (alignment.getVertical()) {
191         case Alignment.TOP:
192             return "top";
193         case Alignment.CENTER:
194             return "middle";
195         case Alignment.BOTTOM:
196             return "bottom";
197         default:
198             return null;
199         }
200     }
201     
202     /** Non-instantiable class. */
203     private AlignmentRender() { }
204 }
205
Popular Tags