KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > page > AbstractPage


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: AbstractPage.java,v 1.8 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.page;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Locale JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.enhydra.barracuda.core.comp.BComponent;
29 import org.enhydra.barracuda.core.comp.ViewContext;
30 import org.enhydra.barracuda.core.event.BaseEvent;
31 import org.enhydra.barracuda.core.event.ControlEventContext;
32 import org.enhydra.barracuda.core.event.DispatchQueue;
33 import org.enhydra.barracuda.core.event.EventException;
34 import org.enhydra.barracuda.core.event.ViewEventContext;
35 import org.enhydra.barracuda.core.event.helper.DefaultViewHandler;
36 import org.apache.log4j.Logger;
37
38
39 /**
40  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
41  * @version 1.0
42  */

43 public abstract class AbstractPage extends DefaultViewHandler implements Page {
44
45     private static final Logger logger = Logger.getLogger(AbstractPage.class.getName());
46     private List JavaDoc dEvents;
47
48     /**
49      * Describe <code>setDefaultEvents</code> method here.
50      *
51      * @param events a <code>List</code> value
52      */

53     public void setDefaultEvents(List JavaDoc events) {
54         dEvents = events;
55     }
56
57     /**
58      * Describe <code>createDefaultModels</code> method here.
59      *
60      * @param ctx a <code>ControlEventContext</code> value
61      */

62     public void createDefaultModels(ControlEventContext ctx) {
63         filterEvents(ctx, dEvents);
64     }
65
66     /**
67      * Return a Document instance. This is called once before the rendering takes place and
68      * should return a new instance.
69      *
70      * @param iLocale the <code>Locale</code> of the client for locale specific documents
71      * @return a <code>Document</code> value
72      */

73     public abstract Document JavaDoc getDocument(Locale JavaDoc iLocale);
74
75     // handleviewevent is functionality that we don't necessarily want to throw
76
// away.. but the whole render/handleviewevent thing is confusing. so we hide it here.
77
// overriding the render method gives the old behavior.
78
/**
79      * Describe <code>handleViewEvent</code> method here.
80      *
81      * @param root a <code>BComponent</code> value
82      * @return a <code>Document</code> value
83      * @exception EventException if an error occurs
84      * @exception ServletException if an error occurs
85      * @exception IOException if an error occurs
86      */

87     public Document JavaDoc handleViewEvent(BComponent root)
88             throws EventException, ServletException JavaDoc, IOException JavaDoc {
89         ViewContext ctx = getViewContext();
90         Locale JavaDoc iLocale = ctx.getViewCapabilities().getClientLocale();
91         if (logger.isDebugEnabled()) logger.debug("Locale sent by the client is: " + iLocale);
92         Document JavaDoc doc = getDocument(iLocale);
93         if (logger.isInfoEnabled()) logger.info("Created document " + doc.getClass().getName());
94         render(root, ctx, doc);
95         return doc;
96     }
97
98     /**
99      * Describe <code>render</code> method here.
100      *
101      * @param vec a <code>ViewEventContext</code> value
102      * @exception EventException if an error occurs
103      * @exception ServletException if an error occurs
104      * @exception IOException if an error occurs
105      */

106     public void render(ViewEventContext vec)
107             throws EventException, ServletException JavaDoc, IOException JavaDoc {
108         handleViewEvent(vec);
109     }
110
111     /**
112      * Overload this method to perform the actual process of
113      * rendering.
114      *
115      * @param root a <code>BComponent</code> value you have to add your BComponents to.
116      * @param vec a <code>ViewContext</code> value
117      * @param doc a <code>Document</code> value which is obtained by calling getDocument(Locale)
118      * @exception EventException if an error occurs
119      * @exception ServletException if an error occurs
120      * @exception IOException if an error occurs
121      */

122     public abstract void render(BComponent root, ViewContext vec, Document JavaDoc doc)
123             throws EventException, ServletException JavaDoc, IOException JavaDoc;
124
125     /**
126      * Describe <code>filterEvents</code> method here.
127      *
128      * @param ctx a <code>ControlEventContext</code> value
129      * @param defaultEvents a <code>List</code> value
130      */

131     public static void filterEvents(ControlEventContext ctx, List JavaDoc defaultEvents) {
132         if (defaultEvents != null) {
133             BaseEvent event = ctx.getEvent();
134
135             for (Iterator JavaDoc it = defaultEvents.iterator(); it.hasNext();) {
136                 if (((BaseEvent)it.next()).equals(event)) {
137                     if (logger.isDebugEnabled()) logger.debug("Removing event " + event.getClass().getName() + " from the queue");
138                     it.remove();
139                 }
140             }
141
142             DispatchQueue queue = ctx.getQueue();
143
144             for (Iterator JavaDoc it = defaultEvents.iterator(); it.hasNext();) {
145                 //add remaining events (less the current event) back to the queue
146
queue.addEvent((BaseEvent)it.next());
147             }
148         }
149     }
150
151 }
152
Popular Tags