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.syncpeer;
031:
032: import org.w3c.dom.Document;
033: import org.w3c.dom.DocumentFragment;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.Node;
036:
037: import nextapp.echo2.app.Border;
038: import nextapp.echo2.app.Component;
039: import nextapp.echo2.app.Insets;
040: import nextapp.echo2.app.Panel;
041: import nextapp.echo2.app.update.ServerComponentUpdate;
042: import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
043: import nextapp.echo2.webcontainer.ContainerInstance;
044: import nextapp.echo2.webcontainer.DomUpdateSupport;
045: import nextapp.echo2.webcontainer.RenderContext;
046: import nextapp.echo2.webcontainer.SynchronizePeerFactory;
047: import nextapp.echo2.webcontainer.propertyrender.BorderRender;
048: import nextapp.echo2.webcontainer.propertyrender.ColorRender;
049: import nextapp.echo2.webcontainer.propertyrender.FontRender;
050: import nextapp.echo2.webcontainer.propertyrender.InsetsRender;
051: import nextapp.echo2.webrender.output.CssStyle;
052: import nextapp.echo2.webrender.servermessage.DomUpdate;
053:
054: /**
055: * Synchronization peer for <code>nextapp.echo2.app.Composite</code>
056: * and <code>nextapp.echo2.app.Panel</code> components.
057: * <p>
058: * This class should not be extended or used by classes outside of the
059: * Echo framework.
060: */
061: public class CompositePeer implements ComponentSynchronizePeer,
062: DomUpdateSupport {
063:
064: /**
065: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#getContainerId(nextapp.echo2.app.Component)
066: */
067: public String getContainerId(Component child) {
068: return ContainerInstance.getElementId(child.getParent());
069: }
070:
071: /**
072: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderAdd(nextapp.echo2.webcontainer.RenderContext,
073: * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String,
074: * nextapp.echo2.app.Component)
075: */
076: public void renderAdd(RenderContext rc,
077: ServerComponentUpdate update, String targetId,
078: Component component) {
079: Element domAddElement = DomUpdate.renderElementAdd(rc
080: .getServerMessage());
081: DocumentFragment htmlFragment = rc.getServerMessage()
082: .getDocument().createDocumentFragment();
083: renderHtml(rc, update, htmlFragment, component);
084: DomUpdate.renderElementAddContent(rc.getServerMessage(),
085: domAddElement, targetId, htmlFragment);
086: }
087:
088: /**
089: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderDispose(nextapp.echo2.webcontainer.RenderContext,
090: * nextapp.echo2.app.update.ServerComponentUpdate,
091: * nextapp.echo2.app.Component)
092: */
093: public void renderDispose(RenderContext rc,
094: ServerComponentUpdate update, Component component) {
095: // Do nothing.
096: }
097:
098: /**
099: * @see nextapp.echo2.webcontainer.DomUpdateSupport#renderHtml(nextapp.echo2.webcontainer.RenderContext,
100: * nextapp.echo2.app.update.ServerComponentUpdate, org.w3c.dom.Node,
101: * nextapp.echo2.app.Component)
102: */
103: public void renderHtml(RenderContext rc,
104: ServerComponentUpdate update, Node parentNode,
105: Component component) {
106: Document document = parentNode.getOwnerDocument();
107: Element divElement = document.createElement("div");
108: divElement.setAttribute("id", ContainerInstance
109: .getElementId(component));
110: parentNode.appendChild(divElement);
111:
112: if (component.getVisibleComponentCount() == 0) {
113: // Return if no children (don't even bother to render CSS style)
114: return;
115: }
116:
117: CssStyle cssStyle = new CssStyle();
118: ColorRender.renderToStyle(cssStyle, component);
119: FontRender.renderToStyle(cssStyle, component);
120: if (component instanceof Panel) {
121: BorderRender.renderToStyle(cssStyle, (Border) component
122: .getRenderProperty(Panel.PROPERTY_BORDER));
123: InsetsRender.renderToStyle(cssStyle, "padding",
124: (Insets) component
125: .getRenderProperty(Panel.PROPERTY_INSETS));
126: }
127:
128: if (cssStyle.hasAttributes()) {
129: divElement.setAttribute("style", cssStyle.renderInline());
130: }
131:
132: Component child = component.getVisibleComponent(0);
133: ComponentSynchronizePeer syncPeer = SynchronizePeerFactory
134: .getPeerForComponent(child.getClass());
135:
136: if (syncPeer instanceof DomUpdateSupport) {
137: ((DomUpdateSupport) syncPeer).renderHtml(rc, update,
138: divElement, child);
139: } else {
140: syncPeer
141: .renderAdd(rc, update, getContainerId(child), child);
142: }
143: }
144:
145: /**
146: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderUpdate(nextapp.echo2.webcontainer.RenderContext,
147: * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String)
148: */
149: public boolean renderUpdate(RenderContext rc,
150: ServerComponentUpdate update, String targetId) {
151: String parentId = ContainerInstance.getElementId(update
152: .getParent());
153: DomUpdate.renderElementRemove(rc.getServerMessage(), parentId);
154: renderAdd(rc, update, targetId, update.getParent());
155: return true;
156: }
157:
158: }
|