1 20 21 package org.apache.directory.ldapstudio.browser.ui.wizards; 22 23 24 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter; 25 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 26 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent; 27 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener; 28 import org.apache.directory.ldapstudio.browser.common.widgets.search.EntryWidget; 29 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 30 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry; 31 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeAttributesJob; 32 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob; 33 import org.apache.directory.ldapstudio.browser.core.model.DN; 34 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 35 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 36 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 38 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils; 39 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter; 40 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 41 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 42 import org.eclipse.jface.dialogs.MessageDialog; 43 import org.eclipse.jface.wizard.IWizardPage; 44 import org.eclipse.jface.wizard.WizardPage; 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.events.SelectionEvent; 47 import org.eclipse.swt.events.SelectionListener; 48 import org.eclipse.swt.layout.GridData; 49 import org.eclipse.swt.layout.GridLayout; 50 import org.eclipse.swt.widgets.Button; 51 import org.eclipse.swt.widgets.Composite; 52 53 54 60 public class NewEntryTypeWizardPage extends WizardPage implements WidgetModifyListener, SelectionListener 61 { 62 63 64 public static final String PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY = NewEntryTypeWizardPage.class 65 .getName() 66 + ".preferredEntryCreationMethod"; 67 68 69 private NewEntryWizard wizard; 70 71 72 private Button schemaButton; 73 74 75 private Button templateButton; 76 77 78 private EntryWidget entryWidget; 79 80 81 87 public NewEntryTypeWizardPage( String pageName, NewEntryWizard wizard ) 88 { 89 super( pageName ); 90 setTitle( "Entry Creation Method" ); 91 setDescription( "Please select the entry creation method." ); 92 setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) ); 93 setPageComplete( false ); 94 95 this.wizard = wizard; 96 } 97 98 99 102 private void validate() 103 { 104 if ( schemaButton.getSelection() ) 105 { 106 setPageComplete( true ); 107 } 108 else if ( templateButton.getSelection() ) 109 { 110 setPageComplete( entryWidget.getConnection() != null && entryWidget.getDn() != null ); 111 } 112 else 113 { 114 setPageComplete( false ); 115 } 116 } 117 118 119 126 public boolean canFlipToNextPage() 127 { 128 return isPageComplete(); 129 } 130 131 132 138 public IWizardPage getNextPage() 139 { 140 if ( templateButton.getSelection() ) 141 { 142 final IConnection connection = entryWidget.getConnection(); 143 final DN dn = entryWidget.getDn(); 144 final IEntry[] templateEntries = new IEntry[1]; 145 146 if ( connection == null ) 147 { 148 getShell().getDisplay().syncExec( new Runnable () 149 { 150 public void run() 151 { 152 MessageDialog.openError( getShell(), "Error", "No connection" ); 153 } 154 } ); 155 return null; 156 } 157 if ( dn == null ) 158 { 159 getShell().getDisplay().syncExec( new Runnable () 160 { 161 public void run() 162 { 163 MessageDialog.openError( getShell(), "Error", "No dn" ); 164 } 165 } ); 166 return null; 167 } 168 169 ReadEntryJob readEntryJob = new ReadEntryJob( connection, dn ); 171 RunnableContextJobAdapter.execute( readEntryJob, getContainer(), false ); 172 templateEntries[0] = readEntryJob.getReadEntry(); 173 if ( templateEntries[0] == null ) 174 { 175 getShell().getDisplay().syncExec( new Runnable () 176 { 177 public void run() 178 { 179 MessageDialog.openError( getShell(), "Error", "Entry " + dn.toString() + " doesn't exists" ); 180 } 181 } ); 182 return null; 183 } 184 185 if ( !templateEntries[0].isAttributesInitialized() ) 187 { 188 InitializeAttributesJob job = new InitializeAttributesJob( templateEntries, false ); 189 RunnableContextJobAdapter.execute( job, getContainer() ); 190 } 191 192 try 194 { 195 EventRegistry.suspendEventFireingInCurrentThread(); 196 197 LdifContentRecord record = ModelConverter.entryToLdifContentRecord( templateEntries[0] ); 198 DummyEntry prototypeEntry = ModelConverter.ldifContentRecordToEntry( record, connection ); 199 IAttribute[] attributes = prototypeEntry.getAttributes(); 200 for ( int i = 0; i < attributes.length; i++ ) 201 { 202 if ( !SchemaUtils.isModifyable( attributes[i].getAttributeTypeDescription() ) ) 203 { 204 prototypeEntry.deleteAttribute( attributes[i] ); 205 } 206 } 207 wizard.setPrototypeEntry( prototypeEntry ); 208 } 209 catch ( Exception e ) 210 { 211 e.printStackTrace(); 212 } 213 finally 214 { 215 EventRegistry.resumeEventFireingInCurrentThread(); 216 } 217 } 218 else 219 { 220 wizard.setPrototypeEntry( new DummyEntry( new DN(), wizard.getSelectedConnection() ) ); 221 } 222 223 return super.getNextPage(); 224 } 225 226 227 230 public void createControl( Composite parent ) 231 { 232 Composite composite = new Composite( parent, SWT.NONE ); 233 GridLayout gl = new GridLayout( 1, false ); 234 composite.setLayout( gl ); 235 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 236 237 schemaButton = BaseWidgetUtils.createRadiobutton( composite, "Create entry from scratch", 1 ); 238 schemaButton.addSelectionListener( this ); 239 templateButton = BaseWidgetUtils.createRadiobutton( composite, "Use existing entry as template", 1 ); 240 templateButton.addSelectionListener( this ); 241 242 Composite entryComposite = BaseWidgetUtils.createColumnContainer( composite, 3, 1 ); 243 BaseWidgetUtils.createRadioIndent( entryComposite, 1 ); 244 entryWidget = new EntryWidget( wizard.getSelectedConnection(), wizard.getSelectedEntry() != null ? wizard 245 .getSelectedEntry().getDn() : null ); 246 entryWidget.createWidget( entryComposite ); 247 entryWidget.addWidgetModifyListener( this ); 248 249 if ( BrowserUIPlugin.getDefault().getDialogSettings().get( PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY ) == null ) 250 BrowserUIPlugin.getDefault().getDialogSettings().put( PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY, 251 true ); 252 schemaButton.setSelection( BrowserUIPlugin.getDefault().getDialogSettings().getBoolean( 253 PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY ) ); 254 templateButton.setSelection( !BrowserUIPlugin.getDefault().getDialogSettings().getBoolean( 255 PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY ) ); 256 widgetSelected( null ); 257 258 setControl( composite ); 259 } 260 261 262 265 public void widgetModified( WidgetModifyEvent event ) 266 { 267 validate(); 268 } 269 270 271 274 public void widgetDefaultSelected( SelectionEvent e ) 275 { 276 } 277 278 279 282 public void widgetSelected( SelectionEvent e ) 283 { 284 entryWidget.setEnabled( templateButton.getSelection() ); 285 validate(); 286 } 287 288 289 292 public void saveDialogSettings() 293 { 294 BrowserUIPlugin.getDefault().getDialogSettings().put( PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY, 295 schemaButton.getSelection() ); 296 } 297 298 } 299 | Popular Tags |