View website source code

Source: Internet
Author: User

Package com. longruan. lrgis. Gui;

Import java. Io. bufferedreader;
Import java. Io. bufferedwriter;
Import java. Io. filewriter;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java.net. url;

Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. browser. browser;
Import org. Eclipse. SWT. Events. selectionadapter;
Import org. Eclipse. SWT. Events. selectionevent;
Import org. Eclipse. SWT. Widgets. Button;
Import org. Eclipse. SWT. Widgets. Combo;
Import org. Eclipse. SWT. Widgets. Control;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. filedialog;
Import org. Eclipse. SWT. Widgets. label;
Import org. Eclipse. SWT. Widgets. Menu;
Import org. Eclipse. SWT. Widgets. menuitem;
Import org. Eclipse. SWT. Widgets. MessageBox;
Import org. Eclipse. SWT. Widgets. progressbar;
Import org. Eclipse. SWT. Widgets. shell;
Import org. Eclipse. SWT. Widgets. tabfolder;
Import org. Eclipse. SWT. Widgets. tabitem;
Import org. Eclipse. SWT. Widgets. text;

Import com. longruan. lrgis. Gui. util. swtresourcemanager;

Public class gethtmlcode {
Private Label label_2;
Private Label label_1;
Private tabfolder;
Private Label label;
Private button savebtn;
Private button exitbtn;
Private button resetbtn;
Private text filenametext;

Private text textcode;

Private combo;

Private text httptext;

Protected shell;

Private button viewbtn;

Private browser;

Private progressbar;

// Main function, Main Entry
Public static void main (string [] ARGs ){
Try {
Gethtmlcode window = new gethtmlcode ();
Window. open ();
} Catch (exception e ){
E. printstacktrace ();
}
}

// Display the device
Public void open (){
Final display = display. getdefault ();
Createcontents ();
Shell. open ();
Shell. layout ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ())
Display. Sleep ();
}
}

// View the source code of the input webpage
Public void ViewCode (){
Try {
Viewbtn. setenabled (false); // after you click the button, the set button is unavailable.
URL url = new URL (httptext. gettext ());
Bufferedreader in = new bufferedreader (New inputstreamreader (URL
. Openstream (), Combo. gettext ()));
Textcode. settext ("");
String tmpcode = "";
Int I = 0;
Progressbar. setselection (I );
While (tmpcode = in. Readline ())! = NULL ){
Textcode. append (tmpcode + '/N ');
Progressbar. setselection (I );
I ++;
}
Progressbar. setselection (5000 );
Viewbtn. setenabled (true );
Urlopenmess ();
} Catch (exception e ){
E. printstacktrace ();
}
}

// Exit prompt
Public void exit (){
MessageBox mess = new MessageBox (shell, SWT. OK | SWT. Cancel
| SWT. icon_question );
Mess. settext ("quit ");
Mess. setmessage ("are you sure you want to exit ");
If (mess. open () = SWT. OK ){
System. Exit (0 );
}
}

// Prompt whether the source code needs to be saved
Public void nullmessbox (){
MessageBox mess = new MessageBox (shell, SWT. OK | SWT. icon_warning );
Mess. settext ("prompt ");
Mess. setmessage ("no source code ");
Mess. open ();
}

Public void savecode (){
// Determine whether the source code exists. If no source code exists, return directly without any operation.
If (textcode. gettext (). Equals ("")){
Nullmessbox ();
Return;
}
// Create a dialog box and prompt the path to be saved
Filedialog file = new filedialog (shell, SWT. Save );
String [] filter = {"*. txt", "*. html", "*. htm ","*.*"};
File. setfilterextensions (filter );
String filename = file. open ();
// Verify whether the file name to be saved is entered. If no file name exists, the system returns the file directly without any operation.
If (filename = NULL)
Return;
Try {
Filewriter in = new filewriter (filename );
Bufferedwriter out = new bufferedwriter (in );
Out. Write (textcode. gettext ());
Out. Flush ();
Out. Close ();
Filenametext. settext (filename); // after saving, the SAVE path is displayed.
} Catch (ioexception e ){
E. printstacktrace ();
}
}

// After the code is displayed, the system prompts whether to browse the webpage.
Public void urlopenmess (){
MessageBox mess = new MessageBox (shell, SWT. OK | SWT. Cancel
| SWT. icon_question );
Mess. settext ("prompt ");
Mess. setmessage ("whether to enable Web Page Preview ");
If (mess. open () = SWT. OK ){
Browser. seturl (httptext. gettext ());
}
}

// Draw a form
Protected void createcontents (){
Shell = new shell (SWT. min );
Shell. setimage (swtresourcemanager
. Getimage (gethtmlcode. class, "ICO. ICO "));
Shell. setsize (685,554 );

// Set the center of the form to obtain the screen resolution and calculate it with the program size.
Int x = (shell. getmonitor (). getclientarea (). Width-shell. getsize (). X)/2;
Int y = (shell. getmonitor (). getclientarea (). Height-shell. getsize (). Y)/2;
Shell. setlocation (x, y );
Shell
. Settext ("Web source code viewer revised by hmilyld 04.11 ------ http://www.hmilyld.cn ");

Final menu = new menu (shell, SWT. bar );
Shell. setmenubar (menu );

Final menuitem = new menuitem (menu, SWT. Cascade );
Menuitem. settext ("file (& F )");

Final menu menu_1 = new menu (menuitem );
Menuitem. setmenu (menu_1 );

// Click the exit button.
Final menuitem exititem = new menuitem (menu_1, SWT. None );
Exititem. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent arg0 ){
Exit ();
}
});
Exititem. settext ("exit (& Q )");

Label = new label (shell, SWT. Separator | SWT. Horizontal );
Label. setbounds (0,459,679, 13 );

Label_1 = new label (shell, SWT. Right );
Label_1.settext ("webpage code viewer revised by hmilyld 04.11 ");
Label_1.setbounds (420,478,249, 13 );

Tabfolder = new tabfolder (shell, SWT. None );
Tabfolder. setbounds (10, 83,659,370 );

Final tabitem = new tabitem (tabfolder, SWT. None );
Tabitem. settext ("source code ");

Textcode = new text (tabfolder, SWT. v_scroll | SWT. Border );
Tabitem. setcontrol (textcode );

Final tabitem tabitem_1 = new tabitem (tabfolder, SWT. None );
Tabitem_1.settext ("webpage preview ");

Browser = new browser (tabfolder, SWT. None );
Browser. seturl ("");
Tabitem_1.setcontrol (browser );

Label_2 = new label (shell, SWT. None );
Label_2.settext ("Enter the webpage address of the source code: (including http ://)");
Label_2.setbounds (ten, 10,270, 13 );

Httptext = new text (shell, SWT. Border );
Httptext. settooltiptext ("Enter the webpage address, including http ://");
Httptext. settext ("http://www.hmilyld.cn ");
Httptext. setbounds (ten, 27,362, 25 );

// Click View.
Viewbtn = new button (shell, SWT. None );
Viewbtn. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent arg0 ){
ViewCode ();
}
});
Viewbtn. settext ("View ");
Viewbtn. setbounds (456, 29, 67, 23 );

// Click the Clear button.
Resetbtn = new button (shell, SWT. None );
Resetbtn. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent arg0 ){
Textcode. settext ("");
}
});
Resetbtn. setbounds (529, 29, 67, 23 );
Resetbtn. settext ("clear ");

Combo = new combo (shell, SWT. None );
Combo. settooltiptext ("webpage encoding type. If garbled characters appear, select here ");
Combo. settext ("gb2312 ");
Combo. Add ("gb2312 ");
Combo. Add ("UTF-8 ");
Combo. setbounds (383, 31, 67, 21 );

// Click the exit button.
Exitbtn = new button (shell, SWT. None );
Exitbtn. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent arg0 ){
Exit ();
}
});
Exitbtn. setbounds (602, 29, 67, 23 );
Exitbtn. settext ("quit ");

Progressbar = new progressbar (shell, SWT. None );
Progressbar. setmaximum (5000 );
Progressbar. setbounds (10, 62,270, 17 );
Progressbar. settooltiptext ("Code read progress bar ");

Filenametext = new text (shell, SWT. Border );
Filenametext. settooltiptext ("Source code storage path ");
Filenametext. setbounds (286, 58,249, 25 );

// Save the source code button
Savebtn = new button (shell, SWT. None );
Savebtn. settooltiptext ("Save the page source code ");
Savebtn. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent arg0 ){
Savecode ();
}
});
Savebtn. setbounds (539, 58,130, 23 );
Savebtn. settext ("Save Code ");
Shell. settablist (new control [] {httptext, combo, viewbtn, resetbtn,
Exitbtn, savebtn });
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.