KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > PackagesViewLabelProvider


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
12 package org.eclipse.jdt.internal.ui.browsing;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.core.resources.IResource;
17
18 import org.eclipse.swt.graphics.Image;
19
20 import org.eclipse.jface.resource.ImageDescriptor;
21
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 import org.eclipse.jdt.ui.JavaElementLabels;
26
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
28 import org.eclipse.jdt.internal.ui.JavaPluginImages;
29 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
30 import org.eclipse.jdt.internal.ui.viewsupport.ColoredString;
31 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
32 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
33 import org.eclipse.jdt.internal.ui.viewsupport.TreeHierarchyLayoutProblemsDecorator;
34
35 /**
36  * Label provider for the Packages view.
37  */

38 class PackagesViewLabelProvider extends AppearanceAwareLabelProvider {
39
40     static final int HIERARCHICAL_VIEW_STATE= 0;
41     static final int FLAT_VIEW_STATE= 1;
42
43     private int fViewState;
44
45     private ImageDescriptorRegistry fRegistry;
46     private TreeHierarchyLayoutProblemsDecorator fDecorator;
47
48     PackagesViewLabelProvider(int state) {
49         this(state, AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED, AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS | JavaElementImageProvider.SMALL_ICONS);
50     }
51
52     PackagesViewLabelProvider(int state, long textFlags, int imageFlags) {
53         super(textFlags, imageFlags);
54
55         Assert.isTrue(isValidState(state));
56         fViewState= state;
57         fRegistry= JavaPlugin.getImageDescriptorRegistry();
58
59         fDecorator= new TreeHierarchyLayoutProblemsDecorator(isFlatView());
60         addLabelDecorator(fDecorator);
61     }
62
63     private boolean isValidState(int state) {
64         return state == FLAT_VIEW_STATE || state == HIERARCHICAL_VIEW_STATE;
65     }
66
67     /*
68      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
69      */

70     public Image getImage(Object JavaDoc element) {
71         if (element instanceof LogicalPackage) {
72             LogicalPackage cp= (LogicalPackage) element;
73             return getLogicalPackageImage(cp);
74         }
75         return super.getImage(element);
76     }
77
78     /*
79      * Decoration is only concerned with error ticks
80      */

81     private Image getLogicalPackageImage(LogicalPackage cp) {
82         IPackageFragment[] fragments= cp.getFragments();
83         for (int i= 0; i < fragments.length; i++) {
84             IPackageFragment fragment= fragments[i];
85             if(!isEmpty(fragment)) {
86                 return decorateCompoundElement(JavaPluginImages.DESC_OBJS_LOGICAL_PACKAGE, cp);
87             }
88         }
89         return decorateCompoundElement(JavaPluginImages.DESC_OBJS_EMPTY_LOGICAL_PACKAGE, cp);
90     }
91
92
93     private Image decorateCompoundElement(ImageDescriptor imageDescriptor, LogicalPackage cp) {
94         Image image= fRegistry.get(imageDescriptor);
95         return decorateImage(image, cp);
96     }
97
98     private boolean isEmpty(IPackageFragment fragment) {
99         try {
100             return (fragment.getCompilationUnits().length == 0) && (fragment.getClassFiles().length == 0);
101         } catch (JavaModelException e) {
102             // ignore
103
}
104         return false;
105     }
106
107     /*
108      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
109      */

110     public String JavaDoc getText(Object JavaDoc element) {
111         if (element instanceof IPackageFragment)
112             return getText((IPackageFragment)element);
113         else if (element instanceof LogicalPackage)
114             return getText((LogicalPackage)element);
115         else
116             return super.getText(element);
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider#getRichTextLabel(java.lang.Object)
121      */

122     public ColoredString getRichTextLabel(Object JavaDoc element) {
123         if (element instanceof IPackageFragment || element instanceof LogicalPackage)
124             return new ColoredString(getText(element));
125         return super.getRichTextLabel(element);
126     }
127     
128
129     private String JavaDoc getText(IPackageFragment fragment) {
130         if (isFlatView())
131             return getFlatText(fragment);
132         else
133             return getHierarchicalText(fragment);
134     }
135
136     private String JavaDoc getText(LogicalPackage logicalPackage) {
137         IPackageFragment[] fragments= logicalPackage.getFragments();
138         return getText(fragments[0]);
139     }
140
141     private String JavaDoc getFlatText(IPackageFragment fragment) {
142         return super.getText(fragment);
143     }
144
145     private boolean isFlatView() {
146         return fViewState==FLAT_VIEW_STATE;
147     }
148
149     private String JavaDoc getHierarchicalText(IPackageFragment fragment) {
150         if (fragment.isDefaultPackage()) {
151             return super.getText(fragment);
152         }
153         IResource res= fragment.getResource();
154         if(res != null && !(res.getType() == IResource.FILE))
155             return decorateText(res.getName(), fragment);
156         else
157             return decorateText(calculateName(fragment), fragment);
158     }
159
160     private String JavaDoc calculateName(IPackageFragment fragment) {
161
162         String JavaDoc name= fragment.getElementName();
163         if (name.indexOf(".") != -1) //$NON-NLS-1$
164
name= name.substring(name.lastIndexOf(".") + 1); //$NON-NLS-1$
165
return name;
166
167     }
168 }
169
Popular Tags