KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISearch;
27 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
28
29
30 /**
31  * A BrowserSearchResultPage is a container for search results or other nested browser search result pages.
32  * It is used when folding searches with many results.
33  *
34  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
35  * @version $Rev$, $Date$
36  */

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

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

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

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

141     public int getLast()
142     {
143         return last;
144     }
145
146
147     /**
148      * Gets the search.
149      *
150      * @return the search
151      */

152     public ISearch getSearch()
153     {
154         return search;
155     }
156
157
158     /**
159      * Gets the parent page if the given search result is contained in this page
160      * or one of the sub pages.
161      *
162      * @param searchResult the search result
163      *
164      * @return the parent page of the given search result.
165      */

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

197     public Object JavaDoc getParent()
198     {
199         return ( parentSearchResultPage != null ) ? ( Object JavaDoc ) parentSearchResultPage : ( Object JavaDoc ) search;
200     }
201
202
203     /**
204      * {@inheritDoc}
205      */

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