Writing a JavaFX PDF Viewer plugin for Netbeans – 7. Linking a FileType (part B)

In part 6 we linked the PDF file type with our plugin. Currently when a PDF is opened, it will open up within Netbeans’ own text editor. In this article we will get the PDF to open using our plugin.

Before we start, it would be wise to modify our TopComponent subclass PDFDisplayTopComponent to be able to receive a PDF file via the constructor.

    public PDFDisplayTopComponent(String file){
        this();
        PDFfile = file;
    }

Creating an Action for the Context Menu

Right click your plugin project go to New and select Action. If it isn’t on the context menu, select Other… and in the folder Module Development select Action.

new_action

 

In the first window of the Wizard select Conditionally Enabled. For the Cookie Class type in the name of your Data Object, in this case we’ll be using PDFDataObject. Make sure User Selects One Node is selected and press next.

 

wiz_1

On the next page, make sure the category is File, deselect Global Menu Item and select File Type Context menu Item. The content type being used for our plugin is application/PDF. Nothing else should need changing. Press next.

wiz_2

The final window is where we name the action. The class name will be called PDFOpenAction and the display name is what is shown when you right-click the file, so we’ll name it “Show in Viewer”. Once that’s done, press finish and Netbeans will generate the necessary boilerplate code.

wiz_3

The generated code should look like this:

package org.jpedal.netbeans;
 
package org.jpedal.netbeans;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
 
@ActionID(
        category = "File",
        id = "org.jpedal.netbeans.PDFOpenAction"
)
@ActionRegistration(
        displayName = "#CTL_PDFOpenAction"
)
@ActionReference(path = "Loaders/application/PDF/Actions", position = -300)
@Messages("CTL_PDFOpenAction=Open in Viewer")
public final class PDFOpenAction implements ActionListener {
 
    private final PDFDataObject context;
 
    public PDFOpenAction(PDFDataObject context) {
        this.context = context;
    }
 
    @Override
    public void actionPerformed(ActionEvent ev) {
        // TODO use context
    }
}

If we run our plugin in the test IDE and try to open a PDF from the Files window, you’ll see that the option on the context menu appears, but it doesn’t do anything. That’s because we need to add some code into actionPerformed() method.

OpenInViewer

Adding our own code

The code we need to add into actionPerformed() to control the TopComponent is the same as the code we inserted into the PDFViewerAction method in part 3, however  we also have to get the file path of the PDFDataObject called context, to do so we get the FileObject from the context and use the FileUtil class to get the path as a string:

            String path="";
            // Get the path of the PDF
            if(context != null){
                FileObject f = context.getPrimaryFile();
                path = FileUtil.toFile(f).getPath();
            }

We can then create a new instance of the PDFDisplayTopComponent and pass through the path string, using the constructor we added in above. The whole method should look like this:

    @Override
    public void actionPerformed(ActionEvent ev) {
        boolean isFXAvailable=JavaFXHelper.isJavaFXAvailable();
 
        if(isFXAvailable){
 
            String path="";
            // Get the path of the PDF
            if(context != null){
                FileObject f = context.getPrimaryFile();
                path = FileUtil.toFile(f).getPath();
            }
 
            TopComponent tc;
            //this gives me a new window each time
            tc=new PDFDisplayTopComponent(path);
 
            tc.open();
            tc.requestActive();
        }else{
            JOptionPane.showMessageDialog(null, "JavaFX is not available");
        }
    }

Now Clean and Build the plugin and then run it in the test IDE. When you double click a PDF in the files browser it will open it within our viewer.

awjune

Hopefully you have found this part useful, keep an eye on the blog for future updates 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.
Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.
Simon

About Simon Lissack

Simon Lissack is a developer at IDR Solutions, working on JavaFX, Android and the Cloud Conversion service.

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>