SWT (JFace) Simple browser production implementation code 1th/2 page _java programming

Source: Internet
Author: User
Tags gettext
The code is as follows:
Browserexample.java
Copy Code code 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.WindowEvent;
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 (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 (WindowEvent 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 (WindowEvent event) {
}
public void Show (WindowEvent 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 (WindowEvent 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 ();
}
}

Current 1/2 page 12 Next read the full text

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.