NewUserPanel.java revision 998747bfaaa3c6b28bbfaf0e282e6c0ccbf46bc0
1276N/A/*
1276N/A * CDDL HEADER START
1276N/A *
1276N/A * The contents of this file are subject to the terms of the
1276N/A * Common Development and Distribution License, Version 1.0 only
1276N/A * (the "License"). You may not use this file except in compliance
1276N/A * with the License.
1276N/A *
6982N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6982N/A * or http://forgerock.org/license/CDDLv1.0.html.
1276N/A * See the License for the specific language governing permissions
1276N/A * and limitations under the License.
1276N/A *
1276N/A * When distributing Covered Code, include this CDDL HEADER in each
6982N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6982N/A * If applicable, add the following below this CDDL HEADER, with the
6982N/A * fields enclosed by brackets "[]" replaced with your own identifying
6982N/A * information:
1276N/A * Portions Copyright [yyyy] [name of copyright owner]
1276N/A *
1276N/A * CDDL HEADER END
1276N/A *
1276N/A *
3215N/A * Copyright 2008-2009 Sun Microsystems, Inc.
1276N/A * Portions Copyright 2014-2015 ForgeRock AS
1276N/A */
1276N/A
1276N/Apackage org.opends.guitools.controlpanel.ui;
1276N/A
1276N/Aimport static org.opends.messages.AdminToolMessages.*;
1276N/A
1276N/Aimport java.awt.Component;
1276N/Aimport java.awt.GridBagConstraints;
1276N/Aimport java.awt.event.ActionEvent;
1276N/Aimport java.awt.event.ActionListener;
1276N/Aimport java.io.IOException;
1276N/Aimport java.util.ArrayList;
1276N/A
1276N/Aimport javax.swing.DefaultComboBoxModel;
1276N/Aimport javax.swing.JComboBox;
2342N/Aimport javax.swing.JLabel;
1276N/Aimport javax.swing.JPasswordField;
1276N/Aimport javax.swing.JTextField;
1276N/Aimport javax.swing.event.DocumentEvent;
1276N/Aimport javax.swing.event.DocumentListener;
1276N/A
1276N/Aimport org.opends.guitools.controlpanel.browser.BrowserController;
1276N/Aimport org.opends.guitools.controlpanel.ui.nodes.BasicNode;
1276N/Aimport org.opends.guitools.controlpanel.util.Utilities;
1276N/Aimport org.forgerock.i18n.LocalizableMessage;
1276N/Aimport org.opends.server.types.OpenDsException;
1276N/A
1276N/A/**
1276N/A * The panel used to create a new user.
1276N/A *
1276N/A */
1276N/Apublic class NewUserPanel extends AbstractNewEntryPanel
1276N/A{
1276N/A private static final long serialVersionUID = -2450090053404111892L;
1276N/A private JLabel lFirstName = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_FIRST_NAME_LABEL.get());
1276N/A private JLabel lLastName = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_LAST_NAME_LABEL.get());
1276N/A private JLabel lCommonNames = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_COMMON_NAMES_LABEL.get());
1276N/A private JLabel lUserID = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_UID_LABEL.get());
1276N/A private JLabel lPassword = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_PASSWORD_LABEL.get());
1276N/A private JLabel lConfirmPassword = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_CONFIRM_PASSWORD_LABEL.get());
1276N/A private JLabel lEmail = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_EMAIL_LABEL.get());
1276N/A private JLabel lTelephoneNumber = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_TELEPHONE_NUMBER_LABEL.get());
1276N/A private JLabel lFaxNumber = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_FAX_NUMBER_LABEL.get());
1276N/A private JLabel lNamingAttribute = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_NAMING_ATTRIBUTE_LABEL.get());
1276N/A private JLabel lEntryDN = Utilities.createPrimaryLabel(
1276N/A INFO_CTRL_PANEL_NEW_USER_ENTRY_DN_LABEL.get());
1276N/A
1276N/A private JLabel[] labels = {lFirstName, lLastName, lCommonNames, lUserID,
1276N/A lPassword, lConfirmPassword, lEmail, lTelephoneNumber, lFaxNumber,
1276N/A lNamingAttribute, lEntryDN
1276N/A };
1276N/A
1276N/A private JTextField firstName = Utilities.createLongTextField();
1276N/A private JTextField lastName = Utilities.createLongTextField();
1276N/A private JTextField commonName = Utilities.createLongTextField();
1276N/A private JTextField userID = Utilities.createLongTextField();
1276N/A private JPasswordField password = Utilities.createPasswordField();
1276N/A private JPasswordField confirmPassword = Utilities.createPasswordField(30);
1276N/A private JTextField eMail = Utilities.createLongTextField();
1276N/A private JTextField telephoneNumber = Utilities.createLongTextField();
1276N/A private JTextField faxNumber = Utilities.createLongTextField();
1276N/A private JComboBox namingAttribute = Utilities.createComboBox();
1276N/A private JLabel dn = Utilities.createDefaultLabel();
1276N/A
1276N/A Component[] comps = {firstName, lastName, commonName, userID,
1276N/A password, confirmPassword, eMail, telephoneNumber, faxNumber,
1276N/A namingAttribute, dn};
1276N/A
1276N/A private final JTextField[] NAMING_ATTRIBUTE_TEXTFIELDS =
1276N/A {commonName, firstName, lastName, userID};
1276N/A private final String[] NAMING_ATTRIBUTES = {"cn", "givenName", "sn", "uid"};
1276N/A
1276N/A /**
1276N/A * Default constructor.
1276N/A *
1276N/A */
1276N/A public NewUserPanel()
1276N/A {
1276N/A super();
1276N/A createLayout();
1276N/A }
1276N/A
1276N/A /**
1276N/A * {@inheritDoc}
1276N/A */
1276N/A public void setParent(BasicNode parentNode, BrowserController controller)
1276N/A {
1276N/A super.setParent(parentNode, controller);
1276N/A dn.setText(namingAttribute.getSelectedItem()+"=,"+parentNode.getDN());
1276N/A for (Component comp : comps)
1276N/A {
1276N/A if (comp instanceof JTextField)
1276N/A {
1276N/A ((JTextField)comp).setText("");
1276N/A }
1276N/A }
1276N/A }
1276N/A
1276N/A /**
1276N/A * {@inheritDoc}
1276N/A */
1276N/A public LocalizableMessage getTitle()
1276N/A {
1276N/A return INFO_CTRL_PANEL_NEW_USER_PANEL_TITLE.get();
1276N/A }
1276N/A
1276N/A /**
1276N/A * {@inheritDoc}
1276N/A */
1276N/A public Component getPreferredFocusComponent()
1276N/A {
1276N/A return firstName;
1276N/A }
1276N/A
1276N/A /**
1276N/A * {@inheritDoc}
1276N/A */
1276N/A protected LocalizableMessage getProgressDialogTitle()
1276N/A {
1276N/A return INFO_CTRL_PANEL_NEW_USER_PANEL_TITLE.get();
1276N/A }
1276N/A
1276N/A /**
1276N/A * {@inheritDoc}
1276N/A */
1276N/A protected void checkSyntax(ArrayList<LocalizableMessage> errors)
1276N/A {
1276N/A for (JLabel label : labels)
1276N/A {
1276N/A setPrimaryValid(label);
1276N/A }
1276N/A
1276N/A JTextField[] requiredFields = {lastName, commonName};
1276N/A LocalizableMessage[] msgs = {ERR_CTRL_PANEL_USER_LAST_NAME_REQUIRED.get(),
1276N/A ERR_CTRL_PANEL_USER_COMMON_NAME_REQUIRED.get()
1276N/A };
1276N/A for (int i=0; i<requiredFields.length; i++)
1276N/A {
1276N/A String v = requiredFields[i].getText().trim();
1276N/A if (v.length() == 0)
1276N/A {
1276N/A errors.add(msgs[i]);
1276N/A }
1276N/A }
1276N/A
1276N/A String attr = (String)namingAttribute.getSelectedItem();
1276N/A for (int i=0 ; i<NAMING_ATTRIBUTE_TEXTFIELDS.length; i++)
1276N/A {
1276N/A boolean isRequired = false;
1276N/A for (JTextField tf : requiredFields)
1276N/A {
1276N/A if (tf == NAMING_ATTRIBUTE_TEXTFIELDS[i])
1276N/A {
1276N/A isRequired = true;
1276N/A break;
1276N/A }
1276N/A }
1276N/A if (!isRequired)
1276N/A {
1276N/A if (attr.equalsIgnoreCase(NAMING_ATTRIBUTES[i]))
1276N/A {
1276N/A String value = NAMING_ATTRIBUTE_TEXTFIELDS[i].getText().trim();
1276N/A if (value.length() == 0)
1276N/A {
1276N/A errors.add(ERR_CTRL_PANEL_USER_NAMING_ATTRIBUTE_REQUIRED.get(attr));
1276N/A }
1276N/A break;
1276N/A }
1276N/A }
1276N/A }
1276N/A
1276N/A char[] pwd1 = password.getPassword();
1276N/A char[] pwd2 = confirmPassword.getPassword();
1276N/A String sPwd1 = new String(pwd1);
1276N/A String sPwd2 = new String(pwd2);
1276N/A if (!sPwd1.equals(sPwd2))
1276N/A {
1276N/A errors.add(ERR_CTRL_PANEL_PASSWORD_DO_NOT_MATCH.get());
1276N/A }
1276N/A
1276N/A if (errors.size() == 0)
1276N/A {
1276N/A try
1276N/A {
1276N/A getEntry();
1276N/A }
1276N/A catch (OpenDsException ode)
1276N/A {
1276N/A errors.add(ode.getMessageObject());
1276N/A }
1276N/A catch (IOException ioe)
1276N/A {
1276N/A // This should not occur
1276N/A throw new RuntimeException("Unexpected error: "+ioe, ioe);
1276N/A }
1276N/A }
1276N/A }
1276N/A
1276N/A
1276N/A /**
1276N/A * Creates the layout of the panel (but the contents are not populated here).
1276N/A */
1276N/A private void createLayout()
1276N/A {
1276N/A GridBagConstraints gbc = new GridBagConstraints();
1276N/A Utilities.setRequiredIcon(lLastName);
1276N/A Utilities.setRequiredIcon(lCommonNames);
1276N/A
1276N/A gbc.gridwidth = 2;
1276N/A gbc.gridy = 0;
1276N/A addErrorPane(gbc);
1276N/A
1276N/A gbc.gridy ++;
1276N/A gbc.gridwidth = 1;
1276N/A gbc.weighty = 0.0;
1276N/A gbc.gridx = 1;
1276N/A gbc.anchor = GridBagConstraints.EAST;
1276N/A gbc.fill = GridBagConstraints.NONE;
1276N/A JLabel requiredLabel = createRequiredLabel();
1276N/A gbc.insets.bottom = 10;
1276N/A add(requiredLabel, gbc);
1276N/A
1276N/A gbc.gridy ++;
1276N/A gbc.fill = GridBagConstraints.HORIZONTAL;
1276N/A gbc.anchor = GridBagConstraints.WEST;
1276N/A gbc.insets.bottom = 0;
1276N/A
1276N/A Component[] inlineHelp = {null, null, null, null, null,
1276N/A null, null, null, null, null, null};
1276N/A
1276N/A for (int i=0 ; i< labels.length; i++)
1276N/A {
1276N/A gbc.insets.left = 0;
1276N/A gbc.weightx = 0.0;
1276N/A gbc.gridx = 0;
1276N/A add(labels[i], gbc);
1276N/A gbc.insets.left = 10;
1276N/A gbc.gridx = 1;
1276N/A if (comps[i] instanceof JComboBox)
1276N/A {
1276N/A gbc.weightx = 0.0;
1276N/A gbc.fill = GridBagConstraints.NONE;
1276N/A }
1276N/A else
1276N/A {
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
}
add(comps[i], gbc);
if (inlineHelp[i] != null)
{
gbc.insets.top = 3;
gbc.gridy ++;
add(inlineHelp[i], gbc);
}
gbc.insets.top = 10;
gbc.gridy ++;
}
addBottomGlue(gbc);
DocumentListener listener = new DocumentListener()
{
/**
* {@inheritDoc}
*/
public void insertUpdate(DocumentEvent ev)
{
updateDNValue();
}
/**
* {@inheritDoc}
*/
public void changedUpdate(DocumentEvent ev)
{
insertUpdate(ev);
}
/**
* {@inheritDoc}
*/
public void removeUpdate(DocumentEvent ev)
{
insertUpdate(ev);
}
};
JTextField[] toAddListener = {firstName, lastName, commonName, userID};
for (JTextField tf : toAddListener)
{
tf.getDocument().addDocumentListener(listener);
}
DefaultComboBoxModel model = new DefaultComboBoxModel(NAMING_ATTRIBUTES);
namingAttribute.setModel(model);
namingAttribute.setSelectedItem(NAMING_ATTRIBUTES[0]);
namingAttribute.addActionListener(new ActionListener()
{
/**
* {@inheritDoc}
*/
public void actionPerformed(ActionEvent ev)
{
updateDNValue();
}
});
}
/**
* Updates the contents of DN value to reflect the data that the user
* is providing.
*
*/
private void updateDNValue()
{
String attr = (String)namingAttribute.getSelectedItem();
for (int i=0 ; i<NAMING_ATTRIBUTE_TEXTFIELDS.length; i++)
{
if (attr.equalsIgnoreCase(NAMING_ATTRIBUTES[i]))
{
String value = NAMING_ATTRIBUTE_TEXTFIELDS[i].getText().trim();
String rdn = Utilities.getRDNString(attr, value);
dn.setText(rdn+","+parentNode.getDN());
break;
}
}
}
/**
* {@inheritDoc}
*/
protected String getLDIF()
{
StringBuilder sb = new StringBuilder();
sb.append("dn: ").append(dn.getText()).append("\n");
String[] attrNames = {"givenName", "sn", "cn", "uid", "userPassword",
"mail", "telephoneNumber", "facsimileTelephoneNumber"};
JTextField[] textFields = {firstName, lastName, commonName, userID,
password, eMail, telephoneNumber, faxNumber};
sb.append("objectclass: top\n");
sb.append("objectclass: person\n");
sb.append("objectclass: inetOrgPerson\n");
for (int i=0; i<attrNames.length; i++)
{
String value = textFields[i].getText().trim();
if (value.length() > 0)
{
sb.append(attrNames[i]).append(": ").append(value).append("\n");
}
}
return sb.toString();
}
}