Writing a JavaFX PDF Viewer plugin for Netbeans – 4. Integrating your code

Introduction

In the part 3, we setup our plugin ready to add in our code. So let’s make the NetBeans plugin use our JavaFX viewer….

To use our JavaFX viewer, we need to plumb it into our existing TopComponent. Here are my code changes:-

1. New objects in our Top Component

public final class PDFDisplayTopComponent extends TopComponent {
 
    BaseViewerFX viewer=new BaseViewerFX();
 
    String PDFfile=null;
 
    JFXPanel fxPanel = new JFXPanel();

BaseViewerFx is our class in jpedalFX.jar (which can also be run as a standalone JavaFX application).

We keep the file name as a String value inside the component as it is accessed in several places.

Our viewer is pure JavaFX but we currently need to use the JFXPanel to show it inside NetBeans which currently uses Swing.

Item to remember in JavaFX

In developing the NetBeans plugin I discovered a small issue with opening a single window, closing it and then trying to open a new window. This is because the default for JavaFX is to ‘shut down’ if it thinks it is no longer used.

        //very important as it stops FX shutting down if you closed single Window
        Platform.setImplicitExit(false);

There is an excellent tutorial on writing a JavaFX plugin for NetBeans which covers this and other issues.

If you want your code to run in Java7, there are various workarounds to make it dynamically accessible in Java7. There is a great tutorial on this here from Geertjan Wielenga.

2. Changes to componentOpened method

We add our code to component opened. It returns a Scene which we wrap inside a JFXPanel which we then display in our TopComponent. Because TopComponent is a Swing component, we currently need the JFXPanel as a wrapper. We then open the PDF file and it will be displayed.

Notice that we need to be careful with the JavaFX thread and call it via Platform.runLater

@Override
    public void componentOpened() {
 
        this.setLayout(new BorderLayout());
        this.add(fxPanel, BorderLayout.CENTER);
 
        Platform.setImplicitExit(false);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX();
            }
       });
    }
 
    private void initFX() {
 
        Scene scene=viewer.setupViewer(this.getBounds().width,this.getBounds().height);
 
        fxPanel.setScene(scene);
 
        String newPDFfile=this.getDisplayName();
 
        if(newPDFfile!=null){
            PDFfile=newPDFfile;
        }
 
        if(PDFfile!=null){
 
            viewer.loadPDF(new File(PDFfile));
 
            viewer.addListeners();
 
        }
    }

Adding support for serialisation

The standard NetBeans skeleton code also includes the ability to save data and restore it later. If you close NetBeans and reopen it, the Windows will be restored (so long as the PDF file has not been deleted). All we have to do is save the String value of the PDF filename and restore it). A neat feature is that this code is called before the other methods are run, so resetting the name is all we need to restore the Window.

void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
 
        //new code for PDF plugin to save file so we can restore 
        p.setProperty("file", viewer.getPDFfilename());
    }
 
    void readProperties(java.util.Properties p) {
 
        String version = p.getProperty("version");
        // TODO read your settings according to their version
 
        //new code for PDF plugin to restore
        String previousPDFfile=p.getProperty("file");
 
        if(previousPDFfile!=null){
            PDFfile=previousPDFfile;
            this.setDisplayName(PDFfile);
        }
    }

 

Conclusion

We now have a simple PDF plugin which will allow us to display PDF files when you run the project. Tomorrow, we will turn it into a standalone plugin which you can install in NetBeans.

Click here for the next article in the series.

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>