標籤:java
import java.awt.BorderLayout;import java.awt.Toolkit;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.Date;import java.util.List;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;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;import chrriis.dj.nativeswing.swtimpl.components.WebBrowserCommandEvent;import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;import chrriis.dj.nativeswing.swtimpl.components.WebBrowserListener;import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowOpeningEvent;import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowWillOpenEvent;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;import com.mohe.utils.system.L;/** * 主表單 * * @author dxm * */public class MainFrame extends JFrame {private static final long serialVersionUID = -6348199501339114346L;// 最後操作時間public long lastTime = new Date().getTime();// 瀏覽器表單private JWebBrowser webBrowser = null;private JLabel lbl = null;private JPanel panel = null;public int pageFlag = 1;/** * 構造器 */public MainFrame() {initData();initGUI();addListance();}private void initData() {Toolkit toolkit = Toolkit.getDefaultToolkit();this.setUndecorated(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(600, 500);this.setLocationRelativeTo(null);this.setSize(toolkit.getScreenSize());this.setLocation(0, 0);webBrowser = new JWebBrowser();webBrowser.navigate(Config.local + Config.index);webBrowser.setBarsVisible(false);webBrowser.setMenuBarVisible(false);webBrowser.setLocationBarVisible(false);webBrowser.setButtonBarVisible(false);webBrowser.setStatusBarVisible(false);ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\城市.gif");File img = new File(System.getProperty("user.dir") + "\\" + Config.img);if (img.exists()) {icon = new ImageIcon(img.getAbsolutePath());}lbl = new JLabel(icon);panel = new JPanel(new BorderLayout());}private void initGUI() {panel.add(webBrowser, BorderLayout.CENTER);this.setContentPane(panel);}private void addListance() {webBrowser.addWebBrowserListener(new WebBrowserListener() {@Overridepublic void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {lastTime = new Date().getTime();L.info("___________ windowWillOpen");}@Overridepublic void windowOpening(WebBrowserWindowOpeningEvent arg0) {lastTime = new Date().getTime();L.info("___________ windowOpening");}@Overridepublic void windowClosing(WebBrowserEvent arg0) {lastTime = new Date().getTime();L.info("___________ windowClosing");}@Overridepublic void titleChanged(WebBrowserEvent arg0) {lastTime = new Date().getTime();L.info("___________ titleChanged");}@Overridepublic void statusChanged(WebBrowserEvent arg0) {lastTime = new Date().getTime();L.info("___________ statusChanged");}@Overridepublic void locationChanging(WebBrowserNavigationEvent arg0) {lastTime = new Date().getTime();L.info("___________ locationChanging");}@Overridepublic void locationChanged(WebBrowserNavigationEvent arg0) {lastTime = new Date().getTime();L.info("___________ locationChanged");// 判斷當前頁面等級String locationURL = arg0.getNewResourceLocation();int index = locationURL.indexOf("pageFlag=");if (index != -1) {pageFlag = Integer.parseInt(locationURL.substring(index + 9, index + 10));} else {pageFlag = 1;}System.out.println("____________________ pageFlag:" + pageFlag);// 判斷下載檔案進行列印index = locationURL.indexOf("fileName=");System.out.println("____________________ file Index:" + index);if (index != -1) {String fs = locationURL.substring(index + 9);L.info("______________________ fileName = " + fs);String[] fns = fs.split(",");try {List<File> flist = downNetFile(Arrays.asList(fns));doPrintDoc(flist);} catch (IOException e) {e.printStackTrace();}}SwingUtilities.invokeLater(new Runnable() {public void run() {webBrowser.setVisible(false);webBrowser.setVisible(true);webBrowser.updateUI();webBrowser.repaint();panel.updateUI();panel.repaint();panel.validate();}});}@Overridepublic void locationChangeCanceled(WebBrowserNavigationEvent arg0) {lastTime = new Date().getTime();L.info("___________ locationChangeCanceled");}@Overridepublic void loadingProgressChanged(WebBrowserEvent arg0) {lastTime = new Date().getTime();L.info("___________ loadingProgressChanged");}@Overridepublic void commandReceived(WebBrowserCommandEvent arg0) {lastTime = new Date().getTime();L.info("___________ commandReceived");}});lbl.addMouseListener(new MouseListener() {@Overridepublic void mouseReleased(MouseEvent arg0) {}@Overridepublic void mousePressed(MouseEvent arg0) {}@Overridepublic void mouseExited(MouseEvent arg0) {}@Overridepublic void mouseEntered(MouseEvent arg0) {}@Overridepublic void mouseClicked(MouseEvent arg0) {change(2);}});}/** * 屏保切換 */public void change(final int type) {lastTime = new Date().getTime();SwingUtilities.invokeLater(new Runnable() {public void run() {if (type == 1) {// 二級,三級頁面跳首頁if (pageFlag == 2 || pageFlag == 3) {webBrowser.navigate(Config.local + Config.index);}// else {// webBrowser.setVisible(false);// panel.add(lbl, BorderLayout.CENTER);// }} else {panel.remove(lbl);webBrowser.setVisible(true);}panel.validate();}});}/** * 下載網路檔案 * * @param nameList * @throws IOException */private List<File> downNetFile(List<String> nameList) throws IOException {L.info("________________________ downNetFile1");if (null == nameList || nameList.isEmpty()) {return new ArrayList<File>();}List<File> flist = new ArrayList<File>();for (String name : nameList) {L.info("________________________ downNetFile2");URL url = new URL(Config.local + "/excel/" + name);HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();// 佈建要求資訊httpConnection.setRequestProperty("GET", "/down.zip HTTP/1.1");// 設定接受資訊httpConnection.setRequestProperty("Accept", "image/gif,image/x-xbitmap,application/msword");// 設定串連資訊httpConnection.setRequestProperty("Connection", "Keep-Alive");// 獲得輸入資料流InputStream input = httpConnection.getInputStream();// 建立隨機檔案flist.add(new File(name));RandomAccessFile oSavedFile = new RandomAccessFile(name, "rw");byte[] b = new byte[1024];int nRead;// 從輸入資料流中讀入位元組流,然後寫到檔案中while ((nRead = input.read(b, 0, 1024)) > 0) {oSavedFile.write(b, 0, nRead);}input.close();oSavedFile.close();httpConnection.disconnect();}return flist;}private void doPrintDoc(List<File> fileList) {if (null == fileList || fileList.isEmpty()) {return;}// 初始化組件ComThread.InitSTA();L.info("___________________ start Print 1");for (File file : fileList) {if (file == null || !file.exists()) {return;}L.info("___________________ start Print 2 :" + file.getAbsolutePath());int index = file.getName().indexOf(".");String extName = file.getName().toUpperCase().substring(index + 1);String comApp = "Word.Application";String property = "Documents";if (extName.equals("DOC") || extName.equals("DOCX") || extName.equals("WPS")) {comApp = "Word.Application";property = "Documents";} else if (extName.equals("XLS") || extName.equals("XLSX") || extName.equals("ET")) {comApp = "Excel.Application";property = "Workbooks";} else if (extName.equals("PPT") || extName.equals("PPTX") || extName.equals("DPS")) {comApp = "PowerPoint.Application";property = "Presentations";}L.info("___________________ start Print 3 :" + comApp);ActiveXComponent axc = new ActiveXComponent(comApp);try {if (!property.equals("Presentations")) {Dispatch.put(axc, "Visible", new Variant(false));}Dispatch document = axc.getProperty(property).toDispatch();Dispatch doc = null;if (property.equals("Presentations")) {doc = Dispatch.call(document, "Open", file.getAbsolutePath(), true, true, false).toDispatch();} else {doc = Dispatch.invoke(document, "Open", Dispatch.Method, new Object[] { file.getAbsolutePath() }, new int[1]).toDispatch();}L.info("___________________ start Print 3 :" + file.getAbsolutePath());Dispatch.call(doc, "PrintOut");Dispatch.call(doc, "Close");if (!property.equals("Presentations")) {axc.invoke("Quit", new Variant[] {});}} catch (Exception e) {e.printStackTrace();} finally {comApp = "";property = "";}}ComThread.Release();}public static void main(String[] args) {L.APPNAME = "Street_cs";L.LOGF = true;L.initLog();UIUtils.setPreferredLookAndFeel();NativeInterface.open();SwingUtilities.invokeLater(new Runnable() {public void run() {MainFrame frame = new MainFrame();frame.setVisible(true);new ChangeWait(frame).tryStart();new ServeSocket();}});NativeInterface.runEventPump();}}
Java Swing嵌入瀏覽器