Eclipse RCP中按需启用/禁用导入向导特定项的实现方法
解决导入向导仅在资源管理器右键显示的问题
要实现自定义导入选项仅在系统资源管理器右键选中条目时显示,而在File->Import...中隐藏,可以通过扩展点配置里的<enablement>元素来控制向导的显示上下文,这和命令的enableWhen逻辑类似,同样适用于导入向导。
具体配置步骤
在你的plugin.xml中找到org.eclipse.ui.importWizards扩展点下的自定义向导节点,添加<enablement>块来过滤显示时机:
<extension point="org.eclipse.ui.importWizards"> <wizard id="com.your.plugin.import.specialWizard" name="特殊类型导入" class="com.your.plugin.wizards.SpecialImportWizard" category="com.your.plugin.import.category"> <!-- 核心配置:控制向导仅在资源管理器右键场景显示 --> <enablement> <!-- 确保有至少一个选中的资源 --> <count value="1"/> <!-- 判断当前激活的视图是资源管理器类视图 --> <with variable="activePartId"> <or> <!-- 适配ProjectExplorer视图 --> <equals value="org.eclipse.ui.navigator.ProjectExplorer"/> <!-- 适配传统的ResourceNavigator视图 --> <equals value="org.eclipse.ui.views.ResourceNavigator"/> </or> </with> </enablement> </wizard> </extension>
配置说明
<enablement>:用于定义向导的激活条件,只有满足条件时,向导才会在导入对话框中显示。<count value="1">:确保用户至少选中了一个资源,符合右键选中条目触发导入的场景。<with variable="activePartId">:获取当前激活的视图ID,通过判断视图ID是否为资源管理器类视图,来区分右键触发和File->Import触发的场景——当通过File->Import打开对话框时,当前激活的不是资源视图,因此条件不满足,向导不会显示。
可选:限制选中资源类型
如果需要仅针对特定类型的资源显示该导入选项,可以在<enablement>中添加资源类型判断:
<enablement> <count value="1"/> <with variable="activePartId"> <or> <equals value="org.eclipse.ui.navigator.ProjectExplorer"/> <equals value="org.eclipse.ui.views.ResourceNavigator"/> </or> </with> <!-- 仅允许选中文件夹或.special类型文件时显示 --> <with variable="selection"> <iterate> <adapt type="org.eclipse.core.resources.IResource"> <or> <test property="org.eclipse.core.resources.resourceType" value="FOLDER"/> <test property="org.eclipse.core.resources.name" value="*.special"/> </or> </adapt> </iterate> </with> </enablement>
内容的提问来源于stack exchange,提问作者Cydonia
相关产品推荐
相关产品推荐

