KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component;
35 import nextapp.echo2.app.ImageReference;
36 import nextapp.echo2.app.Insets;
37 import nextapp.echo2.app.layout.CellLayoutData;
38 import nextapp.echo2.webcontainer.RenderContext;
39 import nextapp.echo2.webcontainer.image.ImageRenderSupport;
40 import nextapp.echo2.webrender.output.CssStyle;
41
42 /**
43  * Utility class for rendering
44  * <code>nextapp.echo2.app.layout.CellLayoutData</code>
45  * layout data properties to CSS.
46  */

47 public class CellLayoutDataRender {
48     
49     private static String JavaDoc IMAGE_ID_PREFIX_LAYOUT_DATA_BACKGROUND_IMAGE = "CellLayoutData.BackgroundImage.";
50     private static int IMAGE_ID_PREFIX_LAYOUT_DATA_BACKGROUND_IMAGE_LENGTH = IMAGE_ID_PREFIX_LAYOUT_DATA_BACKGROUND_IMAGE.length();
51
52     /**
53      * A delegate method to be invoked by the container <code>Component</code>'s
54      * <code>ComponentSynchronizePeer</code>'s
55      * <code>ImageRenderSupport.getImage()</code> implementation. This method
56      * will return the appropriate <code>CellLayoutData</code> background
57      * image if the identifier corresponds to one, otherwise null is returned.
58      *
59      * @param component the container <code>Component</code>
60      * @param imageId the identifier of the image
61      * @return the <code>ImageReference</code> or null if the specified
62      * <code>imageId</code> does not specify a
63      * <code>CellLayoutData</code> <code>BackgroundImage</code>
64      * @see #renderBackgroundImageToStyle(CssStyle, RenderContext,
65      * ImageRenderSupport, Component, Component)
66      */

67     public static ImageReference getCellLayoutDataBackgroundImage(Component component, String JavaDoc imageId) {
68         if (imageId.startsWith(IMAGE_ID_PREFIX_LAYOUT_DATA_BACKGROUND_IMAGE)) {
69             String JavaDoc childRenderId = imageId.substring(IMAGE_ID_PREFIX_LAYOUT_DATA_BACKGROUND_IMAGE_LENGTH);
70             int childCount = component.getComponentCount();
71             for (int i = 0; i < childCount; ++i) {
72                 Component child = component.getComponent(i);
73                 if (child.getRenderId().equals(childRenderId)) {
74                     return ((CellLayoutData) child.getRenderProperty(Component.PROPERTY_LAYOUT_DATA))
75                             .getBackgroundImage().getImage();
76                 }
77             }
78         }
79         return null;
80     }
81     
82     /**
83      * Renders the <code>backgroundImage</code> property of a
84      * <code>CellLayoutDataRender</code> to a <code>CssStyle</code>.
85      * The image will be assigned an identifier by this object
86      * prefaced with the namespace "CellLayoutData".
87      * The <code>ImageRenderSupport.getImage()</code> implementation
88      * may obtain images based on these ids by invoking the
89      * <code>getCellLayoutDataBackgroundImage()</code> method. Note
90      * that any image id may be safely passed to
91      * <code>getCellLayoutDataBackgroundImage()</code> as it will return
92      * null if it does not have an image to match the specified id.
93      *
94      * @param cssStyle the target <code>CssStyle</code>
95      * @param rc the relevant <code>RenderContext</code>
96      * @param irs the <code>ImageRenderSupport</code> which will provide
97      * identified images
98      * @param parent the parent <code>Component</code>
99      * @param child the child <code>Component</code>
100      * @see #getCellLayoutDataBackgroundImage(Component, String)
101      */

102     public static void renderBackgroundImageToStyle(CssStyle cssStyle, RenderContext rc, ImageRenderSupport irs,
103             Component parent, Component child) {
104         CellLayoutData layoutData = (CellLayoutData) child.getRenderProperty(Component.PROPERTY_LAYOUT_DATA);
105         if (layoutData == null || layoutData.getBackgroundImage() == null) {
106             return;
107         }
108         FillImageRender.renderToStyle(cssStyle, rc, irs, parent,
109                 IMAGE_ID_PREFIX_LAYOUT_DATA_BACKGROUND_IMAGE + child.getRenderId(), layoutData.getBackgroundImage(), 0);
110     }
111     
112     /**
113      * Renders a <code>CellLayoutData</code> property to the given CSS style
114      * and HTML element. Null property values are handled properly (and default
115      * insets are still rendered if provided in such cases).
116      * <code>Alignment</code> information will be added to the
117      * <code>Element</code> such that block elements contained within it will
118      * be properly aligned. All other properties will be rendered using the
119      * <code>CssSyle</code>. Use of this method requires a "transitional"
120      * DOCTYPE.
121      *
122      * @param element the target <code>Element</code>
123      * @param cssStyle the target <code>CssStyle</code>
124      * @param component the child <code>Component</code> being laid out (used
125      * to determine <code>LayoutDirection</code> (LTR/RTL).
126      * @param layoutData the property value
127      * @param defaultInsetsAttributeValue the default insets for the cell
128      * (provided as a string in the interest of performance to avoid
129      * repeatedly rendering the same <code>Insets</code> object for
130      * each cell
131      */

132     public static void renderToElementAndStyle(Element JavaDoc element, CssStyle cssStyle, Component component, CellLayoutData layoutData,
133             String JavaDoc defaultInsetsAttributeValue) {
134         if (layoutData == null) {
135             if (defaultInsetsAttributeValue != null) {
136                 cssStyle.setAttribute("padding", defaultInsetsAttributeValue);
137             }
138             return;
139         }
140         
141         // Render padding.
142
Insets cellInsets = layoutData.getInsets();
143         if (cellInsets == null) {
144             if (defaultInsetsAttributeValue != null) {
145                 cssStyle.setAttribute("padding", defaultInsetsAttributeValue);
146             }
147         } else {
148             cssStyle.setAttribute("padding", InsetsRender.renderCssAttributeValue(cellInsets));
149         }
150         
151         // Render background.
152
ColorRender.renderToStyle(cssStyle, null, layoutData.getBackground());
153         
154         // Render alignment.
155
AlignmentRender.renderToElement(element, layoutData.getAlignment(), component);
156     }
157
158     /**
159      * Renders a <code>CellLayoutData</code> property to the given CSS style.
160      * Null property values are handled properly (and default insets are still
161      * rendered if provided in such cases).
162      *
163      * @param cssStyle the target <code>CssStyle</code>
164      * @param component the child <code>Component</code> being laid out (used
165      * to determine <code>LayoutDirection</code> (LTR/RTL).
166      * @param layoutData the property value
167      * @param defaultInsetsAttributeValue the default insets for the cell
168      * (provided as a string in the interest of performance to avoid
169      * repeatedly rendering the same <code>Insets</code> object for
170      * each cell
171      */

172     public static void renderToStyle(CssStyle cssStyle, Component component, CellLayoutData layoutData,
173             String JavaDoc defaultInsetsAttributeValue) {
174         if (layoutData == null) {
175             if (defaultInsetsAttributeValue != null) {
176                 cssStyle.setAttribute("padding", defaultInsetsAttributeValue);
177             }
178             return;
179         }
180         
181         // Render padding.
182
Insets cellInsets = layoutData.getInsets();
183         if (cellInsets == null) {
184             if (defaultInsetsAttributeValue != null) {
185                 cssStyle.setAttribute("padding", defaultInsetsAttributeValue);
186             }
187         } else {
188             cssStyle.setAttribute("padding", InsetsRender.renderCssAttributeValue(cellInsets));
189         }
190         
191         // Render background.
192
ColorRender.renderToStyle(cssStyle, null, layoutData.getBackground());
193         
194         // Render alignment.
195
AlignmentRender.renderToStyle(cssStyle, layoutData.getAlignment(), component);
196     }
197     
198     /** Non-instantiable class. */
199     private CellLayoutDataRender() { }
200 }
201
Popular Tags