1 20 21 package org.apache.directory.ldapstudio.browser.ui.wizards; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter; 31 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 32 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 33 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 34 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry; 35 import org.apache.directory.ldapstudio.browser.core.internal.model.Value; 36 import org.apache.directory.ldapstudio.browser.core.jobs.OpenConnectionsJob; 37 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 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.schema.ObjectClassDescription; 41 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 42 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 43 import org.eclipse.jface.fieldassist.DecoratedField; 44 import org.eclipse.jface.fieldassist.FieldDecoration; 45 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 46 import org.eclipse.jface.fieldassist.IControlCreator; 47 import org.eclipse.jface.viewers.ArrayContentProvider; 48 import org.eclipse.jface.viewers.DoubleClickEvent; 49 import org.eclipse.jface.viewers.IDoubleClickListener; 50 import org.eclipse.jface.viewers.ISelection; 51 import org.eclipse.jface.viewers.IStructuredSelection; 52 import org.eclipse.jface.viewers.LabelProvider; 53 import org.eclipse.jface.viewers.ListViewer; 54 import org.eclipse.jface.viewers.StructuredSelection; 55 import org.eclipse.jface.viewers.Viewer; 56 import org.eclipse.jface.viewers.ViewerFilter; 57 import org.eclipse.jface.viewers.ViewerSorter; 58 import org.eclipse.jface.wizard.WizardPage; 59 import org.eclipse.swt.SWT; 60 import org.eclipse.swt.events.ModifyEvent; 61 import org.eclipse.swt.events.ModifyListener; 62 import org.eclipse.swt.events.SelectionAdapter; 63 import org.eclipse.swt.events.SelectionEvent; 64 import org.eclipse.swt.layout.GridData; 65 import org.eclipse.swt.layout.GridLayout; 66 import org.eclipse.swt.widgets.Button; 67 import org.eclipse.swt.widgets.Composite; 68 import org.eclipse.swt.widgets.Control; 69 import org.eclipse.swt.widgets.Label; 70 import org.eclipse.swt.widgets.Text; 71 72 73 79 public class NewEntryObjectclassWizardPage extends WizardPage 80 { 81 82 83 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250; 84 85 86 private final static int SIZING_SELECTION_WIDGET_WIDTH = 400; 87 88 89 private NewEntryWizard wizard; 90 91 92 private List <ObjectClassDescription> availableObjectClasses; 93 94 95 private Text availableObjectClassesInstantSearch; 96 97 98 private ListViewer availableObjectClassesViewer; 99 100 101 private List <ObjectClassDescription> selectedObjectClasses; 102 103 104 private ListViewer selectedObjectClassesViewer; 105 106 107 private Button addButton; 108 109 110 private Button removeButton; 111 112 113 119 public NewEntryObjectclassWizardPage( String pageName, NewEntryWizard wizard ) 120 { 121 super( pageName ); 122 setTitle( "Object Classes" ); 123 setDescription( "Please select object classes of the new entry. Select at least one structural object class." ); 124 setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) ); 125 setPageComplete( false ); 126 127 this.wizard = wizard; 128 this.availableObjectClasses = new ArrayList <ObjectClassDescription>(); 129 this.selectedObjectClasses = new ArrayList <ObjectClassDescription>(); 130 } 131 132 133 136 private void validate() 137 { 138 if ( !selectedObjectClasses.isEmpty() ) 139 { 140 setPageComplete( true ); 141 saveState(); 142 } 143 else 144 { 145 setPageComplete( false ); 146 } 147 } 148 149 150 154 private void loadState() 155 { 156 availableObjectClasses.clear(); 157 selectedObjectClasses.clear(); 158 159 if ( wizard.getSelectedConnection() != null ) 160 { 161 if( !wizard.getSelectedConnection().isOpened() ) 162 { 163 OpenConnectionsJob job = new OpenConnectionsJob(wizard.getSelectedConnection()); 164 RunnableContextJobAdapter.execute( job, getContainer() ); 165 } 166 167 availableObjectClasses.addAll( Arrays.asList( wizard.getSelectedConnection().getSchema() 168 .getObjectClassDescriptions() ) ); 169 170 DummyEntry newEntry = wizard.getPrototypeEntry(); 171 IAttribute ocAttribute = newEntry.getAttribute( IAttribute.OBJECTCLASS_ATTRIBUTE ); 172 if ( ocAttribute != null ) 173 { 174 String [] ocValues = ocAttribute.getStringValues(); 175 for ( int i = 0; i < ocValues.length; i++ ) 176 { 177 String ocValue = ocValues[i]; 178 ObjectClassDescription ocd = wizard.getSelectedConnection().getSchema().getObjectClassDescription( 179 ocValue ); 180 availableObjectClasses.remove( ocd ); 181 selectedObjectClasses.add( ocd ); 182 } 183 } 184 } 185 186 availableObjectClassesViewer.refresh(); 187 selectedObjectClassesViewer.refresh(); 188 } 189 190 191 194 private void saveState() 195 { 196 DummyEntry newEntry = wizard.getPrototypeEntry(); 197 198 try 199 { 200 EventRegistry.suspendEventFireingInCurrentThread(); 201 202 IAttribute ocAttribute = newEntry.getAttribute( IAttribute.OBJECTCLASS_ATTRIBUTE ); 204 if ( ocAttribute == null ) 205 { 206 ocAttribute = new Attribute( newEntry, IAttribute.OBJECTCLASS_ATTRIBUTE ); 207 newEntry.addAttribute( ocAttribute ); 208 } 209 IValue[] values = ocAttribute.getValues(); 210 for ( int i = 0; i < values.length; i++ ) 211 { 212 ocAttribute.deleteValue( values[i] ); 213 } 214 for ( Iterator <ObjectClassDescription> it = selectedObjectClasses.iterator(); it.hasNext(); ) 215 { 216 ObjectClassDescription ocd = it.next(); 217 ocAttribute.addValue( new Value( ocAttribute, ocd.getNames()[0] ) ); 218 } 219 } 220 catch ( ModelModificationException e ) 221 { 222 e.printStackTrace(); 223 } 224 finally 225 { 226 EventRegistry.resumeEventFireingInCurrentThread(); 227 } 228 } 229 230 231 237 public void setVisible( boolean visible ) 238 { 239 super.setVisible( visible ); 240 241 if ( visible ) 242 { 243 loadState(); 244 validate(); 245 availableObjectClassesInstantSearch.setFocus(); 246 } 247 } 248 249 250 253 public void createControl( Composite parent ) 254 { 255 256 Composite composite = new Composite( parent, SWT.NONE ); 257 GridLayout gl = new GridLayout( 3, false ); 258 composite.setLayout( gl ); 259 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 260 261 Label availableLabel = new Label( composite, SWT.NONE ); 262 availableLabel.setText( "Available object classes" ); 263 Label buttonLabel = new Label( composite, SWT.NONE ); 264 buttonLabel.setText( "" ); 265 Label selectedLabel = new Label( composite, SWT.NONE ); 266 selectedLabel.setText( "Selected object classes" ); 267 268 Composite availableObjectClassesComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 ); 269 270 if ( FieldDecorationRegistry.getDefault().getFieldDecoration( getClass().getName() ) == null ) 271 { 272 FieldDecoration dummy = FieldDecorationRegistry.getDefault().getFieldDecoration( 273 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL ); 274 FieldDecorationRegistry.getDefault().registerFieldDecoration( getClass().getName(), 275 "You may enter a filter to restrict the list below", dummy.getImage() ); 276 } 277 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration( 278 getClass().getName() ); 279 final DecoratedField availabeObjectClassesInstantSearchField = new DecoratedField( 280 availableObjectClassesComposite, SWT.BORDER, new IControlCreator() 281 { 282 public Control createControl( Composite parent, int style ) 283 { 284 return BaseWidgetUtils.createText( parent, "", 1 ); 285 } 286 } ); 287 availabeObjectClassesInstantSearchField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 288 availabeObjectClassesInstantSearchField.getLayoutControl().setLayoutData( 289 new GridData( SWT.FILL, SWT.CENTER, true, false ) ); 290 availableObjectClassesInstantSearch = ( Text ) availabeObjectClassesInstantSearchField.getControl(); 291 availableObjectClassesInstantSearch.addModifyListener( new ModifyListener() 292 { 293 public void modifyText( ModifyEvent e ) 294 { 295 availableObjectClassesViewer.refresh(); 296 if ( availableObjectClassesViewer.getList().getItemCount() == 1 ) 297 { 298 Object item = availableObjectClassesViewer.getElementAt( 0 ); 299 availableObjectClassesViewer.setSelection( new StructuredSelection( item ) ); 300 } 301 } 302 } ); 303 304 availableObjectClassesViewer = new ListViewer( availableObjectClassesComposite ); 305 GridData data = new GridData( GridData.FILL_BOTH ); 306 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 307 data.widthHint = ( int ) ( SIZING_SELECTION_WIDGET_WIDTH * 0.4 ); 308 availableObjectClassesViewer.getList().setLayoutData( data ); 309 availableObjectClassesViewer.setContentProvider( new ArrayContentProvider() ); 310 availableObjectClassesViewer.setLabelProvider( new LabelProvider() ); 311 availableObjectClassesViewer.setSorter( new ViewerSorter() ); 312 availableObjectClassesViewer.addFilter( new InstantSearchFilter( availableObjectClassesInstantSearch ) ); 313 availableObjectClassesViewer.setInput( availableObjectClasses ); 314 availableObjectClassesViewer.addDoubleClickListener( new IDoubleClickListener() 315 { 316 public void doubleClick( DoubleClickEvent event ) 317 { 318 add( event.getSelection() ); 319 } 320 } ); 321 322 Composite buttonComposite = new Composite( composite, SWT.NONE ); 323 gl = new GridLayout( 1, true ); 324 buttonComposite.setLayout( gl ); 325 data = new GridData( GridData.FILL_BOTH ); 326 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 327 data.horizontalAlignment = SWT.CENTER; 329 buttonComposite.setLayoutData( data ); 330 Label label0 = new Label( buttonComposite, SWT.NONE ); 331 data = new GridData(); 332 data.grabExcessHorizontalSpace = true; 333 data.grabExcessVerticalSpace = true; 334 label0.setLayoutData( data ); 335 addButton = BaseWidgetUtils.createButton( buttonComposite, "&Add", 1 ); 336 removeButton = BaseWidgetUtils.createButton( buttonComposite, "&Remove", 1 ); 337 Label label3 = new Label( buttonComposite, SWT.NONE ); 338 data = new GridData(); 339 data.grabExcessHorizontalSpace = true; 340 data.grabExcessVerticalSpace = true; 341 label3.setLayoutData( data ); 342 343 addButton.addSelectionListener( new SelectionAdapter() 344 { 345 public void widgetSelected( SelectionEvent e ) 346 { 347 add( availableObjectClassesViewer.getSelection() ); 348 } 349 } ); 350 351 removeButton.addSelectionListener( new SelectionAdapter() 352 { 353 public void widgetSelected( SelectionEvent e ) 354 { 355 remove( selectedObjectClassesViewer.getSelection() ); 356 } 357 } ); 358 359 selectedObjectClassesViewer = new ListViewer( composite ); 360 data = new GridData( GridData.FILL_BOTH ); 361 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 362 data.widthHint = ( int ) ( SIZING_SELECTION_WIDGET_WIDTH * 0.4 ); 363 selectedObjectClassesViewer.getList().setLayoutData( data ); 364 selectedObjectClassesViewer.setContentProvider( new ArrayContentProvider() ); 365 selectedObjectClassesViewer.setLabelProvider( new LabelProvider() ); 366 selectedObjectClassesViewer.setSorter( new ViewerSorter() ); 367 selectedObjectClassesViewer.setInput( selectedObjectClasses ); 368 selectedObjectClassesViewer.addDoubleClickListener( new IDoubleClickListener() 369 { 370 public void doubleClick( DoubleClickEvent event ) 371 { 372 remove( event.getSelection() ); 373 } 374 } ); 375 376 setControl( composite ); 377 } 378 379 380 386 private void add( ISelection iselection ) 387 { 388 IStructuredSelection selection = ( IStructuredSelection ) iselection; 389 Iterator it = selection.iterator(); 390 while ( it.hasNext() ) 391 { 392 ObjectClassDescription ocd = ( ObjectClassDescription ) it.next(); 393 if ( availableObjectClasses.contains( ocd ) && !selectedObjectClasses.contains( ocd ) ) 394 { 395 availableObjectClasses.remove( ocd ); 396 selectedObjectClasses.add( ocd ); 397 398 ObjectClassDescription[] superiorObjectClassDescriptions = ocd.getSuperiorObjectClassDescriptions(); 400 if ( superiorObjectClassDescriptions.length > 0 ) 401 { 402 add( new StructuredSelection( superiorObjectClassDescriptions ) ); 403 } 404 } 405 } 406 407 availableObjectClassesViewer.refresh(); 408 selectedObjectClassesViewer.refresh(); 409 validate(); 410 411 if ( !"".equals( availableObjectClassesInstantSearch.getText() ) ) 412 { 413 availableObjectClassesInstantSearch.setText( "" ); 414 availableObjectClassesInstantSearch.setFocus(); 415 } 416 } 417 418 419 425 private void remove( ISelection iselection ) 426 { 427 IStructuredSelection selection = ( IStructuredSelection ) iselection; 428 Iterator it = selection.iterator(); 429 while ( it.hasNext() ) 430 { 431 ObjectClassDescription ocd = ( ObjectClassDescription ) it.next(); 432 if ( !availableObjectClasses.contains( ocd ) && selectedObjectClasses.contains( ocd ) ) 433 { 434 selectedObjectClasses.remove( ocd ); 435 availableObjectClasses.add( ocd ); 436 437 ObjectClassDescription[] subObjectClassDescriptions = ocd.getSubObjectClassDescriptions(); 439 if ( subObjectClassDescriptions.length > 0 ) 440 { 441 remove( new StructuredSelection( subObjectClassDescriptions ) ); 442 } 443 } 444 } 445 446 availableObjectClassesViewer.refresh(); 447 selectedObjectClassesViewer.refresh(); 448 validate(); 449 } 450 451 452 455 public void saveDialogSettings() 456 { 457 458 } 459 460 463 private class InstantSearchFilter extends ViewerFilter 464 { 465 466 467 private Text filterText; 468 469 470 475 private InstantSearchFilter( Text filterText ) 476 { 477 this.filterText = filterText; 478 } 479 480 481 484 public boolean select( Viewer viewer, Object parentElement, Object element ) 485 { 486 if ( element instanceof ObjectClassDescription ) 487 { 488 ObjectClassDescription ocd = ( ObjectClassDescription ) element; 489 Set <String > lowerCaseIdentifierSet = ocd.getLowerCaseIdentifierSet(); 490 for ( Iterator <String > it = lowerCaseIdentifierSet.iterator(); it.hasNext(); ) 491 { 492 String s = it.next(); 493 if ( s.toLowerCase().startsWith( filterText.getText().toLowerCase() ) ) 494 { 495 return true; 496 } 497 } 498 } 499 return false; 500 } 501 } 502 } 503 | Popular Tags |