1 11 package org.eclipse.jdt.internal.ui.packageview; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 44 public class PackageExplorerLabelProvider extends AppearanceAwareLabelProvider { 45 46 private PackageExplorerContentProvider fContentProvider; 47 private Map 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 65 public ColoredString getRichTextLabel(Object element) { 66 String 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 getSpecificText(Object element) { 74 if (!fIsFlatLayout && element instanceof IPackageFragment) { 75 IPackageFragment fragment = (IPackageFragment) element; 76 Object parent= fContentProvider.getHierarchicalPackageParent(fragment); 77 if (parent instanceof IPackageFragment) { 78 return getNameDelta((IPackageFragment) parent, fragment); 79 } else if (parent instanceof IFolder) { 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 getText(Object element) { 89 String text= getSpecificText(element); 90 if (text != null) { 91 return decorateText(text, element); 92 } 93 return super.getText(element); 94 } 95 96 private String getNameDelta(IPackageFragment parent, IPackageFragment fragment) { 97 String prefix= parent.getElementName() + '.'; 98 String fullName= fragment.getElementName(); 99 if (fullName.startsWith(prefix)) { 100 return fullName.substring(prefix.length()); 101 } 102 return fullName; 103 } 104 105 private String getNameDelta(IFolder parent, IPackageFragment fragment) { 106 IPath prefix= parent.getFullPath(); 107 IPath fullPath= fragment.getPath(); 108 if (prefix.isPrefixOf(fullPath)) { 109 StringBuffer buf= new StringBuffer (); 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 element) { 121 if (element instanceof IWorkingSet) { 122 ImageDescriptor image= ((IWorkingSet)element).getImageDescriptor(); 123 if (fWorkingSetImages == null) { 124 fWorkingSetImages= new HashMap (); 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 iter= fWorkingSetImages.values().iterator(); iter.hasNext();) { 145 ((Image)iter.next()).dispose(); 146 } 147 } 148 super.dispose(); 149 } 150 } 151 | Popular Tags |