Over a million developers have joined DZone.

Reusing Existing NetBeans IDE JavaFX Infrastructure in NetBeans IDE Plugins

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Let's install the JavaFX WebView in a TopComponent in NetBeans IDE, reusing a lot of JavaFX infrastructure that is already in NetBeans IDE, since the 7.3 release for the support of the official JavaFX browser in 7.3.

That means, no need for JavaFX native libraries, nor for the javafxrt.jar.

The result:

The code:

public final class DeveloperFAQTopComponent extends TopComponent {

    private static URLClassLoader browserCL;

    public DeveloperFAQTopComponent() {
        initComponents();
        setName(Bundle.CTL_DeveloperFAQTopComponent());
        setToolTipText(Bundle.HINT_DeveloperFAQTopComponent());
        WebBrowser browser = createBrowser(null);
        browser.setURL("http://wiki.netbeans.org/NetBeansDeveloperFAQ");
        setLayout(new BorderLayout());
        add(browser.getComponent(), BorderLayout.CENTER);
    }

    private static String[] getFXClassPath() {
        Collection<? extends WebBrowserImplProvider.JFXRuntimePathProvider> pathProviders =
                Lookup.getDefault().lookupAll(WebBrowserImplProvider.JFXRuntimePathProvider.class);
        for (WebBrowserImplProvider.JFXRuntimePathProvider rtPathProvider : pathProviders) {
            String rtPath = rtPathProvider.getJFXRuntimePath();
            if (rtPath == null) {
                continue;
            }
            return new String[]{
                        rtPath + File.separatorChar + "lib" + File.separatorChar + "jfxrt.jar" //NOI18N
                    };
        }
        return null;
    }
   
    static WebBrowser createBrowser(File runtimePath) {
        ClassLoader cl = getBrowserClassLoader(runtimePath);
        try {
            if (cl != null) {
                // test that JavaFX has latest required APIs:
                cl.loadClass("com.sun.javafx.scene.web.Debugger");
                Class platform = cl.loadClass("javafx.application.Platform");
                Method m = platform.getMethod("setImplicitExit", boolean.class);
            }
        } catch (Throwable ex) {
            JOptionPane.showMessageDialog(null, "JavaFX runtime is too old - "
                    + "minimum version required is 2.2.0b20");
        }
        try {
            if (cl != null) {
                Class impl = cl.loadClass("org.netbeans.core.browser.webview.ext.WebBrowserImpl");
                Constructor c = impl.getConstructor(new Class[]{});
                return (WebBrowser) c.newInstance(new Object[]{});
            }
        } catch (Throwable ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
        return null;
    }

    private static URLClassLoader createBrowserClassLoader(File runtimePath) {
        synchronized (WebBrowserImplProvider.class) {
            File extjar = InstalledFileLocator.getDefault().locate("modules/ext/core.browser.webview-ext.jar",
                    "org.netbeans.core.browser.webview", false); // NOI18N
            if (extjar == null) {
                return null;
            }
            String[] fxpath = null;
            if (null == runtimePath) {
                fxpath = getFXClassPath();
            } else {
                fxpath = new String[]{new File(new File(runtimePath, "lib"), "jfxrt.jar").getAbsolutePath()};
            }
            if (fxpath == null) {
                return null;
            }
            List<URL> urls = new ArrayList<URL>();
            try {
                urls.add(extjar.toURI().toURL());
                for (String fx : fxpath) {
                    urls.add(new File(fx).toURI().toURL());
                }
                return new URLClassLoader(urls.toArray(new URL[]{}),
                        WebBrowserImplProvider.class.getClassLoader());
            } catch (MalformedURLException m) {
                return null;
            }
        }
    }

    private static ClassLoader getBrowserClassLoader(File runtimePath) {
        synchronized (WebBrowserImplProvider.class) {
            if (null != runtimePath) {
                return createBrowserClassLoader(runtimePath);
            }
            if (browserCL != null) {
                return browserCL;
            }
            browserCL = createBrowserClassLoader(null);
            return browserCL;
        }
    }

    ...
    ...
    ...

Required import statements:

import java.awt.BorderLayout;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JOptionPane;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.core.browser.api.WebBrowser;
import org.netbeans.core.browser.webview.WebBrowserImplProvider;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.modules.InstalledFileLocator;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

The above code comes from here:

http://hg.netbeans.org/main/file/8029769c5178/core.browser.webview/src/org/netbeans/core/browser/webview/WebBrowserImplProvider.java

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}