/** 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.*/
packagecom.luwenbin.webbrowser;importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjavax.swing.BorderFactory;importjavax.swing.JButton;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.SwingUtilities;importorg.mozilla.interfaces.nsICancelable;importorg.mozilla.interfaces.nsIComponentRegistrar;importorg.mozilla.interfaces.nsIFactory;importorg.mozilla.interfaces.nsILocalFile;importorg.mozilla.interfaces.nsIMIMEInfo;importorg.mozilla.interfaces.nsIRequest;importorg.mozilla.interfaces.nsISupports;importorg.mozilla.interfaces.nsITransfer;importorg.mozilla.interfaces.nsIURI;importorg.mozilla.interfaces.nsIWebProgress;importorg.mozilla.interfaces.nsIWebProgressListener;importorg.mozilla.xpcom.Mozilla;importchrriis.common.UIUtils;importchrriis.dj.nativeswing.swtimpl.NSSystemPropertySWT;importchrriis.dj.nativeswing.swtimpl.NativeInterface;importchrriis.dj.nativeswing.swtimpl.components.JWebBrowser;importchrriis.dj.nativeswing.swtimpl.components.MozillaXPCOM;/***@authorluwenbin006@163.com*/
public classXPCOMDownloadManager
{public staticJComponent createContent()
{
JPanel contentPane= new JPanel(newBorderLayout());
JPanel webBrowserPanel= new JPanel(newBorderLayout());
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 = newJWebBrowser(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(newRunnable()
{public voidrun()
{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, newnsIFactory()
{publicnsISupports queryInterface(String uuid)
{if(uuid.equals(nsIFactory.NS_IFACTORY_IID) ||uuid.equals(nsIFactory.NS_ISUPPORTS_IID))
{return this;
}return null;
}publicnsISupports createInstance(nsISupports outer, String iid)
{returncreateTransfer(downloadsPanel);
}public void lockFactory(booleanlock) {}
});
}catch(Exception e)
{
JOptionPane.showMessageDialog(webBrowser,"Failed to register XPCOM download manager.\nPlease check your XULRunner configuration.", "XPCOM interface", JOptionPane.ERROR_MESSAGE);return;
}
}
});returncontentPane;
}private static nsITransfer createTransfer(finalJPanel downloadsPanel)
{return newnsITransfer()
{publicnsISupports 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;
}privateJComponent downloadComponent;privateJLabel downloadStatusLabel;privateString baseText;public void init(nsIURI source, nsIURI target, String displayName, nsIMIMEInfo MIMEInfo, double startTime, nsILocalFile tempFile, finalnsICancelable 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(newActionListener()
{public voidactionPerformed(ActionEvent e)
{
cancelable.cancel(Mozilla.NS_ERROR_ABORT);
removeDownloadComponent();new File(path + ".part").delete();
}
});
baseText= "Downloading to " +path;
downloadStatusLabel= newJLabel(baseText);
downloadComponent.add(downloadStatusLabel, BorderLayout.CENTER);
downloadsPanel.add(downloadComponent);
downloadsPanel.revalidate();
downloadsPanel.repaint();
}public void onStateChange(nsIWebProgress webProgress, nsIRequest request, long stateFlags, longstatus)
{if((stateFlags & nsIWebProgressListener.STATE_STOP) != 0)
{
removeDownloadComponent();
}
}private voidremoveDownloadComponent()
{
downloadsPanel.remove(downloadComponent);
downloadsPanel.revalidate();
downloadsPanel.repaint();
}public void onProgressChange64(nsIWebProgress webProgress, nsIRequest request, long curSelfProgress, long maxSelfProgress, long curTotalProgress, longmaxTotalProgress)
{long currentKBytes = curTotalProgress / 1024;long totalKBytes = maxTotalProgress / 1024;
downloadStatusLabel.setText(baseText+ " (" + currentKBytes + "/" + totalKBytes + ")");
}public void onStatusChange(nsIWebProgress webProgress, nsIRequest request, longstatus, String message) {}public void onSecurityChange(nsIWebProgress webProgress, nsIRequest request, longstate) {}public void onProgressChange(nsIWebProgress webProgress, nsIRequest request, int curSelfProgress, int maxSelfProgress, int curTotalProgress, intmaxTotalProgress) {}public voidonLocationChange(nsIWebProgress webProgress, nsIRequest request, nsIURI location) {}
};
}/*Standard main method to try that test as a standalone application.*/
public static voidmain(String[] args)
{
NativeInterface.open();
UIUtils.setPreferredLookAndFeel();
SwingUtilities.invokeLater(newRunnable()
{public voidrun()
{
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();
}
}