KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > tutorials > comp > HelloWorld2a


1 package org.enhydra.barracuda.tutorials.comp;
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.enhydra.xml.xmlc.*;
10
11 import org.enhydra.barracuda.core.comp.*;
12 import org.enhydra.barracuda.core.comp.helper.*;
13 import org.enhydra.barracuda.core.util.dom.*;
14 import org.enhydra.barracuda.tutorials.xmlc.*;
15
16 /**
17  * Here's an example of HelloWorld using the BList
18  * component
19  */

20 public class HelloWorld2a extends ComponentGateway {
21
22     //xmlc factory (ok as static because its threadsafe)
23
// private static XMLCFactory xmlcFactory = null;
24

25     //-------------------- ComponentGateway ----------------------
26
/**
27      * Handle the default HttpRequest.
28      */

29     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
30         //load the DOM object
31
// if (xmlcFactory==null) xmlcFactory = new XMLCStdFactory(this.getClass().getClassLoader(), new StreamXMLCLogger());
32
// HelloWorld2aHTML page = (HelloWorld2aHTML) xmlcFactory.create(HelloWorld2aHTML.class);
33

34         //load the localized DOM template
35
HelloWorld2aHTML page = (HelloWorld2aHTML)DefaultDOMLoader.getGlobalInstance().getDOM(HelloWorld2aHTML.class, vc.getViewCapabilities().getClientLocale());
36         
37         //start by setting basic data (here we just leverage existing
38
//XMLC technology)
39
page.setTextTitle1("Hello World 2a example");
40         page.setTextTitle2("Hello World 2a example");
41         page.setTextDescr("This example illustrates how we can create populate a page using other Barracuda components--like BList--without putting anything in the HTML template except for standard XMLC style id values.");
42
43         //ok, now we need to setup the list component and add
44
//it to the root
45
String JavaDoc[] items = new String JavaDoc[] {"Cabbage", "Carrots", "Pickles", "Sugar", "Wheaties", "Flour", "Potatoes"};
46         String JavaDoc[] qtys = new String JavaDoc[] {"3 heads", "1 bag", "1 jar", "5 lbs", "3 bxs", "50 lbs", "25 lbs"};
47         ListModel listModel = new GroceriesModel(items, qtys);
48         View listView = new DefaultView(page.getElementShoppingList());
49         BList wcList = new BList(listModel);
50         wcList.setView(listView);
51         root.addChild(wcList);
52         
53         //set up the footer component (notice we use a list component)
54
DefaultListModel flistModel = new DefaultListModel();
55         View flistView = new DefaultView(page.getElementFooter());
56         BList fwcList = new BList(flistModel);
57         fwcList.setView(flistView);
58         root.addChild(fwcList);
59
60         //now to add data to the footer list we're actually going to
61
//add in all the child nodes
62
Node fn = HelloWorld2Footer.getNode(page.getDocument());
63         NodeList nl = fn.getChildNodes();
64         for (int i=0,max=nl.getLength(); i<max; i++) {
65             flistModel.add(nl.item(i));
66         }
67         
68         //return the page
69
return page;
70     }
71
72     //--------------- GroceriesModel -------------------------
73
/**
74      * Groceries Model
75      */

76     class GroceriesModel extends AbstractListModel {
77
78         String JavaDoc[] items = null;
79         String JavaDoc[] qtys = null;
80
81         //constructor
82
public GroceriesModel(String JavaDoc[] iitems, String JavaDoc[] iqtys) {
83             items = iitems;
84             qtys = iqtys;
85         }
86
87         //provide items by pos
88
public Object JavaDoc getItemAt(int index) {
89             return qtys[index]+" "+items[index];
90         }
91
92         //get the size of the list
93
public int getSize() {
94             return items.length;
95         }
96     }
97
98 }
99
Popular Tags