Java Tutorial/SWT/Browser
Содержание
- 1 Add CloseWindowListener to Browser
- 2 Adding OpenWindowListener
- 3 Adding ProgressListener to Browser
- 4 Adding StatusTextListener to Browser
- 5 Adding TitleListener to Browser
- 6 Adding VisibilityWindowListener
- 7 Add LocationListener to Browser
- 8 Advanced Browser
- 9 Browser: bring up a browser (pop-up blocker)
- 10 Browser: modify DOM (executing javascript)
- 11 Browser: query DOM node value
- 12 Browser: render HTML that includes relative links from memory
- 13 Browser supports the following listeners:
- 14 Renderer HTML in memory
- 15 Using Browser to load a website
- 16 Using ProgressBar to display the Browser progress
Add CloseWindowListener to Browser
The following HTML link closes the browser window:
Adding OpenWindowListener
Browser conventions allow spawning new browser windows. Things that can trigger a new browser window include the following:
- The user right-clicks a link and selects Open in New Window from the context menu (note that you must build this functionality yourself).
- The user holds down Shift while clicking a link.
- The user clicks a link that has a named target for which no browser currently exists.
When the user performs an action within the browser that spawns a new browser window, any OpenWindowListeners are first notified. OpenWindowListener declares one method:
public void open(WindowEvent event)
Adding ProgressListener to Browser
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class BrowserProgressListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5,5,600,600);
final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setBounds(5,650,600,20);
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if (event.total == 0) return;
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
}
public void completed(ProgressEvent event) {
progressBar.setSelection(0);
}
});
browser.setUrl("http://jexp.ru");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Adding StatusTextListener to Browser
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class BroserStatusTextListener {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5,5,600,600);
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
System.out.println(event.text);
}
});
browser.setUrl("http://jexp.ru");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Adding TitleListener to Browser
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*
* Browser example snippet: modify HTML title tag
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HTMLTagModify {
public static void main(String[] args) {
final String newTitle = "New Value for Title";
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Browser browser = new Browser(shell, SWT.NONE);
browser.addTitleListener(new TitleListener() {
public void changed(TitleEvent event) {
System.out.println("TitleEvent: " + event.title);
shell.setText(event.title);
}
});
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
}
public void completed(ProgressEvent event) {
/* Set HTML title tag using JavaScript and DOM when page has been loaded */
boolean result = browser.execute("document.title="" + newTitle + """);
if (!result) {
/* Script may fail or may not be supported on certain platforms. */
System.out.println("Script was not executed.");
}
}
});
/* Load an HTML document */
browser.setUrl("http://www.eclipse.org");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Adding VisibilityWindowListener
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.VisibilityWindowListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class VisibilityWindowListenerUsing {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5, 5, 600, 600);
browser.addVisibilityWindowListener(new VisibilityWindowListener() {
public void hide(WindowEvent event) {
System.out.println("hide");
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
}
public void show(WindowEvent event) {
Browser browser = (Browser) event.widget;
final Shell shell = browser.getShell();
System.out.println("Popup blocked.");
shell.open();
}
});
browser.setUrl("http://jexp.ru");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Add LocationListener to Browser
The "Location" in LocationListener refers to URLs the browser is loading.
LocationListener has two methods:
void changed(LocationEvent event)
void changing(LocationEvent event)
Advanced Browser
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.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.browser.WindowEvent;
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.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class AdvancedBrowser {
private static final String AT_REST = "Ready";
public AdvancedBrowser(String location) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Advanced Browser");
shell.setLayout(new FormLayout());
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);
Label 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);
final Browser browser = new Browser(shell, SWT.BORDER);
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);
controls.setLayout(new GridLayout(7, false));
Button button = new Button(controls, SWT.PUSH);
button.setText("Back");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.back();
}
});
button = new Button(controls, SWT.PUSH);
button.setText("Forward");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.forward();
}
});
button = new Button(controls, SWT.PUSH);
button.setText("Refresh");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.refresh();
}
});
button = new Button(controls, SWT.PUSH);
button.setText("Stop");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.stop();
}
});
final Text url = new Text(controls, SWT.BORDER);
url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
url.setFocus();
button = new Button(controls, SWT.PUSH);
button.setText("Go");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.setUrl(url.getText());
}
});
Label throbber = new Label(controls, SWT.NONE);
throbber.setText(AT_REST);
shell.setDefaultButton(button);
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);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
class AdvancedCloseWindowListener implements CloseWindowListener {
public void close(WindowEvent event) {
((Browser) event.widget).getShell().close();
}
}
class AdvancedLocationListener implements LocationListener {
private Text location;
public AdvancedLocationListener(Text text) {
location = text;
}
public void changing(LocationEvent event) {
location.setText("Loading " + event.location + "...");
}
public void changed(LocationEvent event) {
location.setText(event.location);
}
}
class AdvancedProgressListener implements ProgressListener {
private Label progress;
public AdvancedProgressListener(Label label) {
progress = label;
}
public void changed(ProgressEvent event) {
if (event.total != 0) {
int percent = (int) (event.current / event.total);
progress.setText(percent + "%");
} else {
progress.setText("?");
}
}
public void completed(ProgressEvent event) {
progress.setText(AT_REST);
}
}
class AdvancedStatusTextListener implements StatusTextListener {
private Label status;
public AdvancedStatusTextListener(Label label) {
status = label;
}
public void changed(StatusTextEvent event) {
status.setText(event.text);
}
}
public static void main(String[] args) {
new AdvancedBrowser("http://www.google.ru");
}
}
Browser: bring up a browser (pop-up blocker)
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.snippets;
/*
* Browser snippet: bring up a browser (pop-up blocker)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.browser.*;
public class Snippet173 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Main Window");
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
initialize(display, browser);
shell.open();
/* any website with popups */
browser.setUrl("http://www.cnn.ru");
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/* register WindowEvent listeners */
static void initialize(final Display display, Browser browser) {
browser.addOpenWindowListener(new OpenWindowListener() {
public void open(WindowEvent event) {
Shell shell = new Shell(display);
shell.setText("New Window");
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) {
Browser browser = (Browser)event.widget;
Shell shell = browser.getShell();
shell.setVisible(false);
}
public void show(WindowEvent event) {
Browser browser = (Browser)event.widget;
final Shell shell = browser.getShell();
/* popup blocker - ignore windows with no style */
if (!event.addressBar && !event.menuBar && !event.statusBar && !event.toolBar) {
System.out.println("Popup blocked.");
event.display.asyncExec(new Runnable() {
public void run() {
shell.close();
}
});
return;
}
if (event.location != null) shell.setLocation(event.location);
if (event.size != null) {
Point size = event.size;
shell.setSize(shell.ruputeSize(size.x, size.y));
}
shell.open();
}
});
browser.addCloseWindowListener(new CloseWindowListener() {
public void close(WindowEvent event) {
Browser browser = (Browser)event.widget;
Shell shell = browser.getShell();
shell.close();
}
});
}
}
Browser: modify DOM (executing javascript)
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*
* Browser example snippet: modify DOM (executing javascript)
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class JavascriptExec {
public static void main(String[] args) {
final String html = "<html><title>Snippet</title><body><p id="myid">Best Friends</p><p id="myid2">Cat and Dog</p></body></html>";
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Browser browser = new Browser(shell, SWT.BORDER);
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new FillLayout(SWT.VERTICAL));
final Text text = new Text(comp, SWT.MULTI);
text.setText("var newNode = document.createElement("P"); \r\n"
+ "var text = document.createTextNode("At least when I am around");\r\n"
+ "newNode.appendChild(text);\r\n"
+ "document.getElementById("myid").appendChild(newNode);\r\n" + "\r\n"
+ "document.bgColor="yellow";");
final Button button = new Button(comp, SWT.PUSH);
button.setText("Execute Script");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
boolean result = browser.execute(text.getText());
if (!result) {
/* Script may fail or may not be supported on certain platforms. */
System.out.println("Script was not executed.");
}
}
});
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Browser: query DOM node value
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*
* Browser example snippet: query DOM node value
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
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.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DOMQuerySWTBrowser {
public static void main(String[] args) {
final String html = "<html><title>Snippet</title><body><p id="myid">Best Friends</p><p id="myid2">Cat and Dog</p></body></html>";
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Browser browser = new Browser(shell, SWT.NONE);
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
browser.setData("query", event.text);
}
});
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
}
public void completed(ProgressEvent event) {
/*
* Use JavaScript to query the desired node content through the Document
* Object Model
*
* Assign result to the window property status to pass the result to the
* StatusTextListener This trick is required since <code>execute</code>
* does not return the <code>String</code> directly.
*/
boolean result = browser
.execute("window.status=document.getElementById("myid").childNodes[0].nodeValue;");
if (!result) {
/* Script may fail or may not be supported on certain platforms. */
System.out.println("Script was not executed.");
return;
}
String value = (String) browser.getData("query");
System.out.println("Node value: " + value);
}
});
/* Load an HTML document */
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Browser: render HTML that includes relative links from memory
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*
* Browser example snippet: render HTML that includes relative links from memory
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.0
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HTMLRenderRelativeMemory {
public static void main(String[] args) {
/* Relative links: use the HTML base tag */
String html = "<html><head>" + "<base href=\"http://www.eclipse.org/swt/\" >"
+ "<title>HTML Test</title></head>"
+ "<body></body></html>";
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Browser supports the following listeners:
- CloseWindowListener
- LocationListener
- OpenWindowListener
- ProgressListener
- StatusTextListener
- VisibilityWindowListener
Renderer HTML in memory
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HTMLRendererInMemory {
public static void main(String[] args) {
String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>";
for (int i = 0; i < 100; i++){
html += "<br><br>";
}
html += "</BODY></HTML>";
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.setText(html);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Using Browser to load a website
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class BrowserLoadingWebsite {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5,5,600,600);
browser.setUrl("http://jexp.ru");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Using ProgressBar to display the Browser progress
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class BrowserProgress {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5,5,600,600);
final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setBounds(5,650,600,20);
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if (event.total == 0) return;
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
}
public void completed(ProgressEvent event) {
progressBar.setSelection(0);
}
});
browser.setUrl("http://jexp.ru");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}