1 20 21 package org.apache.directory.ldapstudio.browser.ui.wizards; 22 23 24 import java.util.Arrays ; 25 26 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter; 27 import org.apache.directory.ldapstudio.browser.common.widgets.DnBuilderWidget; 28 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent; 29 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener; 30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 31 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 32 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry; 33 import org.apache.directory.ldapstudio.browser.core.internal.model.Value; 34 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob; 35 import org.apache.directory.ldapstudio.browser.core.model.DN; 36 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 37 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 38 import org.apache.directory.ldapstudio.browser.core.model.IValue; 39 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 40 import org.apache.directory.ldapstudio.browser.core.model.RDN; 41 import org.apache.directory.ldapstudio.browser.core.model.RDNPart; 42 import org.apache.directory.ldapstudio.browser.core.model.schema.Subschema; 43 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 44 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 45 import org.eclipse.jface.dialogs.MessageDialog; 46 import org.eclipse.jface.wizard.IWizardPage; 47 import org.eclipse.jface.wizard.WizardPage; 48 import org.eclipse.swt.widgets.Composite; 49 50 51 58 public class NewEntryDnWizardPage extends WizardPage implements WidgetModifyListener 59 { 60 61 62 private NewEntryWizard wizard; 63 64 65 private DnBuilderWidget dnBuilderWidget; 66 67 68 74 public NewEntryDnWizardPage( String pageName, NewEntryWizard wizard ) 75 { 76 super( pageName ); 77 setTitle( "Distinguished Name" ); 78 setDescription( "Please select the parent of the new entry and enter the RDN." ); 79 setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) ); 80 setPageComplete( false ); 81 82 this.wizard = wizard; 83 } 84 85 86 89 public void dispose() 90 { 91 if ( dnBuilderWidget != null ) 92 { 93 dnBuilderWidget.removeWidgetModifyListener( this ); 94 dnBuilderWidget.dispose(); 95 dnBuilderWidget = null; 96 } 97 super.dispose(); 98 } 99 100 101 104 private void validate() 105 { 106 if ( dnBuilderWidget.getRdn() != null && dnBuilderWidget.getParentDn() != null ) 107 { 108 setPageComplete( true ); 109 saveState(); 110 } 111 else 112 { 113 setPageComplete( false ); 114 } 115 } 116 117 118 122 private void loadState() 123 { 124 DummyEntry newEntry = wizard.getPrototypeEntry(); 125 126 Subschema subschema = newEntry.getSubschema(); 127 String [] attributeNames = subschema.getAllAttributeNames(); 128 129 DN parentDn = null; 130 if ( newEntry.getDn().getParentDn() != null ) 131 { 132 parentDn = newEntry.getDn().getParentDn(); 133 } 134 else if ( wizard.getSelectedEntry() != null ) 135 { 136 parentDn = wizard.getSelectedEntry().getDn(); 137 } 138 139 RDN rdn = newEntry.getRdn(); 140 141 dnBuilderWidget.setInput( wizard.getSelectedConnection(), attributeNames, rdn, parentDn ); 142 } 143 144 145 148 private void saveState() 149 { 150 DummyEntry newEntry = wizard.getPrototypeEntry(); 151 152 try 153 { 154 EventRegistry.suspendEventFireingInCurrentThread(); 155 156 RDNPart[] oldRdnParts = newEntry.getRdn().getParts(); 158 for ( int i = 0; i < oldRdnParts.length; i++ ) 159 { 160 IAttribute attribute = newEntry.getAttribute( oldRdnParts[i].getType() ); 161 if ( attribute != null ) 162 { 163 IValue[] values = attribute.getValues(); 164 for ( int v = 0; v < values.length; v++ ) 165 { 166 if ( values[v].getStringValue().equals( oldRdnParts[i].getUnencodedValue() ) ) 167 { 168 attribute.deleteValue( values[v] ); 169 } 170 } 171 } 172 } 173 174 DN dn = new DN( dnBuilderWidget.getRdn(), dnBuilderWidget.getParentDn() ); 176 newEntry.setDn( dn ); 177 178 RDNPart[] newRdnParts = dn.getRdn().getParts(); 180 for ( int i = 0; i < newRdnParts.length; i++ ) 181 { 182 IAttribute rdnAttribute = newEntry.getAttribute( newRdnParts[i].getType() ); 183 if ( rdnAttribute == null ) 184 { 185 rdnAttribute = new Attribute( newEntry, newRdnParts[i].getType() ); 186 newEntry.addAttribute( rdnAttribute ); 187 } 188 String rdnValue = newRdnParts[i].getUnencodedValue(); 189 String [] stringValues = rdnAttribute.getStringValues(); 190 if ( !Arrays.asList( stringValues ).contains( rdnValue ) ) 191 { 192 rdnAttribute.addValue( new Value( rdnAttribute, rdnValue ) ); 193 } 194 } 195 196 } 197 catch ( ModelModificationException e ) 198 { 199 e.printStackTrace(); 200 } 201 finally 202 { 203 EventRegistry.resumeEventFireingInCurrentThread(); 204 } 205 } 206 207 208 214 public void setVisible( boolean visible ) 215 { 216 super.setVisible( visible ); 217 218 if ( visible ) 219 { 220 loadState(); 221 validate(); 222 } 223 } 224 225 226 233 public boolean canFlipToNextPage() 234 { 235 return isPageComplete(); 236 } 237 238 239 245 public IWizardPage getNextPage() 246 { 247 248 dnBuilderWidget.validate(); 249 final RDN[] rdns = new RDN[] 250 { dnBuilderWidget.getRdn() }; 251 final DN[] parentDns = new DN[] 252 { dnBuilderWidget.getParentDn() }; 253 final DN dn = new DN( rdns[0], parentDns[0] ); 254 255 ReadEntryJob readEntryJob1 = new ReadEntryJob( wizard.getSelectedConnection(), parentDns[0] ); 257 RunnableContextJobAdapter.execute( readEntryJob1, getContainer(), false ); 258 IEntry parentEntry = readEntryJob1.getReadEntry(); 259 if ( parentEntry == null ) 260 { 261 getShell().getDisplay().syncExec( new Runnable () 262 { 263 public void run() 264 { 265 MessageDialog.openError( getShell(), "Error", "Parent " + dnBuilderWidget.getParentDn().toString() 266 + " doesn't exists" ); 267 } 268 } ); 269 return null; 270 } 271 ReadEntryJob readEntryJob2 = new ReadEntryJob( wizard.getSelectedConnection(), dn ); 272 RunnableContextJobAdapter.execute( readEntryJob2, getContainer(), false ); 273 IEntry entry = readEntryJob2.getReadEntry(); 274 if ( entry != null ) 275 { 276 getShell().getDisplay().syncExec( new Runnable () 277 { 278 public void run() 279 { 280 MessageDialog.openError( getShell(), "Error", "Entry " + dn.toString() + " already exists" ); 281 } 282 } ); 283 return null; 284 } 285 286 return super.getNextPage(); 287 } 288 289 290 293 public void createControl( Composite parent ) 294 { 295 dnBuilderWidget = new DnBuilderWidget( true, true ); 296 dnBuilderWidget.addWidgetModifyListener( this ); 297 Composite composite = dnBuilderWidget.createContents( parent ); 298 299 setControl( composite ); 300 } 301 302 303 306 public void widgetModified( WidgetModifyEvent event ) 307 { 308 validate(); 309 } 310 311 312 315 public void saveDialogSettings() 316 { 317 dnBuilderWidget.saveDialogSettings(); 318 } 319 320 } | Popular Tags |