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