【问题标题】:Eclipse RCP - adding properties to exising Project in Project ExplorerEclipse RCP - 在项目资源管理器中向现有项目添加属性
【发布时间】:2013-11-08 15:57:59
【问题描述】:

问候 Stackoverflownians 的同胞,

我正在开发一个 Eclipse RCP 应用程序,其中还有标准的Project Explorer View。

我需要向org.eclipse.core.internal.resources.Project 添加几个属性,以便与标准Properties View 中的常用Resource 属性一起显示。

我的想法是在SelectionService 中添加另一个侦听器:

window =PlatformUI.getWorkbench().getActiveWorkbenchWindow();
window.getSelectionService().addSelectionListener("org.eclipse.ui.navigator.ProjectExplorer", listener);

在这个选择侦听器中,我获得了选定的项目,对其进行修改,然后将其传递给选择服务。

麻烦的是,我没有任何方法可以在没有内容提供者的情况下以编程方式设置选择。

另外,据我所知,Project 没有实现IPropertySource,因此子类化它会相当困难,覆盖getPropertyDescriptors/Values 方法...

如果是,如何获取Project Explorer视图的内容提供者?

或者如何在SelectionService 中设置选择?

任何帮助/意见表示赞赏!

【问题讨论】:

  • IPropertySource 用于 IProject 是使用 IAdapterFactory 完成的(对于某些视图,这将是 StandardPropertiesAdapterFactory)。实际的源类是ResourcePropertySource。
  • 好的,那你会怎么做呢?如何通过它设置属性描述符和值?

标签: java eclipse-plugin eclipse-rcp


【解决方案1】:

我成功地向现有的IProject 添加了一个属性,尽管它没有实现IPropertySource(因此可以通过继承和覆盖getPropertyDescriptors 和getPropertyValue 方法来添加一些功能.

感谢 greg-449,我能够理解 StandardPropertiesAdapterFactory 的功能,它从 IProject(扩展了 IResource)生成了 ResourcePropertySource。

因此,解决所有这些问题的一种方法是使用AdvancedPropertySection 的子类来显示IProject 的属性...

这里是kewd:

我将ProjectExplorer 的视图ID 与plugin.xml 中的TabDescriptorProvider 关联起来:

<extension point="org.eclipse.ui.views.properties.tabbed.propertyContributor">
      <propertyContributor
            contributorId="org.eclipse.ui.navigator.ProjectExplorer"
            tabDescriptorProvider="eb.tresos.bustools.connection.extraproperty.TabDescriptorProvider">
         <propertyCategory
               category="tabbedCategory">
         </propertyCategory>
      </propertyContributor>
   </extension>

之后,我们定义TabDescriptorProvider,并将其链接到我们自定义的AdvancedPropertySection:

public class TabDescriptorProvider implements ITabDescriptorProvider {

    @Override
    public ITabDescriptor[] getTabDescriptors( IWorkbenchPart part, ISelection selection ) {
        AbstractTabDescriptor[] tabs = new AbstractTabDescriptor[1];
        tabs[0] = new TabDescriptor("Aww shucks, TabDescriptorTitle");

        return tabs;
    }

    class TabDescriptor extends AbstractTabDescriptor {
        String label;

        /**
         * Constructor
         * 
         * @param label sets the label text of the tab
         */
        TabDescriptor( String label ) {
            this.label = label;
        }

        @SuppressWarnings("rawtypes")
        @Override
        public List getSectionDescriptors() {
            List<SectionDescriptor> sList = new ArrayList<SectionDescriptor>();
            sList.add( new SectionDescriptor( label ) );

            return sList;
        }

        @Override
        public String getCategory() {
            //Stub
            return "";
        }

        @Override
        public String getId() {
            //stub
            return "";
        }

        @Override
        public String getLabel() {
            return "Resource";
        }
    }

    class SectionDescriptor extends AbstractSectionDescriptor {

        String section;
        List<AbstractPropertySection> sectionTabs = new ArrayList<AbstractPropertySection>();

        public SectionDescriptor( String section ) {
            this.section = section;

        }

        /**
         * SectionId
         */
        @Override
        public String getId() {
            //stub
            return "";
        }

        /**
         * SectionClass
         */
        @Override
        public ISection getSectionClass() {
            return new AuxiliaryProjectSection();
        }

        /**
         * SectionTab
         */
        @Override
        public String getTargetTab() {
            //stub
            return "";
        }

    }

}

还有Section 本身:

public class AuxiliaryProjectSection extends AdvancedPropertySection {
    @Override
    public void setInput(IWorkbenchPart part, ISelection selection) {
        if (selection instanceof StructuredSelection) {
            Object firstElement = ((StructuredSelection)selection).getFirstElement();
            if (firstElement instanceof IProject) {
                final IProject theProject = (IProject) firstElement;
                ISelection selection2 = new StructuredSelection(new ResourcePropertySource(theProject) {

                    @Override
                    public IPropertyDescriptor[] getPropertyDescriptors() {
                        ArrayList<IPropertyDescriptor>  arrayList = new ArrayList<IPropertyDescriptor>();
                        IPropertyDescriptor[] array = {new PropertyDescriptor("ID-ul", "Labelul")};
                        arrayList.addAll(Arrays.asList(super.getPropertyDescriptors()));
                        arrayList.addAll(Arrays.asList(array));
                        return arrayList.toArray(new IPropertyDescriptor[0]);
                    }

                    @Override
                    public Object getPropertyValue(Object id) {
                        if (id.equals("ID-ul"))
                            return "Silly Value";
                        else
                            return super.getPropertyValue(id);
                    }

                });
                super.setInput(part, selection2); 
            } else {
                super.setInput(part, selection);
            }
        }
    }
}

再次感谢格雷格!

【讨论】:

  • 如果您要投反对票,请通过评论提供完整的批评。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
相关资源
最近更新 更多