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.db.gui;
18  
19  
20  import org.apache.ldap.common.filter.BranchNode;
21  import org.apache.ldap.common.filter.ExprNode;
22  
23  import javax.swing.tree.TreeNode;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  
28  
29  /***
30   * A node representing an entry.
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @version $Rev: 159259 $
34   */
35  public class ASTNode implements TreeNode
36  {
37      private final ASTNode parent;
38  	private final ExprNode exprNode;
39      private final ArrayList children;
40  
41  
42      public ASTNode( ASTNode parent, ExprNode exprNode )
43      {
44          this.children = new ArrayList(2);
45          this.exprNode = exprNode;
46  
47          if( parent == null )
48          {
49              this.parent = this;
50          }
51          else
52          {
53              this.parent = parent;
54          }
55  
56  		try
57          {
58              if( exprNode.isLeaf() )
59              {
60                  return;
61              }
62  
63              BranchNode branch = ( BranchNode ) exprNode;
64              ArrayList exprNodes = branch.getChildren();
65              for ( int ii = 0; ii < exprNodes.size(); ii++ )
66              {
67                  ExprNode child = ( ExprNode ) exprNodes.get(ii);
68                  children.add( new ASTNode( this, child ) );
69              }
70          }
71          catch( Exception e )
72          {
73              e.printStackTrace();
74          }
75      }
76  
77  
78      public Enumeration children()
79      {
80          return Collections.enumeration( children );
81      }
82  
83  
84      public boolean getAllowsChildren()
85      {
86          return !exprNode.isLeaf();
87      }
88  
89  
90      public TreeNode getChildAt( int childIndex )
91      {
92  		return ( TreeNode ) children.get( childIndex );
93      }
94  
95  
96      public int getChildCount()
97      {
98          return children.size();
99      }
100 
101 
102     public int getIndex( TreeNode child )
103     {
104         return children.indexOf( child );
105     }
106 
107 
108     public TreeNode getParent()
109     {
110         return parent;
111     }
112 
113 
114     public boolean isLeaf()
115     {
116         return children.size() <= 0;
117     }
118 
119 
120     public String toString()
121     {
122         return exprNode.toString();
123     }
124 
125 
126     public ExprNode getExprNode()
127     {
128         return exprNode;
129     }
130 }