001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.quicksetup.installer.ui;
028
029import org.forgerock.i18n.LocalizableMessage;
030import static org.opends.messages.QuickSetupMessages.*;
031
032import java.awt.Component;
033import java.awt.GridBagConstraints;
034import java.awt.GridBagLayout;
035import java.awt.event.FocusEvent;
036import java.awt.event.FocusListener;
037import java.util.HashMap;
038
039import javax.swing.Box;
040import javax.swing.JLabel;
041import javax.swing.JPanel;
042import javax.swing.text.JTextComponent;
043
044
045import org.opends.quicksetup.UserData;
046import org.opends.quicksetup.ui.FieldName;
047import org.opends.quicksetup.ui.GuiApplication;
048import org.opends.quicksetup.ui.LabelFieldDescriptor;
049import org.opends.quicksetup.ui.QuickSetupStepPanel;
050import org.opends.quicksetup.ui.UIFactory;
051
052/**
053 * This class is used to set the global administrator parameters.
054 */
055public class GlobalAdministratorPanel extends QuickSetupStepPanel
056{
057  private static final long serialVersionUID = 4266485298770553875L;
058
059  private UserData defaultUserData;
060
061  private Component lastFocusComponent;
062
063  private HashMap<FieldName, JLabel> hmLabels = new HashMap<>();
064  private HashMap<FieldName, JTextComponent> hmFields = new HashMap<>();
065
066  /**
067   * Constructor of the panel.
068   * @param application Application represented by this panel and used to
069   * initialize the fields of the panel.
070   */
071  public GlobalAdministratorPanel(GuiApplication application)
072  {
073    super(application);
074    this.defaultUserData = application.getUserData();
075    populateLabelAndFieldMaps();
076    addFocusListeners();
077  }
078
079  /** {@inheritDoc} */
080  public Object getFieldValue(FieldName fieldName)
081  {
082    Object value = null;
083    JTextComponent field = getField(fieldName);
084    if (field != null)
085    {
086      value = field.getText();
087    }
088    return value;
089  }
090
091  /** {@inheritDoc} */
092  public void displayFieldInvalid(FieldName fieldName, boolean invalid)
093  {
094    JLabel label = getLabel(fieldName);
095    if (label != null)
096    {
097      if (invalid)
098      {
099        UIFactory.setTextStyle(label,
100            UIFactory.TextStyle.PRIMARY_FIELD_INVALID);
101      } else
102      {
103        UIFactory
104            .setTextStyle(label, UIFactory.TextStyle.PRIMARY_FIELD_VALID);
105      }
106    }
107  }
108
109  /** {@inheritDoc} */
110  protected Component createInputPanel()
111  {
112    JPanel panel = new JPanel(new GridBagLayout());
113    panel.setOpaque(false);
114
115    GridBagConstraints gbc = new GridBagConstraints();
116    gbc.weightx = 1.0;
117    gbc.fill = GridBagConstraints.HORIZONTAL;
118    gbc.gridwidth = GridBagConstraints.REMAINDER;
119    gbc.insets = UIFactory.getEmptyInsets();
120
121    // Add the server location widgets
122    FieldName[] fields =
123    {
124      FieldName.GLOBAL_ADMINISTRATOR_UID,
125      FieldName.GLOBAL_ADMINISTRATOR_PWD,
126      FieldName.GLOBAL_ADMINISTRATOR_PWD_CONFIRM
127    };
128
129    gbc.insets = UIFactory.getEmptyInsets();
130    for (int i=0; i<fields.length; i++)
131    {
132      if (i != 0)
133      {
134        gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
135      }
136      else
137      {
138        gbc.insets.top = 0;
139      }
140      gbc.gridwidth = GridBagConstraints.RELATIVE;
141      gbc.weightx = 0.0;
142      gbc.insets.left = 0;
143      gbc.anchor = GridBagConstraints.WEST;
144      panel.add(getLabel(fields[i]), gbc);
145
146      JPanel auxPanel = new JPanel(new GridBagLayout());
147      auxPanel.setOpaque(false);
148      gbc.gridwidth = GridBagConstraints.RELATIVE;
149      gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
150      gbc.fill = GridBagConstraints.HORIZONTAL;
151      gbc.weightx = 0.0;
152      auxPanel.add(getField(fields[i]), gbc);
153
154      gbc.gridwidth = GridBagConstraints.REMAINDER;
155      gbc.insets.left = 0;
156      gbc.weightx = 1.0;
157      gbc.fill = GridBagConstraints.HORIZONTAL;
158      auxPanel.add(Box.createHorizontalGlue(), gbc);
159
160      gbc.weightx = 1.0;
161      gbc.fill = GridBagConstraints.HORIZONTAL;
162      gbc.insets = UIFactory.getEmptyInsets();
163      gbc.gridwidth = GridBagConstraints.REMAINDER;
164      panel.add(auxPanel, gbc);
165    }
166
167    addVerticalGlue(panel);
168
169    return panel;
170  }
171
172  /** {@inheritDoc} */
173  protected LocalizableMessage getInstructions()
174  {
175    return INFO_GLOBAL_ADMINISTRATOR_PANEL_INSTRUCTIONS.get();
176  }
177
178  /** {@inheritDoc} */
179  protected LocalizableMessage getTitle()
180  {
181    return INFO_GLOBAL_ADMINISTRATOR_PANEL_TITLE.get();
182  }
183
184  /** {@inheritDoc} */
185  public void endDisplay()
186  {
187    if (lastFocusComponent != null)
188    {
189      lastFocusComponent.requestFocusInWindow();
190    }
191  }
192
193  /**
194   * Returns the default value for the provided field Name.
195   * @param fieldName the field name for which we want to get the default
196   * value.
197   * @return the default value for the provided field Name.
198   */
199  private String getDefaultValue(FieldName fieldName)
200  {
201    String value;
202    switch (fieldName)
203    {
204    case GLOBAL_ADMINISTRATOR_UID:
205      value = defaultUserData.getGlobalAdministratorUID();
206      break;
207
208    case GLOBAL_ADMINISTRATOR_PWD:
209      value = defaultUserData.getGlobalAdministratorPassword();
210      break;
211
212    case GLOBAL_ADMINISTRATOR_PWD_CONFIRM:
213      value = defaultUserData.getGlobalAdministratorPassword();
214      break;
215
216    default:
217      throw new IllegalArgumentException("Unknown field name: " +
218          fieldName);
219    }
220
221    return value;
222  }
223
224  /**
225   * Creates the components and populates the Maps with them.
226   */
227  private void populateLabelAndFieldMaps()
228  {
229    HashMap<FieldName, LabelFieldDescriptor> hm = new HashMap<>();
230
231    hm.put(FieldName.GLOBAL_ADMINISTRATOR_UID, new LabelFieldDescriptor(
232        INFO_GLOBAL_ADMINISTRATOR_UID_LABEL.get(),
233        INFO_GLOBAL_ADMINISTRATOR_UID_TOOLTIP.get(),
234        LabelFieldDescriptor.FieldType.TEXTFIELD,
235        LabelFieldDescriptor.LabelType.PRIMARY, UIFactory.UID_FIELD_SIZE));
236
237    hm.put(FieldName.GLOBAL_ADMINISTRATOR_PWD, new LabelFieldDescriptor(
238        INFO_GLOBAL_ADMINISTRATOR_PWD_LABEL.get(),
239        INFO_GLOBAL_ADMINISTRATOR_PWD_TOOLTIP.get(),
240        LabelFieldDescriptor.FieldType.PASSWORD,
241        LabelFieldDescriptor.LabelType.PRIMARY, UIFactory.PASSWORD_FIELD_SIZE));
242
243    hm.put(FieldName.GLOBAL_ADMINISTRATOR_PWD_CONFIRM,
244        new LabelFieldDescriptor(
245        INFO_GLOBAL_ADMINISTRATOR_PWD_CONFIRM_LABEL.get(),
246        INFO_GLOBAL_ADMINISTRATOR_PWD_CONFIRM_TOOLTIP.get(),
247        LabelFieldDescriptor.FieldType.PASSWORD,
248        LabelFieldDescriptor.LabelType.PRIMARY,
249        UIFactory.PASSWORD_FIELD_SIZE));
250
251    for (FieldName fieldName : hm.keySet())
252    {
253      LabelFieldDescriptor desc = hm.get(fieldName);
254      String defaultValue = getDefaultValue(fieldName);
255      JTextComponent field = UIFactory.makeJTextComponent(desc, defaultValue);
256      JLabel label = UIFactory.makeJLabel(desc);
257
258      hmFields.put(fieldName, field);
259      label.setLabelFor(field);
260
261      hmLabels.put(fieldName, label);
262    }
263  }
264
265  /**
266   * Returns the label associated with the given field name.
267   * @param fieldName the field name for which we want to retrieve the JLabel.
268   * @return the label associated with the given field name.
269   */
270  private JLabel getLabel(FieldName fieldName)
271  {
272    return hmLabels.get(fieldName);
273  }
274
275  /**
276   * Returns the JTextComponent associated with the given field name.
277   * @param fieldName the field name for which we want to retrieve the
278   * JTextComponent.
279   * @return the JTextComponent associated with the given field name.
280   */
281  private JTextComponent getField(FieldName fieldName)
282  {
283    return hmFields.get(fieldName);
284  }
285
286  /**
287   * Adds the required focus listeners to the fields.
288   */
289  private void addFocusListeners()
290  {
291    final FocusListener l = new FocusListener()
292    {
293      public void focusGained(FocusEvent e)
294      {
295        lastFocusComponent = e.getComponent();
296      }
297
298      public void focusLost(FocusEvent e)
299      {
300      }
301    };
302
303    for (JTextComponent tf : hmFields.values())
304    {
305      tf.addFocusListener(l);
306    }
307    lastFocusComponent = getField(FieldName.GLOBAL_ADMINISTRATOR_PWD);
308  }
309}