KickJava   Java API By Example, From Geeks To Geeks.

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


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.BookmarkUpdateEvent;
29 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
30 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.eclipse.osgi.util.NLS;
33
34
35 /**
36  * This class is used to manage Bookmarks.
37  *
38  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
39  * @version $Rev$, $Date$
40  */

41 public class BookmarkManager implements Serializable JavaDoc
42 {
43     private static final long serialVersionUID = 7605293576518974531L;
44
45     private List JavaDoc<IBookmark> bookmarkList;
46
47     private IConnection connection;
48
49
50     /**
51      * Creates a new instance of BookmarkManager.
52      *
53      * @param connection
54      * the attached Connection
55      */

56     public BookmarkManager( IConnection connection )
57     {
58         this.connection = connection;
59         bookmarkList = new ArrayList JavaDoc<IBookmark>();
60     }
61
62
63     /**
64      * Gets the Connection
65      *
66      * @return
67      * the Connection
68      */

69     public IConnection getConnection()
70     {
71         return connection;
72     }
73
74
75     /**
76      * Adds a Bookmark
77      *
78      * @param bookmark
79      * the Bookmark to add
80      */

81     public void addBookmark( IBookmark bookmark )
82     {
83         addBookmark( bookmarkList.size(), bookmark );
84     }
85
86
87     /**
88      * Adds a Bookmark at a specified position.
89      *
90      * @param index
91      * the index at which the specified element is to be inserted.
92      * @param bookmark
93      * the Bookmark to add
94      */

95     public void addBookmark( int index, IBookmark bookmark )
96     {
97         if ( getBookmark( bookmark.getName() ) != null )
98         {
99             String JavaDoc newBookmarkName = NLS.bind( BrowserCoreMessages.copy_n_of_s, "", bookmark.getName() ); //$NON-NLS-1$
100

101             for ( int i = 2; this.getBookmark( newBookmarkName ) != null; i++ )
102             {
103                 newBookmarkName = NLS.bind( BrowserCoreMessages.copy_n_of_s, i + " ", bookmark.getName() ); //$NON-NLS-1$
104
}
105
106             bookmark.setName( newBookmarkName );
107         }
108
109         bookmarkList.add( index, bookmark );
110         EventRegistry.fireBookmarkUpdated( new BookmarkUpdateEvent( bookmark, BookmarkUpdateEvent.Detail.BOOKMARK_ADDED ),
111             this );
112     }
113
114
115     /**
116      * Gets a Bookmark
117      *
118      * @param name
119      * the name of the Bookmark
120      * @return
121      * the corresponding Bookmark
122      */

123     public IBookmark getBookmark( String JavaDoc name )
124     {
125         for ( IBookmark bookmark : bookmarkList )
126         {
127             if ( bookmark.getName().equals( name ) )
128             {
129                 return bookmark;
130             }
131         }
132
133         return null;
134     }
135
136
137     /**
138      * Returns the index in the Bookmarks list of the first occurrence of the specified Bookmark
139      *
140      * @param bookmark
141      * the bookmark to search for
142      * @return
143      * the index in the Bookmarks list of the first occurrence of the specified Bookmark
144      */

145     public int indexOf( IBookmark bookmark )
146     {
147         return bookmarkList.indexOf( bookmark );
148     }
149
150
151     /**
152      * Removes a Bookmark
153      *
154      * @param bookmark
155      * the Bookmark to remove
156      */

157     public void removeBookmark( IBookmark bookmark )
158     {
159         bookmarkList.remove( bookmark );
160         EventRegistry.fireBookmarkUpdated( new BookmarkUpdateEvent( bookmark, BookmarkUpdateEvent.Detail.BOOKMARK_REMOVED ),
161             this );
162     }
163
164
165     /**
166      * Removes a Bookmark
167      *
168      * @param name
169      * the name of the Bookmark to remove
170      */

171     public void removeBookmark( String JavaDoc name )
172     {
173         this.removeBookmark( this.getBookmark( name ) );
174     }
175
176
177     /**
178      * Gets an array containing all Bookmarks
179      *
180      * @return
181      * an array containing all Bookmarks
182      */

183     public IBookmark[] getBookmarks()
184     {
185         return bookmarkList.toArray( new IBookmark[0] );
186     }
187
188
189     /**
190      * Gets the number of Bookmarks
191      *
192      * @return
193      * the number of Bookmarjs
194      */

195     public int getBookmarkCount()
196     {
197         return bookmarkList.size();
198     }
199 }
200
Popular Tags