KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.DocumentFragment JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 import nextapp.echo2.app.Border;
38 import nextapp.echo2.app.Component;
39 import nextapp.echo2.app.Insets;
40 import nextapp.echo2.app.Panel;
41 import nextapp.echo2.app.update.ServerComponentUpdate;
42 import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
43 import nextapp.echo2.webcontainer.ContainerInstance;
44 import nextapp.echo2.webcontainer.DomUpdateSupport;
45 import nextapp.echo2.webcontainer.RenderContext;
46 import nextapp.echo2.webcontainer.SynchronizePeerFactory;
47 import nextapp.echo2.webcontainer.propertyrender.BorderRender;
48 import nextapp.echo2.webcontainer.propertyrender.ColorRender;
49 import nextapp.echo2.webcontainer.propertyrender.FontRender;
50 import nextapp.echo2.webcontainer.propertyrender.InsetsRender;
51 import nextapp.echo2.webrender.output.CssStyle;
52 import nextapp.echo2.webrender.servermessage.DomUpdate;
53
54 /**
55  * Synchronization peer for <code>nextapp.echo2.app.Composite</code>
56  * and <code>nextapp.echo2.app.Panel</code> components.
57  * <p>
58  * This class should not be extended or used by classes outside of the
59  * Echo framework.
60  */

61 public class CompositePeer
62 implements ComponentSynchronizePeer, DomUpdateSupport{
63
64     /**
65      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#getContainerId(nextapp.echo2.app.Component)
66      */

67     public String JavaDoc getContainerId(Component child) {
68         return ContainerInstance.getElementId(child.getParent());
69     }
70
71     /**
72      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderAdd(nextapp.echo2.webcontainer.RenderContext,
73      * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String,
74      * nextapp.echo2.app.Component)
75      */

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

88     public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) {
89         // Do nothing.
90
}
91
92     /**
93      * @see nextapp.echo2.webcontainer.DomUpdateSupport#renderHtml(nextapp.echo2.webcontainer.RenderContext,
94      * nextapp.echo2.app.update.ServerComponentUpdate, org.w3c.dom.Node,
95      * nextapp.echo2.app.Component)
96      */

97     public void renderHtml(RenderContext rc, ServerComponentUpdate update, Node JavaDoc parentNode, Component component) {
98         Document JavaDoc document = parentNode.getOwnerDocument();
99         Element JavaDoc divElement = document.createElement("div");
100         divElement.setAttribute("id", ContainerInstance.getElementId(component));
101         parentNode.appendChild(divElement);
102         
103         if (component.getVisibleComponentCount() == 0) {
104             // Return if no children (don't even bother to render CSS style)
105
return;
106         }
107         
108         CssStyle cssStyle = new CssStyle();
109         ColorRender.renderToStyle(cssStyle, component);
110         FontRender.renderToStyle(cssStyle, component);
111         if (component instanceof Panel) {
112             BorderRender.renderToStyle(cssStyle, (Border) component.getRenderProperty(Panel.PROPERTY_BORDER));
113             InsetsRender.renderToStyle(cssStyle, "padding", (Insets) component.getRenderProperty(Panel.PROPERTY_INSETS));
114         }
115         
116         if (cssStyle.hasAttributes()) {
117             divElement.setAttribute("style", cssStyle.renderInline());
118         }
119
120         Component child = component.getVisibleComponent(0);
121         ComponentSynchronizePeer syncPeer = SynchronizePeerFactory.getPeerForComponent(child.getClass());
122         
123         if (syncPeer instanceof DomUpdateSupport) {
124             ((DomUpdateSupport) syncPeer).renderHtml(rc, update, divElement, child);
125         } else {
126             syncPeer.renderAdd(rc, update, getContainerId(child), child);
127         }
128     }
129
130     /**
131      * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderUpdate(nextapp.echo2.webcontainer.RenderContext,
132      * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String)
133      */

134     public boolean renderUpdate(RenderContext rc, ServerComponentUpdate update, String JavaDoc targetId) {
135         String JavaDoc parentId = ContainerInstance.getElementId(update.getParent());
136         DomUpdate.renderElementRemove(rc.getServerMessage(), parentId);
137         renderAdd(rc, update, targetId, update.getParent());
138         return true;
139     }
140
141 }
142
Popular Tags