Home > Eclipse development, Software Development, XML > Writing an Eclipse Plug-in (Part 9): Custom Project: Defining a Custom File Type

Writing an Eclipse Plug-in (Part 9): Custom Project: Defining a Custom File Type


So things have been going swimmingly for the last 8 parts. Time to return to our roots: the original project.

But first: time to floss. Open customplugin MANIFEST.MF and click on the light bulb to fix the warning. It should now display:

MANIFEST.MF
...
Export-Package: customplugin,
 customplugin.natures,
 customplugin.perspectives,
 customplugin.projects,
 customplugin.wizards

Also, return to the Overview tab and click on the Externalize Strings Wizard. That should take care of externalizing 4 strings from the MANIFEST.MF and plugin.xml files. Click Finish.

There. My teeth feel so much better.

Okay, now on to the serious stuff.

The custom project will use 3 files types (for now):

  1. Schema definition
  2. Deployment descriptor
  3. Java source code

The two descriptor files will be XML files. They are what we need to define.

The current tasks are to:

  • Define a custom project file type for the Schema Definition.
  • Define a custom project file type for the Deployment Definition.

There are two things we need to know before defining the file type:

  • how can we determine the type of a file?
  • can we leverage existing Eclipse functionality?

The butler did it. Metaphorically anyway. There is no need to wait until the end of the story to be told what the answer is: Eclipse can associate the content type of an XML file by looking inside it and reading the root element.

By configuring the content type extension with a base-type of org.eclipse.core.runtime.xml and selecting a parser of type org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 we can create an extensive list of content types that are all XML files, but have different file formats. Very convenient. Very cool. That’s two verys.

Define a Custom Project File Type for the Schema Definition.

First:

  1. Open customplugin –> plugin.xml –> Extensions –> Add –> contenttypes
  2. Select org.eclipse.core.contenttype.contentTypes
  3. Click Finish
  4. Enter:
    • ID: customplugin.contenttype
  5. Select org.eclipse.core.contenttype.contentTypes –> new –> content-type
  6. Enter:
    • id: customplugin.contenttype.schema
    • name: Hidden Clause Schema Definition
    • base-type: org.eclipse.core.runtime.xml
    • file-extensions: xml
    • priority: normal
  7. Select Schema Definition –> new –> describer
  8. Enter:
    • class: org.eclipse.core.runtime.content.XMLRootElementContentDescriber2
  9. Select org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 –> new –> parameter
  10. Enter:
    • name: element
    • value: hc-schema

Start the runtime workbench and open Window –> Preferences –> General –> Content Types –> Text –> XML. Hidden Clause Schema Definition should be listed as an XML content type.

customplugin-part-9-preferences-with-new-contenttype

From the Resource Perspective create a project, custom or otherwise. Create a file and name it special-schema.xml. Enter the following into the file:

<hc-schema>
  <tables>
  </tables>
</hc-schema>

Right click on the file and select Properties –> Resource. The file should be listed as Hidden Clause Schema Definition (the name we gave it above).

customplugin-part-9-properties-file-hc-schema-def

Define a Custom Project File Type for the Deployment Definition.

Time for the second file type:

  1. Select org.eclipse.core.contenttype.contentTypes –> new –> content-type
  2. Enter:
    • id: customplugin.contenttype.deployment
    • name: Hidden Clause Deployment Definition
    • base-type: org.eclipse.core.runtime.xml
    • file-extensions: xml
    • priority: normal
  3. Select Schema Definition –> new –> describer
  4. Enter:
    • class: org.eclipse.core.runtime.content.XMLRootElementContentDescriber2
  5. Select org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 –> new –> parameter
  6. Enter:
    • name: element
    • value: hc-deployment

Start the runtime workbench and open Window –> Preferences –> General –> Content Types –> Text –> XML. Hidden Clause Deployment Definition should be listed as an XML content type.

customplugin-part-9-preferences-with-contenttype-deployment

From the Resource Perspective, using the same project as before create another file and name it special-dep.xml. Enter the following into the file:

<hc-deployment>
  <tables>
  </tables>
</hc-deployment>

Right click on the file and select Properties –> Resource. The file should be listed as Hidden Clause Deployment Definition.

customplugin-part-9-properties-file-hc-deploy-def

Take a bow.

Cleaning Up

Return to the Overview tab of plugin.xml and click on the Externalize Strings Wizard. Oh, look: the two custom file type names are there.

  • Assign the key content-type.name.schema to Hidden Clause Schema Definition
  • Assign the key content-type.name.deployment to Hidden Clause Deployment Definition
  • Click Finish

I think we might have missed a spot behind the ears, but they feel dry so I think we are okay.

What Just Happened?

Well, adding two new content types was pretty straightforward. Some would say that we should have done this directly in XML. Others think we should learn latin in school. Others think we should go back to the oceans.

Personally, I prefer to use editors to control configuration files; it keeps me from doing something complicated like…forgetting a closing brace, or misspelling an element name.

References

The Eclipse help files were somewhat useful, but not as useful as just trying it out. This is one of the few times where stumbling around in the room is actually useful.

Of course the Eclipse Tip: Define Custom Content Types to Identify Your Data Files was rather helpful in pointing out the use of org.eclipse.core.runtime.content.XMLRootElementContentDescriber2 as an existing content type XML parser that also supports namespaces. Very cool. Very useful.

The cat is alive.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: