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 25 public class CompEx1 extends ComponentGateway { 26 27 private static String SAMPLE_SCREEN = CompEx1.class+".SampleScreen"; 28 29 33 public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException { 34 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 screen.getText().setText("[Dynamic text]"); 46 DefaultListModel listModel = screen.getListModel(); 47 if (listModel.size()>=15) listModel.clear(); 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 screen.getPage(); 59 } 60 61 67 public DOMWriter getDOMWriter() { 68 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 page = (CompEx1HTML) DefaultDOMLoader.getGlobalInstance().getDOM(CompEx1HTML.class); 84 85 86 wcRoot = new BComponent(); 89 wcRoot.setName("Root"); 90 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 129 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 174 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(); 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 213 class CustomTableModel1 extends AbstractTableModel { 214 public Object 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 225 class CustomHeaderModel1 extends AbstractTableModel { 226 public Object 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 e) { 235 return "Dflt Sample Header"; 236 } 237 } 238 public int getColumnCount() {return 1;} 239 public int getRowCount() {return 3;} 240 } 241 242 246 class CustomFooterModel1 extends AbstractTableModel { 247 public Object 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 e) { 256 return "Dflt Sample Footer"; 257 } 258 } 259 public int getColumnCount() {return 1;} 260 public int getRowCount() {return 2;} 261 } 262 263 269 class CustomTableModel2 extends AbstractTableModel { 270 271 int rowCnt = 5; 272 int hdrCnt = 2; 273 int ftrCnt = 1; 274 275 public Object getItemAt(int row, int col) { 276 ViewContext vc = getViewContext(); 277 if (row==0) { 279 if (col>0) return null; try { 281 ElementFactory ef = vc.getElementFactory(); 282 Node node = ef.getElement("Table2HdrText").cloneNode(true); 283 BText bt = new BText("Table B: (everything is <tr> tags)"); 284 bt.setView(new DefaultView(node)); 285 return bt; 286 } catch (Exception e) { 287 return "Dflt Hdr 0"; 288 } 289 290 } 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 e) { 299 return "Hdr Col "+col; 300 } 301 302 } else if (row==(getRowCount()-1)) { 304 if (col>0) return null; 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 e) { 312 return "Dflt Ftr 0"; 313 } 314 315 } 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 332 class CustomListModel2 extends DefaultListModel { 333 public Object getItemAt(int index) { 334 ViewContext vc = getViewContext(); 335 Object [] 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 [] {title, def}; 344 } 345 346 public int getSize() {return 3;} 347 } 348 349 350 354 class CustomListModel3 extends DefaultListModel { 355 public Object getItemAt(int index) { 356 ViewContext vc = getViewContext(); 357 ElementFactory ef = vc.getElementFactory(); 358 359 if (index==0) { 361 Element el = (Element) ef.getDocument().createElement("OPTION"); 362 DOMUtil.setTextInNode(el, "[Dynamic none]", false); 363 return el; 364 365 } else { 367 Element el = (Element) ef.getDocument().createElement("OPTGROUP"); 368 String 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 descr = null; 382 int max = 1; 383 384 public CustomListModel3a(String idescr, int imax) { 385 descr = idescr; 386 max = imax; 387 } 388 389 public Object 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; } 400 401 public int getSize() {return 1;} 402 } 403 404 405 410 class CustomListModel4 extends DefaultListModel { 411 412 public Object 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 [] {el, textNode}; 421 } 422 423 public int getSize() {return 3;} 424 } 425 426 431 class CustomListModel5 extends DefaultListModel { 432 433 public Object getItemAt(int index) { 434 ViewContext vc = getViewContext(); 435 ElementFactory ef = vc.getElementFactory(); 436 Node node = null; 437 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 toString() { 454 return "[Error rendering Snippet list]"; } 456 } 457 } 458 | Popular Tags |