001: /*
002: * This file is part of the Tucana Echo2 Library.
003: * Copyright (C) 2006.
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 tucana.echo2.webcontainer;
031:
032: import nextapp.echo2.app.Component;
033: import nextapp.echo2.app.update.ServerComponentUpdate;
034: import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
035: import nextapp.echo2.webcontainer.ContainerInstance;
036: import nextapp.echo2.webcontainer.DomUpdateSupport;
037: import nextapp.echo2.webcontainer.RenderContext;
038: import nextapp.echo2.webcontainer.SynchronizePeerFactory;
039: import nextapp.echo2.webrender.ServerMessage;
040: import nextapp.echo2.webrender.Service;
041: import nextapp.echo2.webrender.WebRenderServlet;
042: import nextapp.echo2.webrender.output.CssStyle;
043: import nextapp.echo2.webrender.servermessage.DomUpdate;
044: import nextapp.echo2.webrender.service.JavaScriptService;
045:
046: import org.w3c.dom.Document;
047: import org.w3c.dom.DocumentFragment;
048: import org.w3c.dom.Element;
049: import org.w3c.dom.Node;
050:
051: import tucana.echo2.app.widgetdash.WidgetGrabPoint;
052:
053: /**
054: * Synchronization peer for <code>nextapp.echo2.contrib.dnd.DragSource</code> components.
055: * <p>
056: */
057: public class WidgetGrabPointPeer implements ComponentSynchronizePeer,
058: DomUpdateSupport {
059:
060: /**
061: * Service to provide supporting JavaScript library.
062: */
063: private static final Service WIDGET_GRAB_SERVICE = JavaScriptService
064: .forResource("Tucana.WidgetGrabPoint",
065: "/tucana/echo2/webcontainer/resource/js/WidgetGrabPoint.js");
066:
067: static {
068: WebRenderServlet.getServiceRegistry().add(WIDGET_GRAB_SERVICE);
069: }
070:
071: /**
072: * Default constructor.
073: */
074: public WidgetGrabPointPeer() {
075: }
076:
077: /**
078: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#getContainerId(nextapp.echo2.app.Component)
079: */
080: public String getContainerId(Component child) {
081: return ContainerInstance.getElementId(child.getParent());
082: }
083:
084: /**
085: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderAdd(nextapp.echo2.webcontainer.RenderContext,
086: * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String, nextapp.echo2.app.Component)
087: */
088: public void renderAdd(RenderContext rc,
089: ServerComponentUpdate update, String targetId,
090: Component component) {
091: Element domAddElement = DomUpdate.renderElementAdd(rc
092: .getServerMessage());
093: DocumentFragment htmlFragment = rc.getServerMessage()
094: .getDocument().createDocumentFragment();
095: renderHtml(rc, update, htmlFragment, component);
096: DomUpdate.renderElementAddContent(rc.getServerMessage(),
097: domAddElement, targetId, htmlFragment);
098: }
099:
100: /**
101: * Renders a child component.
102: *
103: * @param rc the relevant <code>RenderContext</code>
104: * @param update the update
105: * @param parentElement the HTML element which should contain the child
106: * @param child the child component to render
107: */
108: private void renderAddChild(RenderContext rc,
109: ServerComponentUpdate update, Element parentElement,
110: Component child) {
111: ComponentSynchronizePeer syncPeer = SynchronizePeerFactory
112: .getPeerForComponent(child.getClass());
113: if (syncPeer instanceof DomUpdateSupport) {
114: ((DomUpdateSupport) syncPeer).renderHtml(rc, update,
115: parentElement, child);
116: } else {
117: syncPeer
118: .renderAdd(rc, update, getContainerId(child), child);
119: }
120: }
121:
122: /**
123: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderDispose(nextapp.echo2.webcontainer.RenderContext,
124: * nextapp.echo2.app.update.ServerComponentUpdate,
125: * nextapp.echo2.app.Component)
126: */
127: public void renderDispose(RenderContext rc,
128: ServerComponentUpdate update, Component component) {
129: WidgetGrabPoint grabPoint = (WidgetGrabPoint) component;
130:
131: ServerMessage serverMessage = rc.getServerMessage();
132: serverMessage.addLibrary(WIDGET_GRAB_SERVICE.getId());
133:
134: Element partElement = serverMessage.addPart(
135: ServerMessage.GROUP_ID_UPDATE,
136: "TucanaWidgetGrabPoint.MessageProcessor");
137: Element initElement = serverMessage.getDocument()
138: .createElement("dispose");
139:
140: String elementId = ContainerInstance.getElementId(grabPoint);
141:
142: initElement.setAttribute("eid", elementId);
143:
144: partElement.appendChild(initElement);
145: }
146:
147: /**
148: * @see nextapp.echo2.webcontainer.DomUpdateSupport#renderHtml(nextapp.echo2.webcontainer.RenderContext,
149: * nextapp.echo2.app.update.ServerComponentUpdate, org.w3c.dom.Node, nextapp.echo2.app.Component)
150: */
151: public void renderHtml(RenderContext rc,
152: ServerComponentUpdate update, Node parentNode,
153: Component component) {
154: ServerMessage serverMessage = rc.getServerMessage();
155: serverMessage.addLibrary(WIDGET_GRAB_SERVICE.getId());
156:
157: WidgetGrabPoint grabPoint = (WidgetGrabPoint) component;
158:
159: String elementId = ContainerInstance.getElementId(grabPoint);
160: Document document = parentNode.getOwnerDocument();
161:
162: Element divElement = document.createElement("div");
163: divElement.setAttribute("id", elementId);
164: divElement.setAttribute("class", "widgetGrabPoint");
165: CssStyle style = new CssStyle();
166: style.setAttribute("cursor", (String) component
167: .getProperty(WidgetGrabPoint.PROPERTY_CURSOR));
168: divElement.setAttribute("style", style.renderInline());
169:
170: parentNode.appendChild(divElement);
171:
172: renderInitDirective(rc, component);
173:
174: renderAddChild(rc, update, divElement, grabPoint.getGrabbable());
175: }
176:
177: private void renderInitDirective(RenderContext rc,
178: Component component) {
179: WidgetGrabPoint grabPoint = (WidgetGrabPoint) component;
180:
181: ServerMessage serverMessage = rc.getServerMessage();
182: Element partElement = serverMessage.addPart(
183: ServerMessage.GROUP_ID_UPDATE,
184: "TucanaWidgetGrabPoint.MessageProcessor");
185: Element initElement = serverMessage.getDocument()
186: .createElement("init");
187:
188: String elementId = ContainerInstance.getElementId(grabPoint);
189: String targetId = ContainerInstance.getElementId(grabPoint
190: .getWidgetContainer());
191:
192: initElement.setAttribute("eid", elementId);
193: initElement.setAttribute("widgetcontainer-eid", targetId);
194:
195: partElement.appendChild(initElement);
196: }
197:
198: /**
199: * @see nextapp.echo2.webcontainer.ComponentSynchronizePeer#renderUpdate(nextapp.echo2.webcontainer.RenderContext,
200: * nextapp.echo2.app.update.ServerComponentUpdate, java.lang.String)
201: */
202: public boolean renderUpdate(RenderContext rc,
203: ServerComponentUpdate update, String targetId) {
204: DomUpdate.renderElementRemove(rc.getServerMessage(),
205: ContainerInstance.getElementId(update.getParent()));
206: renderAdd(rc, update, targetId, update.getParent());
207: return true;
208: }
209: }
|