001    package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_NAME;
023    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_SELECTABLE;
024    import static org.apache.myfaces.tobago.TobagoConstants.SUBCOMPONENT_SEP;
025    import org.apache.myfaces.tobago.component.UITreeListbox;
026    import org.apache.myfaces.tobago.component.UITreeListboxBox;
027    import org.apache.myfaces.tobago.component.UITreeOldNode;
028    import org.apache.myfaces.tobago.renderkit.LayoutableRendererBase;
029    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
030    import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
031    import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtil;
032    import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
033    
034    import javax.faces.component.UIComponent;
035    import javax.faces.context.FacesContext;
036    import javax.swing.tree.DefaultMutableTreeNode;
037    import java.io.IOException;
038    import java.util.List;
039    
040    /*
041     * User: weber
042     * Date: Mar 21, 2005
043     * Time: 11:19:03 AM
044     */
045    public class TreeListboxBoxRenderer extends LayoutableRendererBase {
046    
047      private static final Log LOG = LogFactory.getLog(TreeListboxBoxRenderer.class);
048    
049      public void encodeEnd(FacesContext facesContext, UIComponent component)
050          throws IOException {
051    
052        int level = ((UITreeListboxBox) component).getLevel();
053    
054        UITreeListbox tree = (UITreeListbox) component.getParent();
055        List<UITreeOldNode> selectionPath = tree.getSelectionPath();
056        String className = "tobago-treeListbox-default";
057        if (selectionPath.size() > 0 &&  selectionPath.size() - 1 <= level
058            && selectionPath.get(selectionPath.size() - 1).getTreeNode().isLeaf()) {
059          className += " tobago-treeListbox-unused";
060        }
061    
062        String treeId = tree.getClientId(facesContext);
063        TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
064    
065        final boolean siblingMode
066            = "siblingLeafOnly".equals(tree.getAttributes().get(ATTR_SELECTABLE));
067    
068        String listboxId = treeId + SUBCOMPONENT_SEP + "cont_" + level;
069        String onChange = "tbgTreeListboxChange(this, '" + treeId + "')";
070        String onClick  = "tbgTreeListboxClick(this, '" + treeId + "')";
071        writer.startElement(HtmlConstants.SELECT, component);
072        writer.writeIdAttribute(listboxId);
073        writer.writeClassAttribute(className);
074        writer.writeStyleAttribute();
075        writer.writeAttribute(HtmlAttributes.SIZE, 2);
076        if (siblingMode) {
077          writer.writeAttribute(HtmlAttributes.ONCHANGE, onChange, null);
078        } else {
079          writer.writeAttribute(HtmlAttributes.ONCLICK, onClick, null);
080        }
081        writer.writeAttribute(HtmlAttributes.MULTIPLE, siblingMode);
082    
083    
084        List nodes = ((UITreeListboxBox) component).getNodes();
085    
086        for (int i = 0; i < nodes.size(); i++) {
087          UITreeOldNode treeNode = (UITreeOldNode) nodes.get(i);
088          DefaultMutableTreeNode node = treeNode.getTreeNode();
089    
090          writer.startElement(HtmlConstants.OPTION, treeNode);
091    //      writer.writeAttribute(HtmlAttributes.ONCLICK, "tbgTreeListboxClick(this, '" + treeId + "')", null);
092          writer.writeAttribute(HtmlAttributes.VALUE, i);
093          if (treeNode.equals(tree.getSelectedNode(level))
094              || tree.isSelectedNode(node)) {
095            writer.writeAttribute(HtmlAttributes.SELECTED, true);
096          }
097          HtmlRendererUtil.renderTip(treeNode, writer);
098          writer.writeText(treeNode.getAttributes().get(ATTR_NAME), null);
099          if (node.getChildCount() > 0) {
100            writer.writeText(" \u2192");
101          }
102          writer.endElement(HtmlConstants.OPTION);
103        }
104        writer.endElement(HtmlConstants.SELECT);
105      }
106    
107    }