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  /** Default constructor. */
043  public BaseDNPanel()
044  {
045    super();
046    createLayout();
047  }
048
049  @Override
050  public LocalizableMessage getTitle()
051  {
052    return INFO_CTRL_PANEL_OTHER_BASE_DN_TITLE.get();
053  }
054
055  /**
056   * Returns the base DN chosen by the user.
057   * @return the base DN chosen by the user.
058   */
059  public String getBaseDn()
060  {
061    return baseDn;
062  }
063
064  /** Creates the layout of the panel (but the contents are not populated here). */
065  private void createLayout()
066  {
067    GridBagConstraints gbc = new GridBagConstraints();
068    gbc.anchor = GridBagConstraints.WEST;
069    gbc.gridx = 0;
070    gbc.gridy = 0;
071
072    gbc.weightx = 0.0;
073    gbc.gridwidth = 1;
074    gbc.fill = GridBagConstraints.NONE;
075    dnLabel = Utilities.createPrimaryLabel(INFO_CTRL_PANEL_BASE_DN_LABEL.get());
076    add(dnLabel, gbc);
077    gbc.insets.left = 10;
078    gbc.gridx = 1;
079    dn = Utilities.createLongTextField();
080    gbc.weightx = 1.0;
081    gbc.fill = GridBagConstraints.HORIZONTAL;
082    add(dn, gbc);
083
084    addBottomGlue(gbc);
085  }
086
087  @Override
088  public Component getPreferredFocusComponent()
089  {
090    return dn;
091  }
092
093  @Override
094  public void configurationChanged(ConfigurationChangeEvent ev)
095  {
096  }
097
098  @Override
099  public void okClicked()
100  {
101    setPrimaryValid(dnLabel);
102    LinkedHashSet<LocalizableMessage> errors = new LinkedHashSet<>();
103
104    if ("".equals(dn.getText().trim()))
105    {
106      errors.add(ERR_CTRL_PANEL_NO_BASE_DN_PROVIDED.get());
107    }
108    else
109    {
110      try
111      {
112        DN.valueOf(dn.getText());
113      }
114      catch (LocalizedIllegalArgumentException e)
115      {
116        errors.add(ERR_CTRL_PANEL_INVALID_BASE_DN_PROVIDED.get(e.getMessageObject()));
117      }
118    }
119
120    if (errors.isEmpty())
121    {
122      baseDn = dn.getText().trim();
123      Utilities.getParentDialog(this).setVisible(false);
124    }
125    else
126    {
127      setPrimaryInvalid(dnLabel);
128      displayErrorDialog(errors);
129      dn.setSelectionStart(0);
130      dn.setSelectionEnd(dn.getText().length());
131      dn.requestFocusInWindow();
132    }
133  }
134
135  @Override
136  public void cancelClicked()
137  {
138    setPrimaryValid(dnLabel);
139    baseDn = null;
140    super.cancelClicked();
141  }
142
143  @Override
144  public void toBeDisplayed(boolean visible)
145  {
146    super.toBeDisplayed(visible);
147    if (visible)
148    {
149      baseDn = null;
150    }
151  }
152}
153