KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > CPListLabelProvider


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.wizards.buildpaths;
12
13 import org.eclipse.core.runtime.IPath;
14
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.swt.graphics.Image;
20
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.LabelProvider;
23
24 import org.eclipse.ui.IWorkbench;
25
26 import org.eclipse.ui.ide.IDE;
27
28 import org.eclipse.jdt.core.ClasspathContainerInitializer;
29 import org.eclipse.jdt.core.IAccessRule;
30 import org.eclipse.jdt.core.IClasspathContainer;
31 import org.eclipse.jdt.core.IClasspathEntry;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34
35 import org.eclipse.jdt.internal.corext.util.Messages;
36
37 import org.eclipse.jdt.ui.ISharedImages;
38 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
39 import org.eclipse.jdt.ui.JavaElementLabels;
40 import org.eclipse.jdt.ui.JavaUI;
41 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration;
42 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration.ClasspathAttributeAccess;
43
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.JavaPluginImages;
46 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
47 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
48 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
49
50 public class CPListLabelProvider extends LabelProvider {
51         
52     private String JavaDoc fNewLabel, fClassLabel, fCreateLabel;
53         
54     private ImageDescriptorRegistry fRegistry;
55     private ISharedImages fSharedImages;
56
57     private ImageDescriptor fProjectImage;
58     
59     private ClasspathAttributeConfigurationDescriptors fAttributeDescriptors;
60     
61     
62     public CPListLabelProvider() {
63         fNewLabel= NewWizardMessages.CPListLabelProvider_new;
64         fClassLabel= NewWizardMessages.CPListLabelProvider_classcontainer;
65         fCreateLabel= NewWizardMessages.CPListLabelProvider_willbecreated;
66         fRegistry= JavaPlugin.getImageDescriptorRegistry();
67     
68         fSharedImages= JavaUI.getSharedImages();
69
70         IWorkbench workbench= JavaPlugin.getDefault().getWorkbench();
71         
72         fProjectImage= workbench.getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT);
73         fAttributeDescriptors= JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors();
74     }
75     
76     public String JavaDoc getText(Object JavaDoc element) {
77         if (element instanceof CPListElement) {
78             return getCPListElementText((CPListElement) element);
79         } else if (element instanceof CPListElementAttribute) {
80             CPListElementAttribute attribute= (CPListElementAttribute) element;
81             String JavaDoc text= getCPListElementAttributeText(attribute);
82             if (attribute.isNonModifiable()) {
83                 return Messages.format(NewWizardMessages.CPListLabelProvider_non_modifiable_attribute, text);
84             }
85             return text;
86         } else if (element instanceof CPUserLibraryElement) {
87             return getCPUserLibraryText((CPUserLibraryElement) element);
88         } else if (element instanceof IAccessRule) {
89             IAccessRule rule= (IAccessRule) element;
90             return Messages.format(NewWizardMessages.CPListLabelProvider_access_rules_label, new String JavaDoc[] { AccessRulesLabelProvider.getResolutionLabel(rule.getKind()), rule.getPattern().toString()});
91         }
92         return super.getText(element);
93     }
94     
95     public String JavaDoc getCPUserLibraryText(CPUserLibraryElement element) {
96         String JavaDoc name= element.getName();
97         if (element.isSystemLibrary()) {
98             name= Messages.format(NewWizardMessages.CPListLabelProvider_systemlibrary, name);
99         }
100         return name;
101     }
102
103     public String JavaDoc getCPListElementAttributeText(CPListElementAttribute attrib) {
104         String JavaDoc notAvailable= NewWizardMessages.CPListLabelProvider_none;
105         String JavaDoc key= attrib.getKey();
106         if (key.equals(CPListElement.SOURCEATTACHMENT)) {
107             String JavaDoc arg;
108             IPath path= (IPath) attrib.getValue();
109             if (path != null && !path.isEmpty()) {
110                 if (attrib.getParent().getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
111                     arg= getVariableString(path);
112                 } else {
113                     arg= getPathString(path, path.getDevice() != null);
114                 }
115             } else {
116                 arg= notAvailable;
117             }
118             return Messages.format(NewWizardMessages.CPListLabelProvider_source_attachment_label, new String JavaDoc[] { arg });
119         } else if (key.equals(CPListElement.OUTPUT)) {
120             String JavaDoc arg= null;
121             IPath path= (IPath) attrib.getValue();
122             if (path != null) {
123                 arg= path.makeRelative().toString();
124             } else {
125                 arg= NewWizardMessages.CPListLabelProvider_default_output_folder_label;
126             }
127             return Messages.format(NewWizardMessages.CPListLabelProvider_output_folder_label, new String JavaDoc[] { arg });
128         } else if (key.equals(CPListElement.EXCLUSION)) {
129             String JavaDoc arg= null;
130             IPath[] patterns= (IPath[]) attrib.getValue();
131             if (patterns != null && patterns.length > 0) {
132                 int patternsCount= 0;
133                 StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
134                 for (int i= 0; i < patterns.length; i++) {
135                     String JavaDoc pattern= patterns[i].toString();
136                     if (pattern.length() > 0) {
137                         if (patternsCount > 0) {
138                             buf.append(NewWizardMessages.CPListLabelProvider_exclusion_filter_separator);
139                         }
140                         buf.append(pattern);
141                         patternsCount++;
142                     }
143                 }
144                 if (patternsCount > 0) {
145                     arg= buf.toString();
146                 } else {
147                     arg= notAvailable;
148                 }
149             } else {
150                 arg= notAvailable;
151             }
152             return Messages.format(NewWizardMessages.CPListLabelProvider_exclusion_filter_label, new String JavaDoc[] { arg });
153         } else if (key.equals(CPListElement.INCLUSION)) {
154             String JavaDoc arg= null;
155             IPath[] patterns= (IPath[]) attrib.getValue();
156             if (patterns != null && patterns.length > 0) {
157                 int patternsCount= 0;
158                 StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
159                 for (int i= 0; i < patterns.length; i++) {
160                     String JavaDoc pattern= patterns[i].toString();
161                     if (pattern.length() > 0) {
162                         if (patternsCount > 0) {
163                             buf.append(NewWizardMessages.CPListLabelProvider_inclusion_filter_separator);
164                         }
165                         buf.append(pattern);
166                         patternsCount++;
167                     }
168                 }
169                 if (patternsCount > 0) {
170                     arg= buf.toString();
171                 } else {
172                     arg= notAvailable;
173                 }
174             } else {
175                 arg= NewWizardMessages.CPListLabelProvider_all;
176             }
177             return Messages.format(NewWizardMessages.CPListLabelProvider_inclusion_filter_label, new String JavaDoc[] { arg });
178         } else if (key.equals(CPListElement.ACCESSRULES)) {
179             IAccessRule[] rules= (IAccessRule[]) attrib.getValue();
180             int nRules= rules != null ? rules.length : 0;
181             
182             int parentKind= attrib.getParent().getEntryKind();
183             if (parentKind == IClasspathEntry.CPE_PROJECT) {
184                 Boolean JavaDoc combined= (Boolean JavaDoc) attrib.getParent().getAttribute(CPListElement.COMBINE_ACCESSRULES);
185                 if (nRules > 0) {
186                     if (combined.booleanValue()) {
187                         return Messages.format(NewWizardMessages.CPListLabelProvider_project_access_rules_combined, String.valueOf(nRules));
188                     } else {
189                         return Messages.format(NewWizardMessages.CPListLabelProvider_project_access_rules_not_combined, String.valueOf(nRules));
190                     }
191                 } else {
192                     return NewWizardMessages.CPListLabelProvider_project_access_rules_no_rules;
193                 }
194             } else if (parentKind == IClasspathEntry.CPE_CONTAINER) {
195                 if (nRules > 0) {
196                     return Messages.format(NewWizardMessages.CPListLabelProvider_container_access_rules, String.valueOf(nRules));
197                 } else {
198                     return NewWizardMessages.CPListLabelProvider_container_no_access_rules;
199                 }
200             } else {
201                 if (nRules > 0) {
202                     return Messages.format(NewWizardMessages.CPListLabelProvider_access_rules_enabled, String.valueOf(nRules));
203                 } else {
204                     return NewWizardMessages.CPListLabelProvider_access_rules_disabled;
205                 }
206             }
207         } else {
208             ClasspathAttributeConfiguration config= fAttributeDescriptors.get(key);
209             if (config != null) {
210                 ClasspathAttributeAccess access= attrib.getClasspathAttributeAccess();
211                 String JavaDoc nameLabel= config.getNameLabel(access);
212                 String JavaDoc valueLabel= config.getValueLabel(access);
213                 return Messages.format(NewWizardMessages.CPListLabelProvider_attribute_label, new String JavaDoc[] { nameLabel, valueLabel });
214             }
215             String JavaDoc arg= (String JavaDoc) attrib.getValue();
216             if (arg == null) {
217                 arg= notAvailable;
218             }
219             return Messages.format(NewWizardMessages.CPListLabelProvider_attribute_label, new String JavaDoc[] { key, arg });
220         }
221     }
222     
223     public String JavaDoc getCPListElementText(CPListElement cpentry) {
224         IPath path= cpentry.getPath();
225         switch (cpentry.getEntryKind()) {
226             case IClasspathEntry.CPE_LIBRARY: {
227                 IResource resource= cpentry.getResource();
228                 if (resource instanceof IContainer) {
229                     StringBuffer JavaDoc buf= new StringBuffer JavaDoc(path.makeRelative().toString());
230                     IPath linkTarget= cpentry.getLinkTarget();
231                     if (linkTarget != null) {
232                         buf.append(JavaElementLabels.CONCAT_STRING);
233                         buf.append(linkTarget.toOSString());
234                     }
235                     buf.append(' ');
236                     buf.append(fClassLabel);
237                     if (!resource.exists()) {
238                         buf.append(' ');
239                         if (cpentry.isMissing()) {
240                             buf.append(fCreateLabel);
241                         } else {
242                             buf.append(fNewLabel);
243                         }
244                     }
245                     return buf.toString();
246                 } else if (ArchiveFileFilter.isArchivePath(path)) {
247                     return getPathString(path, resource == null);
248                 }
249                 // should not get here
250
return path.makeRelative().toString();
251             }
252             case IClasspathEntry.CPE_VARIABLE: {
253                 return getVariableString(path);
254             }
255             case IClasspathEntry.CPE_PROJECT:
256                 return path.lastSegment();
257             case IClasspathEntry.CPE_CONTAINER:
258                 try {
259                     IClasspathContainer container= JavaCore.getClasspathContainer(path, cpentry.getJavaProject());
260                     if (container != null) {
261                         return container.getDescription();
262                     }
263                     ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(path.segment(0));
264                     if (initializer != null) {
265                         String JavaDoc description= initializer.getDescription(path, cpentry.getJavaProject());
266                         return Messages.format(NewWizardMessages.CPListLabelProvider_unbound_library, description);
267                     }
268                 } catch (JavaModelException e) {
269     
270                 }
271                 return path.toString();
272             case IClasspathEntry.CPE_SOURCE: {
273                 StringBuffer JavaDoc buf= new StringBuffer JavaDoc(path.makeRelative().toString());
274                 IPath linkTarget= cpentry.getLinkTarget();
275                 if (linkTarget != null) {
276                     buf.append(JavaElementLabels.CONCAT_STRING);
277                     buf.append(linkTarget.toOSString());
278                 }
279                 IResource resource= cpentry.getResource();
280                 if (resource != null && !resource.exists()) {
281                     buf.append(' ');
282                     if (cpentry.isMissing()) {
283                         buf.append(fCreateLabel);
284                     } else {
285                         buf.append(fNewLabel);
286                     }
287                 } else if (cpentry.getOrginalPath() == null) {
288                     buf.append(' ');
289                     buf.append(fNewLabel);
290                 }
291                 return buf.toString();
292             }
293             default:
294                 // pass
295
}
296         return NewWizardMessages.CPListLabelProvider_unknown_element_label;
297     }
298     
299     private String JavaDoc getPathString(IPath path, boolean isExternal) {
300         if (ArchiveFileFilter.isArchivePath(path)) {
301             IPath appendedPath= path.removeLastSegments(1);
302             String JavaDoc appended= isExternal ? appendedPath.toOSString() : appendedPath.makeRelative().toString();
303             return Messages.format(NewWizardMessages.CPListLabelProvider_twopart, new String JavaDoc[] { path.lastSegment(), appended });
304         } else {
305             return isExternal ? path.toOSString() : path.makeRelative().toString();
306         }
307     }
308     
309     private String JavaDoc getVariableString(IPath path) {
310         String JavaDoc name= path.makeRelative().toString();
311         IPath entryPath= JavaCore.getClasspathVariable(path.segment(0));
312         if (entryPath != null) {
313             String JavaDoc appended= entryPath.append(path.removeFirstSegments(1)).toOSString();
314             return Messages.format(NewWizardMessages.CPListLabelProvider_twopart, new String JavaDoc[] { name, appended });
315         } else {
316             return name;
317         }
318     }
319     
320     private ImageDescriptor getCPListElementBaseImage(CPListElement cpentry) {
321         switch (cpentry.getEntryKind()) {
322             case IClasspathEntry.CPE_SOURCE:
323                 if (cpentry.getPath().segmentCount() == 1) {
324                     return fProjectImage;
325                 } else {
326                     return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
327                 }
328             case IClasspathEntry.CPE_LIBRARY:
329                 IResource res= cpentry.getResource();
330                 IPath path= (IPath) cpentry.getAttribute(CPListElement.SOURCEATTACHMENT);
331                 if (res == null) {
332                     if (path == null || path.isEmpty()) {
333                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
334                     } else {
335                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE);
336                     }
337                 } else if (res instanceof IFile) {
338                     if (path == null || path.isEmpty()) {
339                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_JAR);
340                     } else {
341                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_JAR_WITH_SOURCE);
342                     }
343                 } else {
344                     return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
345                 }
346             case IClasspathEntry.CPE_PROJECT:
347                 return fProjectImage;
348             case IClasspathEntry.CPE_VARIABLE:
349                 ImageDescriptor variableImage= fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_CLASSPATH_VAR_ENTRY);
350                 if (cpentry.isDeprecated()) {
351                     return new JavaElementImageDescriptor(variableImage, JavaElementImageDescriptor.DEPRECATED, JavaElementImageProvider.SMALL_SIZE);
352                 }
353                 return variableImage;
354             case IClasspathEntry.CPE_CONTAINER:
355                 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY);
356             default:
357                 return null;
358         }
359     }
360         
361     public Image getImage(Object JavaDoc element) {
362         if (element instanceof CPListElement) {
363             CPListElement cpentry= (CPListElement) element;
364             ImageDescriptor imageDescriptor= getCPListElementBaseImage(cpentry);
365             if (imageDescriptor != null) {
366                 if (cpentry.isMissing()) {
367                     imageDescriptor= new JavaElementImageDescriptor(imageDescriptor, JavaElementImageDescriptor.WARNING, JavaElementImageProvider.SMALL_SIZE);
368                 }
369                 return fRegistry.get(imageDescriptor);
370             }
371         } else if (element instanceof CPListElementAttribute) {
372             CPListElementAttribute attribute= (CPListElementAttribute) element;
373             String JavaDoc key= (attribute).getKey();
374             if (key.equals(CPListElement.SOURCEATTACHMENT)) {
375                 return fRegistry.get(JavaPluginImages.DESC_OBJS_SOURCE_ATTACH_ATTRIB);
376             } else if (key.equals(CPListElement.OUTPUT)) {
377                 return fRegistry.get(JavaPluginImages.DESC_OBJS_OUTPUT_FOLDER_ATTRIB);
378             } else if (key.equals(CPListElement.EXCLUSION)) {
379                 return fRegistry.get(JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB);
380             } else if (key.equals(CPListElement.INCLUSION)) {
381                 return fRegistry.get(JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB);
382             } else if (key.equals(CPListElement.ACCESSRULES)) {
383                 return fRegistry.get(JavaPluginImages.DESC_OBJS_ACCESSRULES_ATTRIB);
384             } else {
385                 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(key);
386                 if (config != null) {
387                     return fRegistry.get(config.getImageDescriptor(attribute.getClasspathAttributeAccess()));
388                 }
389             }
390             return fSharedImages.getImage(ISharedImages.IMG_OBJS_CLASSPATH_VAR_ENTRY);
391         } else if (element instanceof CPUserLibraryElement) {
392             return fSharedImages.getImage(ISharedImages.IMG_OBJS_LIBRARY);
393         } else if (element instanceof IAccessRule) {
394             IAccessRule rule= (IAccessRule) element;
395             return AccessRulesLabelProvider.getResolutionImage(rule.getKind());
396         }
397         return null;
398     }
399
400
401 }
402
Popular Tags