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.io.IOException; 024import java.util.ArrayList; 025 026import javax.swing.JLabel; 027import javax.swing.JTextField; 028import javax.swing.event.DocumentEvent; 029import javax.swing.event.DocumentListener; 030 031import org.forgerock.i18n.LocalizableMessage; 032import org.opends.guitools.controlpanel.browser.BrowserController; 033import org.opends.guitools.controlpanel.ui.nodes.BasicNode; 034import org.opends.guitools.controlpanel.util.Utilities; 035import org.opends.server.types.OpenDsException; 036 037/** The panel used to create a new organizational unit. */ 038public class NewOrganizationalUnitPanel extends AbstractNewEntryPanel 039{ 040 private static final long serialVersionUID = -7145648120019856161L; 041 private JLabel lName = Utilities.createPrimaryLabel( 042 INFO_CTRL_PANEL_NEW_OU_NAME_LABEL.get()); 043 private JLabel lDescription = Utilities.createPrimaryLabel( 044 INFO_CTRL_PANEL_NEW_OU_DESCRIPTION_LABEL.get()); 045 private JLabel lAddress = Utilities.createPrimaryLabel( 046 INFO_CTRL_PANEL_NEW_OU_ADDRESS_LABEL.get()); 047 private JLabel lTelephoneNumber = Utilities.createPrimaryLabel( 048 INFO_CTRL_PANEL_NEW_OU_TELEPHONE_NUMBER_LABEL.get()); 049 private JLabel lFaxNumber = Utilities.createPrimaryLabel( 050 INFO_CTRL_PANEL_NEW_OU_FAX_NUMBER_LABEL.get()); 051 private JLabel lEntryDN = Utilities.createPrimaryLabel( 052 INFO_CTRL_PANEL_NEW_OU_ENTRY_DN_LABEL.get()); 053 054 private JLabel[] labels = {lName, lDescription, lAddress, 055 lTelephoneNumber, lFaxNumber, lEntryDN 056 }; 057 058 private JTextField name = Utilities.createLongTextField(); 059 private JTextField description = Utilities.createLongTextField(); 060 private JTextField address = Utilities.createLongTextField(); 061 private JTextField telephoneNumber = Utilities.createLongTextField(); 062 private JTextField faxNumber = Utilities.createLongTextField(); 063 private JLabel dn = Utilities.createDefaultLabel(); 064 065 private Component[] comps = {name, description, address, 066 telephoneNumber, faxNumber, dn}; 067 068 /** Default constructor. */ 069 public NewOrganizationalUnitPanel() 070 { 071 super(); 072 createLayout(); 073 } 074 075 @Override 076 public void setParent(BasicNode parentNode, BrowserController controller) 077 { 078 super.setParent(parentNode, controller); 079 dn.setText(","+parentNode.getDN()); 080 for (Component comp : comps) 081 { 082 if (comp instanceof JTextField) 083 { 084 ((JTextField)comp).setText(""); 085 } 086 } 087 } 088 089 @Override 090 public LocalizableMessage getTitle() 091 { 092 return INFO_CTRL_PANEL_NEW_OU_PANEL_TITLE.get(); 093 } 094 095 @Override 096 public Component getPreferredFocusComponent() 097 { 098 return name; 099 } 100 101 @Override 102 protected LocalizableMessage getProgressDialogTitle() 103 { 104 return INFO_CTRL_PANEL_NEW_OU_PANEL_TITLE.get(); 105 } 106 107 @Override 108 protected void checkSyntax(ArrayList<LocalizableMessage> errors) 109 { 110 for (JLabel label : labels) 111 { 112 setPrimaryValid(label); 113 } 114 115 JTextField[] requiredFields = {name}; 116 LocalizableMessage[] msgs = {ERR_CTRL_PANEL_NAME_OF_OU_REQUIRED.get()}; 117 for (int i=0; i<requiredFields.length; i++) 118 { 119 String v = requiredFields[i].getText().trim(); 120 if (v.length() == 0) 121 { 122 errors.add(msgs[i]); 123 } 124 } 125 126 if (errors.isEmpty()) 127 { 128 try 129 { 130 getEntry(); 131 } 132 catch (OpenDsException ode) 133 { 134 errors.add(ode.getMessageObject()); 135 } 136 catch (IOException ioe) 137 { 138 // This should not occur 139 throw new RuntimeException("Unexpected error: "+ioe, ioe); 140 } 141 } 142 } 143 144 /** Creates the layout of the panel (but the contents are not populated here). */ 145 private void createLayout() 146 { 147 GridBagConstraints gbc = new GridBagConstraints(); 148 Utilities.setRequiredIcon(lName); 149 150 gbc.gridwidth = 2; 151 gbc.gridy = 0; 152 addErrorPane(gbc); 153 154 gbc.gridy ++; 155 gbc.gridwidth = 1; 156 gbc.weighty = 0.0; 157 gbc.gridx = 1; 158 gbc.anchor = GridBagConstraints.EAST; 159 gbc.fill = GridBagConstraints.NONE; 160 JLabel requiredLabel = createRequiredLabel(); 161 gbc.insets.bottom = 10; 162 add(requiredLabel, gbc); 163 164 gbc.gridy ++; 165 gbc.fill = GridBagConstraints.HORIZONTAL; 166 gbc.anchor = GridBagConstraints.WEST; 167 gbc.insets.bottom = 0; 168 169 Component[] inlineHelp = {null, null, null, null, 170 null, null}; 171 172 for (int i=0 ; i< labels.length; i++) 173 { 174 gbc.insets.left = 0; 175 gbc.weightx = 0.0; 176 gbc.gridx = 0; 177 add(labels[i], gbc); 178 gbc.insets.left = 10; 179 gbc.weightx = 1.0; 180 gbc.gridx = 1; 181 add(comps[i], gbc); 182 if (inlineHelp[i] != null) 183 { 184 gbc.insets.top = 3; 185 gbc.gridy ++; 186 add(inlineHelp[i], gbc); 187 } 188 gbc.insets.top = 10; 189 gbc.gridy ++; 190 } 191 addBottomGlue(gbc); 192 193 DocumentListener listener = new DocumentListener() 194 { 195 @Override 196 public void insertUpdate(DocumentEvent ev) 197 { 198 updateDNValue(); 199 } 200 201 @Override 202 public void changedUpdate(DocumentEvent ev) 203 { 204 insertUpdate(ev); 205 } 206 207 @Override 208 public void removeUpdate(DocumentEvent ev) 209 { 210 insertUpdate(ev); 211 } 212 }; 213 JTextField[] toAddListener = {name}; 214 for (JTextField tf : toAddListener) 215 { 216 tf.getDocument().addDocumentListener(listener); 217 } 218 } 219 220 /** Updates the contents of DN value to reflect the data that the user is providing. */ 221 private void updateDNValue() 222 { 223 String value = name.getText().trim(); 224 if (value.length() > 0) 225 { 226 dn.setText("ou" + "=" + value + "," + parentNode.getDN()); 227 } 228 else 229 { 230 dn.setText(","+parentNode.getDN()); 231 } 232 } 233 234 @Override 235 protected String getLDIF() 236 { 237 StringBuilder sb = new StringBuilder(); 238 sb.append("dn: ").append(dn.getText()).append("\n"); 239 String[] attrNames = {"ou", "description", "postalAddress", 240 "telephoneNumber", "facsimileTelephoneNumber"}; 241 JTextField[] textFields = {name, description, address, 242 telephoneNumber, faxNumber}; 243 sb.append("objectclass: top\n"); 244 sb.append("objectclass: organizationalUnit\n"); 245 for (int i=0; i<attrNames.length; i++) 246 { 247 String value = textFields[i].getText().trim(); 248 if (value.length() > 0) 249 { 250 sb.append(attrNames[i]).append(": ").append(value).append("\n"); 251 } 252 } 253 return sb.toString(); 254 } 255}