View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.partition.impl.btree.gui;
21  
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.List;
27  
28  import javax.swing.tree.TreeNode;
29  
30  import org.apache.directory.shared.ldap.filter.BranchNode;
31  import org.apache.directory.shared.ldap.filter.ExprNode;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  
36  /**
37   * A node representing an entry.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 638228 $
41   */
42  public class ASTNode implements TreeNode
43  {
44      private static final Logger log = LoggerFactory.getLogger( ASTNode.class );
45  
46      private final ASTNode parent;
47      private final ExprNode exprNode;
48      private final List<ASTNode> children;
49  
50  
51      public ASTNode(ASTNode parent, ExprNode exprNode)
52      {
53          children = new ArrayList<ASTNode>( 2 );
54          this.exprNode = exprNode;
55  
56          if ( parent == null )
57          {
58              this.parent = this;
59          }
60          else
61          {
62              this.parent = parent;
63          }
64  
65          try
66          {
67              if ( exprNode.isLeaf() )
68              {
69                  return;
70              }
71  
72              BranchNode branch = ( BranchNode ) exprNode;
73              
74              for ( ExprNode child:branch.getChildren() )
75              {
76                  children.add( new ASTNode( this, child ) );
77              }
78          }
79          catch ( Exception e )
80          {
81              // FIXME What exception could be thrown here?
82              log.warn( "Unexpected exception: parent=" + parent + ", exprNode=" + exprNode, e );
83          }
84      }
85  
86  
87      public Enumeration<ASTNode> children()
88      {
89          return Collections.enumeration( children );
90      }
91  
92  
93      public boolean getAllowsChildren()
94      {
95          return !exprNode.isLeaf();
96      }
97  
98  
99      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 }