Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » Connect Custom View to XML Editor
Connect Custom View to XML Editor [message #230388] Wed, 15 April 2009 11:47 Go to next message
Dan Hoyt is currently offline Dan Hoyt
Messages: 13
Registered: July 2009
Junior Member
How can I connect a custom view to an XML Editor? (I'm using Eclipse
3.3.2)

Using the information from Nitin Dahyabhai's EclipseCon 2008 presentation,
"Extending the XML and SSE editors from the Web Tools Platform Project,"
the EclipseCon 2009 presentation, "Building the XML Editor you've always
wanted," and a few other sources, I have successfully configured the stock
(internal) XML editor as a custom XML editor with my content type and icon:

<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
base-type="org.eclipse.core.runtime.xml"

describer=" org.eclipse.core.runtime.content.XMLRootElementContentDescri ber:myelement "
file-extensions="xml"
id="com.mycompany.myproject.mycustomXML"
name="My Custom XML"
priority="normal">
</content-type>
</extension>

<extension point="org.eclipse.wst.sse.ui.editorConfiguration">
<sourceViewerConfiguration

class=" com.mycompany.myproject.editor.xml.myXMLStructuredTextViewer Configuration "
target="com.mycompany.myproject.mycustomXML">
</sourceViewerConfiguration>
</extension>

<extension point="org.eclipse.ui.editors">
<editor
class=" org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditor Part "

contributorClass=" org.eclipse.wst.xml.ui.internal.tabletree.XMLMultiPageEditor ActionBarContributor "
icon="icons/mycustom.gif"
id="com.mycompany.myproject.xml"
name="My Custom XML Editor">
<contentTypeBinding
contentTypeId="com.mycompany.myproject.mycustomXML">
</contentTypeBinding>
</editor>
</extension>

This has the effect of opening any XML document with the root element
"myelement" in my "custom" editor. So far, so good.

Now, I have an existing custom view for a custom file format that has a
custom editor with a custom data model backing it and connecting the
editor to the view. I've written an XML converter for this custom file
format, and now I want to connect my existing custom view to the XML
editor (with a bridge between my custom data model and the DOM backing the
XML editor).

Using the connectors already in place for the existing custom view/editor,
I tried defining an adapter for the custom view's PageBookView page:

<extension
point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="org.eclipse.wst.sse.ui.StructuredTextEditor"
class="com.mycompany.myproject.myAdapterFactory">
<adapter
type="com.mycompany.myproject.IMyPage">
</adapter>
</factory>
</extension>

The problem is that this catches ALL XML editors in my IMyPage
implementation, but there's no visibility that I can find to the content
type (which I need in this scenario to differentiate which XML editors
connect to my custom view).

I traced PageBookView's getAdapter(Class key) method, which gets the
XMLMultiPageEditor class as an argument and eventually tries to adapt the
underlying editor's model and/or editor, but never takes into account the
content type.

My feeling is that the adapter angle is off track, especially since the
SSE classes have limited visibility outside their package and are clearly
intended for extension but NOT subclassing.

So, how else can I accomplish this? Do I need to create my own XML
editor? Is there an Eclipse 3.4 interface that solves the problem?

Thanks in advance for your consideration.

Regards,
Dan Hoyt
Senior Systems Architect, TSC
Re: Connect Custom View to XML Editor [message #230398 is a reply to message #230388] Wed, 15 April 2009 13:19 Go to previous messageGo to next message
Dan Hoyt is currently offline Dan Hoyt
Messages: 13
Registered: July 2009
Junior Member
Update: I found the content type in the IStructuredModel, which means I
can make the adapter work the way I want it to, but not without a bunch of
"DiscouragedAccess" warnings (note that I've skipped the null checks for
clarity):

public class MyAdapterFactory implements IAdapterFactory
{
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adaptableObject instanceof StructuredTextEditor) {
StructuredTextEditor xmlTextEditor = (StructuredTextEditor)
adaptableObject;
StructuredTextViewer xmlTextViewer = xmlTextEditor.getTextViewer();
// IStructuredModel model = xmlTextEditor.getModel();
IEditorInput input = xmlTextEditor.getEditorInput();
IDocumentProvider provider = xmlTextEditor.getDocumentProvider();
IDocument document = provider.getDocument(input);
IModelManager manager = StructuredModelManager.getModelManager();
IStructuredModel model = manager.getExistingModelForRead(document);
String id = model.getContentTypeIdentifier();
if ("com.mycompany.myproject.mycustomXML".equalsIgnoreCase(id)) {
IMyPage myPage = new MyPage(xmlTextEditor, model);
return myPage;
}
}
return null;
}
}

While this works, considering all the discouraged accesses and internal
class references, I have to wonder: Is this the RIGHT way to achieve my
goal?

Regards,
Dan Hoyt
Senior Systems Architect, TSC
Re: Connect Custom View to XML Editor [message #230458 is a reply to message #230398] Thu, 16 April 2009 10:34 Go to previous message
Eclipse User
Originally posted by: dcarver.starstandard.org

Dan Hoyt wrote:
> Update: I found the content type in the IStructuredModel, which means I
> can make the adapter work the way I want it to, but not without a bunch
> of "DiscouragedAccess" warnings (note that I've skipped the null checks
> for clarity):
>
> public class MyAdapterFactory implements IAdapterFactory
> {
> public Object getAdapter(Object adaptableObject, Class adapterType) {
> if (adaptableObject instanceof StructuredTextEditor) {
> StructuredTextEditor xmlTextEditor = (StructuredTextEditor)
> adaptableObject;
> StructuredTextViewer xmlTextViewer = xmlTextEditor.getTextViewer();
> // IStructuredModel model = xmlTextEditor.getModel();
> IEditorInput input = xmlTextEditor.getEditorInput();
> IDocumentProvider provider = xmlTextEditor.getDocumentProvider();
> IDocument document = provider.getDocument(input);
> IModelManager manager = StructuredModelManager.getModelManager();
> IStructuredModel model = manager.getExistingModelForRead(document);
> String id = model.getContentTypeIdentifier();
> if ("com.mycompany.myproject.mycustomXML".equalsIgnoreCase(id)) {
> IMyPage myPage = new MyPage(xmlTextEditor, model);
> return myPage;
> }
> }
> return null;
> }
> }
>
> While this works, considering all the discouraged accesses and internal
> class references, I have to wonder: Is this the RIGHT way to achieve my
> goal?

Unfortunately, it's probably the only way. I would open a bug asking
for the items you need to be made API, so that the group can look at it
and see if the internals you are using should be exposed as API in some
way, or if there is another way you should be accomplishing this.

To go beyond the simplistic implementations, it is necessary to ask what
is currently marked as internal in many cases.
Previous Topic:HTML Source Formatting
Next Topic:Validation of XPaths in xsd?
Goto Forum:
  


Current Time: Mon Jul 28 12:44:22 EDT 2014

Powered by FUDForum. Page generated in 0.02866 seconds