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