mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
14.27.2016 7028d9f1483d6f1e77bb0f5ebd0ecc6239e431c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.quicksetup.installer.ui;
 
import org.forgerock.i18n.LocalizableMessage;
import static org.opends.messages.QuickSetupMessages.*;
 
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.HashMap;
import java.util.Map;
 
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;
 
 
import org.opends.quicksetup.UserData;
import org.opends.quicksetup.ui.FieldName;
import org.opends.quicksetup.ui.GuiApplication;
import org.opends.quicksetup.ui.LabelFieldDescriptor;
import org.opends.quicksetup.ui.QuickSetupStepPanel;
import org.opends.quicksetup.ui.UIFactory;
 
/** This class is used to set the global administrator parameters. */
public class GlobalAdministratorPanel extends QuickSetupStepPanel
{
  private static final long serialVersionUID = 4266485298770553875L;
 
  private UserData defaultUserData;
 
  private Component lastFocusComponent;
 
  private HashMap<FieldName, JLabel> hmLabels = new HashMap<>();
  private HashMap<FieldName, JTextComponent> hmFields = new HashMap<>();
 
  /**
   * Constructor of the panel.
   * @param application Application represented by this panel and used to
   * initialize the fields of the panel.
   */
  public GlobalAdministratorPanel(GuiApplication application)
  {
    super(application);
    this.defaultUserData = application.getUserData();
    populateLabelAndFieldMaps();
    addFocusListeners();
  }
 
  @Override
  public Object getFieldValue(FieldName fieldName)
  {
    Object value = null;
    JTextComponent field = getField(fieldName);
    if (field != null)
    {
      value = field.getText();
    }
    return value;
  }
 
  @Override
  public void displayFieldInvalid(FieldName fieldName, boolean invalid)
  {
    JLabel label = getLabel(fieldName);
    if (label != null)
    {
      if (invalid)
      {
        UIFactory.setTextStyle(label,
            UIFactory.TextStyle.PRIMARY_FIELD_INVALID);
      } else
      {
        UIFactory
            .setTextStyle(label, UIFactory.TextStyle.PRIMARY_FIELD_VALID);
      }
    }
  }
 
  @Override
  protected Component createInputPanel()
  {
    JPanel panel = new JPanel(new GridBagLayout());
    panel.setOpaque(false);
 
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.insets = UIFactory.getEmptyInsets();
 
    // Add the server location widgets
    FieldName[] fields =
    {
      FieldName.GLOBAL_ADMINISTRATOR_UID,
      FieldName.GLOBAL_ADMINISTRATOR_PWD,
      FieldName.GLOBAL_ADMINISTRATOR_PWD_CONFIRM
    };
 
    gbc.insets = UIFactory.getEmptyInsets();
    for (int i=0; i<fields.length; i++)
    {
      if (i != 0)
      {
        gbc.insets.top = UIFactory.TOP_INSET_SECONDARY_FIELD;
      }
      else
      {
        gbc.insets.top = 0;
      }
      gbc.gridwidth = GridBagConstraints.RELATIVE;
      gbc.weightx = 0.0;
      gbc.insets.left = 0;
      gbc.anchor = GridBagConstraints.WEST;
      panel.add(getLabel(fields[i]), gbc);
 
      JPanel auxPanel = new JPanel(new GridBagLayout());
      auxPanel.setOpaque(false);
      gbc.gridwidth = GridBagConstraints.RELATIVE;
      gbc.insets.left = UIFactory.LEFT_INSET_SECONDARY_FIELD;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.weightx = 0.0;
      auxPanel.add(getField(fields[i]), gbc);
 
      gbc.gridwidth = GridBagConstraints.REMAINDER;
      gbc.insets.left = 0;
      gbc.weightx = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      auxPanel.add(Box.createHorizontalGlue(), gbc);
 
      gbc.weightx = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = UIFactory.getEmptyInsets();
      gbc.gridwidth = GridBagConstraints.REMAINDER;
      panel.add(auxPanel, gbc);
    }
 
    addVerticalGlue(panel);
 
    return panel;
  }
 
  @Override
  protected LocalizableMessage getInstructions()
  {
    return INFO_GLOBAL_ADMINISTRATOR_PANEL_INSTRUCTIONS.get();
  }
 
  @Override
  protected LocalizableMessage getTitle()
  {
    return INFO_GLOBAL_ADMINISTRATOR_PANEL_TITLE.get();
  }
 
  @Override
  public void endDisplay()
  {
    if (lastFocusComponent != null)
    {
      lastFocusComponent.requestFocusInWindow();
    }
  }
 
  /**
   * Returns the default value for the provided field Name.
   * @param fieldName the field name for which we want to get the default
   * value.
   * @return the default value for the provided field Name.
   */
  private String getDefaultValue(FieldName fieldName)
  {
    String value;
    switch (fieldName)
    {
    case GLOBAL_ADMINISTRATOR_UID:
      value = defaultUserData.getGlobalAdministratorUID();
      break;
 
    case GLOBAL_ADMINISTRATOR_PWD:
      value = defaultUserData.getGlobalAdministratorPassword();
      break;
 
    case GLOBAL_ADMINISTRATOR_PWD_CONFIRM:
      value = defaultUserData.getGlobalAdministratorPassword();
      break;
 
    default:
      throw new IllegalArgumentException("Unknown field name: " +
          fieldName);
    }
 
    return value;
  }
 
  /** Creates the components and populates the Maps with them. */
  private void populateLabelAndFieldMaps()
  {
    HashMap<FieldName, LabelFieldDescriptor> hm = new HashMap<>();
 
    hm.put(FieldName.GLOBAL_ADMINISTRATOR_UID, new LabelFieldDescriptor(
        INFO_GLOBAL_ADMINISTRATOR_UID_LABEL.get(),
        INFO_GLOBAL_ADMINISTRATOR_UID_TOOLTIP.get(),
        LabelFieldDescriptor.FieldType.TEXTFIELD,
        LabelFieldDescriptor.LabelType.PRIMARY, UIFactory.UID_FIELD_SIZE));
 
    hm.put(FieldName.GLOBAL_ADMINISTRATOR_PWD, new LabelFieldDescriptor(
        INFO_GLOBAL_ADMINISTRATOR_PWD_LABEL.get(),
        INFO_GLOBAL_ADMINISTRATOR_PWD_TOOLTIP.get(),
        LabelFieldDescriptor.FieldType.PASSWORD,
        LabelFieldDescriptor.LabelType.PRIMARY, UIFactory.PASSWORD_FIELD_SIZE));
 
    hm.put(FieldName.GLOBAL_ADMINISTRATOR_PWD_CONFIRM,
        new LabelFieldDescriptor(
        INFO_GLOBAL_ADMINISTRATOR_PWD_CONFIRM_LABEL.get(),
        INFO_GLOBAL_ADMINISTRATOR_PWD_CONFIRM_TOOLTIP.get(),
        LabelFieldDescriptor.FieldType.PASSWORD,
        LabelFieldDescriptor.LabelType.PRIMARY,
        UIFactory.PASSWORD_FIELD_SIZE));
 
    for (Map.Entry<FieldName, LabelFieldDescriptor> entry : hm.entrySet())
    {
      FieldName fieldName = entry.getKey();
      LabelFieldDescriptor desc = entry.getValue();
      String defaultValue = getDefaultValue(fieldName);
      JTextComponent field = UIFactory.makeJTextComponent(desc, defaultValue);
      JLabel label = UIFactory.makeJLabel(desc);
 
      hmFields.put(fieldName, field);
      label.setLabelFor(field);
 
      hmLabels.put(fieldName, label);
    }
  }
 
  /**
   * Returns the label associated with the given field name.
   * @param fieldName the field name for which we want to retrieve the JLabel.
   * @return the label associated with the given field name.
   */
  private JLabel getLabel(FieldName fieldName)
  {
    return hmLabels.get(fieldName);
  }
 
  /**
   * Returns the JTextComponent associated with the given field name.
   * @param fieldName the field name for which we want to retrieve the
   * JTextComponent.
   * @return the JTextComponent associated with the given field name.
   */
  private JTextComponent getField(FieldName fieldName)
  {
    return hmFields.get(fieldName);
  }
 
  /** Adds the required focus listeners to the fields. */
  private void addFocusListeners()
  {
    final FocusListener l = new FocusListener()
    {
      @Override
      public void focusGained(FocusEvent e)
      {
        lastFocusComponent = e.getComponent();
      }
 
      @Override
      public void focusLost(FocusEvent e)
      {
      }
    };
 
    for (JTextComponent tf : hmFields.values())
    {
      tf.addFocusListener(l);
    }
    lastFocusComponent = getField(FieldName.GLOBAL_ADMINISTRATOR_PWD);
  }
}