KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > xmlc > CompEx4


1 package org.enhydra.barracuda.examples.xmlc;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7
8 import org.w3c.dom.*;
9 import org.w3c.dom.html.*;
10 import org.enhydra.xml.xmlc.*;
11
12 import org.enhydra.barracuda.core.comp.*;
13 import org.enhydra.barracuda.core.comp.helper.*;
14 import org.enhydra.barracuda.core.util.dom.*;
15 import org.enhydra.barracuda.core.event.*;
16 import org.enhydra.barracuda.core.view.*;
17 import org.enhydra.barracuda.examples.xmlc.data.*;
18
19 /**
20  * Here's an example of how the component stuff can be implemented
21  * in a standalone servlet.
22  */

23 public class CompEx4 extends ComponentGateway {
24
25     private ListenerFactory getLinkFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new GetLinkHandler();} public String JavaDoc getListenerID() {return getID(GetLinkHandler.class);}};
26
27     //-------------------- ComponentGateway ----------------------
28
/**
29      * Handle the default HttpRequest.
30      */

31     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
32         //load the localized DOM template
33
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx4HTML.class);
34
35         //create the component and bind it to the views
36
BTemplate templateComp = new BTemplate(new SimpleModel());
37         templateComp.setView(new DefaultTemplateView(page.getElementById("TemplateID")));
38         root.addChild(templateComp);
39
40         //return the page
41
return page;
42     }
43
44     /**
45      * <p>Get a DOMWriter. By default, we use a DefaultDOMWriter. If
46      * you'd like to use something else, override this method.
47      *
48      * @return a DOMWriter to be used to render the DOM
49      */

50     public DOMWriter getDOMWriter() {
51         //return DefaultDOM writer
52
return new DefaultDOMWriter(true);
53     }
54
55     //--------------- UserReportModel ------------------------
56
/**
57      * SimpleModel
58      */

59     class SimpleModel extends AbstractTemplateModel {
60         //register the model by name
61
public String JavaDoc getName() {
62             return "SimpleModel";
63         }
64         
65         //provide items by key
66
public Object JavaDoc getItem(TemplateDirective td) {
67             ViewContext vc = getViewContext();
68             String JavaDoc key = td.getKeyName();
69             if (key.equals("Link1")) {
70                 BAction baction = new BAction();
71                 //baction.addView(new DefaultView(vc.getTemplateNode().cloneNode(true)));
72
baction.addEventListener(getLinkFactory);
73                 baction.setParam("foo1","blah1");
74                 baction.setParam("foo2","blah2");
75                 return baction;
76             } else if (key.equals("Link2")) {
77                 BAction baction = new BAction("http://blah.com/testLink2.html");
78                 return baction;
79             } else if (key.equals("Link3")) {
80                 BLink blink = new BLink("link example (event)");
81                 blink.addEventListener(getLinkFactory);
82                 blink.setParam("foo1","blah1");
83                 blink.setParam("foo2","blah2");
84                 return blink;
85             } else if (key.equals("Link4")) {
86                 BLink blink = new BLink("link example (url)", "http://blah.com/testBLink.html");
87                 return blink;
88             } else if (key.equals("Link5")) {
89                 BAction baction = new BAction("http://blah.com/testLink2.html");
90                 baction.setEnabled(false);
91                 return baction;
92             } else if (key.equals("Form1")) {
93                 BAction baction = new BAction("http://blah.com/formTest1.html");
94                 return baction;
95             } else if (key.equals("Button1")) {
96                 BAction baction = new BAction();
97                 baction.addEventListener(getLinkFactory);
98                 return baction;
99             } else if (key.equals("Button2")) {
100                 BLink blink = new BLink("link example on a button", "http://blah.com/testBLink.html");
101                 return blink;
102             } else return super.getItem(key);
103         }
104     }
105
106     /**
107      * GetLinkHandler - handle the request to render the test
108      * screen
109      */

110     class GetLinkHandler extends DefaultBaseEventListener {
111         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
112
113             //something would actually happen here
114
}
115     }
116
117
118 }
119
Popular Tags