KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
26 import org.apache.directory.ldapstudio.browser.core.internal.model.AliasBaseEntry;
27 import org.apache.directory.ldapstudio.browser.core.internal.model.BaseDNEntry;
28 import org.apache.directory.ldapstudio.browser.core.internal.model.DirectoryMetadataEntry;
29 import org.apache.directory.ldapstudio.browser.core.internal.model.ReferralBaseEntry;
30 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
31 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
32 import org.apache.directory.ldapstudio.browser.core.model.IRootDSE;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
34 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
35 import org.apache.directory.ldapstudio.browser.core.model.RDN;
36 import org.apache.directory.ldapstudio.browser.core.model.RDNPart;
37 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
38 import org.eclipse.jface.preference.PreferenceConverter;
39 import org.eclipse.jface.viewers.IColorProvider;
40 import org.eclipse.jface.viewers.IFontProvider;
41 import org.eclipse.jface.viewers.LabelProvider;
42 import org.eclipse.swt.graphics.Color;
43 import org.eclipse.swt.graphics.Font;
44 import org.eclipse.swt.graphics.FontData;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.swt.graphics.RGB;
47 import org.eclipse.ui.ISharedImages;
48 import org.eclipse.ui.PlatformUI;
49
50
51 /**
52  * The BrowserLabelProvider implements the label provider for
53  * the browser widget.
54  *
55  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
56  * @version $Rev$, $Date$
57  */

58 public class BrowserLabelProvider extends LabelProvider implements IFontProvider, IColorProvider
59 {
60
61     /** The preferences. */
62     private BrowserPreferences preferences;
63
64
65     /**
66      * Creates a new instance of BrowserLabelProvider.
67      *
68      * @param preferences the preferences
69      */

70     public BrowserLabelProvider( BrowserPreferences preferences )
71     {
72         this.preferences = preferences;
73     }
74
75
76     /**
77      * {@inheritDoc}
78      */

79     public String JavaDoc getText( Object JavaDoc obj )
80     {
81         if ( obj instanceof IEntry )
82         {
83             IEntry entry = ( IEntry ) obj;
84
85             StringBuffer JavaDoc append = new StringBuffer JavaDoc();
86             if ( entry instanceof IRootDSE )
87             {
88                 append.append( "Root DSE" );
89             }
90             if ( entry.isChildrenInitialized() && ( entry.getChildrenCount() > 0 ) || entry.getChildrenFilter() != null )
91             {
92                 append.append( " (" ).append( entry.getChildrenCount() );
93                 if ( entry.hasMoreChildren() )
94                 {
95                     append.append( "+" );
96                 }
97                 if ( entry.getChildrenFilter() != null )
98                 {
99                     append.append( ", filtered" );
100                 }
101                 append.append( ")" );
102             }
103
104             if ( entry instanceof ReferralBaseEntry )
105             {
106                 return entry.getUrl().toString() + " " + append.toString();
107             }
108             else if ( entry instanceof AliasBaseEntry )
109             {
110                 return entry.getDn().toString() + " " + append.toString();
111             }
112             else if ( entry instanceof BaseDNEntry )
113             {
114                 return entry.getDn().toString() + " " + append.toString();
115             }
116             else if ( entry.hasParententry() )
117             {
118
119                 String JavaDoc label = "";
120                 if ( preferences.getEntryLabel() == BrowserCommonConstants.SHOW_DN )
121                 {
122                     label = entry.getDn().toString();
123                 }
124                 else if ( preferences.getEntryLabel() == BrowserCommonConstants.SHOW_RDN )
125                 {
126                     label = entry.getRdn().toString();
127
128                 }
129                 else if ( preferences.getEntryLabel() == BrowserCommonConstants.SHOW_RDN_VALUE )
130                 {
131                     label = entry.getRdn().getValue();
132                 }
133
134                 label += append.toString();
135
136                 if ( preferences.isEntryAbbreviate() && label.length() > preferences.getEntryAbbreviateMaxLength() )
137                 {
138                     label = Utils.shorten( label, preferences.getEntryAbbreviateMaxLength() );
139                 }
140
141                 return label;
142             }
143             else
144             {
145                 return entry.getDn() + append.toString();
146             }
147         }
148         else if ( obj instanceof BrowserEntryPage )
149         {
150             BrowserEntryPage container = ( BrowserEntryPage ) obj;
151             return "[" + ( container.getFirst() + 1 ) + "..." + ( container.getLast() + 1 ) + "]";
152         }
153         else if ( obj instanceof BrowserSearchResultPage )
154         {
155             BrowserSearchResultPage container = ( BrowserSearchResultPage ) obj;
156             return "[" + ( container.getFirst() + 1 ) + "..." + ( container.getLast() + 1 ) + "]";
157         }
158         else if ( obj instanceof ISearch )
159         {
160             ISearch search = ( ISearch ) obj;
161             ISearchResult[] results = search.getSearchResults();
162             StringBuffer JavaDoc append = new StringBuffer JavaDoc( search.getName() );
163             if ( results != null )
164             {
165                 append.append( " (" ).append( results.length );
166                 if ( search.isCountLimitExceeded() )
167                 {
168                     append.append( "+" );
169                 }
170                 append.append( ")" );
171             }
172             return append.toString();
173         }
174         else if ( obj instanceof IBookmark )
175         {
176             IBookmark bookmark = ( IBookmark ) obj;
177             return bookmark.getName();
178         }
179         else if ( obj instanceof ISearchResult )
180         {
181             ISearchResult sr = ( ISearchResult ) obj;
182
183             if ( !sr.getSearch().getConnection().equals( sr.getEntry().getConnection() ) )
184             {
185                 return sr.getEntry().getUrl().toString();
186             }
187             else if ( sr.getEntry().hasParententry() || sr.getEntry() instanceof IRootDSE )
188             {
189                 String JavaDoc label = "";
190                 if ( sr.getEntry() instanceof IRootDSE )
191                 {
192                     label = "Root DSE";
193                 }
194                 else if ( preferences.getSearchResultLabel() == BrowserCommonConstants.SHOW_DN )
195                 {
196                     label = sr.getEntry().getDn().toString();
197                 }
198                 else if ( preferences.getSearchResultLabel() == BrowserCommonConstants.SHOW_RDN )
199                 {
200                     label = sr.getEntry().getRdn().toString();
201                 }
202                 else if ( preferences.getSearchResultLabel() == BrowserCommonConstants.SHOW_RDN_VALUE )
203                 {
204                     label = sr.getEntry().getRdn().getValue();
205                 }
206
207                 if ( preferences.isSearchResultAbbreviate()
208                     && label.length() > preferences.getSearchResultAbbreviateMaxLength() )
209                 {
210                     label = Utils.shorten( label, preferences.getSearchResultAbbreviateMaxLength() );
211                 }
212
213                 return label;
214             }
215             else
216             {
217                 return sr.getEntry().getDn().toString();
218             }
219
220         }
221         else if ( obj instanceof BrowserCategory )
222         {
223             BrowserCategory category = ( BrowserCategory ) obj;
224             return category.getTitle();
225         }
226         else if ( obj != null )
227         {
228             return obj.toString();
229         }
230         else
231         {
232             return "";
233         }
234     }
235
236
237     /**
238      * {@inheritDoc}
239      */

240     public Image getImage( Object JavaDoc obj )
241     {
242         if ( obj instanceof IEntry )
243         {
244             IEntry entry = ( IEntry ) obj;
245             return getImageByRdn( entry );
246         }
247         else if ( obj instanceof BrowserEntryPage )
248         {
249             return PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_OBJ_FOLDER );
250         }
251         else if ( obj instanceof BrowserSearchResultPage )
252         {
253             return PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_OBJ_FOLDER );
254         }
255         else if ( obj instanceof ISearch )
256         {
257             ISearch search = ( ISearch ) obj;
258             if ( search.getConnection().isOpened() && search.getSearchResults() != null )
259             {
260                 return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_SEARCH );
261             }
262             else
263             {
264                 return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_SEARCH_UNPERFORMED );
265             }
266         }
267         else if ( obj instanceof IBookmark )
268         {
269             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_BOOKMARK );
270         }
271         else if ( obj instanceof ISearchResult )
272         {
273             ISearchResult sr = ( ISearchResult ) obj;
274             IEntry entry = sr.getEntry();
275             return getImageByRdn( entry );
276         }
277         else if ( obj instanceof BrowserCategory )
278         {
279             BrowserCategory category = ( BrowserCategory ) obj;
280             if ( category.getType() == BrowserCategory.TYPE_DIT )
281             {
282                 return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_DIT );
283             }
284             else if ( category.getType() == BrowserCategory.TYPE_SEARCHES )
285             {
286                 return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_SEARCHES );
287             }
288             else if ( category.getType() == BrowserCategory.TYPE_BOOKMARKS )
289             {
290                 return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_BOOKMARKS );
291             }
292             else
293             {
294                 return null;
295             }
296         }
297         else
298         {
299             // return
300
// Activator.getDefault().getImage("icons/sandglass.gif");
301
return null;
302         }
303     }
304
305
306     /**
307      * Gets the image depending on the RDN attribute
308      *
309      * @param entry the entry
310      * @return the image
311      */

312     private Image getImageByRdn( IEntry entry )
313     {
314         if ( entry instanceof IRootDSE )
315         {
316             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY_ROOT );
317         }
318         else if ( entry instanceof DirectoryMetadataEntry && ( ( DirectoryMetadataEntry ) entry ).isSchemaEntry() )
319         {
320             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_BROWSER_SCHEMABROWSEREDITOR );
321         }
322         else if ( entry.getDn().equals( entry.getConnection().getSchema().getDn() ) )
323         {
324             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_BROWSER_SCHEMABROWSEREDITOR );
325         }
326         else if ( preferences.isDerefAliasesAndReferralsWhileBrowsing() && entry.isAlias() )
327         {
328             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY_ALIAS );
329         }
330         else if ( preferences.isDerefAliasesAndReferralsWhileBrowsing() && entry.isReferral() )
331         {
332             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY_REF );
333         }
334         else if ( entry.isSubentry() )
335         {
336             return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_BROWSER_SCHEMABROWSEREDITOR );
337         }
338         else
339         {
340             RDN rdn = entry.getRdn();
341             RDNPart[] rdnParts = rdn.getParts();
342             for ( int i = 0; i < rdnParts.length; i++ )
343             {
344                 RDNPart part = rdnParts[i];
345                 if ( "cn".equals( part.getType() ) || "sn".equals( part.getType() ) || "uid".equals( part.getType() )
346                     || "userid".equals( part.getType() ) )
347                 {
348                     return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY_PERSON );
349                 }
350                 else if ( "ou".equals( part.getType() ) || "o".equals( part.getType() ) )
351                 {
352                     return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY_ORG );
353                 }
354                 else if ( "dc".equals( part.getType() ) || "c".equals( part.getType() ) || "l".equals( part.getType() ) )
355                 {
356                     return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY_DC );
357                 }
358             }
359         }
360
361         return BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_ENTRY );
362     }
363
364
365     // private Image getImageByObjectclass(IEntry entry) {
366
// IAttribute oc = entry.getAttribute(IAttribute.OBJECTCLASS_ATTRIBUTE);
367
// if(oc != null && oc.getStringValues() != null) {
368
// String[] ocValues = oc.getStringValues();
369
// Set ocSet = new HashSet();
370
// for(int i=0; i<ocValues.length; i++) {
371
// ocSet.add(ocValues[i].toUpperCase());
372
// }
373
//
374
// if(entry instanceof IRootDSE) {
375
// return
376
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY_ROOT);
377
// }
378
// else
379
// if(entry.getDn().equals(entry.getConnection().getSchema().getDn()))
380
// {
381
// return
382
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_BROWSER_SCHEMABROWSEREDITOR);
383
// }
384
// else if(ocSet.contains(ObjectClassDescription.OC_ALIAS.toUpperCase())
385
// || ocSet.contains(ObjectClassDescription.OC_REFERRAL.toUpperCase()))
386
// {
387
// return
388
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY_REF);
389
// }
390
// else
391
// if(ocSet.contains(ObjectClassDescription.OC_PERSON.toUpperCase())
392
// ||
393
// ocSet.contains(ObjectClassDescription.OC_ORGANIZATIONALPERSON.toUpperCase())
394
// ||
395
// ocSet.contains(ObjectClassDescription.OC_INETORGPERSON.toUpperCase())
396
// ||
397
// ocSet.contains(ObjectClassDescription.OC_RESIDENTIALPERSON.toUpperCase())
398
// ||
399
// ocSet.contains(ObjectClassDescription.OC_PILOTPERSON.toUpperCase())
400
// ||
401
// ocSet.contains(ObjectClassDescription.OC_NEWPILOTPERSON.toUpperCase())
402
// ||
403
// ocSet.contains(ObjectClassDescription.OC_ORGANIZATIONALROLE.toUpperCase())
404
// || ocSet.contains(ObjectClassDescription.OC_ACCOUNT.toUpperCase())) {
405
// return
406
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY_PERSON);
407
// }
408
// else
409
// if(ocSet.contains(ObjectClassDescription.OC_ORGANIZATION.toUpperCase())
410
// ||
411
// ocSet.contains(ObjectClassDescription.OC_ORGANIZATIONALUNIT.toUpperCase())
412
// ||
413
// ocSet.contains(ObjectClassDescription.OC_PILOTORGANIZATION.toUpperCase())
414
// || ocSet.contains(ObjectClassDescription.OC_DMD.toUpperCase())
415
// ||
416
// ocSet.contains(ObjectClassDescription.OC_APPLICATIONPROCESS.toUpperCase())
417
// ||
418
// ocSet.contains(ObjectClassDescription.OC_APPLICATIONENTITY.toUpperCase()))
419
// {
420
// return
421
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY_ORG);
422
// }
423
// else
424
// if(ocSet.contains(ObjectClassDescription.OC_COUNTRY.toUpperCase())
425
// || ocSet.contains(ObjectClassDescription.OC_LOCALITY.toUpperCase())
426
// || ocSet.contains(ObjectClassDescription.OC_DCOBJECT.toUpperCase())
427
// || ocSet.contains(ObjectClassDescription.OC_DOMAIN.toUpperCase())) {
428
// return
429
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY_DC);
430
// }
431
// else
432
// if(ocSet.contains(ObjectClassDescription.OC_GROUPOFNAMES.toUpperCase())
433
// ||
434
// ocSet.contains(ObjectClassDescription.OC_GROUPOFUNIQUENAMES.toUpperCase())
435
// ||
436
// ocSet.contains(ObjectClassDescription.OC_POSIXGROUP.toUpperCase())) {
437
// return
438
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY_GROUP);
439
// }
440
//
441
// }
442
//
443
// return
444
// Activator.getDefault().getImage(BrowserWidgetsConstants.IMG_ENTRY);
445
// }
446

447     /**
448      * {@inheritDoc}
449      */

450     public Font getFont( Object JavaDoc element )
451     {
452
453         IEntry entry = null;
454         if ( element instanceof IEntry )
455         {
456             entry = ( IEntry ) element;
457         }
458         else if ( element instanceof ISearchResult )
459         {
460             entry = ( ( ISearchResult ) element ).getEntry();
461         }
462
463         if ( entry != null )
464         {
465             if ( !entry.isConsistent() )
466             {
467                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
468                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_ERROR_FONT );
469                 return BrowserCommonActivator.getDefault().getFont( fontData );
470             }
471         }
472
473         return null;
474     }
475
476
477     /**
478      * {@inheritDoc}
479      */

480     public Color getForeground( Object JavaDoc element )
481     {
482
483         IEntry entry = null;
484         if ( element instanceof IEntry )
485         {
486             entry = ( IEntry ) element;
487         }
488         else if ( element instanceof ISearchResult )
489         {
490             entry = ( ( ISearchResult ) element ).getEntry();
491         }
492
493         if ( entry != null )
494         {
495             if ( !entry.isConsistent() )
496             {
497                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
498                     BrowserCommonConstants.PREFERENCE_ERROR_COLOR );
499                 return BrowserCommonActivator.getDefault().getColor( rgb );
500             }
501         }
502
503         return null;
504     }
505
506
507     /**
508      * {@inheritDoc}
509      */

510     public Color getBackground( Object JavaDoc element )
511     {
512         return null;
513     }
514
515 }
516
Popular Tags