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 */
017
018package org.opends.guitools.controlpanel.ui;
019
020import static org.opends.messages.AdminToolMessages.*;
021
022import java.awt.Component;
023import java.awt.GridBagConstraints;
024import java.util.LinkedHashSet;
025import javax.swing.JLabel;
026import javax.swing.JTextField;
027
028import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
029import org.opends.guitools.controlpanel.util.Utilities;
030import org.forgerock.i18n.LocalizableMessage;
031import org.forgerock.i18n.LocalizedIllegalArgumentException;
032import org.forgerock.opendj.ldap.DN;
033
034/** A simple dialog where the user can provide a base DN. */
035public class BaseDNPanel extends StatusGenericPanel
036{
037  private static final long serialVersionUID = 2742173517231794830L;
038  private JTextField dn;
039  private JLabel dnLabel;
040  private String baseDn;
041
042  /**
043   * Default constructor.
044   *
045   */
046  public BaseDNPanel()
047  {
048    super();
049    createLayout();
050  }
051
052  /** {@inheritDoc} */
053  public LocalizableMessage getTitle()
054  {
055    return INFO_CTRL_PANEL_OTHER_BASE_DN_TITLE.get();
056  }
057
058  /**
059   * Returns the base DN chosen by the user.
060   * @return the base DN chosen by the user.
061   */
062  public String getBaseDn()
063  {
064    return baseDn;
065  }
066
067  /**
068   * Creates the layout of the panel (but the contents are not populated here).
069   */
070  private void createLayout()
071  {
072    GridBagConstraints gbc = new GridBagConstraints();
073    gbc.anchor = GridBagConstraints.WEST;
074    gbc.gridx = 0;
075    gbc.gridy = 0;
076
077    gbc.weightx = 0.0;
078    gbc.gridwidth = 1;
079    gbc.fill = GridBagConstraints.NONE;
080    dnLabel = Utilities.createPrimaryLabel(INFO_CTRL_PANEL_BASE_DN_LABEL.get());
081    add(dnLabel, gbc);
082    gbc.insets.left = 10;
083    gbc.gridx = 1;
084    dn = Utilities.createLongTextField();
085    gbc.weightx = 1.0;
086    gbc.fill = GridBagConstraints.HORIZONTAL;
087    add(dn, gbc);
088
089    addBottomGlue(gbc);
090  }
091
092  /** {@inheritDoc} */
093  public Component getPreferredFocusComponent()
094  {
095    return dn;
096  }
097
098  /** {@inheritDoc} */
099  public void configurationChanged(ConfigurationChangeEvent ev)
100  {
101  }
102
103  /** {@inheritDoc} */
104  public void okClicked()
105  {
106    setPrimaryValid(dnLabel);
107    LinkedHashSet<LocalizableMessage> errors = new LinkedHashSet<>();
108
109    if ("".equals(dn.getText().trim()))
110    {
111      errors.add(ERR_CTRL_PANEL_NO_BASE_DN_PROVIDED.get());
112    }
113    else
114    {
115      try
116      {
117        DN.valueOf(dn.getText());
118      }
119      catch (LocalizedIllegalArgumentException e)
120      {
121        errors.add(ERR_CTRL_PANEL_INVALID_BASE_DN_PROVIDED.get(e.getMessageObject()));
122      }
123    }
124
125    if (errors.isEmpty())
126    {
127      baseDn = dn.getText().trim();
128      Utilities.getParentDialog(this).setVisible(false);
129    }
130    else
131    {
132      setPrimaryInvalid(dnLabel);
133      displayErrorDialog(errors);
134      dn.setSelectionStart(0);
135      dn.setSelectionEnd(dn.getText().length());
136      dn.requestFocusInWindow();
137    }
138  }
139
140  /** {@inheritDoc} */
141  public void cancelClicked()
142  {
143    setPrimaryValid(dnLabel);
144    baseDn = null;
145    super.cancelClicked();
146  }
147
148  /** {@inheritDoc} */
149  public void toBeDisplayed(boolean visible)
150  {
151    super.toBeDisplayed(visible);
152    if (visible)
153    {
154      baseDn = null;
155    }
156  }
157}
158