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 34 private Text mTxtLogin; 35 36 private Text mTxtPassword; 37 38 private String mTitle; 39 40 private String mMessage; 41 42 protected String mLogin; 43 44 protected String mPassword; 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.addModifyListener(new ModifyListener() { 94 public void modifyText(ModifyEvent arg0) { 95 mLogin = mTxtLogin.getText(); 96 } 97 }); 98 99 // add password label and text field 100 Label lblPassword = new Label(upperComposite, SWT.NONE); 101 lblPassword.setText("Password:"); 102 data = new GridData(SWT.LEFT, SWT.CENTER, false, false); 103 lblPassword.setLayoutData(data); 104 105 mTxtPassword = new Text(upperComposite, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER); 106 data = new GridData(SWT.FILL, SWT.CENTER, true, false); 107 mTxtPassword.setLayoutData(data); 108 mTxtPassword.addModifyListener(new ModifyListener() { 109 public void modifyText(ModifyEvent arg0) { 110 mPassword = mTxtPassword.getText(); 111 } 112 }); 113 } 114 115 /** 116 * Retrieves the Login field information 117 * 118 * @return Login field value or empty String. Return value is never null 119 */ getLogin()120 public String getLogin() { 121 return mLogin != null ? mLogin : ""; //$NON-NLS-1$ 122 } 123 124 /** 125 * Retrieves the Password field information 126 * 127 * @return Password field value or empty String. Return value is never null 128 */ getPassword()129 public String getPassword() { 130 return mPassword != null ? mPassword : ""; //$NON-NLS-1$ 131 } 132 } 133