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