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.managers.pages;
019:
020: import java.util.*;
021:
022: import net.ar.webonswing.*;
023: import net.ar.webonswing.helpers.*;
024: import net.ar.webonswing.pages.*;
025: import net.ar.webonswing.wrapping.*;
026:
027: /**
028: * Implementacion de administrador de paginas bastante simple, que mantiene una
029: * relacion de 1:1 entre paginas, ventanas y paths. Esto puede traer
030: * limitaciones con respecto al reuso de paginas y/o ventanas.
031: *
032: * @author Fernando Damian Petrola
033: */
034: public class HtmlPageManager implements PageManager {
035: protected String defaultPageClass;
036: protected String defaultWindowPlace;
037: protected Hashtable pages;
038:
039: public HtmlPageManager() {
040: this (new Hashtable());
041: }
042:
043: public HtmlPageManager(Hashtable aPagesSpecification) {
044: pages = aPagesSpecification;
045: }
046:
047: public Page getPageForPath(String aPath) {
048: try {
049: return createPage(getEntryLike(new HtmlPageManagerEntry(
050: aPath, null, null, null)));
051: } catch (WebOnSwingException e) {
052: throw new PageNotFoundException(aPath, e);
053: }
054: }
055:
056: public Page getPageForWindowName(String aWindowName) {
057: try {
058: return createPage(getEntryLike(new HtmlPageManagerEntry(
059: null, null, aWindowName, null)));
060: } catch (WebOnSwingException e) {
061: throw new PageNotFoundException(aWindowName, e);
062: }
063: }
064:
065: public String getPathForPageName(String aPageName) {
066: return getEntryLike(
067: new HtmlPageManagerEntry(null, aPageName, null, null))
068: .getPath();
069: }
070:
071: public VisualWindow getWindowForPageName(String aPageName) {
072: return createWindow(getEntryLike(new HtmlPageManagerEntry(null,
073: aPageName, null, null)));
074: }
075:
076: public String getPathForWindowName(String aWindowName) {
077: return getEntryLike(
078: new HtmlPageManagerEntry(null, null, aWindowName, null))
079: .getPath();
080: }
081:
082: public VisualWindow getWindowForPath(String aPath) {
083: return createWindow(getEntryLike(new HtmlPageManagerEntry(
084: aPath, null, null, null)));
085: }
086:
087: protected VisualWindow createWindow(HtmlPageManagerEntry anEntry) {
088: try {
089: Object theNewInstance = Class.forName(
090: anEntry.getWindowClassName()).newInstance();
091:
092: HierarchyWrapper theHierarchyWrapper = WosFramework
093: .getInstance().getHierarchyWrapper();
094: if (theHierarchyWrapper != null)
095: return theHierarchyWrapper.wrapWindow(theNewInstance);
096: else if (theNewInstance instanceof VisualWindow)
097: return (VisualWindow) theNewInstance;
098: else
099: throw new Exception("Not available window type for: "
100: + theNewInstance);
101: } catch (Exception e) {
102: throw new PageNotFoundException(anEntry.toString(), e);
103: }
104: }
105:
106: protected Page createPage(HtmlPageManagerEntry anEntry) {
107: try {
108: HtmlPage thePage = null;
109: String thePageClassName = anEntry.getPageClassName();
110:
111: if (thePageClassName.equals(""))
112: thePageClassName = getDefaultPageClass();
113:
114: thePage = (HtmlPage) Class.forName(thePageClassName)
115: .newInstance();
116: thePage.setWindowClassName(anEntry.getWindowClassName());
117: thePage.setWindowInSession(anEntry.getWindowPlace().equals(
118: "session"));
119:
120: return thePage;
121: } catch (Throwable e) {
122: throw new WebOnSwingException(
123: "Cannot find a page for entry like '" + anEntry
124: + "'", e);
125: }
126: }
127:
128: private HtmlPageManagerEntry getEntryLike(
129: HtmlPageManagerEntry anEntry) {
130: HtmlPageManagerEntry foundEntryLike = findEntryLike(anEntry);
131:
132: if (foundEntryLike != null)
133: return foundEntryLike;
134: else {
135: HtmlPageManagerEntry theDefaultEntryLike = anEntry
136: .getDefaultEntryLike();
137: pages.put(theDefaultEntryLike, theDefaultEntryLike);
138: return theDefaultEntryLike;
139: }
140: }
141:
142: private HtmlPageManagerEntry findEntryLike(
143: HtmlPageManagerEntry anEntry) {
144: for (Iterator i = pages.values().iterator(); i.hasNext();) {
145: HtmlPageManagerEntry theEntry = (HtmlPageManagerEntry) i
146: .next();
147: if (theEntry.like(anEntry))
148: return theEntry;
149: }
150:
151: return null;
152: }
153:
154: public void removePageWithPath(String aPath) {
155: for (Iterator i = pages.values().iterator(); i.hasNext();) {
156: HtmlPageManagerEntry theEntry = (HtmlPageManagerEntry) i
157: .next();
158:
159: if (theEntry.getPath().equals(aPath))
160: i.remove();
161: }
162: }
163:
164: public void addPage(String aPath, String aPage, String aWindow,
165: String aPlace) {
166: pages.put(aPath, new HtmlPageManagerEntry(aPath, aPage,
167: aWindow, aPlace));
168: }
169:
170: public Iterator getIterator() {
171: return pages.values().iterator();
172: }
173:
174: public Hashtable getPagesTable() {
175: return pages;
176: }
177:
178: public void setPagesTable(Hashtable unHashtable) {
179: pages = unHashtable;
180: }
181:
182: public Class getPageClassForWindowName(String aWindowName) {
183: try {
184: HtmlPageManagerEntry entryLike = getEntryLike(new HtmlPageManagerEntry(
185: null, null, aWindowName, null));
186:
187: String pageClassName = entryLike.getPageClassName();
188: if (pageClassName.trim().length() == 0)
189: pageClassName = getDefaultPageClass();
190:
191: return Class.forName(pageClassName);
192: } catch (Exception e) {
193: throw new PageNotFoundException(aWindowName, e);
194: }
195: }
196:
197: public String getDefaultPageClass() {
198: return defaultPageClass;
199: }
200:
201: public String getDefaultWindowPlace() {
202: return defaultWindowPlace;
203: }
204: }
|