1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
13 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.DecorationOverlayIcon;
23 import org.eclipse.jface.viewers.IDecoration;
24 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.ide.IDE;
29 import org.eclipse.jdt.core.ClasspathContainerInitializer;
30 import org.eclipse.jdt.core.IAccessRule;
31 import org.eclipse.jdt.core.IClasspathContainer;
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.core.JavaModelException;
36 import org.eclipse.jdt.internal.corext.util.Messages;
38 import org.eclipse.jdt.ui.ISharedImages;
39 import org.eclipse.jdt.ui.JavaElementImageDescriptor;
40 import org.eclipse.jdt.ui.JavaElementLabels;
41 import org.eclipse.jdt.ui.JavaUI;
42 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration;
43 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration.ClasspathAttributeAccess;
45 import org.eclipse.jdt.internal.ui.JavaPlugin;
46 import org.eclipse.jdt.internal.ui.JavaPluginImages;
47 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
48 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
49 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
50 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
52 public class CPListLabelProvider extends LabelProvider {
54 private String fNewLabel, fClassLabel, fMissing;
56 private ImageDescriptorRegistry fRegistry;
57 private ISharedImages fSharedImages;
59 private ImageDescriptor fProjectImage;
61 private ClasspathAttributeConfigurationDescriptors fAttributeDescriptors;
64 public CPListLabelProvider() {
65 fNewLabel= NewWizardMessages.CPListLabelProvider_new;
66 fClassLabel= NewWizardMessages.CPListLabelProvider_classcontainer;
67 fMissing= NewWizardMessages.CPListLabelProvider_missing;
68 fRegistry= JavaPlugin.getImageDescriptorRegistry();
70 fSharedImages= JavaUI.getSharedImages();
72 IWorkbench workbench= JavaPlugin.getDefault().getWorkbench();
74 fProjectImage= workbench.getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT);
75 fAttributeDescriptors= JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors();
79 public String getText(Object element) {
80 if (element instanceof CPListElement) {
81 return getCPListElementText((CPListElement) element);
82 } else if (element instanceof CPListElementAttribute) {
83 CPListElementAttribute attribute= (CPListElementAttribute) element;
84 String text= getCPListElementAttributeText(attribute);
85 if (attribute.isNonModifiable()) {
86 return Messages.format(NewWizardMessages.CPListLabelProvider_non_modifiable_attribute, text);
89 } else if (element instanceof CPUserLibraryElement) {
90 return getCPUserLibraryText((CPUserLibraryElement) element);
91 } else if (element instanceof IAccessRule) {
92 IAccessRule rule= (IAccessRule) element;
93 return Messages.format(NewWizardMessages.CPListLabelProvider_access_rules_label, new String[] { AccessRulesLabelProvider.getResolutionLabel(rule.getKind()), BasicElementLabels.getPathLabel(rule.getPattern(), false)});
95 return super.getText(element);
98 public String getCPUserLibraryText(CPUserLibraryElement element) {
99 String name= element.getName();
100 if (element.isSystemLibrary()) {
101 name= Messages.format(NewWizardMessages.CPListLabelProvider_systemlibrary, name);
106 public String getCPListElementAttributeText(CPListElementAttribute attrib) {
107 String notAvailable= NewWizardMessages.CPListLabelProvider_none;
108 String key= attrib.getKey();
109 if (key.equals(CPListElement.SOURCEATTACHMENT)) {
111 IPath path= (IPath) attrib.getValue();
112 if (path != null && !path.isEmpty()) {
113 if (attrib.getParent().getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
114 arg= getVariableString(path);
116 arg= getPathString(path, path.getDevice() != null);
121 return Messages.format(NewWizardMessages.CPListLabelProvider_source_attachment_label, new String[] { arg });
122 } else if (key.equals(CPListElement.OUTPUT)) {
124 IPath path= (IPath) attrib.getValue();
126 arg= BasicElementLabels.getPathLabel(path, false);
128 arg= NewWizardMessages.CPListLabelProvider_default_output_folder_label;
130 return Messages.format(NewWizardMessages.CPListLabelProvider_output_folder_label, new String[] { arg });
131 } else if (key.equals(CPListElement.EXCLUSION)) {
133 IPath[] patterns= (IPath[]) attrib.getValue();
134 if (patterns != null && patterns.length > 0) {
135 int patternsCount= 0;
136 StringBuffer buf= new StringBuffer();
137 for (int i= 0; i < patterns.length; i++) {
138 if (patterns[i].segmentCount() > 0) {
139 String pattern= BasicElementLabels.getPathLabel(patterns[i], false);
140 if (patternsCount > 0) {
141 buf.append(NewWizardMessages.CPListLabelProvider_exclusion_filter_separator);
147 if (patternsCount > 0) {
155 return Messages.format(NewWizardMessages.CPListLabelProvider_exclusion_filter_label, new String[] { arg });
156 } else if (key.equals(CPListElement.INCLUSION)) {
158 IPath[] patterns= (IPath[]) attrib.getValue();
159 if (patterns != null && patterns.length > 0) {
160 int patternsCount= 0;
161 StringBuffer buf= new StringBuffer();
162 for (int i= 0; i < patterns.length; i++) {
163 if (patterns[i].segmentCount() > 0) {
164 String pattern= BasicElementLabels.getPathLabel(patterns[i], false);
165 if (patternsCount > 0) {
166 buf.append(NewWizardMessages.CPListLabelProvider_inclusion_filter_separator);
172 if (patternsCount > 0) {
178 arg= NewWizardMessages.CPListLabelProvider_all;
180 return Messages.format(NewWizardMessages.CPListLabelProvider_inclusion_filter_label, new String[] { arg });
181 } else if (key.equals(CPListElement.ACCESSRULES)) {
182 IAccessRule[] rules= (IAccessRule[]) attrib.getValue();
183 int nRules= rules != null ? rules.length : 0;
185 int parentKind= attrib.getParent().getEntryKind();
186 if (parentKind == IClasspathEntry.CPE_PROJECT) {
187 Boolean combined= (Boolean) attrib.getParent().getAttribute(CPListElement.COMBINE_ACCESSRULES);
189 if (combined.booleanValue()) {
191 return NewWizardMessages.CPListLabelProvider_project_access_rules_combined_singular;
193 return Messages.format(NewWizardMessages.CPListLabelProvider_project_access_rules_combined_plural, String.valueOf(nRules));
197 return NewWizardMessages.CPListLabelProvider_project_access_rules_not_combined_singular;
199 return Messages.format(NewWizardMessages.CPListLabelProvider_project_access_rules_not_combined_plural, String.valueOf(nRules));
203 return NewWizardMessages.CPListLabelProvider_project_access_rules_no_rules;
205 } else if (parentKind == IClasspathEntry.CPE_CONTAINER) {
207 return Messages.format(NewWizardMessages.CPListLabelProvider_container_access_rules_plural, String.valueOf(nRules));
208 } else if (nRules == 1) {
209 return NewWizardMessages.CPListLabelProvider_container_access_rules_singular;
211 return NewWizardMessages.CPListLabelProvider_container_no_access_rules;
215 return Messages.format(NewWizardMessages.CPListLabelProvider_access_rules_enabled_plural, String.valueOf(nRules));
216 } else if (nRules == 1) {
217 return NewWizardMessages.CPListLabelProvider_access_rules_enabled_singular;
219 return NewWizardMessages.CPListLabelProvider_access_rules_disabled;
222 } else if (key.equals(CPListElement.IGNORE_OPTIONAL_PROBLEMS)) {
224 if ("true".equals(attrib.getValue())) { //$NON-NLS-1$
225 arg= NewWizardMessages.CPListLabelProvider_ignore_optional_problems_yes;
227 arg= NewWizardMessages.CPListLabelProvider_ignore_optional_problems_no;
229 return Messages.format(NewWizardMessages.CPListLabelProvider_ignore_optional_problems_label, arg);
231 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(key);
232 if (config != null) {
233 ClasspathAttributeAccess access= attrib.getClasspathAttributeAccess();
234 String nameLabel= config.getNameLabel(access);
235 String valueLabel= config.getValueLabel(access); // should be LTR marked
236 return Messages.format(NewWizardMessages.CPListLabelProvider_attribute_label, new String[] { nameLabel, valueLabel });
238 String arg= (String) attrib.getValue();
242 return Messages.format(NewWizardMessages.CPListLabelProvider_attribute_label, new String[] { key, arg });
246 public String getCPListElementText(CPListElement cpentry) {
247 IPath path= cpentry.getPath();
248 switch (cpentry.getEntryKind()) {
249 case IClasspathEntry.CPE_LIBRARY: {
250 IResource resource= cpentry.getResource();
251 if (resource instanceof IContainer) {
252 StringBuffer buf= new StringBuffer(BasicElementLabels.getPathLabel(path, false));
253 IPath linkTarget= cpentry.getLinkTarget();
254 if (linkTarget != null) {
255 buf.append(JavaElementLabels.CONCAT_STRING);
256 buf.append(BasicElementLabels.getPathLabel(linkTarget, true));
259 buf.append(fClassLabel);
260 if (!resource.exists()) {
262 if (cpentry.isMissing()) {
263 buf.append(fMissing);
265 buf.append(fNewLabel);
268 return buf.toString();
270 String label= getPathString(path, resource == null);
271 if (cpentry.isMissing()) {
272 label= label + ' ' + fMissing;
277 case IClasspathEntry.CPE_VARIABLE: {
278 String label= getVariableString(path);
279 if (cpentry.isMissing()) {
280 label= label + ' ' + fMissing;
284 case IClasspathEntry.CPE_PROJECT:
285 String label= path.lastSegment();
286 if (cpentry.isMissing()) {
287 label= label + ' ' + fMissing;
290 case IClasspathEntry.CPE_CONTAINER:
292 IClasspathContainer container= JavaCore.getClasspathContainer(path, cpentry.getJavaProject());
293 if (container != null) {
294 return container.getDescription();
296 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(path.segment(0));
297 if (initializer != null) {
298 String description= initializer.getDescription(path, cpentry.getJavaProject());
299 return Messages.format(NewWizardMessages.CPListLabelProvider_unbound_library, description);
301 } catch (JavaModelException e) {
304 return BasicElementLabels.getPathLabel(path, false);
305 case IClasspathEntry.CPE_SOURCE: {
306 String pathLabel= BasicElementLabels.getPathLabel(path, false);
307 StringBuffer buf= new StringBuffer(pathLabel);
308 IPath linkTarget= cpentry.getLinkTarget();
309 if (linkTarget != null) {
310 buf.append(JavaElementLabels.CONCAT_STRING);
311 buf.append(BasicElementLabels.getPathLabel(linkTarget, true));
313 IResource resource= cpentry.getResource();
314 if (resource != null && !resource.exists()) {
316 if (cpentry.isMissing()) {
317 buf.append(fMissing);
319 buf.append(fNewLabel);
321 } else if (cpentry.getOrginalPath() == null) {
323 buf.append(fNewLabel);
325 return buf.toString();
330 return NewWizardMessages.CPListLabelProvider_unknown_element_label;
333 private String getPathString(IPath path, boolean isExternal) {
334 if (ArchiveFileFilter.isArchivePath(path, true)) {
335 String appended= BasicElementLabels.getPathLabel(path.removeLastSegments(1), isExternal);
336 String lastSegment= BasicElementLabels.getResourceName(path.lastSegment());
337 return Messages.format(NewWizardMessages.CPListLabelProvider_twopart, new String[] { lastSegment, appended });
339 return BasicElementLabels.getPathLabel(path, isExternal);
343 private String getVariableString(IPath path) {
344 String name= BasicElementLabels.getPathLabel(path, false);
345 IPath entryPath= JavaCore.getClasspathVariable(path.segment(0));
346 if (entryPath != null) {
347 String appended= BasicElementLabels.getPathLabel(entryPath.append(path.removeFirstSegments(1)), true);
348 return Messages.format(NewWizardMessages.CPListLabelProvider_twopart, new String[] { name, appended });
354 private ImageDescriptor getCPListElementBaseImage(CPListElement cpentry) {
355 switch (cpentry.getEntryKind()) {
356 case IClasspathEntry.CPE_SOURCE:
357 if (cpentry.getPath().segmentCount() == 1) {
358 return fProjectImage;
360 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
362 case IClasspathEntry.CPE_LIBRARY:
363 IResource res= cpentry.getResource();
364 IPath path= (IPath) cpentry.getAttribute(CPListElement.SOURCEATTACHMENT);
366 if (ArchiveFileFilter.isArchivePath(cpentry.getPath(), true)) {
367 if (path == null || path.isEmpty()) {
368 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
370 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE);
373 if (path == null || path.isEmpty()) {
374 return JavaPluginImages.DESC_OBJS_CLASSFOLDER;
376 return JavaPluginImages.DESC_OBJS_CLASSFOLDER_WSRC;
379 } else if (res instanceof IFile) {
380 if (path == null || path.isEmpty()) {
381 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_JAR);
383 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_JAR_WITH_SOURCE);
386 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
388 case IClasspathEntry.CPE_PROJECT:
389 return fProjectImage;
390 case IClasspathEntry.CPE_VARIABLE:
391 ImageDescriptor variableImage= fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_CLASSPATH_VAR_ENTRY);
392 if (cpentry.isDeprecated()) {
393 return new JavaElementImageDescriptor(variableImage, JavaElementImageDescriptor.DEPRECATED, JavaElementImageProvider.SMALL_SIZE);
395 return variableImage;
396 case IClasspathEntry.CPE_CONTAINER:
397 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY);
404 public Image getImage(Object element) {
405 if (element instanceof CPListElement) {
406 CPListElement cpentry= (CPListElement) element;
407 ImageDescriptor imageDescriptor= getCPListElementBaseImage(cpentry);
408 if (imageDescriptor != null) {
409 if (cpentry.isMissing() || cpentry.hasMissingChildren()) {
410 imageDescriptor= new JavaElementImageDescriptor(imageDescriptor, JavaElementImageDescriptor.ERROR, JavaElementImageProvider.SMALL_SIZE);
412 return fRegistry.get(imageDescriptor);
414 } else if (element instanceof CPListElementAttribute) {
415 CPListElementAttribute attribute= (CPListElementAttribute) element;
416 String key= (attribute).getKey();
417 if (key.equals(CPListElement.SOURCEATTACHMENT)) {
418 return fRegistry.get(JavaPluginImages.DESC_OBJS_SOURCE_ATTACH_ATTRIB);
419 } else if (key.equals(CPListElement.OUTPUT)) {
420 return fRegistry.get(JavaPluginImages.DESC_OBJS_OUTPUT_FOLDER_ATTRIB);
421 } else if (key.equals(CPListElement.EXCLUSION)) {
422 return fRegistry.get(JavaPluginImages.DESC_OBJS_EXCLUSION_FILTER_ATTRIB);
423 } else if (key.equals(CPListElement.INCLUSION)) {
424 return fRegistry.get(JavaPluginImages.DESC_OBJS_INCLUSION_FILTER_ATTRIB);
425 } else if (key.equals(CPListElement.ACCESSRULES)) {
426 return fRegistry.get(JavaPluginImages.DESC_OBJS_ACCESSRULES_ATTRIB);
427 } else if (key.equals(CPListElement.IGNORE_OPTIONAL_PROBLEMS)) {
428 Image image= fRegistry.get(getCPListElementBaseImage(attribute.getParent()));
430 ImageDescriptor overlay= JavaPluginImages.DESC_OVR_IGNORE_OPTIONAL_PROBLEMS;
431 ImageDescriptor imageDescriptor= new DecorationOverlayIcon(image, overlay, IDecoration.BOTTOM_LEFT);
432 return fRegistry.get(imageDescriptor);
435 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(key);
436 if (config != null) {
437 return fRegistry.get(config.getImageDescriptor(attribute.getClasspathAttributeAccess()));
440 return fSharedImages.getImage(ISharedImages.IMG_OBJS_CLASSPATH_VAR_ENTRY);
441 } else if (element instanceof CPUserLibraryElement) {
442 if (((CPUserLibraryElement) element).hasMissingChildren()) {
443 ImageDescriptor descriptor= fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY);
444 if (descriptor != null) {
445 return fRegistry.get(new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.ERROR, JavaElementImageProvider.SMALL_SIZE));
448 return fSharedImages.getImage(ISharedImages.IMG_OBJS_LIBRARY);
449 } else if (element instanceof IAccessRule) {
450 IAccessRule rule= (IAccessRule) element;
451 return AccessRulesLabelProvider.getResolutionImage(rule.getKind());