001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.partition.impl.btree.gui;
021    
022    
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.Enumeration;
026    import java.util.List;
027    
028    import javax.swing.tree.TreeNode;
029    
030    import org.apache.directory.shared.ldap.filter.BranchNode;
031    import org.apache.directory.shared.ldap.filter.ExprNode;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    
036    /**
037     * A node representing an entry.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 638228 $
041     */
042    public class ASTNode implements TreeNode
043    {
044        private static final Logger log = LoggerFactory.getLogger( ASTNode.class );
045    
046        private final ASTNode parent;
047        private final ExprNode exprNode;
048        private final List<ASTNode> children;
049    
050    
051        public ASTNode(ASTNode parent, ExprNode exprNode)
052        {
053            children = new ArrayList<ASTNode>( 2 );
054            this.exprNode = exprNode;
055    
056            if ( parent == null )
057            {
058                this.parent = this;
059            }
060            else
061            {
062                this.parent = parent;
063            }
064    
065            try
066            {
067                if ( exprNode.isLeaf() )
068                {
069                    return;
070                }
071    
072                BranchNode branch = ( BranchNode ) exprNode;
073                
074                for ( ExprNode child:branch.getChildren() )
075                {
076                    children.add( new ASTNode( this, child ) );
077                }
078            }
079            catch ( Exception e )
080            {
081                // FIXME What exception could be thrown here?
082                log.warn( "Unexpected exception: parent=" + parent + ", exprNode=" + exprNode, e );
083            }
084        }
085    
086    
087        public Enumeration<ASTNode> children()
088        {
089            return Collections.enumeration( children );
090        }
091    
092    
093        public boolean getAllowsChildren()
094        {
095            return !exprNode.isLeaf();
096        }
097    
098    
099        public TreeNode getChildAt( int childIndex )
100        {
101            return children.get( childIndex );
102        }
103    
104    
105        public int getChildCount()
106        {
107            return children.size();
108        }
109    
110    
111        public int getIndex( TreeNode child )
112        {
113            return children.indexOf( child );
114        }
115    
116    
117        public TreeNode getParent()
118        {
119            return parent;
120        }
121    
122    
123        public boolean isLeaf()
124        {
125            return children.size() <= 0;
126        }
127    
128    
129        public String toString()
130        {
131            return exprNode.toString();
132        }
133    
134    
135        public ExprNode getExprNode()
136        {
137            return exprNode;
138        }
139    }