001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webcontainer.propertyrender;
031:
032: import org.w3c.dom.Element;
033:
034: import nextapp.echo2.app.Alignment;
035: import nextapp.echo2.app.Component;
036: import nextapp.echo2.app.LayoutDirection;
037: import nextapp.echo2.webrender.output.CssStyle;
038:
039: /**
040: * Utility class for rendering <code>nextapp.echo2.app.Alignment</code>
041: * properties to CSS.
042: */
043: public class AlignmentRender {
044:
045: /**
046: * Returns the horizontal property of an <code>Alignment</code> object,
047: * with <code>Alignment.LEADING</code> and <code>Alignment.TRAILING</code>
048: * automatically translated based on the layout direction of the provided
049: * <code>Component</code>. If the provided component is null, a
050: * left-to-right layout direction will be assumed.
051: *
052: * @param alignment the <code>Alignment</code> to analyze
053: * @param component the <code>Component</code> to analyze
054: * @return the horizontal alignment constant
055: */
056: public static int getRenderedHorizontal(Alignment alignment,
057: Component component) {
058: LayoutDirection layoutDirection;
059: if (component == null) {
060: layoutDirection = LayoutDirection.LTR;
061: } else {
062: layoutDirection = component.getRenderLayoutDirection();
063: }
064: switch (alignment.getHorizontal()) {
065: case Alignment.LEADING:
066: return layoutDirection.isLeftToRight() ? Alignment.LEFT
067: : Alignment.RIGHT;
068: case Alignment.TRAILING:
069: return layoutDirection.isLeftToRight() ? Alignment.RIGHT
070: : Alignment.LEFT;
071: default:
072: return alignment.getHorizontal();
073: }
074: }
075:
076: /**
077: * Renders an <code>Alignment</code> property to the given element.
078: * The 'align' and 'valign' attributes will be set if they
079: * can be derived from the provided <code>Alignment</code>.
080: * Null property values are ignored.
081: *
082: * @param element the target <code>Element</code>
083: * @param alignment the property value
084: */
085: public static void renderToElement(Element element,
086: Alignment alignment) {
087: renderToElement(element, alignment, null);
088: }
089:
090: /**
091: * Renders an <code>Alignment</code> property to the given element.
092: * The 'align' and 'valign' attributes will be set if they
093: * can be derived from the provided <code>Alignment</code>.
094: * Null property values are ignored.
095: *
096: * @param element the target <code>Element</code>
097: * @param alignment the property value
098: * @param component The <code>Component</code> for which the style is being
099: * rendered (necessary for property translation of leading/trailing
100: * alignment settings).
101: */
102: public static void renderToElement(Element element,
103: Alignment alignment, Component component) {
104: if (alignment == null) {
105: return;
106: }
107:
108: String horizontal = getHorizontalCssAttributeValue(alignment,
109: component);
110: if (horizontal != null) {
111: element.setAttribute("align", horizontal);
112: }
113: String vertical = getVerticalCssAttributeValue(alignment);
114: if (vertical != null) {
115: element.setAttribute("valign", vertical);
116: }
117: }
118:
119: /**
120: * Renders an <code>Alignment</code> property to the given CSS style.
121: * The 'text-align' and 'vertical-align' properties will be set if they
122: * can be derived from the provided <code>Alignment</code>.
123: * Null property values are ignored.
124: *
125: * @param cssStyle the target <code>CssStyle</code>
126: * @param alignment the property value
127: */
128: public static void renderToStyle(CssStyle cssStyle,
129: Alignment alignment) {
130: renderToStyle(cssStyle, alignment, null);
131: }
132:
133: /**
134: * Renders an <code>Alignment</code> property to the given CSS style.
135: * The 'text-align' and 'vertical-align' properties will be set if they
136: * can be derived from the provided <code>Alignment</code>.
137: * Null property values are ignored.
138: *
139: * @param cssStyle the target <code>CssStyle</code>
140: * @param component The <code>Component</code> for which the style is being
141: * rendered (necessary for property translation of leading/trailing
142: * alignment settings).
143: * @param alignment the property value
144: */
145: public static void renderToStyle(CssStyle cssStyle,
146: Alignment alignment, Component component) {
147: if (alignment == null) {
148: return;
149: }
150:
151: String horizontal = getHorizontalCssAttributeValue(alignment,
152: component);
153: if (horizontal != null) {
154: cssStyle.setAttribute("text-align", horizontal);
155: }
156: String vertical = getVerticalCssAttributeValue(alignment);
157: if (vertical != null) {
158: cssStyle.setAttribute("vertical-align", vertical);
159: }
160: }
161:
162: /**
163: * Determines the CSS attribute value of the horizontal property of the
164: * specified <code>Alignment</code>. The provided <code>Component</code>
165: * is used to determine the rendered alignment setting for
166: * <code>Alignment.LEADING</code> and <code>alignment.TRAILING</code>
167: * values.
168: *
169: * @param alignment the <code>Alignment</code> property value
170: * @param component the <code>Component</code> to use in order to determine
171: * appropriate rendered values for <code>Alignment.LEADING</code> /
172: * <code>Alignment.TRAILING</code> (this value may be null, in
173: * which case an LTR layout direction will be assumed)
174: * @return the horizontal CSS attribute value, or null if it is not
175: * specified by the <code>Alignment</code>
176: */
177: private static String getHorizontalCssAttributeValue(
178: Alignment alignment, Component component) {
179: switch (getRenderedHorizontal(alignment, component)) {
180: case Alignment.LEFT:
181: return "left";
182: case Alignment.CENTER:
183: return "center";
184: case Alignment.RIGHT:
185: return "right";
186: default:
187: return null;
188: }
189: }
190:
191: /**
192: * Determines the CSS attribute value of the vertical property of the
193: * specified <code>Alignment</code>.
194: *
195: * @param alignment the <code>Alignment</code> property value
196: * @return the CSS attribute value, or null if it is not specified by the
197: * <code>Alignment</code>
198: */
199: private static String getVerticalCssAttributeValue(
200: Alignment alignment) {
201: switch (alignment.getVertical()) {
202: case Alignment.TOP:
203: return "top";
204: case Alignment.CENTER:
205: return "middle";
206: case Alignment.BOTTOM:
207: return "bottom";
208: default:
209: return null;
210: }
211: }
212:
213: /** Non-instantiable class. */
214: private AlignmentRender() {
215: }
216: }
|