diff options
author | Grant Gayed | 2009-03-11 19:35:40 +0000 |
---|---|---|
committer | Grant Gayed | 2009-03-11 19:35:40 +0000 |
commit | 129772948e568750ac8330b164369b1f823c7d48 (patch) | |
tree | a7c2cd991e99e6832d314cc0a38bc918d2a45667 /examples/org.eclipse.swt.snippets | |
parent | 75df958e1b36b7769da6c14b46d2ac521034ac37 (diff) | |
download | eclipse.platform.swt-129772948e568750ac8330b164369b1f823c7d48.tar.gz eclipse.platform.swt-129772948e568750ac8330b164369b1f823c7d48.tar.xz eclipse.platform.swt-129772948e568750ac8330b164369b1f823c7d48.zip |
*** empty log message ***
Diffstat (limited to 'examples/org.eclipse.swt.snippets')
-rw-r--r-- | examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet317.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet317.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet317.java new file mode 100644 index 0000000000..6022c2ff0d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet317.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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 { + +public static void main(String [] args) { + final String KNOWN_HOST = "www.eclipse.org"; + final String KNOWN_USER = "anonymous"; + final String KNOWN_PASSWORD = "anonymousPassword"; + + 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()); + 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(); +} + +} |