这几天因为北京的项目开展,平台加入了java开发模式,因此工具开始需要支持java工程,但是发现在工具(rcp开发的eclipse开发工具)不能正常显示图标;前边开发的是普通工程没有显示不表是正常的,但是现在没有显示图标就很怪异,因为本身会有一个小J在工程名前边,标示是java工程;但小J左下角正常情况下应该是工程图标的地方,却显示是一个小红方块,很明显是没有找到相应的文件嘛!于是查找了网上的解决方案,这里做一下收集:
用到的连接地址:
http://zhaozhongwei.iteye.com/blog/305824
http://www.blogjava.net/USTCEric/archive/2006/01/08/27141.html
第一篇:
在rcp中(我的环境是eclipse3.4),有的时候需要建立工程,但是默认情况下建立的工程的图片显示不出来(包视图里出现这种情况,别的视图不清楚),这里拿新建java工程举例。
在获得图片的时候,程序会调用到 org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider类的 getBaseImageDescriptor方法,在288行(版本不同会有细微差别)有类似如下代码。
case IJavaElement.JAVA_PROJECT:
IJavaProject jp= (IJavaProject)element;
if (jp.getProject().isOpen()) {
IProject project= jp.getProject();
IWorkbenchAdapter adapter= (IWorkbenchAdapter)project.getAdapter(IWorkbenchAdapter.class);
if (adapter != null) {
ImageDescriptor result= adapter.getImageDescriptor(project);
if (result != null)
return result;
}
return DESC_OBJ_PROJECT;
}
。。。
如果跟到这里,会发现adapter 的值为null,而正常情况下这个adapter数有值的。
在rcp中生成的ApplicationWorkbenchAdvisor的preWindowOpen方法里面会生成
WorkbenchAdapterBuilder.registerAdapters();
但是我用debug发现在eclipse3.4里面,preWindowOpen方法没被调用,导致 WorkbenchAdapterBuilder.registerAdapters();没被执行,从而导致adapter 的值为null。这样工程的前面没有图片,会出现一小块空白!把 WorkbenchAdapterBuilder.registerAdapters();加到ApplicationWorkbenchAdvisor 类的initialize方法里会解决这个问题,默认不生成initialize方法,所以要重载该方法,如下
@Override
public void initialize(IWorkbenchConfigurer configurer) {
// TODO Auto-generated method stub
super.initialize(configurer);
WorkbenchAdapterBuilder.registerAdapters();
}
经过这样的修改后会看到,工程的前面是个红色的图片(没找到对应图片的标志),在这个红图片的右上角有一个J(代表是java工程),这是因为 eclipse调用的是org.eclipse.ui.internal.ide.application.IDEApplication(在 org.eclipse.ui.ide.application插件下),而我们的rcp会调用我们的Application
IDEApplication的start方法代码中有如下代码片段
int returnCode = PlatformUI.createAndRunWorkbench(display,
new IDEWorkbenchAdvisor());
IDEWorkbenchAdvisor类中的initialize方法会调用
// register shared images
declareWorkbenchImages();
而declareWorkbenchImages个方法就是工程等图片被注册的地方!所以我们要做的就是把 declareWorkbenchImages这个方法里的代码全部复制到我们的ApplicationWorkbenchAdvisor类里,然后在 initialize方法里调用就可以了,因为图片都是在org.eclipse.ui.ide.application插件下,而代码也用到了这个插 件,所以要在依赖里家上这个插件。
第二篇:
自己遇到了这方面问题,在瀚海星云上咨询了一下,解决后总结了一下。现在收录到自己的空间里:)
前段时间为了使用jdt中的package explorer进行一个rcp程序的开发。在版面上进行了
提问,得到了simon lei,pandawang 的热情帮助,在此表示感谢。经过一番学习,在几个论
坛上问了一下,当时仍没有找到解决方法(主要是使用package explorer时项目和文件
夹的显示问题),又看到rcp的faq中有reusable 的说法就得出不能在rcp中复用jdt组件
的结论。实际上这个结论是错误的,只要提供相关的插件支持是可以重用jdt中组件的,
我再一次重复这个问题是为了避免我那个回帖误导别人。
我在rcp的perspective中用addview加入package explorer是没有问题的,之所以不能显
示项目名称是因为没有注册相关的adapter。
只要在workbenchadvisor中的initialize方法中加入:
WorkbenchAdapterBuilder.registerAdapters();
declareWorkbenchImages();
就可以解决问题。WorkbenchAdapterBuilder是org.eclipse.ui.ide中的类,用
registerAdapters方法注册adapter后就可以正常显示项目名称以及文件夹名称,但是这
时候还不能显示相关的图标。
declareWorkbenchImages()方法用来解决图标的问题。
实现如下:
/** * Declares all IDE-specific workbench images. This includes both "shared" * images (named in {@link IDE.SharedImages}) and internal images (named in * {@link org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages}). * * @see IWorkbenchConfigurer#declareImage */ private void declareWorkbenchImages() { final String ICONS_PATH = "$nl$/icons/full/";//$NON-NLS-1$ final String PATH_ELOCALTOOL = ICONS_PATH + "elcl16/"; // Enabled //$NON-NLS-1$ // toolbar // icons. final String PATH_DLOCALTOOL = ICONS_PATH + "dlcl16/"; // Disabled //$NON-NLS-1$ // //$NON-NLS-1$ // toolbar // icons. final String PATH_ETOOL = ICONS_PATH + "etool16/"; // Enabled toolbar //$NON-NLS-1$ // //$NON-NLS-1$ // icons. final String PATH_DTOOL = ICONS_PATH + "dtool16/"; // Disabled toolbar //$NON-NLS-1$ // //$NON-NLS-1$ // icons. final String PATH_OBJECT = ICONS_PATH + "obj16/"; // Model object //$NON-NLS-1$ // //$NON-NLS-1$ // icons final String PATH_WIZBAN = ICONS_PATH + "wizban/"; // Wizard //$NON-NLS-1$ // //$NON-NLS-1$ // icons // View icons final String PATH_EVIEW= ICONS_PATH + "eview16/"; //$NON-NLS-1$ Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH); declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC, PATH_ETOOL + "build_exec.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC_HOVER, PATH_ETOOL + "build_exec.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC_DISABLED, PATH_DTOOL + "build_exec.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_SEARCH_SRC, PATH_ETOOL + "search_src.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_SEARCH_SRC_HOVER, PATH_ETOOL + "search_src.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_SEARCH_SRC_DISABLED, PATH_DTOOL + "search_src.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_NEXT_NAV, PATH_ETOOL + "next_nav.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_PREVIOUS_NAV, PATH_ETOOL + "prev_nav.gif", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_NEWPRJ_WIZ, PATH_WIZBAN + "newprj_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_NEWFOLDER_WIZ, PATH_WIZBAN + "newfolder_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_NEWFILE_WIZ, PATH_WIZBAN + "newfile_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_IMPORTDIR_WIZ, PATH_WIZBAN + "importdir_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_IMPORTZIP_WIZ, PATH_WIZBAN + "importzip_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_EXPORTDIR_WIZ, PATH_WIZBAN + "exportdir_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_EXPORTZIP_WIZ, PATH_WIZBAN + "exportzip_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_WIZBAN_RESOURCEWORKINGSET_WIZ, PATH_WIZBAN + "workset_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_DLGBAN_SAVEAS_DLG, PATH_WIZBAN + "saveas_wiz.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_DLGBAN_QUICKFIX_DLG, PATH_WIZBAN + "quick_fix.png", false); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJ_PROJECT, PATH_OBJECT + "prj_obj.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED, PATH_OBJECT + "cprj_obj.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OPEN_MARKER, PATH_ELOCALTOOL + "gotoobj_tsk.gif", true); //$NON-NLS-1$ // Quick fix icons declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ELCL_QUICK_FIX_ENABLED, PATH_ELOCALTOOL + "smartmode_co.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_DLCL_QUICK_FIX_DISABLED, PATH_DLOCALTOOL + "smartmode_co.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_FIXABLE_WARNING, PATH_OBJECT + "quickfix_warning_obj.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_FIXABLE_ERROR, PATH_OBJECT + "quickfix_error_obj.gif", true); //$NON-NLS-1$ // task objects // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_HPRIO_TSK, // PATH_OBJECT+"hprio_tsk.gif"); // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_MPRIO_TSK, // PATH_OBJECT+"mprio_tsk.gif"); // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_LPRIO_TSK, // PATH_OBJECT+"lprio_tsk.gif"); declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJS_TASK_TSK, PATH_OBJECT + "taskmrk_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJS_BKMRK_TSK, PATH_OBJECT + "bkmrk_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_COMPLETE_TSK, PATH_OBJECT + "complete_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_INCOMPLETE_TSK, PATH_OBJECT + "incomplete_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_WELCOME_ITEM, PATH_OBJECT + "welcome_item.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_WELCOME_BANNER, PATH_OBJECT + "welcome_banner.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_ERROR_PATH, PATH_OBJECT + "error_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_WARNING_PATH, PATH_OBJECT + "warn_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_OBJS_INFO_PATH, PATH_OBJECT + "info_tsk.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_LCL_FLAT_LAYOUT, PATH_ELOCALTOOL + "flatLayout.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_LCL_HIERARCHICAL_LAYOUT, PATH_ELOCALTOOL + "hierarchicalLayout.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_PROBLEM_CATEGORY, PATH_ETOOL + "problem_category.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_PROBLEMS_VIEW, PATH_EVIEW + "problems_view.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_PROBLEMS_VIEW_ERROR, PATH_EVIEW + "problems_view_error.gif", true); //$NON-NLS-1$ declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ETOOL_PROBLEMS_VIEW_WARNING, PATH_EVIEW + "problems_view_warning.gif", true); //$NON-NLS-1$ // synchronization indicator objects // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_WBET_STAT, // PATH_OVERLAY+"wbet_stat.gif"); // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_SBET_STAT, // PATH_OVERLAY+"sbet_stat.gif"); // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_CONFLICT_STAT, // PATH_OVERLAY+"conflict_stat.gif"); // content locality indicator objects // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_NOTLOCAL_STAT, // PATH_STAT+"notlocal_stat.gif"); // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_LOCAL_STAT, // PATH_STAT+"local_stat.gif"); // declareRegistryImage(IDEInternalWorkbenchImages.IMG_OBJS_FILLLOCAL_STAT, // PATH_STAT+"filllocal_stat.gif"); } /** * Declares an IDE-specific workbench image. * * @param symbolicName * the symbolic name of the image * @param path * the path of the image file; this path is relative to the base * of the IDE plug-in * @param shared * <code>true</code> if this is a shared image, and * <code>false</code> if this is not a shared image * @see IWorkbenchConfigurer#declareImage */ private void declareWorkbenchImage(Bundle ideBundle, String symbolicName, String path, boolean shared) { URL url = FileLocator.find(ideBundle, new Path(path), null); ImageDescriptor desc = ImageDescriptor.createFromURL(url); getWorkbenchConfigurer().declareImage(symbolicName, desc, shared); }
由于jdt是在为ide服务的,所以在重用组件时遇到的问题应该可以在eclipse源码中找到
答案。