1 20 21 package org.apache.directory.ldapstudio.schemas.view.views; 22 23 24 import java.util.ArrayList ; 25 import java.util.Collections ; 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.controller.actions.HideAttributeTypesAction; 31 import org.apache.directory.ldapstudio.schemas.controller.actions.HideObjectClassesAction; 32 import org.apache.directory.ldapstudio.schemas.model.AttributeType; 33 import org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent; 34 import org.apache.directory.ldapstudio.schemas.model.ObjectClass; 35 import org.apache.directory.ldapstudio.schemas.model.PoolListener; 36 import org.apache.directory.ldapstudio.schemas.model.Schema; 37 import org.apache.directory.ldapstudio.schemas.model.SchemaElement; 38 import org.apache.directory.ldapstudio.schemas.model.SchemaPool; 39 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper; 40 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.FirstNameSorter; 41 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode; 42 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper; 43 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.OidSorter; 44 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaElementsViewRoot; 45 import org.eclipse.jface.preference.IPreferenceStore; 46 import org.eclipse.jface.viewers.IStructuredContentProvider; 47 import org.eclipse.jface.viewers.ITreeContentProvider; 48 import org.eclipse.jface.viewers.TreeViewer; 49 import org.eclipse.jface.viewers.Viewer; 50 51 52 58 public class SchemaElementsViewContentProvider implements IStructuredContentProvider, ITreeContentProvider, 59 PoolListener 60 { 61 62 private SchemaPool schemaPool; 63 64 65 private TreeViewer viewer; 66 67 68 IPreferenceStore store; 69 70 71 private FirstNameSorter firstNameSorter; 72 73 74 private OidSorter oidSorter; 75 76 77 83 public SchemaElementsViewContentProvider( TreeViewer viewer ) 84 { 85 this.viewer = viewer; 86 schemaPool = SchemaPool.getInstance(); 87 schemaPool.addListener( this ); 88 store = Activator.getDefault().getPreferenceStore(); 89 90 firstNameSorter = new FirstNameSorter(); 91 oidSorter = new OidSorter(); 92 } 93 94 95 98 public Object [] getElements( Object inputElement ) 99 { 100 return getChildren( inputElement ); 101 } 102 103 104 107 public Object [] getChildren( Object parentElement ) 108 { 109 if ( parentElement instanceof SchemaElementsViewRoot ) 110 { 111 SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) parentElement; 112 113 List <ITreeNode> children = new ArrayList <ITreeNode>(); 114 115 int group = store.getInt( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING ); 116 int sortBy = store.getInt( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY ); 117 int sortOrder = store.getInt( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER ); 118 boolean hideAttributeTypes = Activator.getDefault().getDialogSettings().getBoolean( 119 HideAttributeTypesAction.HIDE_ATTRIBUTE_TYPES_DS_KEY ); 120 boolean hideObjectClasses = Activator.getDefault().getDialogSettings().getBoolean( 121 HideObjectClassesAction.HIDE_OBJECT_CLASSES_DS_KEY ); 122 123 if ( root.getChildren().isEmpty() ) 124 { 125 List <AttributeType> attributeTypes = schemaPool.getAttributeTypes(); 127 for ( AttributeType attributeType : attributeTypes ) 128 { 129 root.addChild( new AttributeTypeWrapper( attributeType, root ) ); 130 } 131 132 List <ObjectClass> objectClasses = schemaPool.getObjectClasses(); 134 for ( ObjectClass objectClass : objectClasses ) 135 { 136 root.addChild( new ObjectClassWrapper( objectClass, root ) ); 137 } 138 } 139 140 List <AttributeTypeWrapper> atList = new ArrayList <AttributeTypeWrapper>(); 141 List <ObjectClassWrapper> ocList = new ArrayList <ObjectClassWrapper>(); 142 143 if ( !hideAttributeTypes ) 144 { 145 atList = root.getAttributeTypes(); 146 } 147 if ( !hideObjectClasses ) 148 { 149 ocList = root.getObjectClasses(); 150 } 151 152 if ( group == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING_ATFIRST ) 153 { 154 if ( sortBy == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY_FIRSTNAME ) 156 { 157 Collections.sort( atList, firstNameSorter ); 158 Collections.sort( ocList, firstNameSorter ); 159 } 160 else if ( sortBy == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY_OID ) 161 { 162 Collections.sort( atList, oidSorter ); 163 Collections.sort( ocList, oidSorter ); 164 } 165 166 if ( sortOrder == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER_DESCENDING ) 168 { 169 Collections.reverse( atList ); 170 Collections.reverse( ocList ); 171 } 172 173 children.addAll( atList ); 175 children.addAll( ocList ); 176 } 177 else if ( group == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING_OCFIRST ) 178 { 179 if ( sortBy == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY_FIRSTNAME ) 181 { 182 Collections.sort( atList, firstNameSorter ); 183 Collections.sort( ocList, firstNameSorter ); 184 } 185 else if ( sortBy == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY_OID ) 186 { 187 Collections.sort( atList, oidSorter ); 188 Collections.sort( ocList, oidSorter ); 189 } 190 191 if ( sortOrder == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER_DESCENDING ) 193 { 194 Collections.reverse( atList ); 195 Collections.reverse( ocList ); 196 } 197 198 children.addAll( ocList ); 200 children.addAll( atList ); 201 } 202 else if ( group == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING_MIXED ) 203 { 204 children.addAll( atList ); 206 children.addAll( ocList ); 207 208 if ( sortBy == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY_FIRSTNAME ) 210 { 211 Collections.sort( children, firstNameSorter ); 212 } 213 else if ( sortBy == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY_OID ) 214 { 215 Collections.sort( children, oidSorter ); 216 } 217 218 if ( sortOrder == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER_DESCENDING ) 220 { 221 Collections.reverse( children ); 222 } 223 } 224 return children.toArray(); 225 } 226 227 return new Object [0]; 229 } 230 231 232 235 public Object getParent( Object element ) 236 { 237 if ( element instanceof ITreeNode ) 238 { 239 return ( ( ITreeNode ) element ).getParent(); 240 } 241 242 return null; 243 } 244 245 246 249 public boolean hasChildren( Object element ) 250 { 251 if ( element instanceof SchemaElementsViewRoot ) 252 { 253 return true; 254 } 255 256 return false; 257 } 258 259 260 263 public void poolChanged( SchemaPool p, LDAPModelEvent e ) 264 { 265 switch ( e.getReason() ) 266 { 267 case SchemaAdded: 268 schemaAdded( p, e ); 269 break; 270 271 case SchemaRemoved: 272 schemaRemoved( p, e ); 273 break; 274 275 case ATAdded: 276 case OCAdded: 277 aTOrOCAdded( p, e ); 278 break; 279 280 case ATModified: 281 case OCModified: 282 aTOrOCModified( p, e ); 283 break; 284 285 case ATRemoved: 286 case OCRemoved: 287 aTOrOCRemoved( p, e ); 288 break; 289 290 case PoolReloaded: 291 viewer.setInput( new SchemaElementsViewRoot() ); 292 break; 293 294 default: 295 break; 296 } 297 } 298 299 300 308 private void schemaAdded( SchemaPool p, LDAPModelEvent e ) 309 { 310 SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) viewer.getInput(); 311 Schema schema = ( Schema ) e.getNewValue(); 312 313 AttributeType[] attributeTypes = schema.getAttributeTypesAsArray(); 315 for ( AttributeType attributeType : attributeTypes ) 316 { 317 root.addChild( new AttributeTypeWrapper( attributeType, root ) ); 318 } 319 320 ObjectClass[] objectClasses = schema.getObjectClassesAsArray(); 322 for ( ObjectClass objectClass : objectClasses ) 323 { 324 root.addChild( new ObjectClassWrapper( objectClass, root ) ); 325 } 326 327 viewer.refresh(); 328 } 329 330 331 339 private void schemaRemoved( SchemaPool p, LDAPModelEvent e ) 340 { 341 Schema schema = ( Schema ) e.getOldValue(); 342 343 AttributeType[] attributeTypes = schema.getAttributeTypesAsArray(); 345 for ( AttributeType attributeType : attributeTypes ) 346 { 347 ITreeNode wrapper = getWrapper( attributeType ); 348 if ( wrapper != null ) 349 { 350 wrapper.getParent().removeChild( wrapper ); 351 } 352 } 353 354 ObjectClass[] objectClasses = schema.getObjectClassesAsArray(); 356 for ( ObjectClass objectClass : objectClasses ) 357 { 358 ITreeNode wrapper = getWrapper( objectClass ); 359 if ( wrapper != null ) 360 { 361 wrapper.getParent().removeChild( wrapper ); 362 } 363 } 364 365 viewer.refresh(); 366 } 367 368 369 377 private void aTOrOCAdded( SchemaPool p, LDAPModelEvent e ) 378 { 379 SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) viewer.getInput(); 380 Object element = e.getNewValue(); 381 382 if ( element instanceof AttributeType ) 383 { 384 root.addChild( new AttributeTypeWrapper( ( AttributeType ) element, root ) ); 385 } 386 else if ( element instanceof ObjectClass ) 387 { 388 root.addChild( new ObjectClassWrapper( ( ObjectClass ) element, root ) ); 389 } 390 391 viewer.refresh(); 392 } 393 394 395 403 private void aTOrOCModified( SchemaPool p, LDAPModelEvent e ) 404 { 405 ITreeNode wrapper = getWrapper( ( SchemaElement ) e.getNewValue() ); 406 if ( wrapper != null ) 407 { 408 viewer.update( wrapper, null ); 409 } 410 } 411 412 413 421 private void aTOrOCRemoved( SchemaPool p, LDAPModelEvent e ) 422 { 423 ITreeNode wrapper = getWrapper( ( SchemaElement ) e.getOldValue() ); 424 if ( wrapper != null ) 425 { 426 wrapper.getParent().removeChild( wrapper ); 427 viewer.refresh(); 428 } 429 } 430 431 432 440 private ITreeNode getWrapper( SchemaElement schemaElement ) 441 { 442 SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) viewer.getInput(); 443 444 if ( schemaElement instanceof AttributeType ) 445 { 446 List <AttributeTypeWrapper> atws = root.getAttributeTypes(); 447 for ( AttributeTypeWrapper atw : atws ) 448 { 449 if ( atw.getMyAttributeType().equals( schemaElement ) ) 450 { 451 return atw; 452 } 453 } 454 } 455 else if ( schemaElement instanceof ObjectClass ) 456 { 457 List <ObjectClassWrapper> ocws = root.getObjectClasses(); 458 for ( ObjectClassWrapper ocw : ocws ) 459 { 460 if ( ocw.getMyObjectClass().equals( schemaElement ) ) 461 { 462 return ocw; 463 } 464 } 465 } 466 467 return null; 468 } 469 470 471 474 public void dispose() 475 { 476 } 477 478 479 482 public void inputChanged( Viewer viewer, Object oldInput, Object newInput ) 483 { 484 } 485 } 486 | Popular Tags |