Writing a JavaFX PDF Viewer plugin for Netbeans – 6a. Link a FileType

Introduction

In the part 5, we shipped our first release of a NetBeans plugin. Now we want to  link it into the NetBeans System so that Netbeans will automatically open PDF files in our PDF viewer.

Creating a FIleType

Our first step is to create a filetype. As you would expect there is lots of built in help to do the work for us.

filetype

 

In the Menu add in the Mime type and the ways to define the file (for a Tiff file it might be tif and Tiff). If you are not sure of the mime type, click here for a useful reference page.

mimeOn the next menu, make sure that  Use MultiView is ticked and you will need to supply an image for your FileType. Choose a logical name for the Class Name Prefix (in this case I am using PDF).

fileType

Click finish and you will see that NetBeans can created some new code for us!

new classes

 

It sort of works…..

As a developer (or maybe it is just me) you instantly run this to see what happens. A second copy of NetBeans fires up and when you choose Open File and select a PDF file you get this.

Not exactly what you wanted (unless maybe you are me or Bruno Lowagie, the developer of IText and and want to see the guts of a PDF file).PDF

The really interesting code is in the DataObject class. The NetBeans developers have gone to a lot of work to reduce the amount of code you need to write to make this work (it is much more compact that my 2008 implementation!) and made use of new Java features.

The key parts are the use of Annotations (like org.openide.actions.OpenAction to activate functionality) and at the bottom to add in a Visual editor. Altering these will allow the plugin to display our PDF file in our TopComponent. We will be modifying this code next time to provide our custom view. You are welcome to post your own solutions in the meantime!

If you want to read up on FileTypes, I recommend the FileTypes tutorial on Netbeans and Geertjan’s excellent blog post.

package org.jpedal.netbeans;
 
import java.io.IOException;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.MIMEResolver;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectExistsException;
import org.openide.loaders.MultiDataObject;
import org.openide.loaders.MultiFileLoader;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
 
@Messages({
    "LBL_PDF_LOADER=Files of PDF"
})
@MIMEResolver.ExtensionRegistration(
        displayName = "#LBL_PDF_LOADER",
        mimeType = "application/PDF",
        extension = {"PDF", "pdf"}
)
@DataObject.Registration(
        mimeType = "application/PDF",
        iconBase = "org/jpedal/netbeans/pdf.png",
        displayName = "#LBL_PDF_LOADER",
        position = 300
)
@ActionReferences({
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "System", id = "org.openide.actions.OpenAction"),
            position = 100,
            separatorAfter = 200
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "Edit", id = "org.openide.actions.CutAction"),
            position = 300
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "Edit", id = "org.openide.actions.CopyAction"),
            position = 400,
            separatorAfter = 500
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "Edit", id = "org.openide.actions.DeleteAction"),
            position = 600
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "System", id = "org.openide.actions.RenameAction"),
            position = 700,
            separatorAfter = 800
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "System", id = "org.openide.actions.SaveAsTemplateAction"),
            position = 900,
            separatorAfter = 1000
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "System", id = "org.openide.actions.FileSystemAction"),
            position = 1100,
            separatorAfter = 1200
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "System", id = "org.openide.actions.ToolsAction"),
            position = 1300
    ),
    @ActionReference(
            path = "Loaders/application/PDF/Actions",
            id = @ActionID(category = "System", id = "org.openide.actions.PropertiesAction"),
            position = 1400
    )
})
public class PDFDataObject extends MultiDataObject {
 
    public PDFDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
        super(pf, loader);
        registerEditor("application/PDF", true);
    }
 
    @Override
    protected int associateLookup() {
        return 1;
    }
 
    @MultiViewElement.Registration(
            displayName = "#LBL_PDF_EDITOR",
            iconBase = "org/jpedal/netbeans/pdf.png",
            mimeType = "application/PDF",
            persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED,
            preferredID = "PDF",
            position = 1000
    )
    @Messages("LBL_PDF_EDITOR=Source")
    public static MultiViewEditorElement createEditor(Lookup lkp) {
        return new MultiViewEditorElement(lkp);
    }
}

Next time

We will have our solution, an update for the plugin and hopefully a few other surprises…

This post is part of our “NetBeans article Index” series. In these articles, we aim to explore NetBeans in different ways, from useful hint and tips, to our how-to’s, experiences and usage of the NetBeans IDE.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>