KickJava   Java API By Example, From Geeks To Geeks.

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


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

25 public class CompEx1 extends ComponentGateway {
26
27      private static String JavaDoc SAMPLE_SCREEN = CompEx1.class+".SampleScreen";
28
29     //-------------------- ComponentGateway ----------------------
30
/**
31      * Handle the default HttpRequest.
32      */

33     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
34         //see if the component tree is in the session. If not, create it
35
//and place it there
36
SampleScreen screen = null;
37         HttpSession session = SessionServices.getSession(req, false);
38         if (session!=null) screen = (SampleScreen) session.getAttribute(SAMPLE_SCREEN);
39         if (screen==null) {
40             screen = new SampleScreen();
41             if (session!=null) session.setAttribute(SAMPLE_SCREEN, screen);
42         }
43
44         //now, update the data/models
45
screen.getText().setText("[Dynamic text]");
46         DefaultListModel listModel = screen.getListModel();
47         if (listModel.size()>=15) listModel.clear(); //this will cause the lists that get repopulated to grow everytime you refresh the page, so you can see the thing changing. Once it gets big enough, truncate.
48
int cntr = listModel.size();
49         listModel.add("[Dynamic list item "+(++cntr)+"]");
50         listModel.add("[Dynamic list item "+(++cntr)+"]");
51         listModel.add("[Dynamic list item "+(++cntr)+"]");
52         listModel.add("[Dynamic list item "+(++cntr)+"]");
53         listModel.add("[Dynamic list item "+(++cntr)+"]");
54         root.addChild(screen.getRoot());
55             
56
57         //return the page
58
return screen.getPage();
59     }
60
61     /**
62      * <p>Get a DOMWriter. By default, we use a DefaultDOMWriter. If
63      * you'd like to use something else, override this method.
64      *
65      * @return a DOMWriter to be used to render the DOM
66      */

67     public DOMWriter getDOMWriter() {
68         //return DefaultDOM writer
69
return new DefaultDOMWriter(false);
70     }
71
72
73
74     class SampleScreen {
75
76         CompEx1HTML page = null;
77         BComponent wcRoot = null;
78         BText wcText = null;
79         DefaultListModel listModel1 = null;
80
81         public SampleScreen() throws IOException {
82             //load the localized DOM template
83
page = (CompEx1HTML) DefaultDOMLoader.getGlobalInstance().getDOM(CompEx1HTML.class);
84
85
86             //now build our component hierarchy (in many cases, fairly flat)
87
// and bind the components to the views
88
wcRoot = new BComponent();
89             wcRoot.setName("Root");
90             //...Text
91
wcText = new BText();
92             wcText.setName("Text1");
93             wcText.addView(new DefaultView("View0",page.getElementText0()));
94             wcText.addView(new DefaultView("View1",page.getElementText1()));
95             wcText.addView(new DefaultView("View2",page.getElementText2()));
96             wcText.addView(new DefaultView("View3",page.getElementText3()));
97             wcText.addView(new DefaultView("View4",page.getElementText4()));
98             wcText.addView(new DefaultView("View5",page.getElementText5()));
99             wcText.addView(new DefaultView("View6",page.getElementText6()));
100             wcText.addView(new DefaultView("View7",page.getElementText7()));
101             wcText.addView(new DefaultView("View8",page.getElementText8()));
102             wcText.addView(new DefaultView("View9",page.getElementText9()));
103             wcText.addView(new DefaultView("View10",page.getElementText10()));
104             wcText.addView(new DefaultView("View11",page.getElementText11()));
105             wcText.addView(new DefaultView("View12",page.getElementText12()));
106             wcText.addView(new DefaultView("View13",page.getElementText13()));
107             wcText.addView(new DefaultView("View14",page.getElementText14()));
108             wcText.addView(new DefaultView("View15",page.getElementText15()));
109             wcText.addView(new DefaultView("View16",page.getElementText16()));
110             wcText.addView(new DefaultView("View17",page.getElementText17()));
111             wcText.addView(new DefaultView("View18",page.getElementText18()));
112             wcText.addView(new DefaultView("View19",page.getElementText19()));
113             wcText.addView(new DefaultView("View20",page.getElementText20()));
114             wcText.addView(new DefaultView("View21",page.getElementText21()));
115             wcText.addView(new DefaultView("View22",page.getElementText22()));
116             wcText.addView(new DefaultView("View23",page.getElementText23()));
117             wcText.addView(new DefaultView("View24",page.getElementText24()));
118             wcText.addView(new DefaultView("View25",page.getElementText25()));
119             wcText.addView(new DefaultView("View26",page.getElementText26()));
120             wcText.addView(new DefaultView("View27",page.getElementText27()));
121             wcText.addView(new DefaultView("View28",page.getElementText28()));
122             wcText.addView(new DefaultView("View29",page.getElementText29()));
123             wcText.addView(new DefaultView("View30",page.getElementText30()));
124             wcText.addView(new DefaultView("View31",page.getElementText31()));
125             wcRoot.addChild(wcText);
126 //wcText.setVisible(false);
127
//wcText.setEnabled(false);
128

129             //...Lists
130
BText bt1 = new BText("[Dynamic list item XX]");
131             bt1.setView(new DefaultView(page.getElementDynamicListText()));
132             wcRoot.addChild(bt1);
133
134             listModel1 = new DefaultListModel();
135             BList wcList1 = new BList(listModel1);
136             wcList1.setName("List1");
137             wcList1.addView(new DefaultView(page.getElementList1()));
138             wcList1.addView(new DefaultView(page.getElementList2()));
139             wcList1.addView(new DefaultView(page.getElementList3()));
140             wcList1.addView(new DefaultView(page.getElementList4()));
141             wcList1.addView(new DefaultView(page.getElementList5()));
142             wcRoot.addChild(wcList1);
143             
144             ListModel listModel2 = new CustomListModel2();
145             BList wcList2 = new BList(listModel2);
146             wcList2.setName("List2");
147             wcList2.addView(new DefaultView(page.getElementDefList()));
148             wcRoot.addChild(wcList2);
149
150             ListModel listModel3 = new CustomListModel3();
151             BList wcList3 = new BList(listModel3);
152             wcList3.setName("List3");
153             wcList3.addView(new DefaultView(page.getElementOptGroupList()));
154             wcRoot.addChild(wcList3);
155             
156             ListModel listModel4 = new CustomListModel4();
157             BList wcList4 = new BList(listModel4);
158             wcList4.setName("List4");
159             wcList4.addView(new DefaultView(page.getElementList6()));
160             wcRoot.addChild(wcList4);
161             
162             ListModel listModel5 = new CustomListModel5();
163             BList wcList5 = new BList(listModel5);
164             wcList5.setName("List5");
165             wcList5.addView(new DefaultView(page.getElementList7()));
166             wcRoot.addChild(wcList5);
167
168 //wcList1.setEnabled(false);
169
//wcList2.setEnabled(false);
170
//wcList3.setEnabled(false);
171
//wcList4.setEnabled(false);
172
//wcList5.setEnabled(false);
173

174             //..Tables
175
TableModel tableModel1 = new CustomTableModel1();
176             BTable wcTable1 = new BTable(tableModel1);
177             wcTable1.setName("Table1");
178             wcTable1.addView(new DefaultTableView(page.getElementTable1()));
179             wcRoot.addChild(wcTable1);
180
181             TableModel tableModel2 = new CustomTableModel2();
182             BTable wcTable2 = new BTable(tableModel2);
183             wcTable2.setName("Table2");
184             wcTable2.addView(new DefaultTableView(page.getElementTable2()));
185             wcRoot.addChild(wcTable2);
186
187             TableModel tableModel3 = new CustomTableModel1();
188             TableModel tableHdrModel3 = new CustomHeaderModel1();
189             TableModel tableFtrModel3 = new CustomFooterModel1();
190 // BTable wcTable3 = new BTable(tableHdrModel3, tableModel3, tableFtrModel3);
191
BTable wcTable3 = new BTable();
192             wcTable3.setHeaderModel(tableHdrModel3);
193             wcTable3.setModel(tableModel3);
194             wcTable3.setFooterModel(tableFtrModel3);
195             wcTable3.setName("Table3");
196             wcTable3.addView(new DefaultTableView(page.getElementTable3()));
197             wcRoot.addChild(wcTable3);
198
199         }
200         
201         public CompEx1HTML getPage() {return page;}
202         public BComponent getRoot() {return wcRoot;}
203         public BText getText() {return wcText;}
204         public DefaultListModel getListModel() {return listModel1;}
205
206     }
207     
208     /**
209      * demonstrate a simple table model that just passes
210      * back strings. The HTML table uses <thead>,<tbody>, and <tfoot>
211      * tags, so all we have to pass back is the data. Cool.
212      */

213     class CustomTableModel1 extends AbstractTableModel {
214         public Object JavaDoc getItemAt(int row, int col) {
215             return ""+(row*col);
216         }
217         public int getColumnCount() {return 4;}
218         public int getRowCount() {return 5;}
219     }
220
221     /**
222      * demonstrate how we can implement the header portion as a table
223      * model on its own
224      */

225     class CustomHeaderModel1 extends AbstractTableModel {
226         public Object JavaDoc getItemAt(int row, int col) {
227             ViewContext vc = getViewContext();
228             try {
229                 ElementFactory ef = vc.getElementFactory();
230                 Node node = ef.getElement("Table3Header").cloneNode(true);
231                 BText bt = new BText("Sample Header "+row);
232                 bt.setView(new DefaultView(node));
233                 return bt;
234             } catch (Exception JavaDoc e) {
235                 return "Dflt Sample Header";
236             }
237         }
238         public int getColumnCount() {return 1;}
239         public int getRowCount() {return 3;}
240     }
241     
242     /**
243      * demonstrate how we can implement the footer portion as a table
244      * model on its own
245      */

246     class CustomFooterModel1 extends AbstractTableModel {
247         public Object JavaDoc getItemAt(int row, int col) {
248             ViewContext vc = getViewContext();
249             try {
250                 ElementFactory ef = vc.getElementFactory();
251                 Node node = ef.getElement("Table3Footer").cloneNode(true);
252                 BText bt = new BText("Sample Footer "+row);
253                 bt.setView(new DefaultView(node));
254                 return bt;
255             } catch (Exception JavaDoc e) {
256                 return "Dflt Sample Footer";
257             }
258         }
259         public int getColumnCount() {return 1;}
260         public int getRowCount() {return 2;}
261     }
262     
263     /**
264      * demonstrate a simple table model that just passes
265      * back strings. The HTML table simpler in that it doesn't
266      * have a <tbody> structure but this in turn means we have
267      * to provide those in addition to the data...
268      */

269     class CustomTableModel2 extends AbstractTableModel {
270     
271         int rowCnt = 5;
272         int hdrCnt = 2;
273         int ftrCnt = 1;
274     
275         public Object JavaDoc getItemAt(int row, int col) {
276             ViewContext vc = getViewContext();
277             //header 0
278
if (row==0) {
279                 if (col>0) return null; //the node returned will span all the columns, so only return data for the first col
280
try {
281                     ElementFactory ef = vc.getElementFactory();
282                     Node node = ef.getElement("Table2HdrText").cloneNode(true);
283                     BText bt = new BText("Table B: (everything is &lt;tr&gt; tags)");
284                     bt.setView(new DefaultView(node));
285                     return bt;
286                 } catch (Exception JavaDoc e) {
287                     return "Dflt Hdr 0";
288                 }
289
290             //header 1
291
} else if (row==1) {
292                 try {
293                     ElementFactory ef = vc.getElementFactory();
294                     Node node = ef.getElement("Table2HdrCell").cloneNode(true);
295                     BText bt = new BText("Column "+col);
296                     bt.setView(new DefaultView(node));
297                     return bt;
298                 } catch (Exception JavaDoc e) {
299                     return "Hdr Col "+col;
300                 }
301                 
302             //footer 0
303
} else if (row==(getRowCount()-1)) {
304                  if (col>0) return null; //the node returned will span all the columns, so only return data for the first col
305
try {
306                     ElementFactory ef = vc.getElementFactory();
307                     Node node = ef.getElement("Table2FtrList").cloneNode(true);
308                     BText bt = new BText("Look ma! Ye can see me footer!");
309                     bt.setView(new DefaultView(node));
310                     return bt;
311                 } catch (Exception JavaDoc e) {
312                     return "Dflt Ftr 0";
313                 }
314
315             //data
316
} else {
317                 return ""+((row-hdrCnt)*col);
318             }
319         }
320         
321         public int getColumnCount() {return 4;}
322         public int getRowCount() {return rowCnt+hdrCnt+ftrCnt;}
323     }
324
325     /**
326      * this is really kind of hokey since the data is completely
327      * canned, but it demonstrates
328      * a) how you can write an inner class that implements your model
329      * b) how you can pass back an array of objects and they will
330      * all get added to the list
331      */

332     class CustomListModel2 extends DefaultListModel {
333         public Object JavaDoc getItemAt(int index) {
334             ViewContext vc = getViewContext();
335             Object JavaDoc[] items = null;
336             ElementFactory ef = vc.getElementFactory();
337             Element title = (Element) (ef.getElement("DT").cloneNode(true));
338             title.removeAttribute("id");
339             DOMUtil.setTextInNode(title, "[Dynamic title "+(index+1)+"]", false);
340             Element def = (Element) (ef.getElement("DD").cloneNode(true));
341             def.removeAttribute("id");
342             DOMUtil.setTextInNode(def, "[Dynamic def "+(index+1)+"]", false);
343             return new Object JavaDoc[] {title, def};
344         }
345
346         public int getSize() {return 3;}
347     }
348
349
350     /**
351      * this is also kind of hokey, but it demonstrates how you can pass back
352      * nested BComponents...(we're operating against a SELECT node here)
353      */

354     class CustomListModel3 extends DefaultListModel {
355         public Object JavaDoc getItemAt(int index) {
356             ViewContext vc = getViewContext();
357             ElementFactory ef = vc.getElementFactory();
358         
359             //for the first index, pass back a NONE option
360
if (index==0) {
361                 Element el = (Element) ef.getDocument().createElement("OPTION");
362                 DOMUtil.setTextInNode(el, "[Dynamic none]", false);
363                 return el;
364                 
365             //otherwise pass back a group of options (note: here we return a BComponent)
366
} else {
367                 Element el = (Element) ef.getDocument().createElement("OPTGROUP");
368                 String JavaDoc descr = "Subport A."+index;
369                 if (index==2) descr = "Subport B."+index;
370                 else if (index==3) descr = "Subport C."+index;
371                 BList elList = new BList(new CustomListModel3a(descr, index));
372                 elList.setView(new DefaultView(el));
373                 return elList;
374             }
375         }
376
377         public int getSize() {return 4;}
378     }
379
380     class CustomListModel3a extends DefaultListModel {
381         String JavaDoc descr = null;
382         int max = 1;
383
384         public CustomListModel3a(String JavaDoc idescr, int imax) {
385             descr = idescr;
386             max = imax;
387         }
388
389         public Object JavaDoc getItemAt(int index) {
390             ViewContext vc = getViewContext();
391             ElementFactory ef = vc.getElementFactory();
392             List list = new ArrayList();
393             for (int i=0; i<max; i++) {
394                 Element el = (Element) ef.getDocument().createElement("OPTION");
395                 DOMUtil.setTextInNode(el, "[Dynamic "+descr+i+"]", false);
396                 list.add(el);
397             }
398             return list; //note that here we return a list of nodes
399
}
400         
401         public int getSize() {return 1;}
402     }
403
404
405     /**
406      * This list model returns a list of complex components (just
407      * to demonstrate how its done). In this case we return an option control
408      * followed by some text.
409      */

410     class CustomListModel4 extends DefaultListModel {
411     
412         public Object JavaDoc getItemAt(int index) {
413             ViewContext vc = getViewContext();
414             ElementFactory ef = vc.getElementFactory();
415             Element el = (Element) ef.getDocument().createElement("INPUT");
416             el.setAttribute("type","radio");
417             el.setAttribute("value","value"+index);
418             el.setAttribute("name","LM4");
419             Node textNode = (Node) ef.getDocument().createTextNode("Dyn"+index+" ");
420             return new Object JavaDoc[] {el, textNode};
421         }
422
423         public int getSize() {return 3;}
424     }
425
426     /**
427      * This list model demonstrates how we can return complex
428      * snippets of HTML that actually come from other XMLC pages
429      * (note we have to do importNode() to make this work)...
430      */

431     class CustomListModel5 extends DefaultListModel {
432     
433         public Object JavaDoc getItemAt(int index) {
434             ViewContext vc = getViewContext();
435             ElementFactory ef = vc.getElementFactory();
436             Node node = null;
437 // if (index==0) node = ((SnippetEx1HTML) xmlcFactory.create(SnippetEx1HTML.class)).getElementSnippet();
438
// else if (index==1) node = ((SnippetEx2HTML) xmlcFactory.create(SnippetEx2HTML.class)).getElementSnippet();
439
// else node = ((SnippetEx3HTML) xmlcFactory.create(SnippetEx3HTML.class)).getElementSnippet();
440
try {
441                 if (index==0) node = ((SnippetEx1HTML) DefaultDOMLoader.getGlobalInstance().getDOM(SnippetEx1HTML.class)).getElementSnippet();
442                 else if (index==1) node = ((SnippetEx2HTML) DefaultDOMLoader.getGlobalInstance().getDOM(SnippetEx2HTML.class)).getElementSnippet();
443                 else node = ((SnippetEx3HTML) DefaultDOMLoader.getGlobalInstance().getDOM(SnippetEx3HTML.class)).getElementSnippet();
444                 node = ef.getDocument().importNode(node, true);
445                 return node;
446             } catch (IOException e) {
447                 return "[snippet render failed "+e+"]";
448             }
449         }
450
451         public int getSize() {return 3;}
452         
453         public String JavaDoc toString() {
454             return "[Error rendering Snippet list]"; //if we see this message in the HTML it indicates there was a problem
455
}
456     }
457 }
458
Popular Tags