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-2009 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.awt.event.ActionEvent;
035import java.awt.event.ActionListener;
036import java.io.IOException;
037import java.util.ArrayList;
038
039import javax.swing.Box;
040import javax.swing.JButton;
041import javax.swing.JLabel;
042import javax.swing.JScrollPane;
043import javax.swing.JTextArea;
044import javax.swing.event.DocumentEvent;
045import javax.swing.event.DocumentListener;
046
047import org.opends.guitools.controlpanel.browser.BrowserController;
048import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
049import org.opends.guitools.controlpanel.util.Utilities;
050import org.forgerock.i18n.LocalizableMessage;
051import org.opends.server.util.LDIFException;
052
053/**
054 * The panel used to create a new entry using an LDIF representation.
055 *
056 */
057public class NewEntryFromLDIFPanel extends AbstractNewEntryPanel
058{
059  private static final long serialVersionUID = -3923907357481784964L;
060  private JTextArea ldif;
061  private JButton checkSyntax;
062  private JLabel lSyntaxCorrect;
063
064  /**
065   * Default constructor.
066   *
067   */
068  public NewEntryFromLDIFPanel()
069  {
070    super();
071    createLayout();
072  }
073
074  /** {@inheritDoc} */
075  public Component getPreferredFocusComponent()
076  {
077    return ldif;
078  }
079
080  /** {@inheritDoc} */
081  public boolean requiresScroll()
082  {
083    return false;
084  }
085
086  /** {@inheritDoc} */
087  public void setParent(BasicNode parentNode, BrowserController controller)
088  {
089    super.setParent(parentNode, controller);
090    StringBuilder sb = new StringBuilder();
091    final String emptyDn = "dn: ";
092    sb.append(emptyDn);
093    if (parentNode != null)
094    {
095      sb.append(",").append(parentNode.getDN());
096    }
097    sb.append("\nobjectClass: top");
098    ldif.setText(sb.toString());
099    ldif.setCaretPosition(emptyDn.length());
100  }
101
102  /** {@inheritDoc} */
103  protected LocalizableMessage getProgressDialogTitle()
104  {
105    return INFO_CTRL_PANEL_NEW_ENTRY_FROM_LDIF_TITLE.get();
106  }
107
108  /** {@inheritDoc} */
109  public LocalizableMessage getTitle()
110  {
111    return INFO_CTRL_PANEL_NEW_ENTRY_FROM_LDIF_TITLE.get();
112  }
113
114  /**
115   * Creates the layout of the panel (but the contents are not populated here).
116   */
117  private void createLayout()
118  {
119    GridBagConstraints gbc = new GridBagConstraints();
120    gbc.gridx = 0;
121    gbc.gridy = 0;
122
123    gbc.gridy = 0;
124    addErrorPane(gbc);
125
126    gbc.gridy ++;
127
128    gbc.weightx = 0.0;
129    gbc.weighty = 0.0;
130    gbc.fill = GridBagConstraints.HORIZONTAL;
131
132    gbc.gridx = 0;
133    gbc.insets.left = 0;
134    gbc.weightx = 1.0;
135    gbc.gridwidth = 3;
136    JLabel label = Utilities.createDefaultLabel(
137        INFO_CTRL_PANEL_LDIF_SYNTAX_LABEL.get());
138    add(label, gbc);
139
140    lSyntaxCorrect = Utilities.createDefaultLabel(
141        INFO_CTRL_PANEL_SYNTAX_CORRECT_LABEL.get());
142    lSyntaxCorrect.setIcon(Utilities.createImageIcon(
143        "org/opends/quicksetup/images/info_small.gif"));
144
145    ldif = Utilities.createTextArea(LocalizableMessage.EMPTY, 20, 50);
146    ldif.getDocument().addDocumentListener(new DocumentListener()
147    {
148      /** {@inheritDoc} */
149      public void removeUpdate(DocumentEvent ev)
150      {
151        lSyntaxCorrect.setVisible(false);
152      }
153
154      /** {@inheritDoc} */
155      public void changedUpdate(DocumentEvent ev)
156      {
157        removeUpdate(ev);
158      }
159
160      /** {@inheritDoc} */
161      public void insertUpdate(DocumentEvent ev)
162      {
163       removeUpdate(ev);
164      }
165    });
166    gbc.weightx = 1.0;
167    gbc.weighty = 1.0;
168    JScrollPane scroll = Utilities.createScrollPane(ldif);
169    gbc.gridy ++;
170    gbc.insets.top = 5;
171    gbc.fill = GridBagConstraints.BOTH;
172    add(scroll, gbc);
173
174    gbc.weighty = 0.0;
175    gbc.weightx = 0.0;
176    checkSyntax = Utilities.createButton(
177        INFO_CTRL_PANEL_CHECK_SYNTAX_BUTTON.get());
178    checkSyntax.setOpaque(false);
179    checkSyntax.addActionListener(new ActionListener()
180    {
181      /** {@inheritDoc} */
182      public void actionPerformed(ActionEvent ev)
183      {
184        ArrayList<LocalizableMessage> errors = new ArrayList<>();
185        checkSyntax(errors);
186        if (!errors.isEmpty())
187        {
188          displayErrorDialog(errors);
189        }
190        else
191        {
192          lSyntaxCorrect.setVisible(true);
193        }
194      }
195    });
196    gbc.gridy ++;
197    gbc.gridwidth = 1;
198    gbc.fill = GridBagConstraints.NONE;
199    gbc.weightx = 0.0;
200    gbc.anchor = GridBagConstraints.WEST;
201    gbc.gridx = 0;
202    add(lSyntaxCorrect, gbc);
203    gbc.weightx = 1.0;
204    gbc.fill = GridBagConstraints.HORIZONTAL;
205    gbc.gridx = 1;
206    add(Box.createHorizontalGlue(), gbc);
207    gbc.fill = GridBagConstraints.NONE;
208    gbc.weightx = 0.0;
209    gbc.anchor = GridBagConstraints.EAST;
210    gbc.gridx = 2;
211    add(checkSyntax, gbc);
212  }
213
214  /** {@inheritDoc} */
215  public void toBeDisplayed(boolean visible)
216  {
217    lSyntaxCorrect.setVisible(false);
218  }
219
220  /** {@inheritDoc} */
221  protected void checkSyntax(ArrayList<LocalizableMessage> errors)
222  {
223    try
224    {
225      getEntry();
226    }
227    catch (IOException ioe)
228    {
229      errors.add(ERR_CTRL_PANEL_ERROR_CHECKING_ENTRY.get(ioe));
230    }
231    catch (LDIFException le)
232    {
233      errors.add(le.getMessageObject());
234    }
235  }
236
237  /** {@inheritDoc} */
238  protected String getLDIF()
239  {
240    return ldif.getText();
241  }
242}