package swt;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Test {
private Composite composite;
private Text tfUrl;
private Button btnGo;
private Browser browser;
public Test(Composite parent, int style) {
this.composite = new Composite(parent, style);
GridLayout layout = new GridLayout(2, false);
composite.setLayout(layout);
GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
tfUrl = new Text(composite, SWT.BORDER);
tfUrl.setLayoutData(layoutData);
tfUrl.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent event) {
if (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {
if (!tfUrl.getText().isEmpty()) {
browser.setUrl(tfUrl.getText());
}
}
}
});
layoutData = new GridData(SWT.FILL, SWT.CENTER, false, false);
btnGo = new Button(composite, SWT.PUSH);
btnGo.setLayoutData(layoutData);
btnGo.setText("Surfen");
btnGo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!tfUrl.getText().isEmpty()) {
browser.setUrl(tfUrl.getText());
}
}
});
layoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
browser = new Browser(composite, SWT.BORDER);
browser.setLayoutData(layoutData);
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
tfUrl.setText(event.location);
}
public void changing(LocationEvent event) {
}
});
composite.pack();
browser.forceFocus();
}
public void setUrl(String url) {
tfUrl.setText(url);
browser.setUrl(url);
}
public String getUrl() {
return browser.getUrl();
}
public void addLocationListener(LocationListener listener) {
browser.addLocationListener(listener);
}
public void removeLocationListener(LocationListener listener) {
browser.removeLocationListener(listener);
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Test browser = new Test(shell, SWT.NONE);
browser.setUrl("http://www.java-forum.org");
// Hier eigene LocationListener einbringen, sofern nötig.
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
int screenWidth = 0;
int screenHeight = 0;
for (int i = 0; i < gs.length; i++) {
DisplayMode dm = gs[i].getDisplayMode();
screenWidth = dm.getWidth();
screenHeight = dm.getHeight();
}
shell.setBounds(0, 0, screenWidth, screenHeight);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}