Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8e6aa44abfaa0bf4d2a36db6d9f9731590831fd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
 * Copyright (c) 2000, 2009 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 example snippet: provide credentials for a basic authentication challenge
 * 
 * Note that the KNOWN_HOST, KNOWN_USER and KNOWN_PASSWORD fields in the snippet
 * below require valid values in order to fully demonstrate the functionality.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.5
 */
import java.net.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;

public class Snippet317 {

static String KNOWN_HOST = "www.eclipse.org";
static String KNOWN_USER = "anonymous";
static String KNOWN_PASSWORD = "anonymousPassword";

public static void main(String [] args) {
	Display display = new Display();
	final Shell shell = new Shell(display);
	GridLayout gridLayout = new GridLayout();
	gridLayout.numColumns = 2;
	shell.setLayout(gridLayout);
	final Text location = new Text(shell, SWT.BORDER);
	GridData data = new GridData();
	data.horizontalAlignment = GridData.FILL;
	data.grabExcessHorizontalSpace = true;
	location.setLayoutData(data);
	Button go = new Button(shell, SWT.PUSH);
	go.setText("Go");

	final Browser browser;
	try {
		browser = new Browser(shell, SWT.NONE);
	} catch (SWTError e) {
		System.out.println("Could not instantiate Browser: " + e.getMessage());
		display.dispose();
		return;
	}
	data = new GridData();
	data.horizontalAlignment = data.verticalAlignment = GridData.FILL;
	data.grabExcessHorizontalSpace = data.grabExcessVerticalSpace = true;
	data.horizontalSpan = 2;
	browser.setLayoutData(data);
	browser.setUrl ("eclipse.org");
	browser.addLocationListener(new LocationAdapter() {
		public void changed(LocationEvent event) {
			location.setText(event.location);
		}
	});

	Listener navigateListener = new Listener() {
		public void handleEvent(Event event) {
			browser.setUrl(location.getText());
		}
	};
	go.addListener(SWT.Selection, navigateListener);
	location.addListener(SWT.DefaultSelection, navigateListener);

	browser.addAuthenticationListener(new AuthenticationListener(){
		public void authenticate(AuthenticationEvent event) {
			try {
				URL url = new URL(event.location);
				if (url.getHost().equals(KNOWN_HOST)) {
					event.user = KNOWN_USER;
					event.password = KNOWN_PASSWORD;
				} else {
					/* do nothing, let default prompter run */
				}
			} catch (MalformedURLException e) {
				/* should not happen, let default prompter run */
			}
		}
	});

	shell.setBounds(10,10,500,500);
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose();
}

}

Back to the top