~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/PasswordDialog.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
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.
 
9
 * 
 
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.
 
14
 * 
 
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 **/
 
19
 
 
20
package com.netscape.admin.dirserv;
 
21
 
 
22
import java.awt.*;
 
23
import java.awt.event.*;
 
24
import javax.swing.*;
 
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;
 
35
 
 
36
/**
 
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.
 
42
 *
 
43
 */
 
44
 
 
45
public class PasswordDialog extends AbstractDialog
 
46
                            implements SwingConstants, SuiConstants {
 
47
        /**
 
48
         * constructor
 
49
         *
 
50
         * @param parentFrame parent frame
 
51
         * @param authName User ID
 
52
         */
 
53
        public PasswordDialog(JFrame parent, String authName) {
 
54
                this(parent, authName,
 
55
                         resource.getString("PasswordDialog", "title"));
 
56
        }
 
57
 
 
58
        /**
 
59
         * constructor which let the caller decides the title
 
60
         *
 
61
         * @param parentFrame parent frame
 
62
         * @param authName User ID
 
63
         * @param title dialog title
 
64
         */
 
65
        /**
 
66
         * @param parent Parent container.
 
67
         */
 
68
        public PasswordDialog( JFrame parent, String authName, String title ) {
 
69
                this(parent, authName, null,
 
70
                         resource.getString("PasswordDialog", "title"));
 
71
        }
 
72
 
 
73
        /**
 
74
         * constructor which let the caller decides the title
 
75
         *
 
76
         * @param parentFrame parent frame
 
77
         * @param authName User ID
 
78
         * @param title dialog title
 
79
         */
 
80
        /**
 
81
         * @param parent Parent container.
 
82
         */
 
83
        public PasswordDialog( JFrame parent, String authName, String authPassword, String title ) {
 
84
                super(parent, title, true, OK | CANCEL);
 
85
                getAccessibleContext().setAccessibleDescription(resource.getString("PasswordDialog",
 
86
                                                                                                                                                   "description"));
 
87
                createDialogPanel();
 
88
                if (authName != null) {
 
89
                        _useridField.setText(authName);
 
90
                }
 
91
                if (authPassword != null) {
 
92
                        _passwordField.setText(authPassword);
 
93
                }
 
94
 
 
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
 
100
        
 
101
                setMinimumSize(getPreferredSize());
 
102
                setResizable(true);
 
103
        }
 
104
 
 
105
    public void show() {
 
106
        _useridField.selectAll();
 
107
        super.show();        
 
108
    }    
 
109
    
 
110
        /**
 
111
         * create the actual dialog
 
112
         */
 
113
        protected void createDialogPanel() {
 
114
                JPanel panel = new JPanel();
 
115
                GridBagLayout gridbag = new GridBagLayout();
 
116
                panel.setLayout(gridbag);
 
117
                commonPanelLayout(panel);
 
118
                setComponent(panel);
 
119
        }
 
120
 
 
121
        protected void commonPanelLayout(JPanel panel) {
 
122
                JLabel usernameLabel = new JLabel(resource.getString("PasswordDialog",
 
123
                                                                                                                         "dn"));
 
124
                usernameLabel.setLabelFor(_useridField);
 
125
                GridBagUtil.constrain(panel, usernameLabel, 
 
126
                                                          0, GridBagConstraints.RELATIVE, 
 
127
                                                          1, 1, 0.0, 0.0, 
 
128
                                                          GridBagConstraints.EAST,
 
129
                                                          GridBagConstraints.NONE, 
 
130
                                                          0, 0, 0, DIFFERENT_COMPONENT_SPACE);
 
131
 
 
132
                GridBagUtil.constrain(panel, _useridField, 
 
133
                                                          1, GridBagConstraints.RELATIVE, 
 
134
                                                          1, 1, 1.0, 0.0, 
 
135
                                                          GridBagConstraints.NORTHWEST,
 
136
                                                          GridBagConstraints.HORIZONTAL, 
 
137
                                                          0, 0, 0, 0);
 
138
 
 
139
                setFocusComponent(_useridField);
 
140
 
 
141
                JLabel passwordLabel = new JLabel(resource.getString("PasswordDialog",
 
142
                                                                                                                         "password"));
 
143
                passwordLabel.setLabelFor(_passwordField);
 
144
                GridBagUtil.constrain(panel, passwordLabel, 
 
145
                                                          0, GridBagConstraints.RELATIVE, 
 
146
                                                          1, 1, 0.0, 0.0, 
 
147
                                                          GridBagConstraints.EAST,
 
148
                                                          GridBagConstraints.NONE, 
 
149
                                                          COMPONENT_SPACE, 0, 0,
 
150
                                                          DIFFERENT_COMPONENT_SPACE);
 
151
 
 
152
                GridBagUtil.constrain(panel, _passwordField, 
 
153
                                                          1, GridBagConstraints.RELATIVE, 
 
154
                                                          1, 1, 1.0, 0.0, 
 
155
                                                          GridBagConstraints.NORTHWEST,
 
156
                                                          GridBagConstraints.HORIZONTAL, 
 
157
                                                          COMPONENT_SPACE, 0, 0, 0);
 
158
 
 
159
                if (_useridField.getText().length() > 0)
 
160
                        setFocusComponent(_passwordField);
 
161
        }
 
162
 
 
163
        /**
 
164
         * return the login user name or full dn
 
165
         *
 
166
         * @return return the user id field value
 
167
         */
 
168
        public String getUsername() {
 
169
                return _useridField.getText();
 
170
        }
 
171
 
 
172
        /**
 
173
         * return the user password
 
174
         *
 
175
         * @return return the password field value
 
176
         */
 
177
        public String getPassword() {
 
178
                return _passwordField.getText();
 
179
        }
 
180
 
 
181
        /**
 
182
         * set the dialog location
 
183
         *
 
184
         * @param parentFrame parent frame
 
185
         */
 
186
        protected void setDialogLocation(Frame parentFrame) {
 
187
                if (_x > 0 && _y > 0)
 
188
                        setLocation(_x, _y);
 
189
                else
 
190
                        setLocationRelativeTo(parentFrame);
 
191
        }
 
192
 
 
193
        /**
 
194
         * invoke help
 
195
         */
 
196
        protected void helpInvoked() {
 
197
                Help.showContextHelp("login","help");
 
198
        }
 
199
 
 
200
    // Monitor if input fields are empty
 
201
    class EmptyFieldListener implements DocumentListener {
 
202
        public void insertUpdate(DocumentEvent e) {
 
203
            stateChanged();
 
204
        }
 
205
 
 
206
        public void changedUpdate(DocumentEvent e) {
 
207
            stateChanged();
 
208
        }
 
209
 
 
210
        public void removeUpdate(DocumentEvent e) {
 
211
            stateChanged();
 
212
        }
 
213
 
 
214
        void stateChanged() {
 
215
            boolean passwdEmpty = _passwordField.getText().trim().length() == 0;
 
216
            boolean useridEmpty = _useridField.getText().trim().length() == 0;
 
217
            setOKButtonEnabled( !passwdEmpty && !useridEmpty);
 
218
        }
 
219
    }
 
220
 
 
221
        public static void main( String[] args ) {
 
222
                Debug.setTrace( true );
 
223
       try {
 
224
            UIManager.setLookAndFeel(
 
225
                                "com.netscape.management.nmclf.SuiLookAndFeel" );
 
226
           } catch (Exception e) {
 
227
                   System.err.println("Cannot load nmc look and feel.");
 
228
                   System.exit(-1);
 
229
           }
 
230
                PasswordDialog dlg = new PasswordDialog( new JFrame(),
 
231
                                                                                                 "cn=Directory Manager" );
 
232
                dlg.show();
 
233
                System.out.println( "Password = " + dlg.getPassword() );
 
234
                System.exit( 0 );
 
235
        }
 
236
 
 
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";
 
242
        int _x = -1;
 
243
        int _y = -1;
 
244
}