1 19 20 package com.sslexplorer.agent.client.gui.swt; 21 22 import java.text.MessageFormat ; 23 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.PaintEvent; 26 import org.eclipse.swt.events.PaintListener; 27 import org.eclipse.swt.graphics.Image; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.layout.GridLayout; 30 import org.eclipse.swt.widgets.Canvas; 31 import org.eclipse.swt.widgets.Label; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.swt.widgets.Text; 34 35 import com.maverick.http.HttpAuthenticator; 36 import com.maverick.http.NTLMAuthentication; 37 import com.maverick.http.PasswordCredentials; 38 39 45 public class SWTAuthenticationDialog { 46 47 53 public static boolean promptForCredentials(boolean proxy, Shell parent, HttpAuthenticator authenticator, final Image image) { 54 SWTOptionDialog opt = new SWTOptionDialog(parent, 55 SWT.TITLE | SWT.CLOSE | SWT.BORDER, 56 Messages.getString("SWTAuthenticationDialog.ok"), 57 Messages.getString("SWTAuthenticationDialog.cancel"), 58 Messages.getString("SWTAuthenticationDialog.title"), 59 null); 60 61 Shell shell = opt.getShell(); 62 63 GridLayout gridLayout = new GridLayout(1, false); 64 shell.setLayout(gridLayout); 65 66 if (image != null) { 68 Canvas canvas = new Canvas(shell, SWT.NONE); 69 canvas.addPaintListener(new PaintListener() { 70 public void paintControl(PaintEvent e) { 71 e.gc.drawImage(image, 0, 0); 72 } 73 }); 74 GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true); 75 data.widthHint = image.getBounds().width; 76 data.heightHint = image.getBounds().height; 77 canvas.setLayoutData(data); 78 } 79 80 Label textLabel = new Label(shell, SWT.NONE | SWT.WRAP); 82 String info = authenticator.getInformation(); 83 info = info == null ? "Authentication" : info; 84 String host = authenticator.getPort() == 0 ? authenticator.getHost() 85 : (authenticator.getHost() + ":" + authenticator.getPort()); 86 textLabel.setText(MessageFormat.format(Messages.getString("SWTAuthenticationDialog.text"), new Object [] { info, host, authenticator.getScheme() })); 87 GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true); 88 textLabel.setLayoutData(data); 89 90 Text domainText = null; 92 if (authenticator instanceof NTLMAuthentication) { 93 Label domainLabel = new Label(shell, SWT.NONE); 94 domainLabel.setText(Messages.getString("SWTAuthenticationDialog.domain")); 95 domainText = new Text(shell, SWT.BORDER); 96 data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true); 97 data.widthHint = 300; 98 domainText.setLayoutData(data); 99 } 100 101 Label usernameLabel = new Label(shell, SWT.NONE); 103 usernameLabel.setText(Messages.getString("SWTAuthenticationDialog.username")); 104 Text usernameText = new Text(shell, SWT.BORDER); 105 usernameText.setText(""); 106 data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true); 107 data.widthHint = 300; 108 usernameText.setLayoutData(data); 109 110 Label passwordLabel = new Label(shell, SWT.NONE); 112 passwordLabel.setText(Messages.getString("SWTAuthenticationDialog.password")); 113 114 Text passwordText = new Text(shell, SWT.BORDER); 115 passwordText.setEchoChar('*'); 116 data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true); 117 data.widthHint = 300; 118 passwordText.setLayoutData(data); 119 120 122 try { 123 if (opt.open()) { 124 authenticator.setCredentials(new PasswordCredentials(usernameText.getText().trim(), passwordText.getText())); 125 if (domainText != null && !domainText.getText().trim().equals("")) { ((NTLMAuthentication) authenticator).setDomain(domainText.getText().trim()); 127 } 128 return true; 129 } 130 131 return false; 132 } finally { 133 opt.close(); 134 } 135 } 136 137 } 138 | Popular Tags |