RCP PackageExplorer里不能显示工程图片的问题解决

import java.net.URL;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.internal.ide.model.WorkbenchAdapterBuilder;
import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
import org.eclipse.ui.views.navigator.ResourceNavigator;
import org.osgi.framework.Bundle;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

		@Override
	public void postStartup() {
		// TODO Auto-generated method stub
		try {
			IWorkbenchPage activePage = this.getWorkbenchConfigurer().getWorkbench().getActiveWorkbenchWindow().getActivePage();
			IViewPart view = activePage.showView("org.eclipse.jdt.ui.PackageExplorer");
			PackageExplorerPart packageExplorer = ((PackageExplorerPart)view);
			packageExplorer.getTreeViewer().setLabelProvider(new PackageExplorerLabelProvider());
			
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		super.postStartup();
	}
	
	class PackageExplorerLabelProvider extends LabelProvider{
		@Override
		public Image getImage(Object element) {
			// TODO Auto-generated method stub
			Image image = null;
			String text = element.toString();
			System.out.println(text);
			final Bundle bundle = Platform.getBundle("pluginid");
			final URL url = bundle.getEntry("icons/full/eview16/projects.gif");
			final URL wfUrl = bundle.getEntry("icons/wfeditor.gif");
			
//			image = ImageDescriptor.createFromURL(url).createImage();
			if(text.startsWith("P/",0)){
				image = ImageDescriptor.createFromURL(url).createImage();
			}
			if(text.startsWith("L/",0)){
				image = ImageDescriptor.createFromURL(wfUrl).createImage();
			}
			return image;
		}
		@Override
		public String getText(Object element) {
			// TODO Auto-generated method stub
			String text = element.toString();
			text = text.substring(2,text.length());
			int pos = text.indexOf("/");
			if(pos == -1){
				return text;
			}else{
				if(text.endsWith(".project")){
					return "Your name";
				}else{
					text = text.substring(pos+1,text.length());
				}
			}
			return text;
		}
	}
	
	
}

 注意要在rcp插件的dependencies里加入org.eclipse.jdt.ui依赖才行,否则PackageExplorerPart packageExplorer = ((PackageExplorerPart)view);不能引用到。

你可能感兴趣的:(eclipse,UI,.net,ide,osgi)