KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > browser > BrowserEntryPage


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
21 package org.apache.directory.ldapstudio.browser.common.widgets.browser;
22
23
24 import java.util.Arrays JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
27
28
29 /**
30  * A BrowserEntryPage is a container for entries or other nested browser entry pages.
31  * It is used when folding large branches.
32  *
33  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
34  * @version $Rev$, $Date$
35  */

36 public class BrowserEntryPage
37 {
38     /** The tree sorter */
39     private BrowserSorter sorter;
40
41     /** The index of the first child entry in this page */
42     private int first;
43
44     /** The index of the last child entry in this page */
45     private int last;
46
47     /** The parent entry */
48     private IEntry entry;
49
50     /** The parent entry page or null if not nested */
51     private BrowserEntryPage parentEntryPage;
52
53     /** The sub pages */
54     private BrowserEntryPage[] subpages;
55
56
57     /**
58      * Creates a new instance of BrowserEntryPage.
59      *
60      * @param entry the parent entry
61      * @param first the index of the first child entry in this page
62      * @param last the index of the last child entry in this page
63      * @param subpages the sub pages
64      * @param sorter the sorter
65      */

66     public BrowserEntryPage( IEntry entry, int first, int last, BrowserEntryPage[] subpages, BrowserSorter sorter )
67     {
68         this.entry = entry;
69         this.first = first;
70         this.last = last;
71         this.subpages = subpages;
72         this.sorter = sorter;
73
74         if ( subpages != null )
75         {
76             for ( int i = 0; i < subpages.length; i++ )
77             {
78                 subpages[i].parentEntryPage = this;
79             }
80         }
81     }
82
83
84     /**
85      * Gets the children, either the sub pages or
86      * the entries contained in this page.
87      *
88      * @return the children
89      */

90     public Object JavaDoc[] getChildren()
91     {
92         if ( subpages != null )
93         {
94             return subpages;
95         }
96         else
97         {
98             // 1. get children
99
IEntry[] children = entry.getChildren();
100
101             // 2. sort
102
sorter.sort( null, children );
103
104             // 3. extraxt range
105
if ( children != null )
106             {
107                 IEntry[] childrenRange = new IEntry[last - first + 1];
108                 for ( int i = first; i <= last; i++ )
109                 {
110                     childrenRange[i - first] = children[i];
111                 }
112                 return childrenRange;
113             }
114             else
115             {
116                 return null;
117             }
118         }
119     }
120
121
122     
123     /**
124      * Gets the first.
125      *
126      * @return the first
127      */

128     public int getFirst()
129     {
130         return first;
131     }
132
133
134     /**
135      * Gets the last.
136      *
137      * @return the last
138      */

139     public int getLast()
140     {
141         return last;
142     }
143
144
145     /**
146      * Gets the parent entry.
147      *
148      * @return the parent entry
149      */

150     public IEntry getEntry()
151     {
152         return entry;
153     }
154
155
156     /**
157      * Gets the parent page if the given entry is contained in this page
158      * or one of the sub pages.
159      *
160      * @param entry the entry
161      *
162      * @return the parent page of the given entry.
163      */

164     public BrowserEntryPage getParentOf( IEntry entry )
165     {
166         if ( subpages != null )
167         {
168             BrowserEntryPage ep = null;
169             for ( int i = 0; i < subpages.length && ep == null; i++ )
170             {
171                 ep = subpages[i].getParentOf( entry );
172             }
173             return ep;
174         }
175         else
176         {
177             IEntry[] sr = ( IEntry[] ) getChildren();
178             if ( sr != null && Arrays.asList( sr ).contains( entry ) )
179             {
180                 return this;
181             }
182             else
183             {
184                 return null;
185             }
186         }
187     }
188
189
190     /**
191      * Gets the direct parent, either a page or the entry.
192      *
193      * @return the direct parent
194      */

195     public Object JavaDoc getParent()
196     {
197         return ( parentEntryPage != null ) ? ( Object JavaDoc ) parentEntryPage : ( Object JavaDoc ) entry;
198     }
199
200
201     /**
202      * {@inheritDoc}
203      */

204     public String JavaDoc toString()
205     {
206         return entry.toString() + "[" + first + "..." + last + "]" + hashCode();
207     }
208
209 }
210
Popular Tags