1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.sdkuilib.ui; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.ModifyEvent; 21 import org.eclipse.swt.events.ModifyListener; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Label; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.swt.widgets.Text; 28 29 /** 30 * Dialog which collects from the user his/her login and password. 31 */ 32 public class AuthenticationDialog extends GridDialog { 33 private Text mTxtLogin; 34 private Text mTxtPassword; 35 private Text mTxtWorkstation; 36 private Text mTxtDomain; 37 38 private String mTitle; 39 private String mMessage; 40 41 private static String sLogin = ""; 42 private static String sPassword = ""; 43 private static String sWorkstation = ""; 44 private static String sDomain = ""; 45 46 /** 47 * Constructor which retrieves the parent {@link Shell} and the message to 48 * be displayed in this dialog. 49 * 50 * @param parentShell Parent Shell 51 * @param title Title of the window. 52 * @param message Message the be displayed in this dialog. 53 */ AuthenticationDialog(Shell parentShell, String title, String message)54 public AuthenticationDialog(Shell parentShell, String title, String message) { 55 super(parentShell, 1, false); 56 // assign fields 57 mTitle = title; 58 mMessage = message; 59 } 60 61 @Override createDialogContent(Composite parent)62 public void createDialogContent(Composite parent) { 63 // Configure Dialog 64 getShell().setText(mTitle); 65 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 66 parent.setLayoutData(data); 67 68 // Upper Composite 69 Composite upperComposite = new Composite(parent, SWT.NONE); 70 GridLayout layout = new GridLayout(2, false); 71 layout.verticalSpacing = 10; 72 upperComposite.setLayout(layout); 73 data = new GridData(SWT.FILL, SWT.CENTER, true, true); 74 upperComposite.setLayoutData(data); 75 76 // add message label 77 Label lblMessage = new Label(upperComposite, SWT.WRAP); 78 lblMessage.setText(mMessage); 79 data = new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 1); 80 data.widthHint = 500; 81 lblMessage.setLayoutData(data); 82 83 // add user name label and text field 84 Label lblUserName = new Label(upperComposite, SWT.NONE); 85 lblUserName.setText("Login:"); 86 data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 87 lblUserName.setLayoutData(data); 88 89 mTxtLogin = new Text(upperComposite, SWT.SINGLE | SWT.BORDER); 90 data = new GridData(SWT.FILL, SWT.CENTER, true, false); 91 mTxtLogin.setLayoutData(data); 92 mTxtLogin.setFocus(); 93 mTxtLogin.setText(sLogin); 94 mTxtLogin.addModifyListener(new ModifyListener() { 95 @Override 96 public void modifyText(ModifyEvent arg0) { 97 sLogin = mTxtLogin.getText().trim(); 98 } 99 }); 100 101 // add password label and text field 102 Label lblPassword = new Label(upperComposite, SWT.NONE); 103 lblPassword.setText("Password:"); 104 data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 105 lblPassword.setLayoutData(data); 106 107 mTxtPassword = new Text(upperComposite, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER); 108 data = new GridData(SWT.FILL, SWT.CENTER, true, false); 109 mTxtPassword.setLayoutData(data); 110 mTxtPassword.setText(sPassword); 111 mTxtPassword.addModifyListener(new ModifyListener() { 112 @Override 113 public void modifyText(ModifyEvent arg0) { 114 sPassword = mTxtPassword.getText(); 115 } 116 }); 117 118 // add a label indicating that the following two fields are optional 119 Label lblInfo = new Label(upperComposite, SWT.NONE); 120 lblInfo.setText("Provide the following info if your proxy uses NTLM authentication. Leave blank otherwise."); 121 data = new GridData(); 122 data.horizontalSpan = 2; 123 lblInfo.setLayoutData(data); 124 125 // add workstation label and text field 126 Label lblWorkstation = new Label(upperComposite, SWT.NONE); 127 lblWorkstation.setText("Workstation:"); 128 data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 129 lblWorkstation.setLayoutData(data); 130 131 mTxtWorkstation = new Text(upperComposite, SWT.SINGLE | SWT.BORDER); 132 data = new GridData(SWT.FILL, SWT.CENTER, true, false); 133 mTxtWorkstation.setLayoutData(data); 134 mTxtWorkstation.setText(sWorkstation); 135 mTxtWorkstation.addModifyListener(new ModifyListener() { 136 @Override 137 public void modifyText(ModifyEvent arg0) { 138 sWorkstation = mTxtWorkstation.getText().trim(); 139 } 140 }); 141 142 // add domain label and text field 143 Label lblDomain = new Label(upperComposite, SWT.NONE); 144 lblDomain.setText("Domain:"); 145 data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 146 lblDomain.setLayoutData(data); 147 148 mTxtDomain = new Text(upperComposite, SWT.SINGLE | SWT.BORDER); 149 data = new GridData(SWT.FILL, SWT.CENTER, true, false); 150 mTxtDomain.setLayoutData(data); 151 mTxtDomain.setText(sDomain); 152 mTxtDomain.addModifyListener(new ModifyListener() { 153 @Override 154 public void modifyText(ModifyEvent arg0) { 155 sDomain = mTxtDomain.getText().trim(); 156 } 157 }); 158 } 159 160 /** 161 * Retrieves the Login field information 162 * 163 * @return Login field value or empty String. Return value is never null 164 */ getLogin()165 public String getLogin() { 166 return sLogin; 167 } 168 169 /** 170 * Retrieves the Password field information 171 * 172 * @return Password field value or empty String. Return value is never null 173 */ getPassword()174 public String getPassword() { 175 return sPassword; 176 } 177 178 /** 179 * Retrieves the workstation field information 180 * 181 * @return Workstation field value or empty String. Return value is never null 182 */ getWorkstation()183 public String getWorkstation() { 184 return sWorkstation; 185 } 186 187 /** 188 * Retrieves the domain field information 189 * 190 * @return Domain field value or empty String. Return value is never null 191 */ getDomain()192 public String getDomain() { 193 return sDomain; 194 } 195 } 196