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.Color;
033: import nextapp.echo2.app.Component;
034: import nextapp.echo2.app.ImageReference;
035: import nextapp.echo2.app.update.ServerComponentUpdate;
036: import nextapp.echo2.webcontainer.ComponentSynchronizePeer;
037: import nextapp.echo2.webcontainer.ContainerInstance;
038: import nextapp.echo2.webcontainer.RenderContext;
039: import nextapp.echo2.webcontainer.image.ImageRenderSupport;
040: import nextapp.echo2.webcontainer.image.ImageTools;
041: import nextapp.echo2.webcontainer.propertyrender.ColorRender;
042: import nextapp.echo2.webrender.ServerMessage;
043: import nextapp.echo2.webrender.Service;
044: import nextapp.echo2.webrender.WebRenderServlet;
045: import nextapp.echo2.webrender.servermessage.DomUpdate;
046: import nextapp.echo2.webrender.service.JavaScriptService;
047:
048: import org.w3c.dom.Element;
049:
050: import tucana.echo2.app.ModalDimmer;
051:
052: /**
053: * WebRender peer class for {@link ModalDimmer}.
054: * @author Jeremy Volkman
055: *
056: */
057: public class ModalDimmerPeer implements ComponentSynchronizePeer,
058: ImageRenderSupport {
059:
060: /**
061: * Service to provide supporting JavaScript library.
062: */
063: private static final Service MODAL_DIMMER_SERVICE = JavaScriptService
064: .forResource("Tucana.ModalDimmer",
065: "/tucana/echo2/webcontainer/resource/js/ModalDimmer.js");
066:
067: static {
068: WebRenderServlet.getServiceRegistry().add(MODAL_DIMMER_SERVICE);
069: }
070:
071: public String getContainerId(Component child) {
072: return null;
073: }
074:
075: public void renderAdd(RenderContext rc,
076: ServerComponentUpdate update, String targetId,
077: Component component) {
078: rc.getServerMessage().addLibrary(MODAL_DIMMER_SERVICE.getId());
079: renderInitDirective(rc, component);
080: }
081:
082: public void renderDispose(RenderContext rc,
083: ServerComponentUpdate update, Component component) {
084: rc.getServerMessage().addLibrary(MODAL_DIMMER_SERVICE.getId());
085: renderDisposeDirective(rc, component);
086: }
087:
088: public boolean renderUpdate(RenderContext rc,
089: ServerComponentUpdate update, String targetId) {
090: DomUpdate.renderElementRemove(rc.getServerMessage(),
091: ContainerInstance.getElementId(update.getParent()));
092: renderAdd(rc, update, targetId, update.getParent());
093: return false;
094: }
095:
096: private void renderToStringProperty(Element propertiesElement,
097: Component component, String propertyName) {
098: Object property = component.getProperty(propertyName);
099: if (property != null) {
100: Element propertyElement = propertiesElement
101: .getOwnerDocument().createElement("property");
102: propertyElement.setAttribute("name", propertyName);
103: propertyElement.setAttribute("value", property.toString());
104: propertiesElement.appendChild(propertyElement);
105: }
106: }
107:
108: private void renderInitDirective(RenderContext rc,
109: Component component) {
110: ServerMessage serverMessage = rc.getServerMessage();
111: Element partElement = serverMessage.addPart(
112: ServerMessage.GROUP_ID_UPDATE,
113: "TucanaModalDimmer.MessageProcessor");
114: Element initElement = serverMessage.getDocument()
115: .createElement("init");
116: String elementId = ContainerInstance.getElementId(component);
117: initElement.setAttribute("eid", elementId);
118: Element propertiesElement = serverMessage.getDocument()
119: .createElement("properties");
120: initElement.appendChild(propertiesElement);
121:
122: Integer type = (Integer) component
123: .getProperty(ModalDimmer.PROPERTY_DIM_TYPE);
124: if (type != null) {
125: String strType = null;
126: switch (type.intValue()) {
127: case ModalDimmer.DIM_TYPE_OPACITY:
128: strType = "opacity";
129: break;
130: case ModalDimmer.DIM_TYPE_IMAGE:
131: strType = "image";
132: break;
133: case ModalDimmer.DIM_TYPE_AUTO:
134: strType = "auto";
135: break;
136: }
137:
138: if (strType != null) {
139: Element propertyElement = serverMessage.getDocument()
140: .createElement("property");
141: propertyElement.setAttribute("name",
142: ModalDimmer.PROPERTY_DIM_TYPE);
143: propertyElement.setAttribute("value", strType);
144: propertiesElement.appendChild(propertyElement);
145: }
146: }
147:
148: renderToStringProperty(propertiesElement, component,
149: ModalDimmer.PROPERTY_DIM_OPACITY);
150: renderToStringProperty(propertiesElement, component,
151: ModalDimmer.PROPERTY_DIM_ANIMATED);
152: renderToStringProperty(propertiesElement, component,
153: ModalDimmer.PROPERTY_DIM_ANIMATION_SPEED);
154:
155: Color color = (Color) component
156: .getProperty(ModalDimmer.PROPERTY_DIM_COLOR);
157: if (color != null) {
158: Element propertyElement = serverMessage.getDocument()
159: .createElement("property");
160: propertyElement.setAttribute("name",
161: ModalDimmer.PROPERTY_DIM_COLOR);
162: propertyElement.setAttribute("value", ColorRender
163: .renderCssAttributeValue(color));
164: propertiesElement.appendChild(propertyElement);
165: }
166:
167: String imageUri = ImageTools.getUri(rc, this , component,
168: ModalDimmer.PROPERTY_DIM_IMAGE);
169: if (imageUri != null) {
170: Element propertyElement = serverMessage.getDocument()
171: .createElement("property");
172: propertyElement.setAttribute("name",
173: ModalDimmer.PROPERTY_DIM_IMAGE);
174: propertyElement.setAttribute("value", imageUri);
175: propertiesElement.appendChild(propertyElement);
176: }
177:
178: partElement.appendChild(initElement);
179: }
180:
181: private void renderDisposeDirective(RenderContext rc,
182: Component component) {
183: ServerMessage serverMessage = rc.getServerMessage();
184: Element partElement = serverMessage.addPart(
185: ServerMessage.GROUP_ID_UPDATE,
186: "TucanaModalDimmer.MessageProcessor");
187: Element disposeElement = serverMessage.getDocument()
188: .createElement("dispose");
189: String elementId = ContainerInstance.getElementId(component);
190: disposeElement.setAttribute("eid", elementId);
191: partElement.appendChild(disposeElement);
192: }
193:
194: public ImageReference getImage(Component component, String imageId) {
195: if (ModalDimmer.PROPERTY_DIM_IMAGE.equals(imageId)) {
196: return (ImageReference) component.getProperty(imageId);
197: } else {
198: return null;
199: }
200: }
201: }
|