The Code is as follows:
BrowserExample. java
Copy codeThe Code is as follows: package swt_jface.demo5;
Import java. text. MessageFormat;
Import java. util. MissingResourceException;
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. SWTError;
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. TitleEvent;
Import org. eclipse. swt. browser. TitleListener;
Import org. eclipse. swt. browser. VisibilityWindowListener;
Import org. eclipse. swt. browser. javaswevent;
Import org. eclipse. swt. graphics. Image;
Import org. eclipse. swt. graphics. Point;
Import org. eclipse. swt. graphics. Rectangle;
Import org. eclipse. swt. layout. FillLayout;
Import org. eclipse. swt. layout. FormAttachment;
Import org. eclipse. swt. layout. FormData;
Import org. eclipse. swt. layout. FormLayout;
Import org. eclipse. swt. widgets. Canvas;
Import org. eclipse. swt. widgets. Composite;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Event;
Import org. eclipse. swt. widgets. Label;
Import org. eclipse. swt. widgets. Listener;
Import org. eclipse. swt. widgets. ProgressBar;
Import org. eclipse. swt. widgets. Shell;
Import org. eclipse. swt. widgets. Text;
Import org. eclipse. swt. widgets. ToolBar;
Import org. eclipse. swt. widgets. ToolItem;
Public class BrowserExample {
// Static ResourceBundle resourceBundle = ResourceBundle. getBundle ("examples_browser ");
Int index;
Boolean busy;
Image images [];
Text location;
Browser browser;
Static final String [] imageLocations = {
"Eclipse01.bmp", "eclipse02.bmp", "eclipse03.bmp", "eclipse04.bmp", "eclipse05.bmp ",
"Eclipse06.bmp", "eclipse07.bmp", "eclipse08.bmp", "eclipse09.bmp", "eclipse10.bmp ",
"Eclipse11.bmp", "eclipse12.bmp ",};
Static final String iconLocation = "document.gif ";
Public BrowserExample (Composite parent ){
Final Display display = parent. getDisplay ();
FormLayout layout = new FormLayout ();
Parent. setLayout (layout );
ToolBar toolbar = new ToolBar (parent, SWT. NONE );
Final ToolItem itemBack = new ToolItem (toolbar, SWT. PUSH );
ItemBack. setText (getResourceString ("Back "));
Final ToolItem itemForward = new ToolItem (toolbar, SWT. PUSH );
ItemForward. setText (getResourceString ("Forward "));
Final ToolItem itemStop = new ToolItem (toolbar, SWT. PUSH );
ItemStop. setText (getResourceString ("Stop "));
Final ToolItem itemRefresh = new ToolItem (toolbar, SWT. PUSH );
ItemRefresh. setText (getResourceString ("Refresh "));
Final ToolItem itemGo = new ToolItem (toolbar, SWT. PUSH );
ItemGo. setText (getResourceString ("Go "));
Location = new Text (parent, SWT. BORDER );
Images = new Image [] {new Image (display, "C:/icons/web/go.gif ")};
Final Canvas canvas = new Canvas (parent, SWT. NO_BACKGROUND );
Final Rectangle rect = images [0]. getBounds ();
Canvas. addListener (SWT. Paint, new Listener (){
Public void handleEvent (Event e ){
Point pt = canvas. getSize ();
E. gc. drawImage (images [index], 0, 0, rect. width, rect. height, 0, 0, pt. x, pt. y );
}
});
Canvas. addListener (SWT. MouseDown, new Listener (){
Public void handleEvent (Event e ){
Browser. setUrl (getResourceString ("Startup "));
}
});
Display. asyncExec (new Runnable (){
Public void run (){
If (canvas. isDisposed () return;
If (busy ){
Index ++;
If (index = images. length) index = 0;
Canvas. redraw ();
}
Display. timerExec (150, this );
}
});
Final Label status = new Label (parent, SWT. NONE );
Final ProgressBar progressBar = new ProgressBar (parent, SWT. NONE );
FormData data = new FormData ();
Data. top = new FormAttachment (0, 5 );
Toolbar. setLayoutData (data );
Data = new FormData ();
Data. left = new FormAttachment (0, 0 );
Data. right = new FormAttachment (100, 0 );
Data. top = new FormAttachment (canvas, 5, SWT. DEFAULT );
Data. bottom = new FormAttachment (status,-5, SWT. DEFAULT );
Try {
Browser = new Browser (parent, SWT. NONE );
Browser. setLayoutData (data );
} Catch (SWTError e ){
Label label = new Label (parent, SWT. CENTER | SWT. WRAP );
Label. setText (getResourceString ("BrowserNotCreated "));
Label. setLayoutData (data );
}
Data = new FormData ();
Data. width = 24;
Data. height = 24;
Data. top = new FormAttachment (0, 5 );
Data. right = new FormAttachment (100,-5 );
Canvas. setLayoutData (data );
Data = new FormData ();
Data. top = new FormAttachment (toolbar, 0, SWT. TOP );
Data. left = new FormAttachment (toolbar, 5, SWT. RIGHT );
Data. right = new FormAttachment (canvas,-5, SWT. DEFAULT );
Location. setLayoutData (data );
Data = new FormData ();
Data. left = new FormAttachment (0, 5 );
Data. right = new FormAttachment (progressBar, 0, SWT. DEFAULT );
Data. bottom = new FormAttachment (100,-5 );
Status. setLayoutData (data );
Data = new FormData ();
Data. right = new FormAttachment (100,-5 );
Data. bottom = new FormAttachment (100,-5 );
ProgressBar. setLayoutData (data );
If (browser! = Null ){
ItemBack. setEnabled (browser. isBackEnabled ());
ItemForward. setEnabled (browser. isForwardEnabled ());
Listener listener = new Listener (){
Public void handleEvent (Event event ){
ToolItem item = (ToolItem) event. widget;
If (item = itemBack) browser. back ();
Else if (item = itemForward) browser. forward ();
Else if (item = itemStop) browser. stop ();
Else if (item = itemRefresh) browser. refresh ();
Else if (item = itemGo) browser. setUrl (location. getText ());
}
};
Browser. addLocationListener (new LocationListener (){
Public void changed (LocationEvent event ){
Busy = true;
If (event. top) location. setText (event. location );
}
Public void changing (LocationEvent event ){
}
});
Browser. addProgressListener (new ProgressListener (){
Public void changed (ProgressEvent event ){
If (event. total = 0) return;
Int ratio = event. current * 100/event. total;
ProgressBar. setSelection (ratio );
Busy = event. current! = Event. total;
If (! Busy ){
Index = 0;
Canvas. redraw ();
}
}
Public void completed (ProgressEvent event ){
ItemBack. setEnabled (browser. isBackEnabled ());
ItemForward. setEnabled (browser. isForwardEnabled ());
ProgressBar. setSelection (0 );
Busy = false;
Index = 0;
Canvas. redraw ();
}
});
Browser. addStatusTextListener (new StatusTextListener (){
Public void changed (StatusTextEvent event ){
Status. setText (event. text );
}
});
If (parent instanceof Shell ){
Final Shell shell = (Shell) parent;
Browser. addTitleListener (new TitleListener (){
Public void changed (TitleEvent event ){
Shell. setText (event. title + "-" + getResourceString ("window. title "));
}
});
}
ItemBack. addListener (SWT. Selection, listener );
ItemForward. addListener (SWT. Selection, listener );
ItemStop. addListener (SWT. Selection, listener );
ItemRefresh. addListener (SWT. Selection, listener );
ItemGo. addListener (SWT. Selection, listener );
Location. addListener (SWT. DefaultSelection, new Listener (){
Public void handleEvent (Event e ){
Browser. setUrl (location. getText ());
}
});
Initialize (display, browser );
Browser. setUrl (getResourceString ("Startup "));
}
}
Static String getResourceString (String key ){
Try {
Return "key ";
} Catch (MissingResourceException e ){
Return key;
} Catch (NullPointerException e ){
Return "! "+ Key + "! ";
}
}
Static String getResourceString (String key, Object [] args ){
Try {
Return MessageFormat. format (getResourceString (key), args );
} Catch (MissingResourceException e ){
Return key;
} Catch (NullPointerException e ){
Return "! "+ Key + "! ";
}
}
Static void initialize (final Display display, Browser browser ){
Browser. addOpenWindowListener (new OpenWindowListener (){
Public void open (incluwevent event ){
System. out. println ("Open ");
Shell shell = new Shell (display );
Shell. setLayout (new FillLayout ());
Browser browser = new Browser (shell, SWT. NONE );
Initialize (display, browser );
Event. browser = browser;
}
});
Browser. addVisibilityWindowListener (new VisibilityWindowListener (){
Public void hide (incluwevent event ){
}
Public void show (incluwevent event ){
System. out. println ("Show ");
Browser browser = (Browser) event. widget;
Shell shell = browser. getShell ();
If (event. location! = Null) shell. setLocation (event. location );
If (event. size! = Null ){
Point size = event. size;
Shell. setSize (shell. computeSize (size. x, size. y ));
}
Shell. open ();
}
});
Browser. addCloseWindowListener (new CloseWindowListener (){
Public void close (incluwevent event ){
System. out. println ("Close ");
Browser browser = (Browser) event. widget;
Shell shell = browser. getShell ();
Shell. close ();
}
});
}
Public void dispose (){
FreeResources ();
}
Void freeResources (){
If (images! = Null ){
For (int I = 0; I <images. length; ++ I ){
Final Image image = images [I];
If (image! = Null) image. dispose ();
}
Images = null;
}
}
Public void setFocus (){
Location. setFocus ();
}
Public static void main (String [] args ){
Display display = new Display ();
Shell shell = new Shell (display );
Shell. setLayout (new FillLayout ());
Shell. setText ("Browser example ");
BrowserExample instance = new BrowserExample (shell );
Image icon = new Image (display, "C:/icons/web/go.gif ");
Shell. setImage (icon );
Shell. open ();
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ())
Display. sleep ();
}
Icon. dispose ();
Instance. dispose ();
Display. dispose ();
}
}