Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to open correct XML editor for different XML documents
icon6.gif  How to open correct XML editor for different XML documents [message #666972] Mon, 25 April 2011 09:34 Go to next message
Richard Adams is currently offline Richard AdamsFriend
Messages: 77
Registered: July 2009
Location: Edinburgh
Member
Hello
In our RCP app we use several different XML languages for different data types and have developed specific editors for them. We would like the correct editor to open when a file is double-clicked. The files can have any name (but will end in .xml).
In the org.eclipse.ui.editors extension point though, one can only specify a file extension (xml), so we can't distinguish between different XML languages. Is there a way to do more detailed filtering?
For example, using commands for menu items, one can write an org.eclipse.core.contenttype.ContentTypes extension in which you can write some code to check the xml namespace but this doesn't seem usable for editors?

Thanks
Richard


Re: How to open correct XML editor for different XML documents [message #666998 is a reply to message #666972] Mon, 25 April 2011 12:20 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You use the org.eclipse.core.contenttype.contentTypes extension point,
and then associate your editor with that content type. Content-type
works by allowing the examination of the first 100 bytes (more or less)
of the file, to allow differentiation of files with the same extension.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to open correct XML editor for different XML documents [message #667205 is a reply to message #666972] Wed, 27 April 2011 12:26 Go to previous messageGo to next message
Richard Adams is currently offline Richard AdamsFriend
Messages: 77
Registered: July 2009
Location: Edinburgh
Member
Hello Paul
Thanks for the help. I was a bit dense as I didn't see the contentTypeBinding
extension point sub element in the editor extension.

So I've done that, and created the following contentTypeBinding

<content-type
base-type="org.eclipse.core.runtime.xml"
describer="org.sedml.jlibsedml.editor.SEDMLContentDescriber "
id="org.sedml.jlibsedml.editor.sedmlBinding"
file-extensions="xml"
name="SEDML content type"
priority="normal">
</content-type>

My describer class extends XMLContentDescriber .

Running it through the debugger, the code is called (e.g., when a context menu is requested) and returns 'INVALID' if the file is the 'wrong' type of XML,as it should, but nonetheless, the editor still appears in the Open->With context menu. I.e., the INVALID seems to be ignored.

Am I missing something, to get the editor removed from the 'Open With' context menu?

Thank you,

Richard


Re: How to open correct XML editor for different XML documents [message #667377 is a reply to message #667205] Thu, 28 April 2011 12:21 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

AFAIK content type is for narrowing an editor-file extension
association. So double-clicking or using right-click>Open will open the
correct editor.

I believe the algorithm for Open With to be much more inclusive. If
your editor specifies .xml file extension, it will be available for all
..xml even if they're not appropriate.

Did you remove .xml from the file extension in the editor definition
when you added the content type binding?

You can check out org.eclipse.ant.core which provides the buildfile
content type and org.eclipse.ant.ui which provides the ant buildfile
editor. They don't appear in Open With against random XML files.

PW



--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: How to open correct XML editor for different XML documents [message #670157 is a reply to message #667377] Thu, 12 May 2011 21:40 Go to previous messageGo to next message
Max Mising name is currently offline Max Mising nameFriend
Messages: 54
Registered: September 2010
Member
I'm hoping someone can help me out with a similar problem. I've been trying to figure out this content type extension point for a while and this is what I have. In our application, we have plugins included that have files with .cfg extensions. Some of these plugins have xml in their cfg files and another has text. I have written a generic file browser for the plugins and I am trying to use this extension point to differentiate between the types of extensions and open the xml editor by default in one and the text editor by default on the other.

Right now I'm just trying to get my descriptor working, making sure it's getting used. It should deny all files and I'm thinking everything should default to the text editor when opened. Here it is:
public class TestContentDescriber implements ITextContentDescriber {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.runtime.content.IContentDescriber#describe(java.io.
     * InputStream, org.eclipse.core.runtime.content.IContentDescription)
     */
    @Override
    public int describe(InputStream contents, IContentDescription description)
            throws IOException {
        System.out.println(contents + " : " + description);
        return INVALID;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.core.runtime.content.IContentDescriber#getSupportedOptions()
     */
    @Override
    public QualifiedName[] getSupportedOptions() {
        return new QualifiedName[] {};
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.core.runtime.content.ITextContentDescriber#describe(java.
     * io.Reader, org.eclipse.core.runtime.content.IContentDescription)
     */
    @Override
    public int describe(Reader contents, IContentDescription description)
            throws IOException {
        System.out.println(contents + " : " + description);
        return INVALID;
    }

}


This is my extension point definition:

   <extension
         point="org.eclipse.core.contenttype.contentTypes">
         <content-type
               id="localizationContentType"
               base-type="org.eclipse.core.runtime.xml"
               name="Localization Content Type"
               file-extensions="cfg"
               priority="normal">
               <describer
                     class="com.abc.def.TestContentDescriber">
               </describer>
         </content-type>
   </extension>


What I'm expecting is that if I return VALID, the xml editor will open because my base-type is the core runtime xml contentId and if I return INVALID, text editor will be used. What I'm seeing is that even before my describer is called, IEditorRegistry.getImageDescritor(fileName) is returning the xml image for my cfg file and no matter what I return in my describer, the xml editor is used.

Any ideas

Thanks

Max

[Updated on: Thu, 12 May 2011 21:41]

Report message to a moderator

Re: How to open correct XML editor for different XML documents [message #670323 is a reply to message #670157] Fri, 13 May 2011 14:10 Go to previous messageGo to next message
Mark Leone is currently offline Mark LeoneFriend
Messages: 123
Registered: July 2009
Senior Member
Max,

Are you using the platform's XML editor or one that you developed? It seems the XML editor is somehow registered for files with extension .cfg. If it's your editor, then of course you can change the file association in the editor contribution in plugin.xml. If it's not your editor, then I suggest you set a breakpoint in the IEditorRegistry implementation and see how it's determining that .cfg files are associated with the XML editor.
Re: How to open correct XML editor for different XML documents [message #670334 is a reply to message #670323] Fri, 13 May 2011 14:55 Go to previous messageGo to next message
Max Mising name is currently offline Max Mising nameFriend
Messages: 54
Registered: September 2010
Member
I am using the "built in" wst xml editor. The only time the registry thinks cfg's are associated with the xml editor is when my contentType extension point looks how it is with the file-extensions attribute set to "cfg". I debuged and the ContentTypeCatalog (~ line 585 where it calls hasFileSpec on the type) is claiming that my defined contentType (localizationContentType) is associated with cfg files because it contains "cfg" in the file-extensions attribute and since my contentType's basetype is the xml type, it gets the xml association. I was under the impression that since I have a describer, the content type wouldn't be associated with any cfg files until my describer approved of them. But it appears that no matter what, if my base-type is the xml type, no matter what cfg files will always get the xml content type even if my describer returns invalid when inspecting the file. Again, this is against what my initial thoughts were on how it worked. Can anyone verify I am correct in thinking how it works? Does anyone know if it is possible to do what I am attempting to do? (multiple file types with same extension opened in different editors. I don't care if the extensions always start out getting xml images as long as they are corrected after the file has been opened).

Thanks

Max
Re: How to open correct XML editor for different XML documents [message #671359 is a reply to message #670334] Tue, 17 May 2011 15:51 Go to previous message
Max Mising name is currently offline Max Mising nameFriend
Messages: 54
Registered: September 2010
Member
Anyone have any knowledge of the ContentTypeCatalog and know if what I'm trying to do is possible?

Max
Previous Topic:How launch a terminal SSH in my RCP application
Next Topic:Context Menu on my TreeViewer
Goto Forum:
  


Current Time: Fri Apr 19 19:44:12 GMT 2024

Powered by FUDForum. Page generated in 0.03780 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top