Java tutorial
/* * (C) Copyright 2010, by Francesco Delli Paoli. * * Project Info: http://mwebc.sourceforge.net/ * * This file is part of mwebc - Model Web Controller. * * mwebc is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * mwebc is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with mwebc; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package it.delli.mwebc.ui.impl; import it.delli.mwebc.annotations.CommandDefinition; import it.delli.mwebc.command.Command; import it.delli.mwebc.ui.Application; import it.delli.mwebc.ui.Page; import it.delli.mwebc.ui.Resources; import it.delli.mwebc.utils.CastHelper; import it.delli.mwebc.utils.DefaultCastHelper; import java.io.IOException; import java.io.Writer; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ApplicationImpl implements Application { private static Log log = LogFactory.getLog(ApplicationImpl.class); @SuppressWarnings("rawtypes") private Hashtable<String, Class> widgetClasses = new Hashtable<String, Class>(); @SuppressWarnings("rawtypes") private Hashtable<Class, String> clientWidgets = new Hashtable<Class, String>(); private Hashtable<Class, Class> castHelperClasses = new Hashtable<Class, Class>(); private Hashtable<String, Command> availableCommands = new Hashtable<String, Command>(); private Hashtable<String, Class> eventClasses = new Hashtable<String, Class>(); private LinkedList<String> javaScriptStream = new LinkedList<String>(); private Hashtable<String, Page> pages = new Hashtable<String, Page>(); private Resources resources = null; private HttpServletRequest httpServletRequest = null; private HttpServletResponse httpServletResponse = null; private ServletContext servletContext = null; private String pageForward = null; private String pageOpen = null; @Override public void setHttpServletRequest(HttpServletRequest httpServletRequest) { this.httpServletRequest = httpServletRequest; } @Override public HttpServletRequest getHttpServletRequest() { return httpServletRequest; } @Override public void setHttpServletResponse(HttpServletResponse httpServletResponse) { this.httpServletResponse = httpServletResponse; } @Override public HttpServletResponse getHttpServletResponse() { return httpServletResponse; } @Override public ServletContext getServletContext() { return servletContext; } @Override public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } @SuppressWarnings("rawtypes") public void registerWidget(String widgetName, Class widgetClass, String clientWidget, Class castHelper) { widgetClasses.put(widgetName, widgetClass); clientWidgets.put(widgetClass, clientWidget); if (castHelper != null) { castHelperClasses.put(widgetClass, castHelper); } } @SuppressWarnings("rawtypes") public CastHelper getCastHelper(Class widgetClass) { CastHelper castHelper = null; if (castHelperClasses.get(widgetClass) != null) { try { castHelper = (CastHelper) castHelperClasses.get(widgetClass).newInstance(); } catch (Exception e) { e.printStackTrace(); } } else { castHelper = new DefaultCastHelper(); } return castHelper; } @SuppressWarnings("rawtypes") public Class getWidgetClass(String widgetName) { return widgetClasses.get(widgetName); } @SuppressWarnings("rawtypes") public String getClientWidget(Class widgetClass) { if (clientWidgets.get(widgetClass) == null) { System.out.println(); } return clientWidgets.get(widgetClass); } @SuppressWarnings("rawtypes") public void registerEvent(String eventType, Class eventClass) { eventClasses.put(eventType, eventClass); } @SuppressWarnings("rawtypes") public Class getEventClass(String eventType) { return eventClasses.get(eventType); } public void registerCommand(Command command) { CommandDefinition commandAnnotation = command.getClass().getAnnotation(CommandDefinition.class); if (commandAnnotation != null) { availableCommands.put(commandAnnotation.value(), command); } } public Command getCommand(String action) { return availableCommands.get(action); } public void addPage(Page page) { pages.put(page.getName(), page); } public Page getPage(String id) { return pages.get(id); } public Resources getResources() { return resources; } public void removePage(Page page) { pages.remove(page.getName()); } public void setResources(Resources resources) { this.resources = resources; } public Page createPage(String name) { return new PageImpl(this, name); } public void forward(String pageName) { this.pageForward = pageName; } public void open(String pageName) { this.pageOpen = pageName; } public void printJavaScriptStream(Writer out) { try { out.append("<cmds>"); for (Iterator<String> javaScriptIterator = getJavaScriptStatementIterator(); javaScriptIterator .hasNext();) { String command = javaScriptIterator.next(); out.append("<cmd><![CDATA[" + command + "]]></cmd>\n"); } clearJavaScriptStatementStream(); if (pageForward != null) { out.append("<forward><![CDATA[" + httpServletRequest.getContextPath() + "/" + pageForward + ".mwc ]]></forward>\n"); pageForward = null; } if (pageOpen != null) { out.append("<open><![CDATA[" + httpServletRequest.getContextPath() + "/" + pageOpen + ".mwc ]]></open>\n"); pageOpen = null; } out.append("</cmds>"); } catch (IOException e) { log.error("Exception in rendering Application controller", e); } } public void addJavaScriptStatement(String statement) { javaScriptStream.add(statement); } public void clearJavaScriptStatementStream() { javaScriptStream.clear(); } public Iterator<String> getJavaScriptStatementIterator() { return javaScriptStream.iterator(); } }