Integrating Oracle Documentation into NetBeans IDE with PDF Viewer Plugin

At IDR Solutions I have spent alot of time working with the NetBeans IDE whilst working on our Java PDF Library and PDF to HTML5/SVG Converter. Recently I noticed that Oracle has produced a really nice online guide to NetBeans available as a PDF or HTML.  We decided that we would like to add an option to display the NetBeans documentation from inside NetBeans itself.

We wanted to add an Action to download and display the documentation. As it is also a big file we wanted to have the option to cache it on the machine so it is only downloaded once. This is how we added this to our PDF Viewer plugin.

Adding an Action to NetBeans

Adding a new action to NetBeans is very easy and straight forward. Just right click on your NetBeans project, select new and Action.

1

Press Next

2

Chose where in the menu you want your new action to be displayed and press Next.

3

Type the class name and the display name of your action. Here you can also add an icon for it. when all is done click Finish.

5

Below is the functionality of our action that allows us to open the Oracle Guide.

@Override
public void actionPerformed(ActionEvent e) {
 
    TopComponent tc;
    //this gives me a new window each time
    tc = new PDFDisplayTopComponent("http://docs.oracle.com/cd/E50453_01/doc.80/e50452.pdf", PDFViewerTypes.INTERNAL_OPENVIWERFX);
 
    tc.open();
    tc.requestActive();
 
}

8

Caching the file

At first we were opening and displaying the file in our PDF Viewer from the url.

//Open the PDF File in the Plugin Window.
if(PDFfile!=null && PDFfile.startsWith("http")){
   fullViewer.executeCommand(Commands.OPENURL, new String[]{PDFfile});
}else if (PDFfile != null) { //If the plugin state is still active load and write PDF
   fullViewer.executeCommand(Commands.OPENFILE, new String[]{PDFfile});
}

To reduce the time consumption we added the option of caching the file on the machine so it is only downloaded once.

//Open the PDF File in the Plugin Window.
if (PDFfile != null && PDFfile.startsWith("http")) {
 
    String tmpDir = System.getProperty("java.io.tmpdir");
 
    String fileName=PDFfile.substring(1+PDFfile.lastIndexOf("/"));
 
    String fullPath=tmpDir+fileName;
 
    //see if already stored in TmpDir and cache if not
    File testFile=new File(fullPath);
    if(!testFile.exists()){
       //copy from http to file
       try {
          URL url = new URL(PDFfile);
          Files.copy(url.openStream(), new File(fullPath).toPath(),
                  StandardCopyOption.REPLACE_EXISTING);
       } catch (Exception e) {
       }
}
 
    fullViewer.executeCommand(Commands.OPENFILE, new String[]{fullPath});
}else if (PDFfile != null) { //If the plugin state is still active load and write PDF
    fullViewer.executeCommand(Commands.OPENFILE, new String[]{PDFfile});
}
}else if (PDFfile != null) { //If the plugin state is still active load and write PDF
    fullViewer.executeCommand(Commands.OPENFILE, new String[]{PDFfile});
 
}

Hopefully you have found this article useful.

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.
Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.
Sylwia

About Sylwia Dorota Kedzia

Sylwia is a developer at IDR Solutions working on JavaFX Development and on the NetBeans JavaFX Plugin. She is very passionate about programming and aims to improve constantly.

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>