001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit.dom_html_basic;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Text;
039:
040: import javax.faces.component.UIComponent;
041: import javax.faces.component.UIInput;
042: import javax.faces.context.FacesContext;
043: import javax.faces.context.ResponseWriter;
044: import javax.faces.model.SelectItem;
045: import javax.faces.model.SelectItemGroup;
046: import java.io.IOException;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.Set;
050:
051: public class SelectManyCheckboxListRenderer extends MenuRenderer {
052:
053: public void encodeEnd(FacesContext facesContext,
054: UIComponent uiComponent) throws IOException {
055:
056: validateParameters(facesContext, uiComponent, null);
057:
058: int counter = 0;
059:
060: boolean renderVertically = false;
061: String layout = (String) uiComponent.getAttributes().get(
062: "layout");
063: if (layout != null) {
064: renderVertically = layout.equalsIgnoreCase("pageDirection") ? true
065: : false;
066: }
067:
068: int border = getBorderSize(uiComponent);
069:
070: DOMContext domContext = DOMContext.attachDOMContext(
071: facesContext, uiComponent);
072:
073: Element rootTR = null;
074:
075: // remove all existing table rows from the root table
076: if (domContext.isInitialized()) {
077: DOMContext.removeChildrenByTagName((Element) domContext
078: .getRootNode(), "tr");
079: } else {
080: Element root = domContext.createElement("table");
081: domContext.setRootNode(root);
082: if (idNotNull(uiComponent)) {
083: setRootElementId(facesContext, root, uiComponent);
084: }
085: }
086:
087: Element rootTable = (Element) domContext.getRootNode();
088: String styleClass = (String) uiComponent.getAttributes().get(
089: "styleClass");
090: if (styleClass != null) {
091: rootTable.setAttribute("class", styleClass);
092: }
093: String style = (String) uiComponent.getAttributes()
094: .get("style");
095: if (style != null && style.length() > 0) {
096: rootTable.setAttribute("style", style);
097: } else {
098: rootTable.removeAttribute("style");
099: }
100: rootTable
101: .setAttribute("border", new Integer(border).toString());
102:
103: if (!renderVertically) {
104: rootTR = domContext.createElement("tr");
105: rootTable.appendChild(rootTR);
106: }
107:
108: Iterator options = getSelectItems(uiComponent);
109:
110: //We should call uiComponent.getValue() only once, becase if it binded with the bean,
111: //The bean method would be called as many time as this method call.
112: Object componentValue = ((UIInput) uiComponent).getValue();
113:
114: while (options.hasNext()) {
115: SelectItem nextSelectItem = (SelectItem) options.next();
116:
117: counter++;
118:
119: // render a SelectItemGroup in a nested table
120: if (nextSelectItem instanceof SelectItemGroup) {
121:
122: Element nextTR = null;
123: Element nextTD = null;
124:
125: if (nextSelectItem.getLabel() != null) {
126: if (renderVertically) {
127: nextTR = domContext.createElement("tr");
128: rootTable.appendChild(nextTR);
129: }
130: nextTD = domContext.createElement("td");
131: nextTR.appendChild(nextTD);
132: Text label = domContext.getDocument()
133: .createTextNode(nextSelectItem.getLabel());
134: nextTD.appendChild(label);
135: }
136: if (renderVertically) {
137: nextTR = domContext.createElement("tr");
138: rootTable.appendChild(nextTR);
139: }
140: nextTD = domContext.createElement("td");
141: nextTR.appendChild(nextTD);
142:
143: SelectItem[] selectItemsArray = ((SelectItemGroup) nextSelectItem)
144: .getSelectItems();
145: for (int i = 0; i < selectItemsArray.length; ++i) {
146: renderOption(facesContext, uiComponent,
147: selectItemsArray[i], renderVertically,
148: nextTR, counter, componentValue);
149: }
150: } else {
151: renderOption(facesContext, uiComponent, nextSelectItem,
152: renderVertically, rootTR, counter,
153: componentValue);
154: }
155: }
156:
157: domContext.stepOver();
158: domContext.streamWrite(facesContext, uiComponent);
159: }
160:
161: private int getBorderSize(UIComponent uiComponent) {
162: int border = 0;
163: Object borderAttribute = uiComponent.getAttributes().get(
164: "border");
165: if (borderAttribute instanceof Integer) {
166: if (((Integer) borderAttribute).intValue() != Integer.MIN_VALUE) {
167: border = ((Integer) borderAttribute).intValue();
168: }
169: } else {
170: try {
171: border = Integer.valueOf(borderAttribute.toString())
172: .intValue();
173: } catch (NumberFormatException nfe) {
174: // couldn't parse it; stick with the default (initial) value of 0
175: }
176: }
177: return border;
178: }
179:
180: protected void renderOption(FacesContext facesContext,
181: UIComponent uiComponent, SelectItem selectItem,
182: boolean renderVertically, Element rootTR, int counter,
183: Object componentValue) throws IOException {
184:
185: DOMContext domContext = DOMContext.attachDOMContext(
186: facesContext, uiComponent);
187: Element rootTable = (Element) domContext.getRootNode();
188:
189: boolean disabled = false;
190: if (uiComponent.getAttributes().get("disabled") != null) {
191: if ((uiComponent.getAttributes().get("disabled"))
192: .equals(Boolean.TRUE)) {
193: disabled = true;
194: }
195: }
196: if (selectItem.isDisabled()) {
197: disabled = true;
198: }
199:
200: if (renderVertically) {
201: rootTR = domContext.createElement("tr");
202: rootTable.appendChild(rootTR);
203: }
204: Element td = domContext.createElement("td");
205: rootTR.appendChild(td);
206:
207: Element label = domContext.createElement("label");
208: td.appendChild(label);
209:
210: Element inputElement = domContext.createElement("input");
211: inputElement.setAttribute("name", uiComponent
212: .getClientId(facesContext));
213: inputElement.setAttribute("id", uiComponent
214: .getClientId(facesContext)
215: + ":_" + counter);
216: label.appendChild(inputElement);
217: HashSet excludes = new HashSet();
218: String accesskey = (String) uiComponent.getAttributes().get(
219: "accesskey");
220: if (accesskey != null) {
221: inputElement.setAttribute("accesskey", accesskey);
222: excludes.add("accesskey");
223: }
224:
225: String formattedOptionValue = formatComponentValue(
226: facesContext, uiComponent, selectItem.getValue());
227: inputElement.setAttribute("value", formattedOptionValue);
228: inputElement.setAttribute("type", "checkbox");
229:
230: boolean isSelected;
231: Object submittedValues[] = getSubmittedSelectedValues(uiComponent);
232: if (submittedValues != null) {
233: isSelected = isSelected(formattedOptionValue,
234: submittedValues);
235: } else {
236: Object selectedValues = getCurrentSelectedValues(uiComponent);
237: isSelected = isSelected(selectItem.getValue(),
238: selectedValues);
239: }
240:
241: if (isSelected) {
242: inputElement.setAttribute("checked", Boolean.TRUE
243: .toString());
244: }
245: if (disabled) {
246: inputElement.setAttribute("disabled", "disabled");
247: }
248:
249: String selectItemLabel = selectItem.getLabel();
250: if (selectItemLabel != null) {
251: Text textNode = domContext.getDocument().createTextNode(
252: selectItemLabel);
253: inputElement.appendChild(textNode);
254: }
255: addJavaScript(facesContext, uiComponent, inputElement, excludes);
256: excludes.add("style");
257: excludes.add("border");
258: excludes.add("readonly");
259: String[] excludesStringArray = new String[excludes.size()];
260: excludesStringArray = (String[]) excludes
261: .toArray(excludesStringArray);
262: PassThruAttributeRenderer.renderAttributes(facesContext,
263: uiComponent, inputElement, rootTable,
264: excludesStringArray);
265: }
266:
267: protected void addJavaScript(FacesContext facesContext,
268: UIComponent uiComponent, Element root, Set excludes) {
269: }
270: }
|