KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > SearchManager


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.core;
22
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
29 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent;
30 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
31 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
32 import org.eclipse.osgi.util.NLS;
33
34
35 /**
36  * This class is used to manages {@link ISearch}es of an {@link IConnection}
37  *
38  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
39  * @version $Rev$, $Date$
40  */

41 public class SearchManager implements Serializable JavaDoc
42 {
43
44     /** The Constant serialVersionUID. */
45     private static final long serialVersionUID = 8665227628274097691L;
46
47     /** The list of searches. */
48     private List JavaDoc<ISearch> searchList;
49
50     /** The connection. */
51     private IConnection connection;
52
53
54     /**
55      * Creates a new instance of SearchManager.
56      */

57     protected SearchManager()
58     {
59     }
60
61
62     /**
63      * Creates a new instance of SearchManager.
64      *
65      * @param connection
66      * the attached Connection
67      */

68     public SearchManager( IConnection connection )
69     {
70         this.connection = connection;
71         this.searchList = new ArrayList JavaDoc<ISearch>();
72     }
73
74
75     /**
76      * Gets the Connection.
77      *
78      * @return
79      * the Connection
80      */

81     public IConnection getConnection()
82     {
83         return this.connection;
84     }
85
86
87     /**
88      * Adds a Search.
89      *
90      * @param search
91      * the Search to add
92      */

93     public void addSearch( ISearch search )
94     {
95         this.addSearch( this.searchList.size(), search );
96     }
97
98
99     /**
100      * Adds a Search at a specified position.
101      *
102      * @param index
103      * index at which the specified Search is to be inserted.
104      * @param search
105      * the Search to be inserted
106      */

107     public void addSearch( int index, ISearch search )
108     {
109         if ( getSearch( search.getName() ) != null )
110         {
111             String JavaDoc newSearchName = NLS.bind( BrowserCoreMessages.copy_n_of_s, "", search.getName() ); //$NON-NLS-1$
112

113             for ( int i = 2; this.getSearch( newSearchName ) != null; i++ )
114             {
115                 newSearchName = NLS.bind( BrowserCoreMessages.copy_n_of_s, i + " ", search.getName() ); //$NON-NLS-1$
116
}
117
118             search.setName( newSearchName );
119         }
120
121         searchList.add( index, search );
122         EventRegistry.fireSearchUpdated( new SearchUpdateEvent( search, SearchUpdateEvent.EventDetail.SEARCH_ADDED ), this );
123     }
124
125
126     /**
127      * Gets a Search.
128      *
129      * @param name
130      * the name of the Search
131      * @return
132      * the corresponding Search
133      */

134     public ISearch getSearch( String JavaDoc name )
135     {
136         for ( ISearch search:searchList )
137         {
138             if ( search.getName().equals( name ) )
139             {
140                 return search;
141             }
142         }
143
144         return null;
145     }
146
147
148     /**
149      * Returns the index in the Searches list of the first occurrence of the specified Search.
150      *
151      * @param search
152      * the Search to search for
153      * @return
154      * the index in the Searches list of the first occurrence of the specified Search
155      */

156     public int indexOf( ISearch search )
157     {
158         return searchList.indexOf( search );
159     }
160
161
162     /**
163      * Removes a Search
164      *
165      * @param search
166      * the Search to remove
167      */

168     public void removeSearch( ISearch search )
169     {
170         searchList.remove( search );
171         EventRegistry.fireSearchUpdated( new SearchUpdateEvent( search, SearchUpdateEvent.EventDetail.SEARCH_REMOVED ), this );
172     }
173
174
175     /**
176      * Removes a Search
177      *
178      * @param name
179      * the name of the Search to remove
180      */

181     public void removeSearch( String JavaDoc name )
182     {
183         this.removeSearch( this.getSearch( name ) );
184     }
185
186
187     /**
188      * Gets an array containing all the Searches
189      *
190      * @return
191      * an array containing all the Searches
192      */

193     public ISearch[] getSearches()
194     {
195         return searchList.toArray( new ISearch[0] );
196     }
197
198
199     /**
200      * Gets the number of Searches
201      *
202      * @return
203      * the number of Searches
204      */

205     public int getSearchCount()
206     {
207         return searchList.size();
208     }
209 }
210
Popular Tags