Andrei B.
Andrei B.

Reputation: 1000

Eclipse RCP: How to access internal classes of plugins?

I want to use the default XML editor (org.eclipse.wst.xml.ui) of Eclipse in an RCP application. I need to read the DOM of the xml file currently open. The plugin doesn't offer any extension point, so I'm trying to access the internal classes. I am aware that the I should not access the internal classes, but I don't have another option.

My approach is to create a fragment and an extension point to be able to read data from the plugin. I'm trying not to recompile the plugin, that's why I thought that a fragment was necessary. I just want to load it and extract the data at runtime.

So, my question is: is there another way to access the classes of a plugin? if yes, how? Any tutorial, doc page or useful link for any of the methods is welcome.

Upvotes: 7

Views: 2005

Answers (2)

Andrei B.
Andrei B.

Reputation: 1000

I ended up extending XMLMultiPageEditorPart like this:

public class MultiPageEditor extends XMLMultiPageEditorPart implements
        IResourceChangeListener {

    @Override
    public void resourceChanged(IResourceChangeEvent event) {
        // TODO Auto-generated method stub
        setActivePage(3);
    }

    public Document getDOM() {
        int activePageIndex = getActivePage();

        setActivePage(1);
        StructuredTextEditor fTextEditor = (StructuredTextEditor) getSelectedPage();
        IDocument document = fTextEditor.getDocumentProvider().getDocument(
                fTextEditor.getEditorInput());
        IStructuredModel model = StructuredModelManager.getModelManager()
                .getExistingModelForRead(document);
        Document modelDocument = null;
        try {
            if (model instanceof IDOMModel) {
                // cast the structured model to a DOM Model
                modelDocument =  (Document) (((IDOMModel) model).getDocument());
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
        setActivePage(activePageIndex);
        return modelDocument;
    }
}

This is not a clean implementation, but it gets the job done.

Upvotes: 1

Andrei B.
Andrei B.

Reputation: 1000

Since nobody answered my question and I found the answer after long searches, I will post the answer for others to use if they bump into this problem.

To access a plugin at runtime you must create and extension point and an extension attached to it into the plugin that you are trying to access.

Adding classes to a plugin using a fragment is not recommended if you want to access those classes from outside of the plugin.

So, the best solution for this is to get the plugin source from the CVS Repository and make the modifications directly into the source of the plugin. Add extension points, extensions and the code for functionality.

Tutorials:

Upvotes: 4

Related Questions