KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > syncpeer > LabelPeer


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.syncpeer;
31
32 import nextapp.echo2.app.Alignment;
33 import nextapp.echo2.app.Color;
34 import nextapp.echo2.app.Component;
35 import nextapp.echo2.app.Extent;
36 import nextapp.echo2.app.Font;
37 import nextapp.echo2.app.ImageReference;
38 import nextapp.echo2.app.Label;
39 import nextapp.echo2.app.update.ServerComponentUpdate;
40 import nextapp.echo2.webcontainer.ContainerInstance;
41 import nextapp.echo2.webcontainer.DomUpdateSupport;
42 import nextapp.echo2.webcontainer.RenderContext;
43 import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
44 import nextapp.echo2.webcontainer.image.ImageRenderSupport;
45 import nextapp.echo2.webcontainer.propertyrender.AlignmentRender;
46 import nextapp.echo2.webcontainer.propertyrender.ColorRender;
47 import nextapp.echo2.webcontainer.propertyrender.FontRender;
48 import nextapp.echo2.webcontainer.propertyrender.ImageReferenceRender;
49 import nextapp.echo2.webcontainer.propertyrender.LayoutDirectionRender;
50 import nextapp.echo2.webrender.output.CssStyle;
51 import nextapp.echo2.webrender.servermessage.DomUpdate;
52 import nextapp.echo2.webrender.util.DomUtil;
53
54 import org.w3c.dom.Document JavaDoc;
55 import org.w3c.dom.DocumentFragment JavaDoc;
56 import org.w3c.dom.Element JavaDoc;
57 import org.w3c.dom.Node JavaDoc;
58
59 /**
60  * Synchronization peer for <code>nextapp.echo2.app.Label</code> components.
61  * <p>
62  * This class should not be extended or used by classes outside of the
63  * Echo framework.
64  */

65 public class LabelPeer
66 implements DomUpdateSupport, ImageRenderSupport, ComponentSynchronizePeer {
67     
68     private static final Alignment DEFAULT_TEXT_POSITION = new Alignment(Alignment.TRAILING, Alignment.DEFAULT);
69     private static final Extent DEFAULT_ICON_TEXT_MARGIN = new Extent(3);
70     private static final String JavaDoc IMAGE_ID_ICON = "icon";
71     
72     /**
73      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderAdd(nextapp.echo2.webcontainer.RenderContext,
74      * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String, nextapp.echo2.app.Component)
75      */

76     public void renderAdd(RenderContext rc, ServerComponentUpdate update,
77             String JavaDoc targetId, Component component) {
78         Element JavaDoc domAddElement = DomUpdate.renderElementAdd(rc.getServerMessage());
79         DocumentFragment JavaDoc htmlFragment = rc.getServerMessage().getDocument().createDocumentFragment();
80         renderHtml(rc, update, htmlFragment, component);
81         DomUpdate.renderElementAddContent(rc.getServerMessage(), domAddElement, targetId, htmlFragment);
82     }
83
84     /**
85      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#getContainerId(nextapp.echo2.app.Component)
86      */

87     public String JavaDoc getContainerId(Component child) {
88         throw new UnsupportedOperationException JavaDoc("Component does not support children.");
89     }
90     
91     /**
92      * @see nextapp.echo2.webcontainer.image.ImageRenderSupport#getImage(nextapp.echo2.app.Component, java.lang.String)
93      */

94     public ImageReference getImage(Component component, String JavaDoc imageId) {
95         if (IMAGE_ID_ICON.equals(imageId)) {
96             return (ImageReference) component.getRenderProperty(Label.PROPERTY_ICON);
97         } else {
98             return null;
99         }
100     }
101
102     /**
103      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderDispose(nextapp.echo2.webcontainer.RenderContext,
104      * nextapp.echo2.app.update.ServerComponentUpdate, nextapp.echo2.app.Component)
105      */

106     public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) {
107         // Do nothing.
108
}
109     
110     /**
111      * @see nextapp.echo2.webcontainer.DomUpdateSupport#renderHtml(nextapp.echo2.webcontainer.RenderContext,
112      * nextapp.echo2.app.update.ServerComponentUpdate, org.w3c.dom.Node, nextapp.echo2.app.Component)
113      */

114     public void renderHtml(RenderContext rc, ServerComponentUpdate update, Node JavaDoc parentNode, Component component) {
115         Label label = (Label) component;
116         ImageReference icon = (ImageReference) label.getRenderProperty(Label.PROPERTY_ICON);
117         String JavaDoc text = (String JavaDoc) label.getRenderProperty(Label.PROPERTY_TEXT);
118         
119         if (icon != null) {
120             if (text != null) {
121                 renderIconTextLabel(rc, parentNode, label);
122             } else {
123                 renderIconLabel(rc, parentNode, label);
124             }
125         } else if (text != null) {
126             renderTextLabel(rc, parentNode, label);
127         }
128     }
129     
130     /**
131      * Renders a label containing only an icon.
132      *
133      * @param rc the relevant <code>RenderContext</code>
134      * @param parentNode the parent node
135      * @param label the <code>Label</code>
136      */

137     private void renderIconLabel(RenderContext rc, Node JavaDoc parentNode, Label label) {
138         Element JavaDoc imgElement = ImageReferenceRender.renderImageReferenceElement(rc, this, label, IMAGE_ID_ICON);
139         imgElement.setAttribute("id", ContainerInstance.getElementId(label));
140         imgElement.setAttribute("style", "border:0px none;");
141
142         String JavaDoc toolTipText = (String JavaDoc) label.getRenderProperty(Label.PROPERTY_TOOL_TIP_TEXT);
143         if (toolTipText != null) {
144             imgElement.setAttribute("title", toolTipText);
145         }
146         
147         parentNode.appendChild(imgElement);
148     }
149     
150     /**
151      * Renders a label containing both an icon and text.
152      *
153      * @param rc the relevant <code>RenderContext</code>
154      * @param parentNode the parent node
155      * @param label the <code>Label</code>
156      */

157     private void renderIconTextLabel(RenderContext rc, Node JavaDoc parentNode, Label label) {
158         // TriCellTable rendering note:
159
// Cell 0 = Text
160
// Cell 1 = Icon
161

162         Document JavaDoc document = rc.getServerMessage().getDocument();
163         String JavaDoc text = (String JavaDoc) label.getRenderProperty(Label.PROPERTY_TEXT);
164         Alignment textPosition = (Alignment) label.getRenderProperty(Label.PROPERTY_TEXT_POSITION, DEFAULT_TEXT_POSITION);
165         Extent iconTextMargin = (Extent) label.getRenderProperty(Label.PROPERTY_ICON_TEXT_MARGIN,
166                 DEFAULT_ICON_TEXT_MARGIN);
167         String JavaDoc elementId = ContainerInstance.getElementId(label);
168         
169         int orientation = TriCellTableConfigurator.convertIconTextPositionToOrientation(textPosition, label);
170         TriCellTable tct = new TriCellTable(rc, document, elementId, orientation, iconTextMargin);
171         tct.addCellCssText("padding:0px;");
172         
173         Element JavaDoc textTdElement = tct.getTdElement(0);
174         CssStyle textTdCssStyle = new CssStyle();
175         textTdCssStyle.setAttribute("border", "0px none");
176         if (Boolean.FALSE.equals(label.getRenderProperty(Label.PROPERTY_LINE_WRAP))) {
177             textTdCssStyle.setAttribute("white-space", "nowrap");
178         }
179         AlignmentRender.renderToStyle(textTdCssStyle, (Alignment) label.getRenderProperty(Label.PROPERTY_TEXT_ALIGNMENT), label);
180         textTdElement.setAttribute("style", textTdElement.getAttribute("style") + textTdCssStyle.renderInline());
181         DomUtil.setElementText(textTdElement, text);
182  
183         Element JavaDoc imgElement = ImageReferenceRender.renderImageReferenceElement(rc, this, label, IMAGE_ID_ICON);
184         Element JavaDoc iconTdElement = tct.getTdElement(1);
185         iconTdElement.appendChild(imgElement);
186         
187         Element JavaDoc tableElement = tct.getTableElement();
188         tableElement.setAttribute("id", elementId);
189         
190         String JavaDoc toolTipText = (String JavaDoc) label.getRenderProperty(Label.PROPERTY_TOOL_TIP_TEXT);
191         if (toolTipText != null) {
192             tableElement.setAttribute("title", toolTipText);
193         }
194         
195         CssStyle cssStyle = new CssStyle();
196         LayoutDirectionRender.renderToStyle(cssStyle, label.getLayoutDirection(), label.getLocale());
197         ColorRender.renderToStyle(cssStyle, (Color) label.getRenderProperty(Label.PROPERTY_FOREGROUND),
198                 (Color) label.getRenderProperty(Label.PROPERTY_BACKGROUND));
199         FontRender.renderToStyle(cssStyle, (Font) label.getRenderProperty(Label.PROPERTY_FONT));
200         cssStyle.setAttribute("border", "0px none");
201         cssStyle.setAttribute("border-collapse", "collapse");
202         tableElement.setAttribute("style", cssStyle.renderInline());
203         
204         parentNode.appendChild(tableElement);
205     }
206
207     /**
208      * Renders a label containing only text
209      *
210      * @param rc the relevant <code>RenderContext</code>
211      * @param parentNode the parent node
212      * @param label the <code>Label</code>
213      */

214     private void renderTextLabel(RenderContext rc, Node JavaDoc parentNode, Label label) {
215         Document JavaDoc document = rc.getServerMessage().getDocument();
216         
217         Element JavaDoc spanElement = document.createElement("span");
218         spanElement.setAttribute("id", ContainerInstance.getElementId(label));
219         DomUtil.setElementText(spanElement, (String JavaDoc) label.getRenderProperty(Label.PROPERTY_TEXT));
220
221         CssStyle cssStyle = new CssStyle();
222         if (Boolean.FALSE.equals(label.getRenderProperty(Label.PROPERTY_LINE_WRAP))) {
223             cssStyle.setAttribute("white-space", "nowrap");
224         }
225         ColorRender.renderToStyle(cssStyle, (Color) label.getRenderProperty(Label.PROPERTY_FOREGROUND),
226                 (Color) label.getRenderProperty(Label.PROPERTY_BACKGROUND));
227         FontRender.renderToStyle(cssStyle, (Font) label.getRenderProperty(Label.PROPERTY_FONT));
228         if (cssStyle.hasAttributes()) {
229             spanElement.setAttribute("style", cssStyle.renderInline());
230         }
231
232         String JavaDoc toolTipText = (String JavaDoc) label.getRenderProperty(Label.PROPERTY_TOOL_TIP_TEXT);
233         if (toolTipText != null) {
234             spanElement.setAttribute("title", toolTipText);
235         }
236         
237         parentNode.appendChild(spanElement);
238     }
239     
240     /**
241      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderUpdate(nextapp.echo2.webcontainer.RenderContext,
242      * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String)
243      */

244     public boolean renderUpdate(RenderContext rc, ServerComponentUpdate update, String JavaDoc targetId) {
245         DomUpdate.renderElementRemove(rc.getServerMessage(), ContainerInstance.getElementId(update.getParent()));
246         renderAdd(rc, update, targetId, update.getParent());
247         return false;
248     }
249 }
250
Popular Tags