1 20 21 package org.apache.directory.ldapstudio.schemas.view.views; 22 23 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.schemas.Activator; 29 import org.apache.directory.ldapstudio.schemas.PluginConstants; 30 import org.apache.directory.ldapstudio.schemas.model.AttributeType; 31 import org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent; 32 import org.apache.directory.ldapstudio.schemas.model.ObjectClass; 33 import org.apache.directory.ldapstudio.schemas.model.PoolListener; 34 import org.apache.directory.ldapstudio.schemas.model.Schema; 35 import org.apache.directory.ldapstudio.schemas.model.SchemaElement; 36 import org.apache.directory.ldapstudio.schemas.model.SchemaPool; 37 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper; 38 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.FirstNameSorter; 39 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode; 40 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode; 41 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper; 42 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.OidSorter; 43 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaSorter; 44 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper; 45 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemasViewRoot; 46 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode.IntermediateNodeType; 47 import org.eclipse.jface.preference.IPreferenceStore; 48 import org.eclipse.jface.viewers.IStructuredContentProvider; 49 import org.eclipse.jface.viewers.ITreeContentProvider; 50 import org.eclipse.jface.viewers.StructuredSelection; 51 import org.eclipse.jface.viewers.TreeViewer; 52 import org.eclipse.jface.viewers.Viewer; 53 54 55 61 public class SchemasViewContentProvider implements IStructuredContentProvider, ITreeContentProvider, PoolListener 62 { 63 64 private SchemaPool schemaPool; 65 66 67 private TreeViewer viewer; 68 69 70 private IPreferenceStore store; 71 72 73 private FirstNameSorter firstNameSorter; 74 75 76 private OidSorter oidSorter; 77 78 79 private SchemaSorter schemaSorter; 80 81 82 85 public SchemasViewContentProvider( TreeViewer viewer ) 86 { 87 this.viewer = viewer; 88 schemaPool = SchemaPool.getInstance(); 89 store = Activator.getDefault().getPreferenceStore(); 90 91 firstNameSorter = new FirstNameSorter(); 92 oidSorter = new OidSorter(); 93 schemaSorter = new SchemaSorter(); 94 95 SchemaPool.getInstance().addListener( this ); 96 } 97 98 99 102 public Object [] getElements( Object inputElement ) 103 { 104 return getChildren( inputElement ); 105 } 106 107 108 111 public Object [] getChildren( Object parentElement ) 112 { 113 int group = store.getInt( PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING ); 114 int sortBy = store.getInt( PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_BY ); 115 int sortOrder = store.getInt( PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_ORDER ); 116 List <ITreeNode> children = null; 117 118 if ( parentElement instanceof SchemasViewRoot ) 119 { 120 SchemasViewRoot root = ( SchemasViewRoot ) parentElement; 121 122 if ( root.getChildren().isEmpty() ) 123 { 124 Schema[] schemas = schemaPool.getSchemas(); 125 for ( Schema schema : schemas ) 126 { 127 root.addChild( new SchemaWrapper( schema, root ) ); 128 } 129 } 130 131 children = root.getChildren(); 132 Collections.sort( children, schemaSorter ); 133 } 134 if ( parentElement instanceof IntermediateNode ) 135 { 136 IntermediateNode intermediate = ( IntermediateNode ) parentElement; 137 138 if ( intermediate.getChildren().isEmpty() ) 139 { 140 if ( intermediate.getType().equals( IntermediateNodeType.ATTRIBUTE_TYPE_FOLDER ) ) 141 { 142 Schema schema = ( ( SchemaWrapper ) intermediate.getParent() ).getMySchema(); 143 144 AttributeType[] ats = schema.getAttributeTypesAsArray(); 145 for ( AttributeType at : ats ) 146 { 147 intermediate.addChild( new AttributeTypeWrapper( at, intermediate ) ); 148 } 149 } 150 else if ( intermediate.getType().equals( IntermediateNodeType.OBJECT_CLASS_FOLDER ) ) 151 { 152 Schema schema = ( ( SchemaWrapper ) intermediate.getParent() ).getMySchema(); 153 154 ObjectClass[] ocs = schema.getObjectClassesAsArray(); 155 for ( ObjectClass oc : ocs ) 156 { 157 intermediate.addChild( new ObjectClassWrapper( oc, intermediate ) ); 158 } 159 } 160 } 161 162 children = intermediate.getChildren(); 163 164 if ( sortBy == PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_BY_FIRSTNAME ) 166 { 167 Collections.sort( children, firstNameSorter ); 168 } 169 else if ( sortBy == PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_BY_OID ) 170 { 171 Collections.sort( children, oidSorter ); 172 } 173 174 if ( sortOrder == PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_ORDER_DESCENDING ) 176 { 177 Collections.reverse( children ); 178 } 179 } 180 else if ( parentElement instanceof SchemaWrapper ) 181 { 182 SchemaWrapper schemaWrapper = ( SchemaWrapper ) parentElement; 183 184 if ( group == PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING_FOLDERS ) 185 { 186 if ( schemaWrapper.getChildren().isEmpty() ) 187 { 188 IntermediateNode attributeTypes = new IntermediateNode( 189 "Attribute Types", ( SchemaWrapper ) parentElement, IntermediateNodeType.ATTRIBUTE_TYPE_FOLDER ); IntermediateNode objectClasses = new IntermediateNode( 191 "Object Classes", ( SchemaWrapper ) parentElement, IntermediateNodeType.OBJECT_CLASS_FOLDER ); schemaWrapper.addChild( attributeTypes ); 193 schemaWrapper.addChild( objectClasses ); 194 } 195 196 children = schemaWrapper.getChildren(); 197 } 198 else if ( group == PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING_MIXED ) 199 { 200 if ( schemaWrapper.getChildren().isEmpty() ) 201 { 202 Schema schema = schemaWrapper.getMySchema(); 203 204 AttributeType[] ats = schema.getAttributeTypesAsArray(); 205 for ( AttributeType at : ats ) 206 { 207 schemaWrapper.addChild( new AttributeTypeWrapper( at, schemaWrapper ) ); 208 } 209 210 ObjectClass[] ocs = schema.getObjectClassesAsArray(); 211 for ( ObjectClass oc : ocs ) 212 { 213 schemaWrapper.addChild( new ObjectClassWrapper( oc, schemaWrapper ) ); 214 } 215 } 216 217 children = schemaWrapper.getChildren(); 218 219 if ( sortBy == PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_BY_FIRSTNAME ) 221 { 222 Collections.sort( children, firstNameSorter ); 223 } 224 else if ( sortBy == PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_BY_OID ) 225 { 226 Collections.sort( children, oidSorter ); 227 } 228 229 if ( sortOrder == PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_ORDER_DESCENDING ) 231 { 232 Collections.reverse( children ); 233 } 234 } 235 } 236 237 if ( children == null ) 238 { 239 return new Object [0]; 240 } 241 else 242 { 243 return children.toArray(); 244 } 245 } 246 247 248 251 public Object getParent( Object element ) 252 { 253 if ( element instanceof ITreeNode ) 254 { 255 return ( ( ITreeNode ) element ).getParent(); 256 } 257 258 return null; 260 } 261 262 263 266 public boolean hasChildren( Object element ) 267 { 268 if ( element instanceof SchemaWrapper ) 269 { 270 return true; 271 } 272 else if ( element instanceof IntermediateNode ) 273 { 274 return true; 275 } 276 277 return false; 279 } 280 281 282 285 public void poolChanged( SchemaPool p, LDAPModelEvent e ) 286 { 287 switch ( e.getReason() ) 288 { 289 case SchemaAdded: 290 schemaAdded( p, e ); 291 break; 292 293 case SchemaRemoved: 294 schemaRemoved( p, e ); 295 break; 296 297 case ATAdded: 298 case OCAdded: 299 aTOrOCAdded( p, e ); 300 break; 301 302 case ATModified: 303 case OCModified: 304 aTOrOCModified( p, e ); 305 break; 306 307 case ATRemoved: 308 case OCRemoved: 309 aTOrOCRemoved( p, e ); 310 break; 311 312 case PoolReloaded: 313 viewer.setInput( new SchemasViewRoot() ); 314 break; 315 316 default: 317 break; 318 } 319 } 320 321 322 330 private void schemaAdded( SchemaPool p, LDAPModelEvent e ) 331 { 332 ITreeNode rootNode = ( ITreeNode ) viewer.getInput(); 333 SchemaWrapper schemaWrapper = new SchemaWrapper( ( Schema ) e.getNewValue(), rootNode ); 334 rootNode.addChild( schemaWrapper ); 335 336 Collections.sort( rootNode.getChildren(), new SchemaSorter() ); 337 338 viewer.refresh( rootNode ); 339 viewer.setSelection( new StructuredSelection( schemaWrapper ) ); 340 } 341 342 343 351 private void schemaRemoved( SchemaPool p, LDAPModelEvent e ) 352 { 353 ITreeNode rootNode = ( ITreeNode ) viewer.getInput(); 354 355 List <ITreeNode> schemaWrapperList = rootNode.getChildren(); 356 for ( Iterator iter = schemaWrapperList.iterator(); iter.hasNext(); ) 357 { 358 SchemaWrapper schemaWrapper = ( SchemaWrapper ) iter.next(); 359 if ( schemaWrapper.getMySchema().equals( ( Schema ) e.getOldValue() ) ) 360 { 361 rootNode.removeChild( schemaWrapper ); 362 viewer.refresh( rootNode ); 363 break; 364 } 365 } 366 } 367 368 369 377 private void aTOrOCAdded( SchemaPool p, LDAPModelEvent e ) 378 { 379 SchemaElement element = ( SchemaElement ) e.getNewValue(); 380 381 ITreeNode parentNode = findParentElement( element ); 382 if ( parentNode == null ) 383 { 384 return; 385 } 386 387 getChildren( parentNode ); 389 390 ITreeNode newElement = null; 392 if ( element instanceof AttributeType ) 393 { 394 newElement = new AttributeTypeWrapper( ( AttributeType ) element, parentNode ); 395 } 396 else if ( element instanceof ObjectClass ) 397 { 398 newElement = new ObjectClassWrapper( ( ObjectClass ) element, parentNode ); 399 } 400 parentNode.addChild( newElement ); 401 402 viewer.refresh( parentNode ); 404 viewer.setSelection( new StructuredSelection( newElement ) ); 405 } 406 407 408 416 private void aTOrOCModified( SchemaPool p, LDAPModelEvent e ) 417 { 418 SchemaElement element = ( SchemaElement ) e.getNewValue(); 419 420 ITreeNode parentNode = findParentElement( element ); 421 if ( parentNode == null ) 422 { 423 return; 424 } 425 426 ITreeNode fakeNode = null; 427 if ( element instanceof ObjectClass ) 428 { 429 fakeNode = new ObjectClassWrapper( ( ObjectClass ) element, null ); 430 } 431 else if ( element instanceof AttributeType ) 432 { 433 fakeNode = new AttributeTypeWrapper( ( AttributeType ) element, null ); 434 } 435 436 ITreeNode realNode = null; 437 Object [] children = getChildren( parentNode ); 438 for ( Object child : children ) 439 { 440 if ( child.equals( fakeNode ) ) 441 { 442 realNode = ( ITreeNode ) child; 443 break; 444 } 445 } 446 447 if ( realNode != null ) 448 { 449 viewer.update( realNode, null ); 450 } 451 } 452 453 454 462 private void aTOrOCRemoved( SchemaPool p, LDAPModelEvent e ) 463 { 464 SchemaElement element = ( SchemaElement ) e.getOldValue(); 465 466 ITreeNode parentNode = findParentElement( element ); 467 if ( parentNode == null ) 468 { 469 return; 470 } 471 472 ITreeNode fakeNode = null; 473 if ( element instanceof ObjectClass ) 474 { 475 ( ( ObjectClass ) element ).closeAssociatedEditor(); 476 fakeNode = new ObjectClassWrapper( ( ObjectClass ) element, null ); 477 } 478 else if ( element instanceof AttributeType ) 479 { 480 ( ( AttributeType ) element ).closeAssociatedEditor(); 481 fakeNode = new AttributeTypeWrapper( ( AttributeType ) element, null ); 482 } 483 484 ITreeNode realNode = null; 485 Object [] children = getChildren( parentNode ); 486 for ( Object child : children ) 487 { 488 if ( child.equals( fakeNode ) ) 489 { 490 realNode = ( ITreeNode ) child; 491 break; 492 } 493 } 494 495 if ( realNode != null ) 496 { 497 realNode.getParent().removeChild( realNode ); 498 viewer.refresh( realNode.getParent() ); 499 } 500 } 501 502 503 511 private ITreeNode findSchemaWrapperInTree( SchemaWrapper schemaWrapper ) 512 { 513 Object [] schemaWrappers = getChildren( ( ITreeNode ) viewer.getInput() ); 514 for ( Object sw : schemaWrappers ) 515 { 516 if ( sw.equals( schemaWrapper ) ) 517 { 518 return ( ITreeNode ) sw; 519 } 520 } 521 522 return null; 523 } 524 525 526 534 private ITreeNode findParentElement( SchemaElement element ) 535 { 536 int group = Activator.getDefault().getPreferenceStore().getInt( PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING ); 537 538 SchemaWrapper fakeSchemaWrapper = new SchemaWrapper( element.getOriginatingSchema(), null ); 540 ITreeNode realSchemaWrapper = findSchemaWrapperInTree( fakeSchemaWrapper ); 541 if ( realSchemaWrapper == null ) 542 { 543 return null; 544 } 545 546 ITreeNode parentNode = null; 548 if ( group == PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING_FOLDERS ) 549 { 550 Object [] children = getChildren( realSchemaWrapper ); 551 for ( Object child : children ) 552 { 553 IntermediateNode intermediateNode = ( IntermediateNode ) child; 554 555 if ( element instanceof AttributeType ) 556 { 557 if ( intermediateNode.getType() == IntermediateNodeType.ATTRIBUTE_TYPE_FOLDER ) 558 { 559 parentNode = intermediateNode; 560 } 561 } 562 else if ( element instanceof ObjectClass ) 563 { 564 if ( intermediateNode.getType() == IntermediateNodeType.OBJECT_CLASS_FOLDER ) 565 { 566 parentNode = intermediateNode; 567 } 568 } 569 } 570 } 571 else if ( group == PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING_MIXED ) 572 { 573 parentNode = realSchemaWrapper; 574 } 575 576 return parentNode; 577 } 578 579 580 583 public void dispose() 584 { 585 } 586 587 588 591 public void inputChanged( Viewer viewer, Object oldInput, Object newInput ) 592 { 593 } 594 } 595 | Popular Tags |