KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > event > helper > BTemplateViewHandler


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
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: BTemplateViewHandler.java,v 1.17 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.event.helper;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25
26 import org.w3c.dom.*;
27
28 import org.enhydra.barracuda.core.comp.*;
29 import org.enhydra.barracuda.core.event.*;
30 import org.enhydra.barracuda.core.util.dom.*;
31
32 /**
33  * <p>A custom implementation of the default view handler tailored for
34  * template components. All you have to do is instantiate the class
35  * with the name of the template and a reference to the model; it takes
36  * care of everything else.
37  */

38 public abstract class BTemplateViewHandler extends DefaultViewHandler {
39
40     protected BTemplate templateComp = null; //csc_041102.1
41

42     //-------------------- DefaultViewHandler --------------------
43
/**
44      * Return an instance of the template model; you can either return
45      * a single instance of a TemplateModel, -OR- you can return a
46      * List of TemplateModels
47      */

48 //csc_021102.1 public abstract TemplateModel getTemplateModel();
49
public abstract Object JavaDoc getTemplateModels(); //csc_021102.1
50

51     /**
52      * Return an instance of the template class (must implement Document)
53      */

54     public abstract Class JavaDoc getTemplateClass();
55
56     //csc_041102.1 - added
57
/**
58      * Provide a handle to the underlying BTemplate component
59      */

60     public BTemplate getBTemplate() {
61         return templateComp;
62     }
63
64     /**
65      * Generate the view
66      */

67 //csc_030603.1 public Document handleViewEvent(BComponent root, ViewContext vc) throws EventException, ServletException, IOException {
68
public Document handleViewEvent(BComponent root) throws EventException, ServletException, IOException { //csc_030603.1
69
ViewContext vc = this.getViewContext(); //csc_030603.1
70

71         //load the localized DOM template
72
//sam_061602.1_start
73
//Document page = DefaultDOMLoader.getGlobalInstance().getDOM(getTemplateClass());
74
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(getTemplateClass(), vc.getViewCapabilities().getClientLocale());
75         //sam_061602.1_end
76

77         //create a template component and bind it to the views
78
//csc_021102.1_start
79
// BTemplate templateComp = new BTemplate(getTemplateModel());
80
//csc_041102.1 BTemplate templateComp = new BTemplate();
81
templateComp = new BTemplate(); //csc_041102.1
82
Object JavaDoc o = getTemplateModels();
83         if (o instanceof TemplateModel) templateComp.addModel((TemplateModel) o);
84         else if (o instanceof List) templateComp.addModels((List) o);
85 //jkf_022102.1_start
86
else if (o==null) { }
87 //jkf_022102.1_end
88
else throw new RuntimeException JavaDoc("Fatal err: Model must return either TemplateModel or List");
89 //csc_021102.1_end
90
templateComp.setView(new DefaultTemplateView(page.getDocumentElement()));
91         root.addChild(templateComp);
92
93         //return the page
94
return page;
95     }
96 }
97
Popular Tags