KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > entry > EntryEditorNavigationLocation


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.ui.editors.entry;
22
23
24 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
25 import org.apache.directory.ldapstudio.browser.core.model.DN;
26 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
27 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
28 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
29 import org.apache.directory.ldapstudio.browser.core.model.IRootDSE;
30 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
31 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
32 import org.apache.directory.ldapstudio.browser.core.model.NameException;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IMemento;
35 import org.eclipse.ui.INavigationLocation;
36 import org.eclipse.ui.NavigationLocation;
37
38
39 /**
40  * This class is used to mark the entry editor input to the navigation history.
41  *
42  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
43  * @version $Rev$, $Date$
44  */

45 public class EntryEditorNavigationLocation extends NavigationLocation
46 {
47
48     /**
49      * Creates a new instance of EntryEditorNavigationLocation.
50      *
51      * @param editor the entry editor
52      */

53     EntryEditorNavigationLocation( EntryEditor editor )
54     {
55         super( editor );
56     }
57
58
59     /**
60      * {@inheritDoc}
61      */

62     public String JavaDoc getText()
63     {
64         EntryEditorInput eei = getEntryEditorInput();
65         if ( eei != null )
66         {
67             if ( eei.getEntryInput() != null )
68             {
69                 if ( eei.getEntryInput() instanceof IRootDSE )
70                 {
71                     return "Root DSE";
72                 }
73                 else
74                 {
75                     return "Entry " + eei.getEntryInput().getDn().toString();
76                 }
77             }
78             else if ( eei.getSearchResultInput() != null )
79             {
80                 if ( eei.getSearchResultInput() instanceof IRootDSE )
81                 {
82                     return "Root DSE";
83                 }
84                 else
85                 {
86                     return "Search Result " + eei.getSearchResultInput().getDn().toString();
87                 }
88             }
89             else if ( eei.getBookmarkInput() != null )
90             {
91                 if ( eei.getBookmarkInput() instanceof IRootDSE )
92                 {
93                     return "Root DSE";
94                 }
95                 else
96                 {
97                     return "Bookmark " + eei.getBookmarkInput().getDn().toString();
98                 }
99             }
100         }
101         return super.getText();
102     }
103
104
105     /**
106      * {@inheritDoc}
107      */

108     public void saveState( IMemento memento )
109     {
110         EntryEditorInput eei = getEntryEditorInput();
111         if ( eei != null )
112         {
113             if ( eei.getEntryInput() != null )
114             {
115                 IEntry entry = eei.getEntryInput();
116                 memento.putString( "TYPE", "IEntry" );
117                 memento.putString( "DN", entry.getDn().toString() );
118                 memento.putString( "CONNECTION", entry.getConnection().getName() );
119             }
120             else if ( eei.getSearchResultInput() != null )
121             {
122                 ISearchResult searchResult = eei.getSearchResultInput();
123                 memento.putString( "TYPE", "ISearchResult" );
124                 memento.putString( "DN", searchResult.getDn().toString() );
125                 memento.putString( "SEARCH", searchResult.getSearch().getName() );
126                 memento.putString( "CONNECTION", searchResult.getSearch().getConnection().getName() );
127             }
128             else if ( eei.getBookmarkInput() != null )
129             {
130                 IBookmark bookmark = eei.getBookmarkInput();
131                 memento.putString( "TYPE", "IBookmark" );
132                 memento.putString( "BOOKMARK", bookmark.getName() );
133                 memento.putString( "CONNECTION", bookmark.getConnection().getName() );
134             }
135         }
136
137     }
138
139
140     /**
141      * {@inheritDoc}
142      */

143     public void restoreState( IMemento memento )
144     {
145         try
146         {
147             String JavaDoc type = memento.getString( "TYPE" );
148             if ( "IEntry".equals( type ) )
149             {
150                 IConnection connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection(
151                     memento.getString( "CONNECTION" ) );
152                 DN dn = new DN( memento.getString( "DN" ) );
153                 IEntry entry = connection.getEntryFromCache( dn );
154                 super.setInput( new EntryEditorInput( entry ) );
155             }
156             else if ( "ISearchResult".equals( type ) )
157             {
158                 IConnection connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection(
159                     memento.getString( "CONNECTION" ) );
160                 ISearch search = connection.getSearchManager().getSearch( memento.getString( "SEARCH" ) );
161                 ISearchResult[] searchResults = search.getSearchResults();
162                 DN dn = new DN( memento.getString( "DN" ) );
163                 for ( int i = 0; i < searchResults.length; i++ )
164                 {
165                     if ( dn.equals( searchResults[i].getDn() ) )
166                     {
167                         super.setInput( new EntryEditorInput( searchResults[i] ) );
168                         break;
169                     }
170                 }
171             }
172             else if ( "IBookmark".equals( type ) )
173             {
174                 IConnection connection = BrowserCorePlugin.getDefault().getConnectionManager().getConnection(
175                     memento.getString( "CONNECTION" ) );
176                 IBookmark bookmark = connection.getBookmarkManager().getBookmark( memento.getString( "BOOKMARK" ) );
177                 super.setInput( new EntryEditorInput( bookmark ) );
178             }
179         }
180         catch ( NameException e )
181         {
182             e.printStackTrace();
183         }
184
185     }
186
187
188     /**
189      * {@inheritDoc}
190      */

191     public void restoreLocation()
192     {
193         IEditorPart editorPart = getEditorPart();
194         if ( editorPart != null && editorPart instanceof EntryEditor )
195         {
196             EntryEditor entryEditor = ( EntryEditor ) editorPart;
197             entryEditor.setInput( ( EntryEditorInput ) getInput() );
198         }
199     }
200
201
202     /**
203      * {@inheritDoc}
204      */

205     public boolean mergeInto( INavigationLocation currentLocation )
206     {
207         if ( currentLocation == null )
208         {
209             return false;
210         }
211
212         if ( getClass() != currentLocation.getClass() )
213         {
214             return false;
215         }
216
217         EntryEditorNavigationLocation location = ( EntryEditorNavigationLocation ) currentLocation;
218         Object JavaDoc other = location.getEntryEditorInput().getInput();
219         Object JavaDoc entry = getEntryEditorInput().getInput();
220
221         if ( other == null && entry == null )
222         {
223             return true;
224         }
225         else if ( other == null || entry == null )
226         {
227             return false;
228         }
229         else
230         {
231             return entry.equals( other );
232         }
233     }
234
235
236     /**
237      * {@inheritDoc}
238      */

239     public void update()
240     {
241     }
242
243
244     /**
245      * Gets the input.
246      *
247      * @return the input
248      */

249     private EntryEditorInput getEntryEditorInput()
250     {
251
252         Object JavaDoc editorInput = getInput();
253         if ( editorInput != null && editorInput instanceof EntryEditorInput )
254         {
255             EntryEditorInput entryEditorInput = ( EntryEditorInput ) editorInput;
256             return entryEditorInput;
257         }
258
259         return null;
260     }
261
262
263     /**
264      * {@inheritDoc}
265      */

266     public String JavaDoc toString()
267     {
268         return "" + getEntryEditorInput().getInput();
269     }
270
271 }
272
Popular Tags