View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.partition.impl.btree.gui;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Enumeration;
23  
24  import javax.swing.tree.TreeNode;
25  
26  import org.apache.ldap.common.filter.BranchNode;
27  import org.apache.ldap.common.filter.ExprNode;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /***
33   * A node representing an entry.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev: 280870 $
37   */
38  public class ASTNode implements TreeNode
39  {
40      private static final Logger log = LoggerFactory.getLogger( ASTNode.class );
41  
42      private final ASTNode parent;
43  	private final ExprNode exprNode;
44      private final ArrayList children;
45  
46  
47      public ASTNode( ASTNode parent, ExprNode exprNode )
48      {
49          this.children = new ArrayList(2);
50          this.exprNode = exprNode;
51  
52          if( parent == null )
53          {
54              this.parent = this;
55          }
56          else
57          {
58              this.parent = parent;
59          }
60  
61  		try
62          {
63              if( exprNode.isLeaf() )
64              {
65                  return;
66              }
67  
68              BranchNode branch = ( BranchNode ) exprNode;
69              ArrayList exprNodes = branch.getChildren();
70              for ( int ii = 0; ii < exprNodes.size(); ii++ )
71              {
72                  ExprNode child = ( ExprNode ) exprNodes.get(ii);
73                  children.add( new ASTNode( this, child ) );
74              }
75          }
76          catch( Exception e )
77          {
78              // FIXME What exception could be thrown here?
79              log.warn( "Unexpected exception: parent=" + parent + ", exprNode=" + exprNode, e );
80          }
81      }
82  
83  
84      public Enumeration children()
85      {
86          return Collections.enumeration( children );
87      }
88  
89  
90      public boolean getAllowsChildren()
91      {
92          return !exprNode.isLeaf();
93      }
94  
95  
96      public TreeNode getChildAt( int childIndex )
97      {
98  		return ( TreeNode ) children.get( childIndex );
99      }
100 
101 
102     public int getChildCount()
103     {
104         return children.size();
105     }
106 
107 
108     public int getIndex( TreeNode child )
109     {
110         return children.indexOf( child );
111     }
112 
113 
114     public TreeNode getParent()
115     {
116         return parent;
117     }
118 
119 
120     public boolean isLeaf()
121     {
122         return children.size() <= 0;
123     }
124 
125 
126     public String toString()
127     {
128         return exprNode.toString();
129     }
130 
131 
132     public ExprNode getExprNode()
133     {
134         return exprNode;
135     }
136 }