]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/CPListLabelProvider.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / CPListLabelProvider.java
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
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.swt.graphics.Image;
14
15 import org.eclipse.core.runtime.IPath;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20
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;
25
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.ide.IDE;
28
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;
35
36 import org.eclipse.jdt.internal.corext.util.Messages;
37
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;
44
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;
51
52 public class CPListLabelProvider extends LabelProvider {
53
54         private String fNewLabel, fClassLabel, fMissing;
55
56         private ImageDescriptorRegistry fRegistry;
57         private ISharedImages fSharedImages;
58
59         private ImageDescriptor fProjectImage;
60
61         private ClasspathAttributeConfigurationDescriptors fAttributeDescriptors;
62
63
64         public CPListLabelProvider() {
65                 fNewLabel= NewWizardMessages.CPListLabelProvider_new;
66                 fClassLabel= NewWizardMessages.CPListLabelProvider_classcontainer;
67                 fMissing= NewWizardMessages.CPListLabelProvider_missing;
68                 fRegistry= JavaPlugin.getImageDescriptorRegistry();
69
70                 fSharedImages= JavaUI.getSharedImages();
71
72                 IWorkbench workbench= JavaPlugin.getDefault().getWorkbench();
73
74                 fProjectImage= workbench.getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT);
75                 fAttributeDescriptors= JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors();
76         }
77
78         @Override
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);
87                         }
88                         return 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)});
94                 }
95                 return super.getText(element);
96         }
97
98         public String getCPUserLibraryText(CPUserLibraryElement element) {
99                 String name= element.getName();
100                 if (element.isSystemLibrary()) {
101                         name= Messages.format(NewWizardMessages.CPListLabelProvider_systemlibrary, name);
102                 }
103                 return name;
104         }
105
106         public String getCPListElementAttributeText(CPListElementAttribute attrib) {
107                 String notAvailable= NewWizardMessages.CPListLabelProvider_none;
108                 String key= attrib.getKey();
109                 if (key.equals(CPListElement.SOURCEATTACHMENT)) {
110                         String arg;
111                         IPath path= (IPath) attrib.getValue();
112                         if (path != null && !path.isEmpty()) {
113                                 if (attrib.getParent().getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
114                                         arg= getVariableString(path);
115                                 } else {
116                                         arg= getPathString(path, path.getDevice() != null);
117                                 }
118                         } else {
119                                 arg= notAvailable;
120                         }
121                         return Messages.format(NewWizardMessages.CPListLabelProvider_source_attachment_label, new String[] { arg });
122                 } else if (key.equals(CPListElement.OUTPUT)) {
123                         String arg= null;
124                         IPath path= (IPath) attrib.getValue();
125                         if (path != null) {
126                                 arg= BasicElementLabels.getPathLabel(path, false);
127                         } else {
128                                 arg= NewWizardMessages.CPListLabelProvider_default_output_folder_label;
129                         }
130                         return Messages.format(NewWizardMessages.CPListLabelProvider_output_folder_label, new String[] { arg });
131                 } else if (key.equals(CPListElement.EXCLUSION)) {
132                         String arg= null;
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);
142                                                 }
143                                                 buf.append(pattern);
144                                                 patternsCount++;
145                                         }
146                                 }
147                                 if (patternsCount > 0) {
148                                         arg= buf.toString();
149                                 } else {
150                                         arg= notAvailable;
151                                 }
152                         } else {
153                                 arg= notAvailable;
154                         }
155                         return Messages.format(NewWizardMessages.CPListLabelProvider_exclusion_filter_label, new String[] { arg });
156                 } else if (key.equals(CPListElement.INCLUSION)) {
157                         String arg= null;
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);
167                                                 }
168                                                 buf.append(pattern);
169                                                 patternsCount++;
170                                         }
171                                 }
172                                 if (patternsCount > 0) {
173                                         arg= buf.toString();
174                                 } else {
175                                         arg= notAvailable;
176                                 }
177                         } else {
178                                 arg= NewWizardMessages.CPListLabelProvider_all;
179                         }
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;
184
185                         int parentKind= attrib.getParent().getEntryKind();
186                         if (parentKind == IClasspathEntry.CPE_PROJECT) {
187                                 Boolean combined= (Boolean) attrib.getParent().getAttribute(CPListElement.COMBINE_ACCESSRULES);
188                                 if (nRules > 0) {
189                                         if (combined.booleanValue()) {
190                                                 if (nRules == 1) {
191                                                         return NewWizardMessages.CPListLabelProvider_project_access_rules_combined_singular;
192                                                 } else {
193                                                         return Messages.format(NewWizardMessages.CPListLabelProvider_project_access_rules_combined_plural, String.valueOf(nRules));
194                                                 }
195                                         } else {
196                                                 if (nRules == 1) {
197                                                         return NewWizardMessages.CPListLabelProvider_project_access_rules_not_combined_singular;
198                                                 } else {
199                                                         return Messages.format(NewWizardMessages.CPListLabelProvider_project_access_rules_not_combined_plural, String.valueOf(nRules));
200                                                 }
201                                         }
202                                 } else {
203                                         return NewWizardMessages.CPListLabelProvider_project_access_rules_no_rules;
204                                 }
205                         } else if (parentKind == IClasspathEntry.CPE_CONTAINER) {
206                                 if (nRules > 1) {
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;
210                                 } else {
211                                         return NewWizardMessages.CPListLabelProvider_container_no_access_rules;
212                                 }
213                         } else {
214                                 if (nRules > 1) {
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;
218                                 } else {
219                                         return NewWizardMessages.CPListLabelProvider_access_rules_disabled;
220                                 }
221                         }
222                 } else if (key.equals(CPListElement.IGNORE_OPTIONAL_PROBLEMS)) {
223                         String arg;
224                         if ("true".equals(attrib.getValue())) { //$NON-NLS-1$
225                                 arg= NewWizardMessages.CPListLabelProvider_ignore_optional_problems_yes;
226                         } else {
227                                 arg= NewWizardMessages.CPListLabelProvider_ignore_optional_problems_no;
228                         }
229                         return Messages.format(NewWizardMessages.CPListLabelProvider_ignore_optional_problems_label, arg);
230                 } else {
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 });
237                         }
238                         String arg= (String) attrib.getValue();
239                         if (arg == null) {
240                                 arg= notAvailable;
241                         }
242                         return Messages.format(NewWizardMessages.CPListLabelProvider_attribute_label, new String[] { key, arg });
243                 }
244         }
245
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));
257                                         }
258                                         buf.append(' ');
259                                         buf.append(fClassLabel);
260                                         if (!resource.exists()) {
261                                                 buf.append(' ');
262                                                 if (cpentry.isMissing()) {
263                                                         buf.append(fMissing);
264                                                 } else {
265                                                         buf.append(fNewLabel);
266                                                 }
267                                         }
268                                         return buf.toString();
269                                 } else {
270                                         String label= getPathString(path, resource == null);
271                                         if (cpentry.isMissing()) {
272                                                 label= label + ' ' + fMissing;
273                                         }
274                                         return label;
275                                 }
276                         }
277                         case IClasspathEntry.CPE_VARIABLE: {
278                                 String label= getVariableString(path);
279                                 if (cpentry.isMissing()) {
280                                         label= label + ' ' + fMissing;
281                                 }
282                                 return label;
283                         }
284                         case IClasspathEntry.CPE_PROJECT:
285                                 String label= path.lastSegment();
286                                 if (cpentry.isMissing()) {
287                                         label= label + ' ' + fMissing;
288                                 }
289                                 return label;
290                         case IClasspathEntry.CPE_CONTAINER:
291                                 try {
292                                         IClasspathContainer container= JavaCore.getClasspathContainer(path, cpentry.getJavaProject());
293                                         if (container != null) {
294                                                 return container.getDescription();
295                                         }
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);
300                                         }
301                                 } catch (JavaModelException e) {
302
303                                 }
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));
312                                 }
313                                 IResource resource= cpentry.getResource();
314                                 if (resource != null && !resource.exists()) {
315                                         buf.append(' ');
316                                         if (cpentry.isMissing()) {
317                                                 buf.append(fMissing);
318                                         } else {
319                                                 buf.append(fNewLabel);
320                                         }
321                                 } else if (cpentry.getOrginalPath() == null) {
322                                         buf.append(' ');
323                                         buf.append(fNewLabel);
324                                 }
325                                 return buf.toString();
326                         }
327                         default:
328                                 // pass
329                 }
330                 return NewWizardMessages.CPListLabelProvider_unknown_element_label;
331         }
332
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 });
338                 } else {
339                         return BasicElementLabels.getPathLabel(path, isExternal);
340                 }
341         }
342
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 });
349                 } else {
350                         return name;
351                 }
352         }
353
354         private ImageDescriptor getCPListElementBaseImage(CPListElement cpentry) {
355                 switch (cpentry.getEntryKind()) {
356                         case IClasspathEntry.CPE_SOURCE:
357                                 if (cpentry.getPath().segmentCount() == 1) {
358                                         return fProjectImage;
359                                 } else {
360                                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
361                                 }
362                         case IClasspathEntry.CPE_LIBRARY:
363                                 IResource res= cpentry.getResource();
364                                 IPath path= (IPath) cpentry.getAttribute(CPListElement.SOURCEATTACHMENT);
365                                 if (res == null) {
366                                         if (ArchiveFileFilter.isArchivePath(cpentry.getPath(), true)) {
367                                                 if (path == null || path.isEmpty()) {
368                                                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
369                                                 } else {
370                                                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE);
371                                                 }
372                                         } else {
373                                                 if (path == null || path.isEmpty()) {
374                                                         return JavaPluginImages.DESC_OBJS_CLASSFOLDER;
375                                                 } else {
376                                                         return JavaPluginImages.DESC_OBJS_CLASSFOLDER_WSRC;
377                                                 }
378                                         }
379                                 } else if (res instanceof IFile) {
380                                         if (path == null || path.isEmpty()) {
381                                                 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_JAR);
382                                         } else {
383                                                 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_JAR_WITH_SOURCE);
384                                         }
385                                 } else {
386                                         return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_PACKFRAG_ROOT);
387                                 }
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);
394                                 }
395                                 return variableImage;
396                         case IClasspathEntry.CPE_CONTAINER:
397                                 return fSharedImages.getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY);
398                         default:
399                                 return null;
400                 }
401         }
402
403         @Override
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);
411                                 }
412                                 return fRegistry.get(imageDescriptor);
413                         }
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()));
429                                 if (image != null) {
430                                         ImageDescriptor overlay= JavaPluginImages.DESC_OVR_IGNORE_OPTIONAL_PROBLEMS;
431                                         ImageDescriptor imageDescriptor= new DecorationOverlayIcon(image, overlay, IDecoration.BOTTOM_LEFT);
432                                         return fRegistry.get(imageDescriptor);
433                                 }
434                         } else {
435                                 ClasspathAttributeConfiguration config= fAttributeDescriptors.get(key);
436                                 if (config != null) {
437                                         return fRegistry.get(config.getImageDescriptor(attribute.getClasspathAttributeAccess()));
438                                 }
439                         }
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));
446                                 }
447                         }
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());
452                 }
453                 return null;
454         }
455 }