Web browser compiled by SWT

Source: Internet
Author: User

I want to use RCP to do this. I found it when I was searching for information, but I did not need to mention it. The Code is as follows:
Package SRC;
Import org. Eclipse. SWT. SWT;
Import org. Eclipse. SWT. browser. browser;
Import org. Eclipse. SWT. browser. closewindowlistener;
Import org. Eclipse. SWT. browser. locationevent;
Import org. Eclipse. SWT. browser. locationlistener;
Import org. Eclipse. SWT. browser. openwindowlistener;
Import org. Eclipse. SWT. browser. progressevent;
Import org. Eclipse. SWT. browser. progresslistener;
Import org. Eclipse. SWT. browser. statustextevent;
Import org. Eclipse. SWT. browser. statustextlistener;
Import org. Eclipse. SWT. browser. javaswevent;
Import org. Eclipse. SWT. Events. selectionadapter;
Import org. Eclipse. SWT. Events. selectionevent;
Import org. Eclipse. SWT. layout. formattachment;
Import org. Eclipse. SWT. layout. formdata;
Import org. Eclipse. SWT. layout. formlayout;
Import org. Eclipse. SWT. layout. griddata;
Import org. Eclipse. SWT. layout. gridlayout;
Import org. Eclipse. SWT. Widgets. Button;
Import org. Eclipse. SWT. Widgets. Combo;
Import org. Eclipse. SWT. Widgets. composite;
Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. label;
Import org. Eclipse. SWT. Widgets. shell;

/**
* This class implements a web browser
*/
Public class testbrowser {
// The "at rest" text of the throbber

Private Label throbber;

Private button;

Private combo URL;

Private button button_4;

Private button button_3;

Private browser;

Private browser browser2;

Private Label status;

Private button button_2;

Private button button_1;

Private Static final string at_rest = "ready ";

Private string [] urllist = new string [256];

Int urllistitemcount = 0;

/**
* Runs the application
*
* @ Param location
* The initial location to display
*/
Public void run (string location ){
Display display = New Display ();
Shell shell = new shell (Display );
Shell. settext ("advanced browser ");
Createcontents (shell, location );
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
Display. Dispose ();
}

/**
* Creates the main window's contents
*
* @ Param Shell
* The Main Window
* @ Param location
* The initial location
*/
Public void createcontents (shell, string location ){
Shell. setlayout (New formlayout ());

// Create the composite to hold the buttons and text field

Composite controls = new composite (shell, SWT. None );
Formdata DATA = new formdata ();
Data. Top = new formattachment (0, 0 );
Data. Left = new formattachment (0, 0 );
Data. Right = new formattachment (100, 0 );
Controls. setlayoutdata (data );

// Create the status bar

Status = new label (shell, SWT. None );
Data = new formdata ();
Data. Left = new formattachment (0, 0 );
Data. Right = new formattachment (100, 0 );
Data. Bottom = new formattachment (100, 0 );
Status. setlayoutdata (data );

// Create the web browser

Browser = new browser (shell, SWT. Border );
Browser. addopenwindowlistener (New openwindowlistener (){
Public void open (incluwevent arg0 ){
String urlstr = arg0.browser. geturl ();
Int flag = 1;
Browser. setvisible (false );
Browser2.setvisible (true );
Arg0.browser = browser2;
For (INT I = 0; I <urllistitemcount; I ++ ){
If (urllist [I]. Equals (urlstr )){
Flag = 0;
}
}
If (flag = 1 ){
Urllist [urllistitemcount] = urlstr;
URL. Add (urlstr );
URL. settext (urlstr );
Urllistitemcount ++;
}
}
});
Data = new formdata ();
Data. Top = new formattachment (Controls );
Data. Bottom = new formattachment (Status );
Data. Left = new formattachment (0, 0 );
Data. Right = new formattachment (100, 0 );
Browser. setlayoutdata (data );

// Create the web browser

Browser2 = new browser (shell, SWT. Border );
Browser2.addopenwindowlistener (New openwindowlistener (){
Public void open (incluwevent arg0 ){
String urlstr = arg0.browser. geturl ();
Int flag = 1;
Browser2.setvisible (false );
Browser. setvisible (true );
Arg0.browser = browser;
For (INT I = 0; I <urllistitemcount; I ++ ){
If (urllist [I]. Equals (urlstr )){
Flag = 0;
}
}
If (flag = 1 ){
Urllist [urllistitemcount] = urlstr;
URL. Add (urlstr );
URL. settext (urlstr );
Urllistitemcount ++;
}
}
});
Data = new formdata ();
Data. Top = new formattachment (Controls );
Data. Bottom = new formattachment (Status );
Data. Left = new formattachment (0, 0 );
Data. Right = new formattachment (100, 0 );
Browser2.setlayoutdata (data );

// Create the controls and wire them to the browser

Controls. setlayout (New gridlayout (7, false ));

// Create the back button

Button_1 = new button (controls, SWT. Push );
Button_1.settext ("back ");
Button_1.addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
Browser. Back ();
}
});

// Create the forward button

Button_2 = new button (controls, SWT. Push );
Button_2.settext ("Forward ");
Button_2.addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
Browser. Forward ();
}
});

// Create the refresh button

Button_3 = new button (controls, SWT. Push );
Button_3.settext ("refresh ");
Button_3.addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
Browser. Refresh ();
}
});

// Create the stop button

Button_4 = new button (controls, SWT. Push );
Button_4.settext ("stop ");
Button_4.addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
Browser. Stop ();
}
});

// Create the address entry field and set focus to it

Url = new combo (controls, SWT. arrow_down );
URL. setlayoutdata (New griddata (296, SWT. Default ));
URL. setsize (600, URL. getsize (). y );
URL. setfocus ();

// Create the go button

Button = new button (controls, SWT. Push );
Button. settext ("go ");
Button. addselectionlistener (New selectionadapter (){
Public void widgetselected (selectionevent event ){
String urlstr = URL. gettext ();
Int flag = 1;
Browser. seturl (urlstr );
For (INT I = 0; I <urllistitemcount; I ++ ){
If (urllist [I]. Equals (urlstr )){
Flag = 0;
}
}
If (flag = 1 ){
Urllist [urllistitemcount] = urlstr;
URL. Add (urlstr );
Urllistitemcount ++;
}
}
});

// Create the animated "throbber"

Throbber = new label (controls, SWT. None );
Throbber. settext (at_rest );

// Allow users to hit enter to go To the typed URL

Shell. setdefaultbutton (button );

// Add event handlers

Browser. addclosewindowlistener (New advancedclosewindowlistener ());
Browser. addlocationlistener (New advancedlocationlistener (URL ));
Browser. addprogresslistener (New advancedprogresslistener (throbber ));
Browser. addstatustextlistener (New advancedstatustextlistener (Status ));

// Go to the initial URL

If (location! = NULL ){
Browser. seturl (location );
}
}

/**
* This class implements a closewindowlistener for testbrowser
*/
Class advancedclosewindowlistener implements closewindowlistener {
/**
* Called when the parent window shoshould be closed
*/
Public void close (incluwevent event ){
// Close the parent window

(Browser) event. widget). getshell (). Close ();
}
}

/**
* This class implements a locationlistener for testbrowser
*/
Class advancedlocationlistener implements locationlistener {
// The address text box to update

Private combo location;

/**
* Constructs an advancedlocationlistener
*
* @ Param text
* The address text box to update
*/
Public advancedlocationlistener (combo text ){
// Store the address box for updates

Location = text;
}

/**
* Called before the location changes
*
* @ Param event
* The Event
*/
Public void changing (locationevent event ){
// Show the location that's loading

Location. settext ("loading" + event. Location + "...");
}

/**
* Called after the location changes
*
* @ Param event
* The Event
*/
Public void changed (locationevent event ){
// Show the loaded location

Location. settext (event. Location );
}
}

/**
* This class implements a progresslistener for testbrowser
*/
Class advancedprogresslistener implements progresslistener {
// The label on which to report progress

Private Label progress;

/**
* Constructs an advancedprogresslistener
*
* @ Param label
* The label on which to report progress
*/
Public advancedprogresslistener (label Label ){
// Store the label on which to report updates

Progress = label;
}

/**
* Called when progress is made
*
* @ Param event
* The Event
*/
Public void changed (progressevent event ){
// Avoid divide-by-zero

If (event. Total! = 0 ){
// Calculate a percentage and display it

Int percent = (INT) (event. Current/event. Total );
Progress. settext (percent + "% ");
} Else {
// Since we can't calculate a percent, show confusion

Progress. settext ("??? ");
}
}

/**
* Called when load is complete
*
* @ Param event
* The Event
*/
Public void completed (progressevent event ){
// Reset to the "at rest" Message

Progress. settext (at_rest );
}
}

/**
* This class implements a statustextlistener for testbrowser
*/
Class advancedstatustextlistener implements statustextlistener {
// The label on which to report status

Private Label status;

/**
* Constructs an advancedstatustextlistener
*
* @ Param label
* The label on which to report status
*/
Public advancedstatustextlistener (label Label ){
// Store the label on which to report status

Status = label;
}

/**
* Called when the status changes
*
* @ Param event
* The Event
*/
Public void changed (statustextevent event ){
// Report the status

Status. settext (event. Text );
}
}

/**
* The application entry point
*
* @ Param ARGs
* The command line arguments
*/
Public static void main (string [] ARGs ){
New testbrowser (). Run (ARGs. Length = 0? Null: ARGs [0]);
}
}
 
 
 
 

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.