KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > CoolDelayMessage


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38
39 import nextapp.echo2.app.util.Uid;
40 import nextapp.echo2.webcontainer.ContainerContext;
41 import nextapp.echo2.webrender.ClientProperties;
42 import nextapp.echo2.webrender.Connection;
43 import nextapp.echo2.webrender.ContentType;
44 import nextapp.echo2.webrender.ServerDelayMessage;
45 import nextapp.echo2.webrender.Service;
46 import nextapp.echo2.webrender.WebRenderServlet;
47 import nextapp.echo2.webrender.output.HtmlDocument;
48 import nextapp.echo2.webrender.output.XmlDocument;
49
50 /**
51  * An example of a more creative <code>ServerDelayMessage</code>.
52  * <p>
53  * Creating a custom <code>ServerDelayMessage</code>
54  * is a fairly advanced activity that requires working with the Echo
55  * Web Render Engine API.
56  */

57 public class CoolDelayMessage extends ServerDelayMessage {
58
59     private static int BUFFER_SIZE = 4096;
60     private static String JavaDoc IMAGE_RESOURCE_NAME = Styles.IMAGE_PATH + "ShadowOverlay.png";
61     
62     private static Service IMAGE_SERVICE = new ImageService();
63     static {
64         WebRenderServlet.getServiceRegistry().add(IMAGE_SERVICE);
65     }
66     
67     private static class ImageService implements Service {
68
69         private static final String JavaDoc SERVICE_ID = Uid.generateUidString();
70         
71         /**
72          * @see nextapp.echo2.webrender.Service#getId()
73          */

74         public String JavaDoc getId() {
75             return SERVICE_ID;
76         }
77
78         /**
79          * @see nextapp.echo2.webrender.Service#getVersion()
80          */

81         public int getVersion() {
82             return 0;
83         }
84
85         /**
86          * @see nextapp.echo2.webrender.Service#service(nextapp.echo2.webrender.Connection)
87          */

88         public void service(Connection conn) throws IOException JavaDoc {
89             conn.setContentType(ContentType.IMAGE_PNG);
90             OutputStream JavaDoc out = conn.getOutputStream();
91
92             InputStream JavaDoc in = null;
93             byte[] buffer = new byte[BUFFER_SIZE];
94             int bytesRead = 0;
95             
96             try {
97                 in = CoolDelayMessage.class.getResourceAsStream(IMAGE_RESOURCE_NAME);
98                 if (in == null) {
99                     throw new IllegalArgumentException JavaDoc("Specified resource does not exist: " + IMAGE_RESOURCE_NAME + ".");
100                 }
101                 do {
102                     bytesRead = in.read(buffer);
103                     if (bytesRead > 0) {
104                         out.write(buffer, 0, bytesRead);
105                     }
106                 } while (bytesRead > 0);
107             } finally {
108                 if (in != null) { try { in.close(); } catch (IOException JavaDoc ex) { } }
109             }
110         }
111     }
112     
113     private Element JavaDoc messageElement;
114     
115     public CoolDelayMessage(ContainerContext containerContext, String JavaDoc messageText) {
116         XmlDocument xmlDocument = new XmlDocument("div", null, null, HtmlDocument.XHTML_1_0_NAMESPACE_URI);
117         Document JavaDoc document = xmlDocument.getDocument();
118         Element JavaDoc divElement = document.getDocumentElement();
119         divElement.setAttribute("id", ELEMENT_ID_MESSAGE);
120         divElement.setAttribute("style", "position:absolute;top:0px;left:0px;width:100%;height:100%;cursor:wait;"
121                 + "margin:0px;padding:0px;visibility:hidden;z-index:10000;");
122
123         Element JavaDoc tableElement = document.createElement("table");
124         tableElement.setAttribute("style", "width:100%;height:100%;border:0px;padding:0px;");
125         divElement.appendChild(tableElement);
126         
127         Element JavaDoc tbodyElement = document.createElement("tbody");
128         tableElement.appendChild(tbodyElement);
129         
130         Element JavaDoc trElement = document.createElement("tr");
131         tbodyElement.appendChild(trElement);
132         
133         Element JavaDoc tdElement = document.createElement("td");
134         tdElement.setAttribute("style", "width:100%;height:100%;");
135         tdElement.setAttribute("valign", "middle");
136         tdElement.setAttribute("align", "center");
137         trElement.appendChild(tdElement);
138         
139         Element JavaDoc longDivElement = document.createElement("div");
140         longDivElement.setAttribute("id", ELEMENT_ID_LONG_MESSAGE);
141         String JavaDoc longDivStyleText = "color:#4f4f4f;width:277px;padding-top:120px;height:156px;"
142                 + "font-family:verdana,arial,helvetica,sans-serif;font-size:14pt;font-weight:bold;font-style:italic;"
143                 + "text-align:center;";
144         if (containerContext.getClientProperties().getBoolean(ClientProperties.PROPRIETARY_IE_PNG_ALPHA_FILTER_REQUIRED)) {
145             // Use Internet Explorer PNG filter hack to achieve transparency.
146
longDivStyleText += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
147                     + containerContext.getServiceUri(IMAGE_SERVICE) + "', sizingMethod='scale')";
148         } else {
149             longDivStyleText += "background-image:url(" + containerContext.getServiceUri(IMAGE_SERVICE) + ");";
150         }
151         longDivElement.setAttribute("style", longDivStyleText);
152
153         longDivElement.appendChild(document.createTextNode(messageText));
154         tdElement.appendChild(longDivElement);
155         
156         messageElement = divElement;
157     }
158     
159     /**
160      * @see nextapp.echo2.webrender.ServerDelayMessage#getMessage()
161      */

162     public Element JavaDoc getMessage() {
163         return messageElement;
164     }
165 }
166
Popular Tags