aboutsummaryrefslogtreecommitdiffstats
path: root/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/ui/AuthenticationDialog.java
blob: 10e2e6e9da74755ce204f47c60aaabe883176c8f (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.sdkuilib.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Dialog which collects from the user his/her login and password.
 */
public class AuthenticationDialog extends GridDialog {
    private Text mTxtLogin;
    private Text mTxtPassword;
    private Text mTxtWorkstation;
    private Text mTxtDomain;

    private String mTitle;
    private String mMessage;

    private String mLogin;
    private String mPassword;
    private String mWorkstation;
    private String mDomain;

    /**
     * Constructor which retrieves the parent {@link Shell} and the message to
     * be displayed in this dialog.
     *
     * @param parentShell Parent Shell
     * @param title Title of the window.
     * @param message Message the be displayed in this dialog.
     */
    public AuthenticationDialog(Shell parentShell, String title, String message) {
        super(parentShell, 1, false);
        // assign fields
        mTitle = title;
        mMessage = message;
    }

    @Override
    public void createDialogContent(Composite parent) {
        // Configure Dialog
        getShell().setText(mTitle);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        parent.setLayoutData(data);

        // Upper Composite
        Composite upperComposite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        layout.verticalSpacing = 10;
        upperComposite.setLayout(layout);
        data = new GridData(SWT.FILL, SWT.CENTER, true, true);
        upperComposite.setLayoutData(data);

        // add message label
        Label lblMessage = new Label(upperComposite, SWT.WRAP);
        lblMessage.setText(mMessage);
        data = new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 1);
        data.widthHint = 500;
        lblMessage.setLayoutData(data);

        // add user name label and text field
        Label lblUserName = new Label(upperComposite, SWT.NONE);
        lblUserName.setText("Login:");
        data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        lblUserName.setLayoutData(data);

        mTxtLogin = new Text(upperComposite, SWT.SINGLE | SWT.BORDER);
        data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        mTxtLogin.setLayoutData(data);
        mTxtLogin.setFocus();
        mTxtLogin.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent arg0) {
                mLogin = mTxtLogin.getText().trim();
            }
        });

        // add password label and text field
        Label lblPassword = new Label(upperComposite, SWT.NONE);
        lblPassword.setText("Password:");
        data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        lblPassword.setLayoutData(data);

        mTxtPassword = new Text(upperComposite, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER);
        data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        mTxtPassword.setLayoutData(data);
        mTxtPassword.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent arg0) {
                mPassword = mTxtPassword.getText();
            }
        });

        // add a label indicating that the following two fields are optional
        Label lblInfo = new Label(upperComposite, SWT.NONE);
        lblInfo.setText("Provide the following info if your proxy uses NTLM authentication. Leave blank otherwise.");
        data = new GridData();
        data.horizontalSpan = 2;
        lblInfo.setLayoutData(data);

        // add workstation label and text field
        Label lblWorkstation = new Label(upperComposite, SWT.NONE);
        lblWorkstation.setText("Workstation:");
        data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        lblWorkstation.setLayoutData(data);

        mTxtWorkstation = new Text(upperComposite, SWT.SINGLE | SWT.BORDER);
        data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        mTxtWorkstation.setLayoutData(data);
        mTxtWorkstation.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent arg0) {
                mWorkstation = mTxtWorkstation.getText().trim();
            }
        });

        // add domain label and text field
        Label lblDomain = new Label(upperComposite, SWT.NONE);
        lblDomain.setText("Domain:");
        data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        lblDomain.setLayoutData(data);

        mTxtDomain = new Text(upperComposite, SWT.SINGLE | SWT.BORDER);
        data = new GridData(SWT.FILL, SWT.CENTER, true, false);
        mTxtDomain.setLayoutData(data);
        mTxtDomain.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent arg0) {
                mDomain = mTxtDomain.getText().trim();
            }
        });
    }

    /**
     * Retrieves the Login field information
     *
     * @return Login field value or empty String. Return value is never null
     */
    public String getLogin() {
        return mLogin != null ? mLogin : ""; //$NON-NLS-1$
    }

    /**
     * Retrieves the Password field information
     *
     * @return Password field value or empty String. Return value is never null
     */
    public String getPassword() {
        return mPassword != null ? mPassword : ""; //$NON-NLS-1$
    }

    /**
     * Retrieves the workstation field information
     *
     * @return Workstation field value or empty String. Return value is never null
     */
    public String getWorkstation() {
        return mWorkstation != null ? mWorkstation : "";
    }

    /**
     * Retrieves the domain field information
     *
     * @return Domain field value or empty String. Return value is never null
     */
    public String getDomain() {
        return mDomain != null ? mDomain : "";
    }
}