KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.IBookmark;
25 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
26 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
27 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
28 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.ui.IEditorInput;
31 import org.eclipse.ui.IPersistableElement;
32
33
34 /**
35  * The input for the entry editor.
36  *
37  * There is a trick to provide a single instance of the entry editor:
38  * <ul>
39  * <li>If oneInstanceHackEnabled is true the equals method returns always
40  * true as long as the compared object is also of type EntryEditorInput.
41  * With this trick only one instance of the entry editor is opened
42  * by the eclipse editor framework.
43  * <li>If oneInstanceHackEnabled is false the equals method returns
44  * true only if the wrapped objects (IEntry, ISearchResult or
45  * IBookmark) are equal. This is necessary for the history navigation
46  * because it must be able to distinguish the different
47  * input objects.
48  * </ul>
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class EntryEditorInput implements IEditorInput
54 {
55
56     /** The entry input */
57     private IEntry entry;
58     
59     /** The search result input */
60     private ISearchResult searchResult;
61     
62     /** The bookmark input */
63     private IBookmark bookmark;
64
65     /** One instance hack flag */
66     private static boolean oneInstanceHackEnabled = true;
67
68
69     /**
70      * Creates a new instance of EntryEditorInput with an IEntry as
71      * domain object.
72      *
73      * @param entry the entry input
74      */

75     public EntryEditorInput( IEntry entry )
76     {
77         this.entry = entry;
78         this.searchResult = null;
79         this.bookmark = null;
80     }
81
82
83     /**
84      * Creates a new instance of EntryEditorInput with an ISearchResult as
85      * domain object.
86      *
87      * @param searchResult the search result input
88      */

89     public EntryEditorInput( ISearchResult searchResult )
90     {
91         this.entry = null;
92         this.searchResult = searchResult;
93         this.bookmark = null;
94     }
95
96
97     /**
98      * Creates a new instance of EntryEditorInput with an IBookmark as
99      * domain object.
100      *
101      * @param bookmark the bookmark input
102      */

103     public EntryEditorInput( IBookmark bookmark )
104     {
105         this.entry = null;
106         this.searchResult = null;
107         this.bookmark = bookmark;
108     }
109
110
111     /**
112      * This implementation always return false because
113      * an entry should not be visible in the
114      * "File Most Recently Used" menu.
115      *
116      * {@inheritDoc}
117      */

118     public boolean exists()
119     {
120         return false;
121     }
122
123
124     /**
125      * {@inheritDoc}
126      */

127     public ImageDescriptor getImageDescriptor()
128     {
129         return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ATTRIBUTE );
130     }
131
132
133     /**
134      * {@inheritDoc}
135      */

136     public String JavaDoc getName()
137     {
138         return "Entry Editor";
139     }
140
141
142     /**
143      * {@inheritDoc}
144      */

145     public String JavaDoc getToolTipText()
146     {
147         return "";
148     }
149
150     /**
151      * This implementation always return null.
152      *
153      * {@inheritDoc}
154      */

155     public IPersistableElement getPersistable()
156     {
157         return null;
158     }
159
160
161     /**
162      * {@inheritDoc}
163      */

164     public Object JavaDoc getAdapter( Class JavaDoc adapter )
165     {
166         return null;
167     }
168
169
170     /**
171      * Gets the resolved entry, either the entry input itself
172      * or the entry behind the search result intput or the entry behind
173      * the bookmark input.
174      *
175      * @return the resolved entry
176      */

177     public IEntry getResolvedEntry()
178     {
179         if ( entry != null )
180         {
181             return entry;
182         }
183         else if ( searchResult != null )
184         {
185             return searchResult.getEntry();
186         }
187         else if ( bookmark != null )
188         {
189             return bookmark.getEntry();
190         }
191         else
192         {
193             return null;
194         }
195     }
196
197
198     /**
199      * Gets the entry input, may be null.
200      *
201      * @return the entry input or null
202      */

203     public IEntry getEntryInput()
204     {
205         return entry;
206     }
207
208
209     /**
210      * Gets the search result input, may be null.
211      *
212      * @return the search result input or null
213      */

214     public ISearchResult getSearchResultInput()
215     {
216         return searchResult;
217     }
218
219
220     /**
221      * Gets the bookmark input, may be null.
222      *
223      * @return the bookmark input or null
224      */

225     public IBookmark getBookmarkInput()
226     {
227         return bookmark;
228     }
229
230
231     /**
232      * Gets the input, may be null.
233      *
234      * @return the input or null
235      */

236     public Object JavaDoc getInput()
237     {
238         if ( entry != null )
239         {
240             return entry;
241         }
242         else if ( searchResult != null )
243         {
244             return searchResult;
245         }
246         else if ( bookmark != null )
247         {
248             return bookmark;
249         }
250         else
251         {
252             return null;
253         }
254     }
255
256
257     /**
258      * {@inheritDoc}
259      */

260     public int hashCode()
261     {
262         return getToolTipText().hashCode();
263     }
264
265
266     /**
267      * The result depends
268      *
269      * {@inheritDoc}
270      */

271     public boolean equals( Object JavaDoc obj )
272     {
273         boolean equal;
274
275         if ( oneInstanceHackEnabled )
276         {
277             equal = ( obj instanceof EntryEditorInput );
278         }
279         else
280         {
281             if ( obj instanceof EntryEditorInput )
282             {
283                 EntryEditorInput other = ( EntryEditorInput ) obj;
284                 if ( this.getInput() == null && other.getInput() == null )
285                 {
286                     return true;
287                 }
288                 else if ( this.getInput() == null || other.getInput() == null )
289                 {
290                     return false;
291                 }
292                 else
293                 {
294                     equal = other.getInput().equals( this.getInput() );
295                 }
296             }
297             else
298             {
299                 equal = false;
300             }
301         }
302
303         return equal;
304     }
305
306
307     /**
308      * Enables or disabled the one instance hack.
309      *
310      * @param b
311      * true to enable the one instance hack,
312      * false to disable the one instance hack
313      */

314     public static void enableOneInstanceHack( boolean b )
315     {
316         oneInstanceHackEnabled = b;
317     }
318
319 }
320
Popular Tags