1 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 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 70 public Image getImage(Object element) { 71 if (element instanceof LogicalPackage) { 72 LogicalPackage cp= (LogicalPackage) element; 73 return getLogicalPackageImage(cp); 74 } 75 return super.getImage(element); 76 } 77 78 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 } 104 return false; 105 } 106 107 110 public String getText(Object 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 122 public ColoredString getRichTextLabel(Object 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 getText(IPackageFragment fragment) { 130 if (isFlatView()) 131 return getFlatText(fragment); 132 else 133 return getHierarchicalText(fragment); 134 } 135 136 private String getText(LogicalPackage logicalPackage) { 137 IPackageFragment[] fragments= logicalPackage.getFragments(); 138 return getText(fragments[0]); 139 } 140 141 private String 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 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 calculateName(IPackageFragment fragment) { 161 162 String name= fragment.getElementName(); 163 if (name.indexOf(".") != -1) name= name.substring(name.lastIndexOf(".") + 1); return name; 166 167 } 168 } 169 | Popular Tags |