KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > NavigatorContentServiceDescriptionProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.navigator;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.viewers.ILabelProvider;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.ui.navigator.ICommonLabelProvider;
18 import org.eclipse.ui.navigator.IDescriptionProvider;
19
20 /**
21  *
22  * <p>
23  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
24  * part of a work in progress. There is a guarantee neither that this API will
25  * work nor that it will remain the same. Please do not use this API without
26  * consulting with the Platform/UI team.
27  * </p>
28  *
29  * @since 3.2
30  *
31  */

32 public final class NavigatorContentServiceDescriptionProvider implements
33         IDescriptionProvider {
34
35     private final NavigatorContentService contentService;
36
37     /**
38      * Creates a description provider that targets the given service.
39      *
40      * @param aContentService
41      * The content service associated with this provider.
42      */

43     public NavigatorContentServiceDescriptionProvider(
44             NavigatorContentService aContentService) {
45         Assert.isNotNull(aContentService);
46         contentService = aContentService;
47     }
48
49     public String JavaDoc getDescription(Object JavaDoc anElement) {
50
51         Object JavaDoc target;
52
53         if (anElement instanceof IStructuredSelection) {
54
55             IStructuredSelection structuredSelection = (IStructuredSelection) anElement;
56             if (structuredSelection.size() > 1) {
57                 return getDefaultStatusBarMessage(structuredSelection.size());
58             }
59             target = structuredSelection.getFirstElement();
60         } else {
61             target = anElement;
62         }
63         String JavaDoc message = null;
64         ILabelProvider[] providers = contentService
65                 .findRelevantLabelProviders(target);
66         if (providers.length == 0) {
67             return getDefaultStatusBarMessage(0);
68         }
69         for (int i = 0; i < providers.length && (message == null || message.length() == 0); i++) {
70             if (providers[i] instanceof ICommonLabelProvider) {
71                 message = ((ICommonLabelProvider) providers[i])
72                         .getDescription(target);
73             }
74         }
75         message = (message != null) ? message : getDefaultStatusBarMessage(1);
76         return message;
77
78     }
79
80     /**
81      * @param aSize
82      * The number of items selected.
83      * @return A string of the form "# items selected"
84      */

85     protected final String JavaDoc getDefaultStatusBarMessage(int aSize) {
86         return NLS.bind(
87                 CommonNavigatorMessages.Navigator_statusLineMultiSelect,
88                 new Object JavaDoc[] { new Integer JavaDoc(aSize) });
89
90     }
91
92 }
93
Popular Tags