001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.pages;
019:
020: import java.awt.*;
021: import java.io.*;
022: import java.util.*;
023:
024: import javax.servlet.http.*;
025:
026: import net.ar.webonswing.*;
027: import net.ar.webonswing.helpers.*;
028: import net.ar.webonswing.managers.contributors.*;
029: import net.ar.webonswing.managers.persistence.*;
030: import net.ar.webonswing.managers.script.*;
031: import net.ar.webonswing.managers.styles.*;
032: import net.ar.webonswing.managers.templates.*;
033: import net.ar.webonswing.remote.*;
034: import net.ar.webonswing.render.*;
035: import net.ar.webonswing.render.markup.*;
036: import net.ar.webonswing.render.templates.html.*;
037: import net.ar.webonswing.wrapping.*;
038:
039: public class DefaultHtmlPage extends DefaultPage implements HtmlPage {
040: public static final String PERSISTED_DATA = "persisted-data";
041: private static final String CACHE_KEY_PROPERTY = "theCacheKey";
042: protected ContributionContainer theContributionContainer;
043: protected Tag theBeforeFormScript;
044: protected Tag theAfterFormScript;
045: protected Map theRequestParameters;
046: protected String theRequestUrl;
047: protected boolean windowInSession;
048:
049: public DefaultHtmlPage() {
050: init();
051: }
052:
053: public void init() {
054: theContributionContainer = new ContributionContainer(
055: new ScriptContributionContainer(),
056: new StyleContributionContainer(),
057: new DefaultPersistenceContributionContainer());
058:
059: theContributionContainer.getScriptManager().addInclude(
060: WosFramework.getInstance().getCompleteResourcePath()
061: + "/js/WebOnSwing.js");
062: theContributionContainer.getStyleManager().addInclude(
063: WosFramework.getInstance().getCompleteResourcePath()
064: + "/styles/WebOnSwing.css");
065:
066: theBeforeFormScript = new Tag("script");
067: theAfterFormScript = new Tag("script");
068: }
069:
070: public void prepareFromRequest(HttpServletRequest aRequest) {
071: theRequestParameters = WosHelper
072: .RequestParametersToMap(aRequest);
073: theRequestUrl = aRequest.getContextPath()
074: + aRequest.getServletPath();
075:
076: String[] thePersistedData = (String[]) theRequestParameters
077: .get(PERSISTED_DATA);
078: if (thePersistedData != null && thePersistedData[0] != null)
079: theContributionContainer
080: .setPersistenceContainer((PersistenceContributionContainer) Base64
081: .decodeToObject(thePersistedData[0]));
082: }
083:
084: public void processRequest(HttpServletRequest aRequest) {
085: }
086:
087: public void dispatchEvents() {
088: new UpdatesFirstEventDispatcher(getWindow().getRootComponent())
089: .generateAndDispatchEvents(theRequestUrl,
090: theRequestParameters);
091:
092: processPersistedData();
093:
094: theContributionContainer.getPersistenceContainer().clearData();
095: }
096:
097: protected void processPersistedData() {
098: }
099:
100: public void prepareResponse(HttpServletResponse aResponse)
101: throws Exception {
102: if (getWindow().getBounds().equals(new Rectangle(0, 0, 0, 0)))
103: getWindow().setBounds(new Rectangle(0, 0, 640, 480));
104:
105: getContributions(getWindow().getRootComponent());
106: }
107:
108: protected void getContributions(VisualComponent aComponent) {
109: new ContributionMaker(theContributionContainer)
110: .getContributionsFrom(aComponent);
111: }
112:
113: public void renderPage(HttpServletResponse aResponse)
114: throws Exception {
115: VisualComponent aRootComponent = getWindow().getRootComponent();
116:
117: HtmlTemplate theMainTemplate = getMainTemplate();
118: HtmlTemplate theFormTemplate = HtmlTemplateManager
119: .getClonedSubTemplate(theMainTemplate, "mainForm");
120:
121: ComponentRenderer theComponentRenderer = getNewComponentRendererInstance();
122:
123: theContributionContainer.getScriptManager().addCode(
124: "var thePageName='" + getPageName() + "';");
125:
126: theFormTemplate.addIdTagTemplateElement("theBody",
127: theComponentRenderer.render(aRootComponent), null);
128: theMainTemplate.addIdTagTemplateElement("mainForm",
129: theFormTemplate, null);
130:
131: HtmlTemplate theHeadTemplate = getHeadTemplate();
132: theHeadTemplate.addIdTagTemplateElement("theIncludedScripts",
133: theContributionContainer.getScriptManager()
134: .getIncludesCode());
135: theHeadTemplate.addIdTagTemplateElement("theIncludedStyles",
136: theContributionContainer.getStyleManager()
137: .getIncludesCode());
138: theHeadTemplate.addIdTagTemplateElement("theScriptCode",
139: theContributionContainer.getScriptManager().getCode()
140: .toString());
141: theHeadTemplate.addIdTagTemplateElement("theStyleCode",
142: theContributionContainer.getStyleManager().getCode()
143: .toString());
144:
145: theMainTemplate.addIdTagTemplateElement("theHead",
146: theHeadTemplate, null);
147:
148: theBeforeFormScript.addTextToContainer(theContributionContainer
149: .getScriptManager().getCodeBeforeForm().toString());
150: theMainTemplate.addIdTagTemplateElement("theBeforeFormScript",
151: theBeforeFormScript, null);
152:
153: theAfterFormScript.addTextToContainer(getPersistenceScript());
154: theAfterFormScript.addTextToContainer(theComponentRenderer
155: .getRenderingContributionContainer()
156: .getComponentInitScripts(aRootComponent)[0]);
157: theMainTemplate.addIdTagTemplateElement("theAfterFormScript",
158: theAfterFormScript, null);
159:
160: aResponse.getWriter().print(
161: new ContentRenderer(theMainTemplate).getResult());
162: }
163:
164: protected String getPersistenceScript() {
165: //TODO: optimizar el zip y serializacion
166: int options = Base64.DONT_BREAK_LINES;
167: options |= WosFramework.getInstance().isCompressPersistedData() ? Base64.GZIP
168: : 0;
169:
170: return "addOrUpdateHidden('"
171: + PERSISTED_DATA
172: + "', '"
173: + Base64.encodeObject(theContributionContainer
174: .getPersistenceContainer(), options) + "');";
175: }
176:
177: protected ComponentRenderer getNewComponentRendererInstance() {
178: return new CachingComponentRenderer(getWindow().getTypeId(),
179: getCacheKey());
180: }
181:
182: protected String getCacheKey() {
183: return (String) getWindow().getClientProperty(
184: CACHE_KEY_PROPERTY);
185: }
186:
187: protected void setCacheKey(String aKey) {
188: getWindow().putClientProperty(CACHE_KEY_PROPERTY, aKey);
189: }
190:
191: protected HtmlTemplate getMainTemplate() {
192: return (HtmlTemplate) WosFramework.getInstance()
193: .getTemplateManager().getTemplateForName("Main");
194: }
195:
196: protected HtmlTemplate getHeadTemplate() {
197: return (HtmlTemplate) WosFramework.getInstance()
198: .getTemplateManager()
199: .getTemplateForName("Main.theHead");
200: }
201:
202: protected String getPageName() {
203: String result = "";
204:
205: if (getWindow() != null)
206: result = WosFramework.getInstance().getPageManager()
207: .getPathForWindowName(getWindow().getTypeId());
208: else if (theWindowClassName != null)
209: result = WosFramework.getInstance().getPageManager()
210: .getPathForWindowName(theWindowClassName);
211: else
212: result = "unnamed";
213:
214: return WosFramework.getInstance().getContextPath() + result;
215: }
216:
217: public String getUrl() {
218: return getPageName() + ".page";
219: }
220:
221: public Map getPersistedData() {
222: return theContributionContainer.getPersistenceContainer()
223: .getPersistedData();
224: }
225:
226: public void setPersistedData(Map aData) {
227: theContributionContainer.getPersistenceContainer()
228: .setPersistedData(aData);
229: }
230:
231: public void persistValue(String aKey, Serializable aSerializable) {
232: theContributionContainer.getPersistenceContainer()
233: .persistValue(aKey, aSerializable);
234: }
235:
236: public Serializable restoreValue(String aKey) {
237: return theContributionContainer.getPersistenceContainer()
238: .restoreValue(aKey);
239: }
240:
241: public VisualWindow getPersitableWindow() {
242: try {
243: return (VisualWindow) ((Virtualizable) getWindow())
244: .getVirtualInstance((PersistenceContributionContainer) theContributionContainer
245: .getPersistenceContainer().clone());
246: } catch (CloneNotSupportedException e) {
247: throw new WebOnSwingException(
248: "Cannot clone the persistence manager", e);
249: }
250: }
251:
252: public boolean isWindowInSession() {
253: return windowInSession;
254: }
255:
256: public void setWindowInSession(boolean isWindowInSession) {
257: windowInSession = isWindowInSession;
258: }
259:
260: public void setRequest(HttpServletRequest aRequest) {
261: }
262: }
|