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