001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.ui;
019
020import static org.opends.messages.AdminToolMessages.*;
021
022import java.awt.Component;
023import java.awt.GridBagConstraints;
024import java.util.ArrayList;
025
026import javax.swing.JLabel;
027import javax.swing.JPasswordField;
028
029import org.opends.guitools.controlpanel.browser.BrowserController;
030import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
031import org.opends.guitools.controlpanel.task.ResetUserPasswordTask;
032import org.opends.guitools.controlpanel.task.Task;
033import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
034import org.opends.guitools.controlpanel.util.Utilities;
035import org.forgerock.i18n.LocalizableMessage;
036
037/**
038 * Panel that appears when the user wants to change the password of a user.
039 *
040 */
041public class ResetUserPasswordPanel extends StatusGenericPanel
042{
043  private static final long serialVersionUID = 8733172823605832626L;
044  private JLabel dn = Utilities.createDefaultLabel();
045  private JLabel name = Utilities.createDefaultLabel();
046  private JLabel lPassword = Utilities.createPrimaryLabel();
047  private JLabel lConfirmPassword = Utilities.createPrimaryLabel();
048  private JPasswordField password = Utilities.createPasswordField(25);
049  private JPasswordField confirmPassword = Utilities.createPasswordField(25);
050
051  private BasicNode node;
052  private BrowserController controller;
053
054  /**
055   * Constructor of the panel.
056   *
057   */
058  public ResetUserPasswordPanel()
059  {
060    super();
061    createLayout();
062  }
063
064  /**
065   * Sets the node representing the entry that the user wants to modify the
066   * password from.
067   * @param node the node.
068   * @param controller the browser controller.
069   */
070  public void setValue(BasicNode node, BrowserController controller)
071  {
072    this.node = node;
073    this.controller = controller;
074    setPrimaryValid(lPassword);
075    setPrimaryValid(lConfirmPassword);
076
077    dn.setText(node.getDN());
078    name.setText(node.getDisplayName());
079
080    password.setText("");
081    confirmPassword.setText("");
082
083    packParentDialog();
084  }
085
086  /** {@inheritDoc} */
087  public Component getPreferredFocusComponent()
088  {
089    return password;
090  }
091
092  /** {@inheritDoc} */
093  public void okClicked()
094  {
095    final ArrayList<LocalizableMessage> errors = new ArrayList<>();
096
097    setPrimaryValid(lPassword);
098    setPrimaryValid(lConfirmPassword);
099
100    String pwd1 = new String(password.getPassword());
101    String pwd2 = new String(confirmPassword.getPassword());
102
103    if (pwd1.length() == 0)
104    {
105      errors.add(ERR_CTRL_PANEL_NEW_PASSWORD_REQUIRED.get());
106      setPrimaryInvalid(lPassword);
107    }
108    else if (!pwd1.equals(pwd2))
109    {
110      errors.add(ERR_CTRL_PANEL_PASSWORD_DO_NOT_MATCH.get());
111      setPrimaryInvalid(lPassword);
112      setPrimaryInvalid(lConfirmPassword);
113    }
114    if (errors.isEmpty())
115    {
116      ProgressDialog dlg = new ProgressDialog(
117          Utilities.createFrame(),
118          Utilities.getParentDialog(this),
119          INFO_CTRL_PANEL_RESET_USER_PASSWORD_TITLE.get(), getInfo());
120      ResetUserPasswordTask newTask =
121        new ResetUserPasswordTask(getInfo(), dlg, node, controller,
122            password.getPassword());
123      for (Task task : getInfo().getTasks())
124      {
125        task.canLaunch(newTask, errors);
126      }
127      if (errors.isEmpty())
128      {
129        launchOperation(newTask,
130            INFO_CTRL_PANEL_RESETTING_USER_PASSWORD_SUMMARY.get(),
131            INFO_CTRL_PANEL_RESETTING_USER_PASSWORD_SUCCESSFUL_SUMMARY.get(),
132            INFO_CTRL_PANEL_RESETTING_USER_PASSWORD_SUCCESSFUL_DETAILS.get(),
133            ERR_CTRL_PANEL_RESETTING_USER_PASSWORD_ERROR_SUMMARY.get(),
134            ERR_CTRL_PANEL_RESETTING_USER_PASSWORD_ERROR_DETAILS.get(),
135            null,
136            dlg);
137        Utilities.getParentDialog(this).setVisible(false);
138        dlg.setVisible(true);
139      }
140    }
141    if (!errors.isEmpty())
142    {
143      displayErrorDialog(errors);
144    }
145  }
146
147  /** {@inheritDoc} */
148  public LocalizableMessage getTitle()
149  {
150    return INFO_CTRL_PANEL_RESET_USER_PASSWORD_TITLE.get();
151  }
152
153  /** {@inheritDoc} */
154  public void configurationChanged(ConfigurationChangeEvent ev)
155  {
156  }
157
158  /**
159   * Creates the layout of the panel (but the contents are not populated here).
160   */
161  private void createLayout()
162  {
163    GridBagConstraints gbc = new GridBagConstraints();
164    gbc.gridx = 0;
165    gbc.gridy = 0;
166    gbc.weightx = 0.0;
167    gbc.weighty = 0.0;
168    gbc.fill = GridBagConstraints.HORIZONTAL;
169
170    LocalizableMessage[] strings =
171    {
172        INFO_CTRL_PANEL_RESET_USER_PASSWORD_DN_LABEL.get(),
173        INFO_CTRL_PANEL_RESET_USER_PASSWORD_NAME_LABEL.get(),
174        INFO_CTRL_PANEL_RESET_USER_PASSWORD_PWD_LABEL.get(),
175        INFO_CTRL_PANEL_RESET_USER_PASSWORD_CONFIRM_LABEL.get()
176    };
177    JLabel[] labels = {null, null, lPassword, lConfirmPassword};
178    Component[] comps = {dn, name, password, confirmPassword};
179
180    for (int i=0; i<strings.length; i++)
181    {
182      if (labels[i] == null)
183      {
184        labels[i] = Utilities.createPrimaryLabel(strings[i]);
185      }
186      else
187      {
188        labels[i].setText(strings[i].toString());
189      }
190
191      gbc.gridx = 0;
192      gbc.insets.left = 0;
193      gbc.weightx = 0.0;
194      add(labels[i], gbc);
195      gbc.insets.left = 10;
196      gbc.gridx ++;
197      gbc.weightx = 1.0;
198      add(comps[i], gbc);
199
200      gbc.insets.top = 10;
201      gbc.gridy ++;
202    }
203
204    addBottomGlue(gbc);
205  }
206}