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.testapp.interactive;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035:
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Element;
038:
039: import nextapp.echo2.app.util.Uid;
040: import nextapp.echo2.webcontainer.ContainerContext;
041: import nextapp.echo2.webrender.ClientProperties;
042: import nextapp.echo2.webrender.Connection;
043: import nextapp.echo2.webrender.ContentType;
044: import nextapp.echo2.webrender.ServerDelayMessage;
045: import nextapp.echo2.webrender.Service;
046: import nextapp.echo2.webrender.WebRenderServlet;
047: import nextapp.echo2.webrender.output.HtmlDocument;
048: import nextapp.echo2.webrender.output.XmlDocument;
049:
050: /**
051: * An example of a more creative <code>ServerDelayMessage</code>.
052: * <p>
053: * Creating a custom <code>ServerDelayMessage</code>
054: * is a fairly advanced activity that requires working with the Echo
055: * Web Render Engine API.
056: */
057: public class CoolDelayMessage extends ServerDelayMessage {
058:
059: private static int BUFFER_SIZE = 4096;
060: private static String IMAGE_RESOURCE_NAME = Styles.IMAGE_PATH
061: + "ShadowOverlay.png";
062:
063: private static Service IMAGE_SERVICE = new ImageService();
064: static {
065: WebRenderServlet.getServiceRegistry().add(IMAGE_SERVICE);
066: }
067:
068: private static class ImageService implements Service {
069:
070: private static final String SERVICE_ID = Uid
071: .generateUidString();
072:
073: /**
074: * @see nextapp.echo2.webrender.Service#getId()
075: */
076: public String getId() {
077: return SERVICE_ID;
078: }
079:
080: /**
081: * @see nextapp.echo2.webrender.Service#getVersion()
082: */
083: public int getVersion() {
084: return 0;
085: }
086:
087: /**
088: * @see nextapp.echo2.webrender.Service#service(nextapp.echo2.webrender.Connection)
089: */
090: public void service(Connection conn) throws IOException {
091: conn.setContentType(ContentType.IMAGE_PNG);
092: OutputStream out = conn.getOutputStream();
093:
094: InputStream in = null;
095: byte[] buffer = new byte[BUFFER_SIZE];
096: int bytesRead = 0;
097:
098: try {
099: in = CoolDelayMessage.class
100: .getResourceAsStream(IMAGE_RESOURCE_NAME);
101: if (in == null) {
102: throw new IllegalArgumentException(
103: "Specified resource does not exist: "
104: + IMAGE_RESOURCE_NAME + ".");
105: }
106: do {
107: bytesRead = in.read(buffer);
108: if (bytesRead > 0) {
109: out.write(buffer, 0, bytesRead);
110: }
111: } while (bytesRead > 0);
112: } finally {
113: if (in != null) {
114: try {
115: in.close();
116: } catch (IOException ex) {
117: }
118: }
119: }
120: }
121: }
122:
123: private Element messageElement;
124:
125: public CoolDelayMessage(ContainerContext containerContext,
126: String messageText) {
127: XmlDocument xmlDocument = new XmlDocument("div", null, null,
128: HtmlDocument.XHTML_1_0_NAMESPACE_URI);
129: Document document = xmlDocument.getDocument();
130: Element divElement = document.getDocumentElement();
131: divElement.setAttribute("id", ELEMENT_ID_MESSAGE);
132: divElement
133: .setAttribute(
134: "style",
135: "position:absolute;top:0px;left:0px;width:100%;height:100%;cursor:wait;"
136: + "margin:0px;padding:0px;visibility:hidden;z-index:10000;");
137:
138: Element tableElement = document.createElement("table");
139: tableElement.setAttribute("style",
140: "width:100%;height:100%;border:0px;padding:0px;");
141: divElement.appendChild(tableElement);
142:
143: Element tbodyElement = document.createElement("tbody");
144: tableElement.appendChild(tbodyElement);
145:
146: Element trElement = document.createElement("tr");
147: tbodyElement.appendChild(trElement);
148:
149: Element tdElement = document.createElement("td");
150: tdElement.setAttribute("style", "width:100%;height:100%;");
151: tdElement.setAttribute("valign", "middle");
152: tdElement.setAttribute("align", "center");
153: trElement.appendChild(tdElement);
154:
155: Element longDivElement = document.createElement("div");
156: longDivElement.setAttribute("id", ELEMENT_ID_LONG_MESSAGE);
157: String longDivStyleText = "color:#4f4f4f;width:277px;padding-top:120px;height:156px;"
158: + "font-family:verdana,arial,helvetica,sans-serif;font-size:14pt;font-weight:bold;font-style:italic;"
159: + "text-align:center;";
160: if (containerContext
161: .getClientProperties()
162: .getBoolean(
163: ClientProperties.PROPRIETARY_IE_PNG_ALPHA_FILTER_REQUIRED)) {
164: // Use Internet Explorer PNG filter hack to achieve transparency.
165: longDivStyleText += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
166: + containerContext.getServiceUri(IMAGE_SERVICE)
167: + "', sizingMethod='scale')";
168: } else {
169: longDivStyleText += "background-image:url("
170: + containerContext.getServiceUri(IMAGE_SERVICE)
171: + ");";
172: }
173: longDivElement.setAttribute("style", longDivStyleText);
174:
175: longDivElement
176: .appendChild(document.createTextNode(messageText));
177: tdElement.appendChild(longDivElement);
178:
179: messageElement = divElement;
180: }
181:
182: /**
183: * @see nextapp.echo2.webrender.ServerDelayMessage#getMessage()
184: */
185: public Element getMessage() {
186: return messageElement;
187: }
188: }
|