KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > PackageExplorerLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.packageview;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IPath;
19
20 import org.eclipse.core.resources.IFolder;
21
22 import org.eclipse.swt.graphics.Image;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25
26 import org.eclipse.ui.IWorkingSet;
27
28 import org.eclipse.jdt.core.IPackageFragment;
29
30 import org.eclipse.jdt.ui.JavaElementLabels;
31
32 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
33 import org.eclipse.jdt.internal.ui.viewsupport.ColoredString;
34 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
35
36 /**
37  * Provides the labels for the Package Explorer.
38  * <p>
39  * It provides labels for the packages in hierarchical layout and in all
40  * other cases delegates it to its super class.
41  * </p>
42  * @since 2.1
43  */

44 public class PackageExplorerLabelProvider extends AppearanceAwareLabelProvider {
45     
46     private PackageExplorerContentProvider fContentProvider;
47     private Map JavaDoc fWorkingSetImages;
48     
49     private boolean fIsFlatLayout;
50     private PackageExplorerProblemsDecorator fProblemDecorator;
51
52     public PackageExplorerLabelProvider(PackageExplorerContentProvider cp) {
53         super(DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED | JavaElementLabels.ALL_CATEGORY, DEFAULT_IMAGEFLAGS | JavaElementImageProvider.SMALL_ICONS);
54         
55         fProblemDecorator= new PackageExplorerProblemsDecorator();
56         addLabelDecorator(fProblemDecorator);
57         Assert.isNotNull(cp);
58         fContentProvider= cp;
59         fWorkingSetImages= null;
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider#getRichTextLabel(java.lang.Object)
64      */

65     public ColoredString getRichTextLabel(Object JavaDoc element) {
66         String JavaDoc text= getSpecificText(element);
67         if (text != null) {
68             return new ColoredString(decorateText(text, element));
69         }
70         return super.getRichTextLabel(element);
71     }
72     
73     private String JavaDoc getSpecificText(Object JavaDoc element) {
74         if (!fIsFlatLayout && element instanceof IPackageFragment) {
75             IPackageFragment fragment = (IPackageFragment) element;
76             Object JavaDoc parent= fContentProvider.getHierarchicalPackageParent(fragment);
77             if (parent instanceof IPackageFragment) {
78                 return getNameDelta((IPackageFragment) parent, fragment);
79             } else if (parent instanceof IFolder) { // bug 152735
80
return getNameDelta((IFolder) parent, fragment);
81             }
82         } else if (element instanceof IWorkingSet) {
83             return ((IWorkingSet) element).getLabel();
84         }
85         return null;
86     }
87     
88     public String JavaDoc getText(Object JavaDoc element) {
89         String JavaDoc text= getSpecificText(element);
90         if (text != null) {
91             return decorateText(text, element);
92         }
93         return super.getText(element);
94     }
95     
96     private String JavaDoc getNameDelta(IPackageFragment parent, IPackageFragment fragment) {
97         String JavaDoc prefix= parent.getElementName() + '.';
98         String JavaDoc fullName= fragment.getElementName();
99         if (fullName.startsWith(prefix)) {
100             return fullName.substring(prefix.length());
101         }
102         return fullName;
103     }
104     
105     private String JavaDoc getNameDelta(IFolder parent, IPackageFragment fragment) {
106         IPath prefix= parent.getFullPath();
107         IPath fullPath= fragment.getPath();
108         if (prefix.isPrefixOf(fullPath)) {
109             StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
110             for (int i= prefix.segmentCount(); i < fullPath.segmentCount(); i++) {
111                 if (buf.length() > 0)
112                     buf.append('.');
113                 buf.append(fullPath.segment(i));
114             }
115             return buf.toString();
116         }
117         return fragment.getElementName();
118     }
119     
120     public Image getImage(Object JavaDoc element) {
121         if (element instanceof IWorkingSet) {
122             ImageDescriptor image= ((IWorkingSet)element).getImageDescriptor();
123             if (fWorkingSetImages == null) {
124                 fWorkingSetImages= new HashMap JavaDoc();
125             }
126                 
127             Image result= (Image) fWorkingSetImages.get(image);
128             if (result == null) {
129                 result= image.createImage();
130                 fWorkingSetImages.put(image, result);
131             }
132             return decorateImage(result, element);
133         }
134         return super.getImage(element);
135     }
136     
137     public void setIsFlatLayout(boolean state) {
138         fIsFlatLayout= state;
139         fProblemDecorator.setIsFlatLayout(state);
140     }
141     
142     public void dispose() {
143         if (fWorkingSetImages != null) {
144             for (Iterator JavaDoc iter= fWorkingSetImages.values().iterator(); iter.hasNext();) {
145                 ((Image)iter.next()).dispose();
146             }
147         }
148         super.dispose();
149     }
150 }
151
Popular Tags