xref: /titanic_41/usr/src/cmd/print/printmgr/com/sun/admin/pm/client/pmLogin.java (revision 62d717f5277d7b19b63db2d800310f877b57c197)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  *
28  * pmLogin.java
29  * Login dialog
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import java.awt.*;
35 import java.awt.event.*;
36 import javax.swing.JPanel;
37 import javax.swing.*;
38 import com.sun.admin.pm.server.*;
39 
40 
41 
42 /*
43  * a panel dialog which captures a username and password.
44  */
45 
46 public class pmLogin extends pmDialog {
47     private pmTop theTop = null;
48     private String theTag = null;
49     private JFrame theFrame = null;
50 
51     protected pmButton okButton = null;
52     protected pmButton cancelButton = null;
53     protected pmButton helpButton = null;
54 
pmLogin(JFrame f, String title, String msg)55     public pmLogin(JFrame f, String title, String msg) {
56         this(f, title, msg, null, null);
57     }
58 
pmLogin(JFrame f, String title, String msg, pmTop t, String h)59     public pmLogin(JFrame f, String title, String msg, pmTop t, String h) {
60 
61         super(f, title, true);	// modal
62 
63         theTop = t;
64         theTag = h;
65 	theFrame = f;
66 
67         JLabel l;
68         JPanel p;
69 
70         // initialize constraints
71         GridBagConstraints c = new GridBagConstraints();
72         c.gridx = 0;
73         c.gridy = GridBagConstraints.RELATIVE;
74         c.gridwidth = 1;
75         c.gridheight = 1;
76         c.insets = new Insets(10, 10, 10, 10);
77         c.anchor = GridBagConstraints.EAST;
78 
79         // top panel contains the desired message
80         p = new JPanel();
81         p.setLayout(new GridBagLayout());
82 
83         l = new JLabel(msg, SwingConstants.CENTER);
84         p.add(l, c);
85         this.getContentPane().add(p, "North");
86 
87 	// NIS middle panel
88 	// contains username and password
89 	if (t.ns.getNameService().equals("nis")) {
90 
91         p = new JPanel();
92         p.setLayout(new GridBagLayout());
93 
94         l = new JLabel(pmUtility.getResource("Hostname:"),
95                         SwingConstants.RIGHT);
96         p.add(l, c);
97 
98         l = new JLabel(pmUtility.getResource("Username:"),
99                         SwingConstants.RIGHT);
100         p.add(l, c);
101 
102         l = new JLabel(pmUtility.getResource("Password:"),
103                         SwingConstants.RIGHT);
104         p.add(l, c);
105 
106         passwordField = new JPasswordField(12);
107         passwordField.addActionListener(new ActionListener() {
108             public void actionPerformed(ActionEvent e) {
109                 okPressed();
110             }
111         });
112         l.setLabelFor(passwordField);
113         // for consistency, don't implement this until all are...
114         // l.setDisplayedMnemonic(
115 	// 	pmUtility.getIntResource("Password.mnemonic"));
116 
117         c.gridx = 1;
118         c.weightx = 1.0;
119 
120         String nisMaster;
121         try {
122             nisMaster = theTop.host.getNisMaster();
123         } catch (Exception e) {
124             nisMaster = new String("Unknown");
125             Debug.warning("pmLogin: getNisMaster() returns exception: " + e);
126         }
127 
128         c.anchor = GridBagConstraints.WEST;
129 
130         l = new JLabel(nisMaster, SwingConstants.LEFT);
131         p.add(l, c);
132 
133         l = new JLabel(("root"), SwingConstants.LEFT);
134         p.add(l, c);
135 
136 
137         c.fill = GridBagConstraints.HORIZONTAL;
138         c.anchor = GridBagConstraints.CENTER;
139         c.gridy = GridBagConstraints.RELATIVE;
140 
141         p.add(passwordField, c);
142         passwordField.setEchoChar('*');
143 
144         this.getContentPane().add(p, "Center");
145 
146 	} else if (t.ns.getNameService().equals("ldap")) {
147 
148             // middle panel contains LDAP server name, distinguished name,
149             // and password
150             p = new JPanel();
151             p.setLayout(new GridBagLayout());
152 
153             // LDAP Server Name
154             l = new JLabel(pmUtility.getResource("LDAP.Server:"),
155                         SwingConstants.RIGHT);
156             p.add(l, c);
157 
158             serverField = new pmTextField(25);
159             serverField.addActionListener(new ActionListener() {
160                 public void actionPerformed(ActionEvent e) {
161                     okPressed();
162                 }
163             });
164 
165             String ldapMaster;
166             try {
167                 ldapMaster = theTop.host.getLDAPMaster();
168             } catch (Exception e) {
169                 ldapMaster = new String("");
170                 Debug.warning(
171 		    "pmLdap: getLDAPMaster() returns exception: " + e);
172             }
173 
174             serverField.setText(ldapMaster);
175             c.gridx = 1;
176             p.add(serverField, c);
177 
178 
179             // Distinguished Name
180             c.gridx = 0;
181             l = new JLabel(pmUtility.getResource("Distinguished.Name:"),
182                             SwingConstants.RIGHT);
183             p.add(l, c);
184 
185             dnField = new pmTextField(25);
186             dnField.addActionListener(new ActionListener() {
187                 public void actionPerformed(ActionEvent e) {
188                     okPressed();
189                 }
190             });
191 
192             String defaultDN;
193             try {
194                 defaultDN = theTop.host.getDefaultAdminDN();
195             } catch (Exception e) {
196                 defaultDN = new String("");
197                 Debug.warning(
198 		    "pmLdap: getDefaultAdminDN() returns exception: " + e);
199             }
200 
201             dnField.setText(defaultDN);
202             c.gridx = 1;
203             p.add(dnField, c);
204 
205         // Password
206         c.gridx = 0;
207         l = new JLabel(pmUtility.getResource("Password:"),
208                             SwingConstants.RIGHT);
209         p.add(l, c);
210 
211         passwordField = new JPasswordField(12);
212         passwordField.addActionListener(new ActionListener() {
213             public void actionPerformed(ActionEvent e) {
214                 okPressed();
215            }
216         });
217         l.setLabelFor(passwordField);
218         // for consistency, don't implement this until all are...
219         // l.setDisplayedMnemonic(
220 	//	pmUtility.getIntResource("Password.mnemonic"));
221 
222         c.gridx = 1;
223         c.weightx = 1.0;
224 
225         c.fill = GridBagConstraints.HORIZONTAL;
226         c.anchor = GridBagConstraints.CENTER;
227         c.gridy = GridBagConstraints.RELATIVE;
228 
229         p.add(passwordField, c);
230         passwordField.setEchoChar('*');
231 
232         this.getContentPane().add(p, "Center");
233 
234 	}
235 
236 
237         // bottom panel contains buttons
238         c.gridx = 0;
239         c.weightx = 1.0;
240         c.weighty = 0.0;
241         c.gridwidth = GridBagConstraints.REMAINDER;
242         c.fill = GridBagConstraints.HORIZONTAL;
243         c.anchor = GridBagConstraints.CENTER;
244 
245         JPanel thePanel = new JPanel();
246 
247         okButton = new pmButton(
248             pmUtility.getResource("OK"));
249         okButton.setMnemonic(
250             pmUtility.getIntResource("OK.mnemonic"));
251         okButton.addActionListener(new ActionListener() {
252             public void actionPerformed(ActionEvent evt) {
253                 okPressed();
254             }
255         });
256         thePanel.add(okButton, c);
257 
258         cancelButton = new pmButton(
259             pmUtility.getResource("Cancel"));
260         cancelButton.setMnemonic(
261             pmUtility.getIntResource("Cancel.mnemonic"));
262         cancelButton.addActionListener(new ActionListener() {
263             public void actionPerformed(ActionEvent evt) {
264                 cancelPressed();
265             }
266         });
267         thePanel.add(cancelButton, c);
268 
269         if (theTag != null && theTop != null) {
270 
271             helpButton = new pmButton(
272                 pmUtility.getResource("Help"));
273             helpButton.setMnemonic(
274                 pmUtility.getIntResource("Help.mnemonic"));
275             p.add(helpButton);
276             helpButton.addActionListener(new ActionListener() {
277                 public void actionPerformed(ActionEvent evt) {
278 					helpPressed();
279                 }
280             });
281             thePanel.add(helpButton, c);
282         }
283 
284         this.getContentPane().add(thePanel, "South");
285 
286         addWindowListener(new WindowAdapter() {
287             public void windowClosing(WindowEvent e) {
288                 returnValue = JOptionPane.CLOSED_OPTION;
289                 pmLogin.this.setVisible(false);
290             }
291         });
292 
293         // handle Esc as cancel in any case
294         this.getRootPane().registerKeyboardAction(new ActionListener() {
295             public void actionPerformed(ActionEvent e) {
296                 Debug.message("CLNT:  default cancel action");
297                 cancelPressed();
298             }},
299             KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
300             JComponent.WHEN_IN_FOCUSED_WINDOW);
301 
302         // lay out the dialog
303         this.pack();
304 
305         // set focus and defaults after packing...
306         // this.getRootPane().setDefaultButton(okButton);
307         okButton.setAsDefaultButton();
308 
309         passwordField.requestFocus();
310     }
311 
getValue()312     public int getValue() {
313         return returnValue;
314     }
315 
getLDAPServer()316     public void getLDAPServer() throws pmIncompleteFormException {
317     // LDAP Server name is required
318 	String LDAPserver = null;
319 	LDAPserver = serverField.getText();
320 	if (LDAPserver.equals("")) {
321 	    serverField.requestFocus();
322 	    throw new pmIncompleteFormException(
323 		pmUtility.getResource("LDAP.server.name.required."));
324 	}
325     }
326 
getLDAPDN()327     public void getLDAPDN() throws pmIncompleteFormException {
328     // LDAP Distinguished name is required
329 	String LDAPdn = null;
330 	LDAPdn = dnField.getText();
331 	if (LDAPdn.equals("")) {
332 		dnField.requestFocus();
333 		throw new pmIncompleteFormException(
334 		    pmUtility.getResource("LDAP.Distinguished.name.required."));
335 	}
336     }
337 
getLDAPPassword()338     public void getLDAPPassword() throws pmIncompleteFormException {
339 
340     // LDAP password is required
341 
342 	String tmpp = new String(passwordField.getPassword());
343 	String LDAPpass = new String(tmpp.trim());
344 
345 	if (LDAPpass.equals("")) {
346 		passwordField.requestFocus();
347 		throw new pmIncompleteFormException(
348 			pmUtility.getResource("LDAP.Password.required."));
349 	}
350     }
351 
okPressed()352     public void okPressed() {
353 
354 	// For LDAP, Check Server, Distinguished Name and Password
355 	boolean complete = true;
356 
357 	if (theTop.ns.getNameService().equals("ldap")) {
358 		complete = false;
359 		try {
360 			getLDAPServer();
361 			getLDAPDN();
362 			getLDAPPassword();
363 			complete = true;
364 		} catch (pmIncompleteFormException fe) {
365                     pmMessageDialog m = new pmMessageDialog(
366                         theFrame,
367                         pmUtility.getResource("Error"),
368                         fe.getMessage());   // "FormError"
369                     m.setVisible(true);
370 		}
371 	}
372 
373 	if (complete) {
374             returnValue = JOptionPane.OK_OPTION;
375             pmLogin.this.setVisible(false);
376 	}
377     }
378 
379 
380 
cancelPressed()381     public void cancelPressed() {
382        	returnValue = JOptionPane.CANCEL_OPTION;
383        	pmLogin.this.setVisible(false);
384     }
385 
386 
clearPressed()387     public void clearPressed() {
388 
389         passwordField.setText("");
390     }
391 
helpPressed()392     public void helpPressed() {
393         theTop.showHelpItem(theTag);
394     }
395 
main(String[] args)396     public static void main(String[] args) {
397         JFrame f = new JFrame("Password test");
398 
399         f.setSize(300, 100);
400         f.addWindowListener(new WindowAdapter() {
401             public void windowClosing(WindowEvent e) {
402                 System.exit(0);
403             }
404         });
405         f.setVisible(true);
406 
407 	while (true) {
408 	    pmLogin d = new pmLogin(f, "Test Login",
409 			"NIS/LDAP Authentication.");
410         d.setVisible(true);
411 
412 
413     }
414      // System.exit(0);
415     }
416 
417     public JPasswordField passwordField = null;
418     public pmTextField serverField = null;
419     public pmTextField dnField = null;
420 
421     protected int returnValue = JOptionPane.CLOSED_OPTION;
422 
423 }
424