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;/** * Main form * * @author DXM * */public class MainFrame extends JFrame {private static final long Serialversionuid = -6348199501339114346l;//Last operation time public long lasttime = new Date (). GetTime (); Browser form private Jwebbrowser WebBrowser = null;private JLabel lbl = null;private JPanel panel = null;public int pageflag = 1;/** * Constructor */public MainFrame () {initdata (); Initgui (); Addlistance ();} private void InitData () {Toolkit Toolkit = Toolkit.getdefaulttoolkit (); this.setundecorated (true); This.setdefaultcloseoperation (Jframe.exit_on_close); This.setsize (n); 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") + "\ \ City. gif"); File img = new file (System.getproperty ("User.dir") + "\ \" + config.img), if (Img.exists ()) {icon = new ImageIcon (img.getabs Olutepath ());} LBL = new JLabel (icon);p anel = 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");//Determine the current page level string locationurl = Arg0.getnewresourcelocation (); int index = Locationurl.indexof ("pageflag="); if (Index! =-1) {Pageflag = Integer.parseint (locationurl.substring (index + 9, index + 1 0));} else {pageflag = 1;} System.out.println ("____________________ pageflag:" + pageflag);//Determine the download file to print 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));d Oprintdoc (flist);} catch ( IOException e) {e.printstacktrace ();}} Swingutilities.invokelater (New Runnable () {public void run () {webbrowser.setvisible (false); Webbrowser.setvisible ( true); Webbrowser.updateui (); Webbrowser.repaint ();p anel.updateui ();p anel.repaint ();p anel.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);}});} /** * Screensaver Toggle */public void change (final int type) {lasttime = new Date (). GetTime (); Swingutilities.invokelater (New Runnable () {public void run () {if (type = = 1) {//two level, three-level page jump Home 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 ();}});} /** * Download Network files * * @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 ();// Set the request information Httpconnection.setrequestproperty ("GET", "/down.zip http/1.1");//Set the Accept message Httpconnection.setrequestproperty (" Accept "," Image/gif,image/x-xbitmap,application/msword ");//Set Connection information Httpconnection.setrequestproperty (" Connection " , "keep-alive");//Get input stream InputStream input = Httpconnection.getinputstream ();//create Random file Flist.add (new file name); Randomaccessfile osavedfile = new Randomaccessfile (name, "RW"), byte[] b = new Byte[1024];int nread;//reads the byte stream from the input stream and writes to the file W Hile ((nread = Input.read (b, 0, 1024x768)) > 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;} Initialize component 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 = "Wo Rd. Application ";p Roperty =" 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 ();D Ispatch 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]). Todisp Atch ();} L.info ("___________________ start Print 3:" + File.getabsolutepath ());D Ispatch.call (Doc, "PrintOut");D Ispatch.call ( Doc, "Close"), if (!property.equals ("Presentations")) {Axc.invoke ("Quit", new variant[] {});}} catch (Exception e) {e.printstacktrace ();} finally {Comapp = "";p roperty = "";}} 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 Embedded Browser