001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/widgets/src/java/org/sakaiproject/jsf/renderer/ToolBarItemRenderer.java $
003: * $Id: ToolBarItemRenderer.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.renderer;
021:
022: import java.io.IOException;
023:
024: import javax.faces.component.UICommand;
025: import javax.faces.component.UIComponent;
026: import javax.faces.context.FacesContext;
027: import javax.faces.context.ResponseWriter;
028:
029: import org.sakaiproject.jsf.util.JSFDepends;
030: import org.sakaiproject.jsf.util.RendererUtil;
031:
032: public class ToolBarItemRenderer extends JSFDepends.CommandLinkRenderer {
033: public boolean supportsComponentType(UIComponent component) {
034: return (component instanceof org.sakaiproject.jsf.component.ToolBarItemComponent);
035: }
036:
037: public void encodeBegin(FacesContext context, UIComponent component)
038: throws IOException {
039: if (!component.isRendered())
040: return;
041:
042: if (!isDisabled(context, component)) {
043: // use default link rendering
044: super .encodeBegin(context, component);
045: } else {
046: // setup to render the disabled link ourselves
047: ResponseWriter writer = context.getResponseWriter();
048: writer.write("<span class=\"chefToolBarDisabled\">");
049: }
050: }
051:
052: public void encodeChildren(FacesContext context,
053: UIComponent component) throws IOException {
054: if (!component.isRendered())
055: return;
056:
057: if (!isDisabled(context, component)) {
058: // use default rendering
059: super .encodeChildren(context, component);
060: } else {
061: // render the text of the disabled link ourselves
062: String label = "";
063: Object value = ((UICommand) component).getValue();
064: if (value != null) {
065: label = value.toString();
066: }
067:
068: ResponseWriter writer = context.getResponseWriter();
069: writer.write(label);
070: }
071: }
072:
073: public void encodeEnd(FacesContext context, UIComponent component)
074: throws IOException {
075: if (!component.isRendered())
076: return;
077:
078: if (!isDisabled(context, component)) {
079: // use default link rendering
080: super .encodeEnd(context, component);
081: } else {
082: // finish rendering the disabled link ourselves
083: ResponseWriter writer = context.getResponseWriter();
084: writer.write("</span>");
085: }
086: }
087:
088: /**
089: * Check if the component is disabled.
090: * @param component
091: * @return true if the component has a boolean "disabled" attribute set, false if not
092: */
093: protected boolean isDisabled(FacesContext context,
094: UIComponent component) {
095: boolean disabled = false;
096: Object value = RendererUtil.getAttribute(context, component,
097: "disabled");
098: if (value != null) {
099: if (value instanceof Boolean) {
100: disabled = ((Boolean) value).booleanValue();
101: } else {
102: if (!(value instanceof String)) {
103: value = value.toString();
104: }
105: disabled = (new Boolean((String) value)).booleanValue();
106: }
107: }
108:
109: return disabled;
110: }
111: }
|