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.Border;
033: import nextapp.echo2.app.Component;
034: import nextapp.echo2.app.Extent;
035: import nextapp.echo2.app.update.ServerComponentUpdate;
036: import nextapp.echo2.webcontainer.ActionProcessor;
037: import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
038: import nextapp.echo2.webcontainer.ContainerInstance;
039: import nextapp.echo2.webcontainer.RenderContext;
040: import nextapp.echo2.webcontainer.SynchronizePeerFactory;
041: import nextapp.echo2.webcontainer.propertyrender.BorderRender;
042: import nextapp.echo2.webcontainer.propertyrender.ExtentRender;
043: import nextapp.echo2.webrender.ServerMessage;
044: import nextapp.echo2.webrender.Service;
045: import nextapp.echo2.webrender.WebRenderServlet;
046: import nextapp.echo2.webrender.service.JavaScriptService;
047:
048: import org.w3c.dom.Element;
049: import org.w3c.dom.Node;
050:
051: import tucana.echo2.app.WidgetDash;
052:
053: public class WidgetDashPeer implements ComponentSynchronizePeer,
054: ActionProcessor {
055:
056: /**
057: * Service to provide supporting JavaScript library.
058: */
059: private static final Service WIDGET_DASH_SERVICE = JavaScriptService
060: .forResource("Tucana.WidgetDash",
061: "/tucana/echo2/webcontainer/resource/js/WidgetDash.js");
062:
063: static {
064: WebRenderServlet.getServiceRegistry().add(WIDGET_DASH_SERVICE);
065: }
066:
067: public String getContainerId(Component child) {
068: return ContainerInstance.getElementId(child.getParent())
069: + "_onDeck";
070: }
071:
072: public void renderAdd(RenderContext rc,
073: ServerComponentUpdate update, String targetId,
074: Component component) {
075: rc.getServerMessage().addLibrary(WIDGET_DASH_SERVICE.getId());
076: renderInitDirective(rc, (WidgetDash) component, targetId);
077: Component[] children = component.getComponents();
078: for (int i = 0; i < children.length; i++) {
079: Component child = children[i];
080: ComponentSynchronizePeer syncPeer = SynchronizePeerFactory
081: .getPeerForComponent(child.getClass());
082: syncPeer
083: .renderAdd(rc, update, getContainerId(child), child);
084: }
085: }
086:
087: public void renderDispose(RenderContext rc,
088: ServerComponentUpdate update, Component component) {
089: ServerMessage serverMessage = rc.getServerMessage();
090: serverMessage.addLibrary(WIDGET_DASH_SERVICE.getId());
091: Element partElement = serverMessage.addPart(
092: ServerMessage.GROUP_ID_UPDATE,
093: "TucanaWidgetDash.MessageProcessor");
094: Element disposeElement = serverMessage.getDocument()
095: .createElement("dispose");
096:
097: String elementId = ContainerInstance.getElementId(component);
098:
099: disposeElement.setAttribute("eid", elementId);
100:
101: partElement.appendChild(disposeElement);
102: }
103:
104: public boolean renderUpdate(RenderContext rc,
105: ServerComponentUpdate update, String targetId) {
106: ServerMessage serverMessage = rc.getServerMessage();
107: Element partElement = serverMessage.addPart(
108: ServerMessage.GROUP_ID_UPDATE,
109: "TucanaWidgetDash.MessageProcessor");
110: Element updateElement = serverMessage.getDocument()
111: .createElement("update");
112: String componentId = ContainerInstance.getElementId(update
113: .getParent());
114: updateElement.setAttribute("eid", componentId);
115: partElement.appendChild(updateElement);
116:
117: String[] updatedPropertyNames = update
118: .getUpdatedPropertyNames();
119: if (updatedPropertyNames.length > 0) {
120: Element propertiesElement = serverMessage.getDocument()
121: .createElement("properties");
122: updateElement.appendChild(propertiesElement);
123: WidgetDash widgetDash = (WidgetDash) update.getParent();
124: for (int i = 0; i < updatedPropertyNames.length; i++) {
125: String propertyName = updatedPropertyNames[i];
126: if (WidgetDash.PROPERTY_COLUMN_COUNT
127: .equals(propertyName)) {
128: addToStringProperty(propertiesElement, widgetDash,
129: WidgetDash.PROPERTY_COLUMN_COUNT);
130: } else if (WidgetDash.PROPERTY_COLUMN_SPACING
131: .equals(propertyName)) {
132: addExtentProperty(propertiesElement, widgetDash,
133: WidgetDash.PROPERTY_COLUMN_SPACING);
134: } else if (WidgetDash.PROPERTY_DRAG_CONTAINER
135: .equals(propertyName)) {
136: addDragContainerProperty(propertiesElement,
137: widgetDash);
138: } else if (WidgetDash.PROPERTY_DRAG_IN_BODY
139: .equals(propertyName)) {
140: addToStringProperty(propertiesElement, widgetDash,
141: WidgetDash.PROPERTY_DRAG_IN_BODY);
142: } else if (WidgetDash.PROPERTY_DRAGGED_WIDGET_OPACITY
143: .equals(propertyName)) {
144: addToStringProperty(propertiesElement, widgetDash,
145: WidgetDash.PROPERTY_DRAGGED_WIDGET_OPACITY);
146: } else if (WidgetDash.PROPERTY_SHADOW_BORDER
147: .equals(propertyName)) {
148: addShadowBorderProperty(propertiesElement,
149: widgetDash);
150: } else if (WidgetDash.PROPERTY_SHADOW_OPACITY
151: .equals(propertyName)) {
152: addToStringProperty(propertiesElement, widgetDash,
153: WidgetDash.PROPERTY_SHADOW_OPACITY);
154: } else if (WidgetDash.PROPERTY_SHADOW_TYPE
155: .equals(propertyName)) {
156: addShadowTypeProperty(propertiesElement, widgetDash);
157: } else if (WidgetDash.PROPERTY_WIDGET_SPACING
158: .equals(propertyName)) {
159: addExtentProperty(propertiesElement, widgetDash,
160: WidgetDash.PROPERTY_WIDGET_SPACING);
161: } else if (WidgetDash.PROPERTY_RETURN_METHOD
162: .equals(propertyName)) {
163: addReturnMethodProperty(propertiesElement,
164: widgetDash);
165: } else if (WidgetDash.PROPERTY_DRIFT_STEP
166: .equals(propertyName)) {
167: addToStringProperty(propertiesElement, widgetDash,
168: WidgetDash.PROPERTY_DRIFT_STEP);
169: } else if (WidgetDash.PROPERTY_DRIFT_INTERVAL
170: .equals(propertyName)) {
171: addToStringProperty(propertiesElement, widgetDash,
172: WidgetDash.PROPERTY_DRIFT_INTERVAL);
173: }
174: }
175: }
176:
177: Component[] addedChildren = update.getAddedChildren();
178: for (int i = 0; i < addedChildren.length; i++) {
179: Component child = addedChildren[i];
180: ComponentSynchronizePeer syncPeer = SynchronizePeerFactory
181: .getPeerForComponent(child.getClass());
182: syncPeer
183: .renderAdd(rc, update, getContainerId(child), child);
184: }
185:
186: return false;
187: }
188:
189: /**
190: * Renders a directive to the outgoing <code>ServerMessage</code> to
191: * render and intialize the state of a <code>WidgetPane</code>.
192: *
193: * @param rc the relevant <code>RenderContext</code>
194: * @param windowPane the <code>Widget</code>
195: * @param targetId the id of the container element
196: */
197: private void renderInitDirective(RenderContext rc,
198: WidgetDash widgetDash, String targetId) {
199: String elementId = ContainerInstance.getElementId(widgetDash);
200: ServerMessage serverMessage = rc.getServerMessage();
201: Element partElement = serverMessage.addPart(
202: ServerMessage.GROUP_ID_UPDATE,
203: "TucanaWidgetDash.MessageProcessor");
204: Element initElement = serverMessage.getDocument()
205: .createElement("init");
206: initElement.setAttribute("eid", elementId);
207: initElement.setAttribute("container-eid", targetId);
208: Element propertiesElement = serverMessage.getDocument()
209: .createElement("properties");
210: initElement.appendChild(propertiesElement);
211:
212: addToStringProperty(propertiesElement, widgetDash,
213: WidgetDash.PROPERTY_COLUMN_COUNT);
214: addExtentProperty(propertiesElement, widgetDash,
215: WidgetDash.PROPERTY_COLUMN_SPACING);
216: addExtentProperty(propertiesElement, widgetDash,
217: WidgetDash.PROPERTY_WIDGET_SPACING);
218:
219: addShadowTypeProperty(propertiesElement, widgetDash);
220: addShadowBorderProperty(propertiesElement, widgetDash);
221: addDragContainerProperty(propertiesElement, widgetDash);
222: addToStringProperty(propertiesElement, widgetDash,
223: WidgetDash.PROPERTY_SHADOW_OPACITY);
224: addToStringProperty(propertiesElement, widgetDash,
225: WidgetDash.PROPERTY_DRAGGED_WIDGET_OPACITY);
226: addToStringProperty(propertiesElement, widgetDash,
227: WidgetDash.PROPERTY_DRAG_IN_BODY);
228:
229: addReturnMethodProperty(propertiesElement, widgetDash);
230: addToStringProperty(propertiesElement, widgetDash,
231: WidgetDash.PROPERTY_DRIFT_STEP);
232: addToStringProperty(propertiesElement, widgetDash,
233: WidgetDash.PROPERTY_DRIFT_INTERVAL);
234:
235: partElement.appendChild(initElement);
236: }
237:
238: private Element createPropertyElement(Node parentNode,
239: String propertyName, String propertyValue) {
240: Element propertyElement = parentNode.getOwnerDocument()
241: .createElement("property");
242: parentNode.appendChild(propertyElement);
243:
244: propertyElement.setAttribute("name", propertyName);
245: if (propertyValue != null) {
246: propertyElement.setAttribute("value", propertyValue);
247: }
248:
249: return propertyElement;
250: }
251:
252: private void addToStringProperty(Node parentNode,
253: WidgetDash widgetDash, String propertyName) {
254: Object obj = widgetDash.getProperty(propertyName);
255: if (obj != null) {
256: createPropertyElement(parentNode, propertyName, obj
257: .toString());
258: }
259: }
260:
261: private void addExtentProperty(Node parentNode,
262: WidgetDash widgetDash, String propertyName) {
263: Extent extent = (Extent) widgetDash.getProperty(propertyName);
264: if (extent != null) {
265: String extentString = ExtentRender
266: .renderCssAttributeValue(extent);
267: createPropertyElement(parentNode, propertyName,
268: extentString);
269: }
270: }
271:
272: private void addShadowTypeProperty(Node parentNode,
273: WidgetDash widgetDash) {
274: Integer type = (Integer) widgetDash
275: .getProperty(WidgetDash.PROPERTY_SHADOW_TYPE);
276: if (type != null) {
277: int typeVal = type.intValue();
278: String typeString;
279: if (typeVal == WidgetDash.SHADOW_TYPE_BORDER) {
280: typeString = "border";
281: } else {
282: typeString = "clone";
283: }
284: createPropertyElement(parentNode,
285: WidgetDash.PROPERTY_SHADOW_TYPE, typeString);
286: }
287: }
288:
289: private void addReturnMethodProperty(Node parentNode,
290: WidgetDash widgetDash) {
291: Integer type = (Integer) widgetDash
292: .getProperty(WidgetDash.PROPERTY_RETURN_METHOD);
293: if (type != null) {
294: int typeVal = type.intValue();
295: String typeString;
296: if (typeVal == WidgetDash.RETURN_METHOD_DRIFT) {
297: typeString = "drift";
298: } else {
299: typeString = "snap";
300: }
301: createPropertyElement(parentNode,
302: WidgetDash.PROPERTY_RETURN_METHOD, typeString);
303: }
304: }
305:
306: private void addShadowBorderProperty(Node parentNode,
307: WidgetDash widgetDash) {
308: Border border = (Border) widgetDash
309: .getProperty(WidgetDash.PROPERTY_SHADOW_BORDER);
310: if (border != null) {
311: String borderStyle = BorderRender
312: .renderCssAttributeValue(border);
313: createPropertyElement(parentNode,
314: WidgetDash.PROPERTY_SHADOW_BORDER, borderStyle);
315: }
316: }
317:
318: private void addDragContainerProperty(Node parentNode,
319: WidgetDash widgetDash) {
320: Component dragContainer = (Component) widgetDash
321: .getProperty(WidgetDash.PROPERTY_DRAG_CONTAINER);
322: if (dragContainer != null) {
323: String containerId = ContainerInstance
324: .getElementId(dragContainer);
325: if (containerId != null) {
326: createPropertyElement(parentNode,
327: WidgetDash.PROPERTY_DRAG_CONTAINER, containerId);
328: }
329: }
330: }
331:
332: public void processAction(ContainerInstance ci,
333: Component component, Element actionElement) {
334: String actionName = actionElement
335: .getAttribute(ActionProcessor.ACTION_NAME);
336: if (WidgetDash.ACTION_POSITIONS_UPDATED.equals(actionName)) {
337: ci.getUpdateManager().getClientUpdateManager()
338: .setComponentAction(component,
339: WidgetDash.ACTION_POSITIONS_UPDATED, null);
340: }
341: }
342: }
|