1
/** BEGIN COPYRIGHT BLOCK
2
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3
* Copyright (C) 2005 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* END COPYRIGHT BLOCK **/
20
package com.netscape.admin.dirserv;
23
import java.awt.event.*;
25
import javax.swing.event.*;
26
import com.netscape.management.client.console.Console;
27
import com.netscape.management.client.console.LoginDialog;
28
import com.netscape.management.client.util.AbstractDialog;
29
import com.netscape.management.client.util.GridBagUtil;
30
import com.netscape.management.client.util.ResourceSet;
31
import com.netscape.management.client.util.Debug;
32
import com.netscape.management.client.util.Help;
33
import com.netscape.management.nmclf.SuiConstants;
34
import com.netscape.admin.dirserv.panel.UIFactory;
37
* Display this dialog to get a password.
38
* This code is entirely taken from
39
* com.netscape.management.client.console.LoginDialog. That one is not
40
* extensible, and it has an admin URL field which we don't want. This
41
* version just has DN and password fields, and no Help button.
45
public class PasswordDialog extends AbstractDialog
46
implements SwingConstants, SuiConstants {
50
* @param parentFrame parent frame
51
* @param authName User ID
53
public PasswordDialog(JFrame parent, String authName) {
54
this(parent, authName,
55
resource.getString("PasswordDialog", "title"));
59
* constructor which let the caller decides the title
61
* @param parentFrame parent frame
62
* @param authName User ID
63
* @param title dialog title
66
* @param parent Parent container.
68
public PasswordDialog( JFrame parent, String authName, String title ) {
69
this(parent, authName, null,
70
resource.getString("PasswordDialog", "title"));
74
* constructor which let the caller decides the title
76
* @param parentFrame parent frame
77
* @param authName User ID
78
* @param title dialog title
81
* @param parent Parent container.
83
public PasswordDialog( JFrame parent, String authName, String authPassword, String title ) {
84
super(parent, title, true, OK | CANCEL);
85
getAccessibleContext().setAccessibleDescription(resource.getString("PasswordDialog",
88
if (authName != null) {
89
_useridField.setText(authName);
91
if (authPassword != null) {
92
_passwordField.setText(authPassword);
95
// Do not allow anonymous bind with empty password/userid
96
DocumentListener _emptyFieldListener = new EmptyFieldListener();
97
_useridField.getDocument().addDocumentListener(_emptyFieldListener);
98
_passwordField.getDocument().addDocumentListener(_emptyFieldListener);
99
_useridField.setText(_useridField.getText()); // force empty field check
101
setMinimumSize(getPreferredSize());
106
_useridField.selectAll();
111
* create the actual dialog
113
protected void createDialogPanel() {
114
JPanel panel = new JPanel();
115
GridBagLayout gridbag = new GridBagLayout();
116
panel.setLayout(gridbag);
117
commonPanelLayout(panel);
121
protected void commonPanelLayout(JPanel panel) {
122
JLabel usernameLabel = new JLabel(resource.getString("PasswordDialog",
124
usernameLabel.setLabelFor(_useridField);
125
GridBagUtil.constrain(panel, usernameLabel,
126
0, GridBagConstraints.RELATIVE,
128
GridBagConstraints.EAST,
129
GridBagConstraints.NONE,
130
0, 0, 0, DIFFERENT_COMPONENT_SPACE);
132
GridBagUtil.constrain(panel, _useridField,
133
1, GridBagConstraints.RELATIVE,
135
GridBagConstraints.NORTHWEST,
136
GridBagConstraints.HORIZONTAL,
139
setFocusComponent(_useridField);
141
JLabel passwordLabel = new JLabel(resource.getString("PasswordDialog",
143
passwordLabel.setLabelFor(_passwordField);
144
GridBagUtil.constrain(panel, passwordLabel,
145
0, GridBagConstraints.RELATIVE,
147
GridBagConstraints.EAST,
148
GridBagConstraints.NONE,
149
COMPONENT_SPACE, 0, 0,
150
DIFFERENT_COMPONENT_SPACE);
152
GridBagUtil.constrain(panel, _passwordField,
153
1, GridBagConstraints.RELATIVE,
155
GridBagConstraints.NORTHWEST,
156
GridBagConstraints.HORIZONTAL,
157
COMPONENT_SPACE, 0, 0, 0);
159
if (_useridField.getText().length() > 0)
160
setFocusComponent(_passwordField);
164
* return the login user name or full dn
166
* @return return the user id field value
168
public String getUsername() {
169
return _useridField.getText();
173
* return the user password
175
* @return return the password field value
177
public String getPassword() {
178
return _passwordField.getText();
182
* set the dialog location
184
* @param parentFrame parent frame
186
protected void setDialogLocation(Frame parentFrame) {
187
if (_x > 0 && _y > 0)
190
setLocationRelativeTo(parentFrame);
196
protected void helpInvoked() {
197
Help.showContextHelp("login","help");
200
// Monitor if input fields are empty
201
class EmptyFieldListener implements DocumentListener {
202
public void insertUpdate(DocumentEvent e) {
206
public void changedUpdate(DocumentEvent e) {
210
public void removeUpdate(DocumentEvent e) {
214
void stateChanged() {
215
boolean passwdEmpty = _passwordField.getText().trim().length() == 0;
216
boolean useridEmpty = _useridField.getText().trim().length() == 0;
217
setOKButtonEnabled( !passwdEmpty && !useridEmpty);
221
public static void main( String[] args ) {
222
Debug.setTrace( true );
224
UIManager.setLookAndFeel(
225
"com.netscape.management.nmclf.SuiLookAndFeel" );
226
} catch (Exception e) {
227
System.err.println("Cannot load nmc look and feel.");
230
PasswordDialog dlg = new PasswordDialog( new JFrame(),
231
"cn=Directory Manager" );
233
System.out.println( "Password = " + dlg.getPassword() );
237
static ResourceSet resource = /* DSUtil._resource; */
238
new ResourceSet("com.netscape.admin.dirserv.dirserv");
239
JTextField _useridField = new JTextField(22);
240
JTextField _passwordField = new JPasswordField(22);
241
protected static String _resourcePrefix = "login";