DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

OpenURL In SWT

08.30.2006
| 6682 views |
  • submit to reddit
        import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import sun.net.www.protocol.http.HttpURLConnection;

public class OpenUrl {
	
	private Display display;
	private Shell shell;
	private GridLayout layout;
//	private Text textArea;
	private Browser browser;

	public OpenUrl() {
		// Display & Shell holen
		display = new Display();
		shell = new Shell(display);

		shell.setText("OpenURL - WWW demo application");
		shell.setSize(500, 300);
		
		createGUI();
		
		// Shell öffnen
		shell.open();
		
		// Event-Schleife starten
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	private void createGUI() {
		layout = new GridLayout();
		layout.numColumns = 2;
		
 		layout.marginLeft = 5;
 		layout.marginTop = 5;
 		layout.marginRight = 5;
 		layout.marginBottom = 5;
 		
		shell.setLayout(layout);
		
		Label l = new Label(shell, SWT.NONE);
		l.setText("URL:");
		l.setLayoutData(new GridData(SWT.LEFT));
		
		final Text t = new Text(shell, SWT.BORDER);
		t.setText("http://www.google.de/");
		t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));

//	    textArea = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
//	    textArea.setText("Press \"Open\" to load the URL contents...");
//	    GridData gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
//	    gd.horizontalSpan = 2;
//		textArea.setLayoutData(gd);
		
		browser = new Browser(shell, SWT.BORDER);
	    GridData gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
	    gd.horizontalSpan = 2;
		browser.setLayoutData(gd);
		
		Button b = new Button(shell, SWT.NONE);
		b.setText("Open");
		b.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				open(t.getText());
			}
		});
		b.setFocus();
		b.setLayoutData(new GridData(SWT.BEGINNING));
	}

	private void open(String u) {
		System.err.println(OpenUrl.this + " open URL '" + u + "'");

		try {
			StringBuffer urlText = new StringBuffer();
			URL url = new URL(u);
			HttpURLConnection con = new HttpURLConnection(url, "myproxy.domain", 8080);
			InputStream in = con.getInputStream();
			int i = -1;
			while(((i = in.read()) != -1)) {
				urlText.append((char)i);
			}
			
//			textArea.setText(urlText.toString());
			browser.setText(urlText.toString());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new OpenUrl();
	}
}