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 2014-2015 ForgeRock AS
026 */
027
028/*
029 * CDDL HEADER START
030 *
031 * The contents of this file are subject to the terms of the
032 * Common Development and Distribution License, Version 1.0 only
033 * (the "License").  You may not use this file except in compliance
034 * with the License.
035 *
036 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
037 * or http://forgerock.org/license/CDDLv1.0.html.
038 * See the License for the specific language governing permissions
039 * and limitations under the License.
040 *
041 * When distributing Covered Code, include this CDDL HEADER in each
042 * file and include the License file at legal-notices/CDDLv1_0.txt.
043 * If applicable, add the following below this CDDL HEADER, with the
044 * fields enclosed by brackets "[]" replaced with your own identifying
045 * information:
046 *      Portions Copyright [yyyy] [name of copyright owner]
047 *
048 * CDDL HEADER END
049 *
050 *
051 *      Copyright 2008 Sun Microsystems, Inc.
052 *      Portions Copyright 2014-2015 ForgeRock AS
053 */
054
055package org.opends.guitools.controlpanel.ui;
056
057import static org.opends.messages.AdminToolMessages.*;
058
059import java.awt.Component;
060import java.awt.GridBagConstraints;
061import java.util.ArrayList;
062import javax.swing.JLabel;
063import javax.swing.JTextField;
064import javax.swing.event.DocumentEvent;
065import javax.swing.event.DocumentListener;
066
067import org.opends.guitools.controlpanel.browser.BrowserController;
068import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
069import org.opends.guitools.controlpanel.util.Utilities;
070import org.forgerock.i18n.LocalizableMessage;
071
072/**
073 * The panel used to create a new organization.
074 *
075 */
076public class NewOrganizationPanel extends AbstractNewEntryPanel
077{
078  private static final long serialVersionUID = 6560126551083160773L;
079  /**
080   * The label for the name.
081   */
082  protected final JLabel lName = Utilities.createPrimaryLabel();
083  /**
084   * The label for the description.
085   */
086  protected final JLabel lDescription = Utilities.createPrimaryLabel();
087  /**
088   * The label for the DN.
089   */
090  protected final JLabel lDn = Utilities.createPrimaryLabel();
091
092  /**
093   * An array containing all the labels.
094   */
095  protected final JLabel[] labels = {lName, lDescription, lDn};
096
097  /**
098   * The field containing the name.
099   */
100  protected final JTextField name = Utilities.createLongTextField();
101  /**
102   * The field containing the description.
103   */
104  protected final JTextField description = Utilities.createLongTextField();
105  /**
106   * The label containing the DN value.
107   */
108  protected final JLabel dn = Utilities.createDefaultLabel();
109
110  /**
111   * An array containing all the components.
112   */
113  protected final Component[] comps = {name, description, dn};
114
115  /**
116   * Default constructor.
117   *
118   */
119  public NewOrganizationPanel()
120  {
121    super();
122    createLayout();
123  }
124
125  /** {@inheritDoc} */
126  public void setParent(BasicNode parentNode, BrowserController controller)
127  {
128    super.setParent(parentNode, controller);
129    dn.setText(","+parentNode.getDN());
130    for (Component comp : comps)
131    {
132      if (comp instanceof JTextField)
133      {
134        ((JTextField)comp).setText("");
135      }
136    }
137  }
138
139  /** {@inheritDoc} */
140  public LocalizableMessage getTitle()
141  {
142    return INFO_CTRL_NEW_ORGANIZATION_PANEL_TITLE.get();
143  }
144
145  /** {@inheritDoc} */
146  public Component getPreferredFocusComponent()
147  {
148    return name;
149  }
150
151  /**
152   * Returns the title of the progress dialog.
153   * @return the title of the progress dialog.
154   */
155  protected LocalizableMessage getProgressDialogTitle()
156  {
157    return INFO_CTRL_NEW_ORGANIZATION_PANEL_TITLE.get();
158  }
159
160  /** {@inheritDoc} */
161  protected void checkSyntax(ArrayList<LocalizableMessage> errors)
162  {
163    for (JLabel label : labels)
164    {
165      setPrimaryValid(label);
166    }
167
168    JTextField[] requiredFields = {name};
169    LocalizableMessage[] msgs = {ERR_CTRL_PANEL_NAME_OF_ORGANIZATION_REQUIRED.get()};
170    for (int i=0; i<requiredFields.length; i++)
171    {
172      String v = requiredFields[i].getText().trim();
173      if (v.length() == 0)
174      {
175        errors.add(msgs[i]);
176      }
177    }
178  }
179
180
181  /**
182   * Creates the layout of the panel (but the contents are not populated here).
183   */
184  private void createLayout()
185  {
186    GridBagConstraints gbc = new GridBagConstraints();
187    LocalizableMessage[] ls = {
188        INFO_CTRL_PANEL_NEW_ORGANIZATION_NAME_LABEL.get(),
189        INFO_CTRL_PANEL_NEW_ORGANIZATION_DESCRIPTION_LABEL.get(),
190        INFO_CTRL_PANEL_NEW_ORGANIZATION_ENTRY_DN_LABEL.get()};
191    int i = 0;
192    for (LocalizableMessage l : ls)
193    {
194      labels[i].setText(l.toString());
195      i++;
196    }
197    Utilities.setRequiredIcon(lName);
198
199    gbc.gridwidth = 2;
200    gbc.gridy = 0;
201    addErrorPane(gbc);
202
203    gbc.gridy ++;
204    gbc.gridwidth = 1;
205    gbc.weighty = 0.0;
206    gbc.gridx = 1;
207    gbc.anchor = GridBagConstraints.EAST;
208    gbc.fill = GridBagConstraints.NONE;
209    JLabel requiredLabel = createRequiredLabel();
210    gbc.insets.bottom = 10;
211    add(requiredLabel, gbc);
212
213    gbc.gridy ++;
214    gbc.fill = GridBagConstraints.HORIZONTAL;
215    gbc.anchor = GridBagConstraints.WEST;
216    gbc.insets.bottom = 0;
217
218    Component[] inlineHelp = {null, null, null};
219
220    for (i=0 ; i< labels.length; i++)
221    {
222      gbc.insets.left = 0;
223      gbc.weightx = 0.0;
224      gbc.gridx = 0;
225      add(labels[i], gbc);
226      gbc.insets.left = 10;
227      gbc.weightx = 1.0;
228      gbc.gridx = 1;
229      add(comps[i], gbc);
230      if (inlineHelp[i] != null)
231      {
232        gbc.insets.top = 3;
233        gbc.gridy ++;
234        add(inlineHelp[i], gbc);
235      }
236      gbc.insets.top = 10;
237      gbc.gridy ++;
238    }
239    addBottomGlue(gbc);
240
241    DocumentListener listener = new DocumentListener()
242    {
243      /** {@inheritDoc} */
244      public void insertUpdate(DocumentEvent ev)
245      {
246        updateDNValue();
247      }
248
249      /** {@inheritDoc} */
250      public void changedUpdate(DocumentEvent ev)
251      {
252        insertUpdate(ev);
253      }
254
255      /** {@inheritDoc} */
256      public void removeUpdate(DocumentEvent ev)
257      {
258        insertUpdate(ev);
259      }
260    };
261    JTextField[] toAddListener = {name};
262    for (JTextField tf : toAddListener)
263    {
264      tf.getDocument().addDocumentListener(listener);
265    }
266  }
267
268  /**
269   * Updates the contents of DN value to reflect the data that the user
270   * is providing.
271   *
272   */
273  protected void updateDNValue()
274  {
275    String value = name.getText().trim();
276    if (value.length() > 0)
277    {
278       String rdn = Utilities.getRDNString("o", value);
279          dn.setText(rdn+","+parentNode.getDN());
280    }
281    else
282    {
283      dn.setText(","+parentNode.getDN());
284    }
285  }
286
287  /**
288   * Returns the LDIF representing the new entry.
289   * @return the LDIF representing the new entry.
290   */
291  protected String getLDIF()
292  {
293    StringBuilder sb = new StringBuilder();
294    sb.append("dn: ").append(dn.getText()).append("\n");
295    String[] attrNames = {"o", "description"};
296    JTextField[] textFields = {name, description};
297    sb.append("objectclass: top\n");
298    sb.append("objectclass: organization\n");
299    for (int i=0; i<attrNames.length; i++)
300    {
301      String value = textFields[i].getText().trim();
302      if (value.length() > 0)
303      {
304        sb.append(attrNames[i]).append(": ").append(value).append("\n");
305      }
306    }
307    return sb.toString();
308  }
309}
310