Vlad Ilie
Vlad Ilie

Reputation: 1409

Eclipse RCP - adding properties to exising Project in Project Explorer

Greetings fellow Stackoverflownians,

I am developing an Eclipse RCP application, and within it is also the standard Project Explorer View.

I need to add several properties to a org.eclipse.core.internal.resources.Project to be presented alongside the usual Resource properties in the standard Properties View.

My thought process was that I add another listener to the SelectionService:

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

and in this selection listener I get the selected Project, alter it, and pass it forward to the selection service.

Trouble is, I don't have any method to set the selection programatically without a content provider.

Also, as far as I see, Project doesn't implement IPropertySource, hence it would be rather difficult to subclass it, overwrite the getPropertyDescriptors/Values methods...

If so, how do I get the content provider of the Project Explorer view?

Or how can I set the selection within the SelectionService?

Any help/opinions appreciated!

Upvotes: 1

Views: 1063

Answers (1)

Vlad Ilie
Vlad Ilie

Reputation: 1409

I have succeeded in adding a property to an existing IProject, despite it not implementing IPropertySource (and hence would've been able to just add some functionality by subclassing and overwriting the getPropertyDescriptors and getPropertyValue methods.

Thanks to greg-449 I was able to understand the StandardPropertiesAdapterFactory functionality that made a ResourcePropertySource from the IProject (that extends IResource)

So a way to work around all this is that you use a subclass of AdvancedPropertySection to display the IProject's properties...

Here's the kewd:

I link the ProjectExplorer's view ID with a TabDescriptorProvider in plugin.xml:

<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>

After that, we define the TabDescriptorProvider, and link it to our custom 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 "";
        }

    }

}

And the Section itself:

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);
            }
        }
    }
}

Thanks again, Greg!

Upvotes: 1

Related Questions