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.

Writing a JavaFX PDF Viewer plugin for Netbeans – 10. Integrating our plugin into NetBeans with callback

1 min read

PDF viewers in NetBeans
PDF viewers in NetBeans

Introduction

In previous parts we integrated our NetBeans plugin into our Open Source JavaFX PDF viewer so that we can now display it inside a NetBeans TopComponent. Now we want to add some tighter integration between NetBeans and our PDF Viewer. So, for example, when we open a new PDF file inside our PDF viewer we can update the NetBeans display.

NetBeans has a very elegant look-up which makes it very easy for components to find and communicate with other components in NetBeans. However, we also have lots of people using our PDF viewer outside NetBeans as a standalone application. So we do not want to make Netbeans into a required dependency for our code.

So the solution is to create a callback – we will create an optional Object which allows us to callback to NetBeans and update it.

The callback object

Firstly we need a standard Object, which will override a simple interface and which we can pass into our PDF viewer. For the moment, we just add a method which we will call when a new file opens.

public interface PluginHandler {
 
    //allow user to update Plugin when new file loaded
    public void setFileName(String name);
 
}

Hooks in the PDF viewer

We give ourselves a means to pass this object into our PDF viewer and then we can add code to call it if present. For example, here is the code we call when we open a file.

PluginHandler customPluginHandle=(PluginHandler) externalHandlers.getExternalHandler(Options.PluginHandler);
 
if(customPluginHandle!=null){
 customPluginHandle.setFileName(fileAccess.getFilename());
}

Support in the NetBeans plugin

Firstly we have a method to pass our object into the PDF viewer which we can call from our plugin code.

fullViewer = new OpenViewerFX(viewerPane, null); 
fullViewer.addExternalHandler(new PluginCallBackHandler(this), 
Options.PluginHandler);

Secondly  we have an inner class which implements our plugin interface and can access the topComponent. When a new file is opened, the code can now update the display inside NetBeans.

public class PluginCallBackHandler implements PluginHandler {
 
   PDFDisplayTopComponent handler;
 
   public PluginCallBackHandler(PDFDisplayTopComponent handler) {
     this.handler=handler;
   }
 
   @Override
   public void setFileName(final String string) {
 
     //remember this is Swing and not FX
     if (SwingUtilities.isEventDispatchThread()){ 
       handler.setName(string);
     }else {
       final Runnable doPaintComponent = new Runnable() {
         @Override
         public void run() {
           handler.setName(string);
         }
       };
       SwingUtilities.invokeLater(doPaintComponent);
    }
  }
}

Final thoughts

What I really like about this method is that we have made it very easy to add hooks into our PDF viewer for our NetBeans plugin (or other integration) without adding any dependencies to our code for users who just want to use the standalone jar.

And we can add more callback integration very easily with more interface methods and hooks in our PDF viewer.

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.

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 *