Java-Swing嵌入浏览器(一)

今天要说的额是浏览器的第一个版本是用DJnative-swt和swt包开发的调用本地浏览器和webkit浏览器的示例

这是我的工程目录【源码见最后】:

image

 

src下为写的源码,lib为引入的swt和DJnative和mozilla接口包~

我们来看两个类,此两个类是嵌入webkiet mozilla的内核浏览器 要用到xulrunner

这里有一句代码 就是下面这句

//指定xulRunner路径 如不指定就调用系统注册的xulrunner 注册为 xulRunner目录下的xulrunner --register-global
        NSSystemPropertySWT.WEBBROWSER_XULRUNNER_HOME.set(System.getProperty("user.dir") + "/xulrunner");

如果没有写这句代码引入工程下的xulrunner,那么就必须要进行注册注册为xulrunner --register-global 卸载为 xulrunner --unregister-global

XPCOMDownloadManager.java 类 调用mozilla内核浏览器

/*
 * luwenbin006@163.com (luwenbin006@163.com)
 * http://www.luwenbin.com
 *
 * See the file "readme.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
package com.luwenbin.webbrowser;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.mozilla.interfaces.nsICancelable;
import org.mozilla.interfaces.nsIComponentRegistrar;
import org.mozilla.interfaces.nsIFactory;
import org.mozilla.interfaces.nsILocalFile;
import org.mozilla.interfaces.nsIMIMEInfo;
import org.mozilla.interfaces.nsIRequest;
import org.mozilla.interfaces.nsISupports;
import org.mozilla.interfaces.nsITransfer;
import org.mozilla.interfaces.nsIURI;
import org.mozilla.interfaces.nsIWebProgress;
import org.mozilla.interfaces.nsIWebProgressListener;
import org.mozilla.xpcom.Mozilla;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NSSystemPropertySWT;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.MozillaXPCOM;

/**
 * @author luwenbin006@163.com
 */
public class XPCOMDownloadManager
{

    public static JComponent createContent()
    {
        JPanel contentPane = new JPanel(new BorderLayout());
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
        //指定xulRunner路径 如不指定就调用系统注册的xulrunner 注册为 xulRunner目录下的xulrunner --register-global
        NSSystemPropertySWT.WEBBROWSER_XULRUNNER_HOME.set(System.getProperty("user.dir") + "/xulrunner");

        final JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.useXULRunnerRuntime());
        webBrowser.navigate("http://www.eclipse.org/downloads");
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        contentPane.add(webBrowserPanel, BorderLayout.CENTER);
        // Create an additional area to see the downloads in progress.
        final JPanel downloadsPanel = new JPanel(new GridLayout(0, 1));
        downloadsPanel.setBorder(BorderFactory.createTitledBorder("Download manager (on-going downloads are automatically added to this area)"));
        contentPane.add(downloadsPanel, BorderLayout.SOUTH);
        // We can only access XPCOM when it is properly initialized.
        // This happens when the web browser is created so we run our code in sequence.
        webBrowser.runInSequence(new Runnable()
        {
            public void run()
            {
                try
                {
                    nsIComponentRegistrar registrar = MozillaXPCOM.Mozilla.getComponentRegistrar();
                    String NS_DOWNLOAD_CID = "e3fa9D0a-1dd1-11b2-bdef-8c720b597445";
                    String NS_TRANSFER_CONTRACTID = "@mozilla.org/transfer;1";
                    registrar.registerFactory(NS_DOWNLOAD_CID, "Transfer", NS_TRANSFER_CONTRACTID, new nsIFactory()
                    {
                        public nsISupports queryInterface(String uuid)
                        {
                            if(uuid.equals(nsIFactory.NS_IFACTORY_IID) || uuid.equals(nsIFactory.NS_ISUPPORTS_IID))
                            {
                                return this;
                            }
                            return null;
                        }
                        public nsISupports createInstance(nsISupports outer, String iid)
                        {
                            return createTransfer(downloadsPanel);
                        }
                        public void lockFactory(boolean lock) {}
                    });
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(webBrowser, "Failed to register XPCOM download manager.\nPlease check your XULRunner configuration.", "XPCOM interface", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
        });
        return contentPane;
    }

    private static nsITransfer createTransfer(final JPanel downloadsPanel)
    {
        return new nsITransfer()
        {
            public nsISupports queryInterface(String uuid)
            {
                if(uuid.equals(nsITransfer.NS_ITRANSFER_IID) ||
                        uuid.equals(nsITransfer.NS_IWEBPROGRESSLISTENER2_IID) ||
                        uuid.equals(nsITransfer.NS_IWEBPROGRESSLISTENER_IID) ||
                        uuid.equals(nsITransfer.NS_ISUPPORTS_IID))
                {
                    return this;
                }
                return null;
            }
            private JComponent downloadComponent;
            private JLabel downloadStatusLabel;
            private String baseText;
            public void init(nsIURI source, nsIURI target, String displayName, nsIMIMEInfo MIMEInfo, double startTime, nsILocalFile tempFile, final nsICancelable cancelable)
            {
                downloadComponent = new JPanel(new BorderLayout(5, 5));
                downloadComponent.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
                JButton cancelDownloadButton = new JButton("Cancel");
                downloadComponent.add(cancelDownloadButton, BorderLayout.WEST);
                final String path = target.getPath();
                cancelDownloadButton.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        cancelable.cancel(Mozilla.NS_ERROR_ABORT);
                        removeDownloadComponent();
                        new File(path + ".part").delete();
                    }
                });
                baseText = "Downloading to " + path;
                downloadStatusLabel = new JLabel(baseText);
                downloadComponent.add(downloadStatusLabel, BorderLayout.CENTER);
                downloadsPanel.add(downloadComponent);
                downloadsPanel.revalidate();
                downloadsPanel.repaint();
            }
            public void onStateChange(nsIWebProgress webProgress, nsIRequest request, long stateFlags, long status)
            {
                if((stateFlags & nsIWebProgressListener.STATE_STOP) != 0)
                {
                    removeDownloadComponent();
                }
            }
            private void removeDownloadComponent()
            {
                downloadsPanel.remove(downloadComponent);
                downloadsPanel.revalidate();
                downloadsPanel.repaint();
            }
            public void onProgressChange64(nsIWebProgress webProgress, nsIRequest request, long curSelfProgress, long maxSelfProgress, long curTotalProgress, long maxTotalProgress)
            {
                long currentKBytes = curTotalProgress / 1024;
                long totalKBytes = maxTotalProgress / 1024;
                downloadStatusLabel.setText(baseText + " (" + currentKBytes + "/" + totalKBytes + ")");
            }
            public void onStatusChange(nsIWebProgress webProgress, nsIRequest request, long status, String message) {}
            public void onSecurityChange(nsIWebProgress webProgress, nsIRequest request, long state) {}
            public void onProgressChange(nsIWebProgress webProgress, nsIRequest request, int curSelfProgress, int maxSelfProgress, int curTotalProgress, int maxTotalProgress) {}
            public void onLocationChange(nsIWebProgress webProgress, nsIRequest request, nsIURI location) {}
        };
    }

    /* Standard main method to try that test as a standalone application. */
    public static void main(String[] args)
    {
        NativeInterface.open();
        UIUtils.setPreferredLookAndFeel();
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame("DJ Native Swing Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(createContent(), BorderLayout.CENTER);
                frame.setSize(800, 600);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
        NativeInterface.runEventPump();
    }

}

下面是运行效果:

image

XPCOMToggleEditionModer.java 类 调用mozilla内核浏览器 并启用编辑模式

/*
 * luwenbin006@163.com (luwenbin006@163.com)
 * http://www.luwenbin.com
 *
 * See the file "readme.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
package com.luwenbin.webbrowser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.mozilla.interfaces.nsIDOMDocument;
import org.mozilla.interfaces.nsIDOMNSHTMLDocument;
import org.mozilla.interfaces.nsIDOMWindow;
import org.mozilla.interfaces.nsIWebBrowser;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NSSystemPropertySWT;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.MozillaXPCOM;

/**
 * @author luwenbin006@163.com
 */
public class XPCOMToggleEditionMode
{

    public static JComponent createContent()
    {
        JPanel contentPane = new JPanel(new BorderLayout());
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
        //指定xulRunner路径 如不指定就调用系统注册的xulrunner 注册为 xulRunner目录下的xulrunner --register-global
        NSSystemPropertySWT.WEBBROWSER_XULRUNNER_HOME.set(System.getProperty("user.dir") + "/xulrunner");

        final JWebBrowser webBrowser = new JWebBrowser(JWebBrowser.useXULRunnerRuntime());
        webBrowser.navigate("http://www.google.com");
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        contentPane.add(webBrowserPanel, BorderLayout.CENTER);
        // Create an additional bar allowing to toggle the edition mode of the web browser.
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
        JCheckBox designModeCheckBox = new JCheckBox("Edition Mode (allows to type text or resize elements directly in the page)");
        designModeCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                nsIWebBrowser iWebBrowser = MozillaXPCOM.getWebBrowser(webBrowser);
                if(iWebBrowser == null)
                {
                    JOptionPane.showMessageDialog(webBrowser, "The XPCOM nsIWebBrowser interface could not be obtained.\nPlease check your XULRunner configuration.", "XPCOM interface", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                nsIDOMWindow window = iWebBrowser.getContentDOMWindow();
                nsIDOMDocument document = window.getDocument();
                nsIDOMNSHTMLDocument nsDocument = (nsIDOMNSHTMLDocument)document.queryInterface(nsIDOMNSHTMLDocument.NS_IDOMNSHTMLDOCUMENT_IID);
                nsDocument.setDesignMode(e.getStateChange() == ItemEvent.SELECTED ? "on" : "off");
            }
        });
        buttonPanel.add(designModeCheckBox);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
        return contentPane;
    }

    /* Standard main method to try that test as a standalone application. */
    public static void main(String[] args)
    {
        NativeInterface.open();
        UIUtils.setPreferredLookAndFeel();
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame("DJ Native Swing Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(createContent(), BorderLayout.CENTER);
                frame.setSize(800, 600);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
        NativeInterface.runEventPump();
    }

}

下面是运行效果:

image

SimpleWebBrowserExample.java 调用本机默认浏览器

/*
 * luwenbin006@163.com (luwenbin006@163.com)
 * http://www.luwenbin.com
 *
 * See the file "readme.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
package com.luwenbin.webbrowser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

/**
 * @author luwenbin006@163.com
 */
public class SimpleWebBrowserExample
{

    public static JComponent createContent()
    {
        JPanel contentPane = new JPanel(new BorderLayout());
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
        final JWebBrowser webBrowser = new JWebBrowser();
        webBrowser.navigate("http://www.google.com");
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        contentPane.add(webBrowserPanel, BorderLayout.CENTER);
        // Create an additional bar allowing to show/hide the menu bar of the web browser.
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
        JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible());
        menuBarCheckBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
            }
        });
        buttonPanel.add(menuBarCheckBox);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
        return contentPane;
    }

    /* Standard main method to try that test as a standalone application. */
    public static void main(String[] args)
    {
        NativeInterface.open();
        UIUtils.setPreferredLookAndFeel();
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame("DJ Native Swing Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(createContent(), BorderLayout.CENTER);
                frame.setSize(800, 600);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
        NativeInterface.runEventPump();
    }

}

下面是运行效果:

image

放出源码下载地址【鄙视伸手党】:http://pan.baidu.com/s/1mgmjgso

posted @ 2014-11-26 21:48  luwenbin  阅读(32277)  评论(5编辑  收藏  举报