001    package org.apache.myfaces.tobago.model;
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.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    import org.apache.myfaces.tobago.component.Attributes;
023    import org.apache.myfaces.tobago.internal.component.AbstractUITreeNode;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    import java.util.Stack;
028    
029    /**
030     * Date: 23.04.2007 16:10:22
031     */
032    public class MixedTreeModel {
033    
034      private static final Logger LOG = LoggerFactory.getLogger(MixedTreeModel.class);
035    
036      private Node root;
037      private Node current;
038      private Integer nextChildIndex;
039      private Stack<Boolean> junctions = new Stack<Boolean>();
040    
041      public void beginBuildNode(AbstractUITreeNode node) {
042        if (LOG.isDebugEnabled()) {
043          LOG.debug("{}", node.getAttributes().get(Attributes.LABEL));
044        }
045        Node newNode = new Node();
046        newNode.setLabel((String) node.getAttributes().get(Attributes.LABEL));
047        if (root == null) {
048          root = newNode;
049          current = root;
050        } else {
051          current.add(newNode);
052          current = newNode;
053        }
054      }
055    
056      public void endBuildNode(AbstractUITreeNode node) {
057        if (LOG.isDebugEnabled()) {
058          LOG.debug("{}", node.getAttributes().get(Attributes.LABEL));
059        }
060        current = current.getParent();
061      }
062    
063      public void onEncodeBegin() {
064        if (LOG.isDebugEnabled()) {
065          LOG.debug("current=" + current);
066        }
067        if (current == null) {
068          current = root;
069        } else {
070          current = current.getChildAt(nextChildIndex);
071        }
072        nextChildIndex = 0;
073    
074        junctions.push(hasCurrentNodeNextSibling());
075      }
076    
077      public void onEncodeEnd() {
078        if (LOG.isDebugEnabled()) {
079          LOG.debug("current=" + current);
080        }
081        Node parent = current.getParent();
082        if (parent != null) {
083          nextChildIndex = parent.getIndex(current) + 1;
084          current = parent;
085        } else {
086          nextChildIndex = null;
087          current = null;
088        }
089    
090        junctions.pop();
091      }
092    
093      public boolean hasCurrentNodeNextSibling() {
094        return current.hasNextSibling();
095      }
096    
097      public List<Boolean> getJunctions() {
098        Boolean top = junctions.pop();
099        List<Boolean> result = new ArrayList<Boolean>(junctions);
100        junctions.push(top);
101        return result;
102      }
103    
104      public TreePath getPath() {
105        return current.getPath();
106      }
107    }