KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > renderer > html > HTMLSelectRenderer


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: HTMLSelectRenderer.java,v 1.14 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp.renderer.html;
21
22 import java.util.*;
23
24 import org.apache.log4j.*;
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28 import org.enhydra.barracuda.core.comp.*;
29 import org.enhydra.barracuda.core.comp.renderer.*;
30 import org.enhydra.barracuda.core.util.dom.*;
31 import org.enhydra.barracuda.plankton.*;
32
33 /**
34  * This class handles the default rendering of a list in an HTML view.
35  */

36 public class HTMLSelectRenderer extends HTMLListRenderer {
37
38     protected static final Logger logger = Logger.getLogger(HTMLSelectRenderer.class.getName());
39     
40     protected BSelect bscomp = null;
41     protected ListModel lm = null;
42     protected ListSelectionModel lsm = null;
43     
44     /**
45      * The purpose of this method is to create a default Node to be used when
46      * the component is not bound to any specific view.
47      *
48      * @param doc the master Document which can be used to create elements
49      * from scratch
50      * @param comp the component that we're dealing with for the current request
51      * @param vc the view context for the current request
52      * @return a default node (created from scratch)
53      * @throws UnsupportedFormatException if the renderer has no default node
54      */

55 //csc_110501.1 public Node createDefaultNode(Document doc, ViewContext vc) throws UnsupportedFormatException;
56
public Node createDefaultNode(Document doc, BComponent comp, ViewContext vc) throws UnsupportedFormatException { //csc_110501.1
57
//ask the renderer to create the default Node
58
Node templateNode = vc.getTemplateNode();
59         Node defaultNode = null;
60         //csc_101001.2 - previously, it was just always creating a select element
61
//from scratch. We really want to try nd use the template node if at all
62
//possible, however, so that we can inherit any special property settings
63
//it might have (like name)
64
if (templateNode instanceof HTMLSelectElement) {
65             defaultNode = templateNode.cloneNode(true);
66         } else {
67             Element selectEl = (HTMLSelectElement) doc.createElement("SELECT");
68             defaultNode = selectEl;
69         }
70         if (logger.isInfoEnabled()) logger.info("Creating default node:"+defaultNode);
71         return defaultNode;
72     }
73
74     /**
75      *
76      */

77     public void renderComponent(BComponent comp, View view, ViewContext vc) throws RenderException {
78         //make sure the component is a list component
79
if (!(comp instanceof BSelect)) throw new NoSuitableRendererException("This renderer can only render BSelect components; comp is of type:"+comp.getClass().getName());
80         bscomp = (BSelect) comp;
81         lm = bscomp.getModel();
82         lsm = bscomp.getSelectionModel();
83         Node node = view.getNode();
84
85         //show what we're doing
86
if (logger.isDebugEnabled()) showNodeInterfaces(view, logger);
87
88         //first, allow the parent class to do anything it needs to
89
super.renderComponent(comp, view, vc);
90
91         //..HTMLSelectElement - set the "multiples" and "size" attributes,
92
// and then mark items selected/unselected. Finally, set the
93
// "value" attribute for each option element.
94
if (node instanceof HTMLSelectElement) {
95             if (logger.isInfoEnabled()) logger.info("Rendering based on HTMLSelectElement interface...");
96             HTMLSelectElement el = (HTMLSelectElement) node;
97
98             //set the size (if specified)
99
if (bscomp.getViewSize()!=null) {
100                 el.setSize(bscomp.getViewSize().intValue());
101             }
102
103             //set allow multiples (get this from the selection model)
104
el.setMultiple(lsm.getSelectionMode()!=ListSelectionModel.SINGLE_SELECTION);
105             
106             //now iterate through all the children (this list will
107
//already have been populated by the parent class renderer
108
//from the model)
109
HTMLCollection options = el.getOptions();
110             for (int i=0, max=options.getLength(); i<max; i++) {
111                 HTMLOptionElement opt = (HTMLOptionElement) options.item(i);
112                 
113                 //mark them selected/unselected by looking at the selection model
114

115                 //jkr_20030520.1 - setSelected(boolean) isn't actually part of the
116
//html dom level1 api. Xerces1 included an early version of the
117
//html dom level2 draft interfaces which were released with the
118
//same package as the level1 dom. The level2 dom is now in the
119
//namespace org.w3c.dom.html2. The switch in namespaces was made
120
//sometime between the following releases...
121
//http://www.w3.org/TR/2001/WD-DOM-Level-2-HTML-20011210/java-binding.html
122
//and
123
//http://www.w3.org/TR/2002/CR-DOM-Level-2-HTML-20020605/java-binding.html
124
//Xerces2 includes the original level1
125
//html dom. In order to be compatible with that, we can't use the
126
//setSelected(boolean) method (and, apparently, never should have).
127
// opt.setSelected(lsm.isSelectedIndex(i));
128
if (lsm.isSelectedIndex(i)) opt.setAttribute("selected", "selected");
129                 else opt.removeAttribute("selected");
130                 
131                 //set the value: if the item in the model implements ItemMap,
132
//use that value; otherwise, just set it equal to the text
133
// Object item = lm.getItemAt(i, vc);
134
Object JavaDoc item = lm.getItemAt(i);
135                 if (item==null) continue;
136                 if (item instanceof ItemMap) {
137                     opt.setValue(((ItemMap) item).getKey().toString());
138                 } else {
139                     opt.setValue(item.toString());
140                 }
141             }
142             
143         } else {
144             String JavaDoc errmsg = "Node does not implement HTMLSelectElement and cannot be rendered: "+node;
145             logger.warn(errmsg);
146             throw new NoSuitableRendererException(errmsg);
147         }
148     }
149 }
Popular Tags