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 2014-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui;
018
019import static org.opends.messages.AdminToolMessages.*;
020
021import java.util.ArrayList;
022
023import javax.swing.JLabel;
024import javax.swing.JTextField;
025
026import org.forgerock.i18n.LocalizableMessage;
027
028/** The panel to create a domain. */
029public class NewDomainPanel extends NewOrganizationPanel
030{
031  private static final long serialVersionUID = -595396547491445219L;
032
033  @Override
034  public LocalizableMessage getTitle()
035  {
036    return INFO_CTRL_NEW_DOMAIN_PANEL_TITLE.get();
037  }
038
039  @Override
040  protected LocalizableMessage getProgressDialogTitle()
041  {
042    return INFO_CTRL_NEW_DOMAIN_PANEL_TITLE.get();
043  }
044
045  @Override
046  protected void checkSyntax(ArrayList<LocalizableMessage> errors)
047  {
048    for (JLabel label : labels)
049    {
050      setPrimaryValid(label);
051    }
052
053    JTextField[] requiredFields = {name};
054    LocalizableMessage[] msgs = {ERR_CTRL_PANEL_NAME_OF_DOMAIN_REQUIRED.get()};
055    for (int i=0; i<requiredFields.length; i++)
056    {
057      String v = requiredFields[i].getText().trim();
058      if (v.length() == 0)
059      {
060        errors.add(msgs[i]);
061      }
062    }
063  }
064
065  @Override
066  protected void updateDNValue()
067  {
068    String value = name.getText().trim();
069    if (value.length() > 0)
070    {
071      dn.setText("dc" + "=" + value + "," + parentNode.getDN());
072    }
073    else
074    {
075      dn.setText(","+parentNode.getDN());
076    }
077  }
078
079  @Override
080  protected String getLDIF()
081  {
082    StringBuilder sb = new StringBuilder();
083    sb.append("dn: ").append(dn.getText()).append("\n");
084    String[] attrNames = {"dc", "description"};
085    JTextField[] textFields = {name, description};
086    sb.append("objectclass: top\n");
087    sb.append("objectclass: domain\n");
088    for (int i=0; i<attrNames.length; i++)
089    {
090      String value = textFields[i].getText().trim();
091      if (value.length() > 0)
092      {
093        sb.append(attrNames[i]).append(": ").append(value).append("\n");
094      }
095    }
096    return sb.toString();
097  }
098}