KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.dom.*;
17 import org.enhydra.barracuda.core.view.*;
18 import org.enhydra.barracuda.examples.xmlc.data.*;
19
20 /**
21  * Here's an example of how the component stuff can be implemented
22  * in a standalone servlet.
23  */

24 public class CompEx5 extends ComponentGateway {
25
26     //-------------------- ComponentGateway ----------------------
27
/**
28      * Handle the default HttpRequest.
29      */

30     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
31         //load the localized DOM template
32
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx5HTML.class);
33
34
35         //create a template component and bind it to the views
36
BTemplate templateComp = new BTemplate(new SimpleTemplateModel());
37         templateComp.setView(new DefaultTemplateView(page.getElementById("SimpleTemplateID")));
38         root.addChild(templateComp);
39
40
41         //create a list component and bind it to the view
42
//...list1
43
BList blist1 = new BList();
44         blist1.setName("BList 1");
45         DefaultListModel dlm1 = new DefaultListModel();
46         blist1.setModel(dlm1);
47         dlm1.add(new BLink("list link ok 1", "http://blah.com/testBLink.html", vc));
48         dlm1.add(new BLink("list link ok 2", "http://blah.com/testBLink.html", vc));
49         dlm1.add(new BLink("list link ok 3", "http://blah.com/testBLink.html", vc));
50 // blist1.addView(new DefaultView(page.getElementById("SimpleListID1")));
51
// blist1.addView(new DefaultView(page.getElementById("SimpleListID2")));
52
for (int i=1; i<999; i++) {
53             Node n = page.getElementById("SimpleListID"+i);
54             if (n==null) break;
55             blist1.addView(new DefaultView(n));
56         }
57         root.addChild(blist1);
58
59         //...list2
60
BList blist2 = new BList();
61         blist2.setName("BList 2");
62         ListModel dlm2 = new DefinitionListModel();
63         blist2.setModel(dlm2);
64         blist2.addView(new DefaultView(page.getElementById("DefinitionListID1")));
65         root.addChild(blist2);
66
67
68         //create a table component and bind it to the view
69
//...table1
70
BTable btable1 = new BTable();
71         btable1.setName("BTable 1");
72         TableModel tm1 = new SimpleTableModel();
73         btable1.setModel(tm1);
74         btable1.addView(new DefaultTableView(page.getElementById("SimpleTableID1")));
75         btable1.addView(new DefaultTableView(page.getElementById("SimpleTableID2")));
76         root.addChild(btable1);
77
78         //return the page
79
return page;
80     }
81
82     /**
83      * <p>Get a DOMWriter. By default, we use a DefaultDOMWriter. If
84      * you'd like to use something else, override this method.
85      *
86      * @return a DOMWriter to be used to render the DOM
87      */

88     public DOMWriter getDOMWriter() {
89         //return DefaultDOM writer
90
return new DefaultDOMWriter(true);
91 // return new DefaultDOMWriter(false);
92
}
93
94     //--------------- UserReportModel ------------------------
95
/**
96      * SimpleTemplateModel
97      */

98     class SimpleTemplateModel extends AbstractTemplateModel {
99         //register the model by name
100
public String JavaDoc getName() {
101             return "Test1";
102         }
103         
104         //provide items by key
105
public Object JavaDoc getItem(String JavaDoc key) {
106             ViewContext vc = getViewContext();
107             if (key.startsWith("Link")) {
108                 BLink blink = new BLink("template link ok "+key.substring(4), "http://blah.com/testBLink1.html", vc);
109                 return blink;
110             } else if (key.equals("TextAndLink")) {
111                 BLink blink = new BLink("template link ok 5", "http://blah.com/testBLink2.html", vc);
112                 BInput binput = new BInput(BInput.TEXT, "InputName", "value goes here");
113                 binput.setDefaultViewContext(vc); //we must set the default ViewContext since we'll be inlining the component down below
114
BToggleButton btoggle = new BToggleButton(BInput.RADIO, "ToggleName", "value goes here", true);
115                 btoggle.setDefaultViewContext(vc); //we must set the default ViewContext since we'll be inlining the component down below
116
DefaultListModel dlm = new DefaultListModel();
117                 dlm.add(new DefaultItemMap("Key 1","Value 1"));
118                 dlm.add(new DefaultItemMap("Key 2","Value 2"));
119                 dlm.add(new DefaultItemMap("Key 3","Value 3"));
120                 BSelect bselect = new BSelect(dlm);
121                 bselect.setDefaultViewContext(vc); //we must set the default ViewContext since we'll be inlining the component down below
122
// return "Inline text, link ("+blink+"), input control "+binput+", toggle button "+btoggle+" and even a select item "+bselect;
123
BText btext = new BText("Inline text, link ("+blink+"), input control "+binput+", toggle button "+btoggle+" and even a select item "+bselect);
124                 btext.setAllowMarkupInText(true);
125                 return btext;
126             } else if (key.equals("BComp1")) {
127                 //this is not really a link example at all...the purpose is to
128
//ensure that we can pass back a simple BComponent (to control
129
//visibility/enabled on a block of markup). Note that the component
130
//doesn't have to be bound to any node...
131
BComponent bcomp = new BComponent();
132                 bcomp.setVisible(false);
133                 return bcomp;
134             } else return super.getItem(key);
135         }
136     }
137
138
139     //--------------- DefinitionList Model -------------------
140
class DefinitionListModel extends DefaultListModel {
141         public Object JavaDoc getItemAt(int index) {
142             ViewContext vc = getViewContext();
143             Object JavaDoc[] items = null;
144             ElementFactory ef = vc.getElementFactory();
145             BLink blDescr = new BLink("definition descr link ok"+(index+1), "http://blah.com/testBLink.html", vc);
146             return new Object JavaDoc[] {"Term "+(index+1), blDescr};
147         }
148
149         public int getSize() {return 3;}
150     }
151     
152     
153     //--------------- Table Model ----------------------------
154
class SimpleTableModel extends AbstractTableModel {
155         public Object JavaDoc getItemAt(int row, int col) {
156             ViewContext vc = getViewContext();
157             if (col==0) {
158                 return "Row "+(row+1);
159             } else if (col==1) {
160                 return new BLink("table link ok"+(row+1), "http://blah.com/testBLink.html", vc);
161             } else {
162                 return null;
163             }
164         }
165
166         public int getRowCount() {return 3;}
167         public int getColumnCount() {return 2;}
168     }
169     
170 }
171
Popular Tags