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