KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > views > SchemasViewContentProvider


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.schemas.view.views;
22
23
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 /**
56  * This class implements the Content Provider for the Schemas View.
57  *
58  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
59  * @version $Rev$, $Date$
60  */

61 public class SchemasViewContentProvider implements IStructuredContentProvider, ITreeContentProvider, PoolListener
62 {
63     /** The Schema Pool */
64     private SchemaPool schemaPool;
65
66     /** The associated viewer */
67     private TreeViewer viewer;
68
69     /** The preferences store */
70     private IPreferenceStore store;
71
72     /** The FirstName Sorter */
73     private FirstNameSorter firstNameSorter;
74
75     /** The OID Sorter */
76     private OidSorter oidSorter;
77
78     /** The Schema Sorter */
79     private SchemaSorter schemaSorter;
80
81
82     /**
83      * Default constructor
84      */

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     /* (non-Javadoc)
100      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
101      */

102     public Object JavaDoc[] getElements( Object JavaDoc inputElement )
103     {
104         return getChildren( inputElement );
105     }
106
107
108     /* (non-Javadoc)
109      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
110      */

111     public Object JavaDoc[] getChildren( Object JavaDoc 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 JavaDoc<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             // Sort by
165
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             // Sort order
175
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 ); //$NON-NLS-1$
190
IntermediateNode objectClasses = new IntermediateNode(
191                         "Object Classes", ( SchemaWrapper ) parentElement, IntermediateNodeType.OBJECT_CLASS_FOLDER ); //$NON-NLS-1$
192
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                 // Sort by
220
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                 // Sort order
230
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 JavaDoc[0];
240         }
241         else
242         {
243             return children.toArray();
244         }
245     }
246
247
248     /* (non-Javadoc)
249      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
250      */

251     public Object JavaDoc getParent( Object JavaDoc element )
252     {
253         if ( element instanceof ITreeNode )
254         {
255             return ( ( ITreeNode ) element ).getParent();
256         }
257
258         // Default
259
return null;
260     }
261
262
263     /* (non-Javadoc)
264      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
265      */

266     public boolean hasChildren( Object JavaDoc 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         // Default
278
return false;
279     }
280
281
282     /* (non-Javadoc)
283      * @see org.apache.directory.ldapstudio.schemas.model.PoolListener#poolChanged(org.apache.directory.ldapstudio.schemas.model.SchemaPool, org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent)
284      */

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     /**
323      * Refreshes the viewer when a SchemaAdded event is fired.
324      *
325      * @param p
326      * the schema pool
327      * @param e
328      * the event
329      */

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     /**
344      * Refreshes the viewer when a SchemaRemoved event is fired.
345      *
346      * @param p
347      * the schema pool
348      * @param e
349      * the event
350      */

351     private void schemaRemoved( SchemaPool p, LDAPModelEvent e )
352     {
353         ITreeNode rootNode = ( ITreeNode ) viewer.getInput();
354
355         List JavaDoc<ITreeNode> schemaWrapperList = rootNode.getChildren();
356         for ( Iterator JavaDoc 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     /**
370      * Refreshes the viewer when a ATAdded or OCAdded event is fired.
371      *
372      * @param p
373      * the schema pool
374      * @param e
375      * the event
376      */

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         // Forcing the load of the children
388
getChildren( parentNode );
389
390         // Creating and adding the new element
391
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         // Refreshing the UI and selecting the newly created element
403
viewer.refresh( parentNode );
404         viewer.setSelection( new StructuredSelection( newElement ) );
405     }
406
407
408     /**
409      * Refreshes the viewer when a ATModified or OCModified event is fired.
410      *
411      * @param p
412      * the schema pool
413      * @param e
414      * the event
415      */

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 JavaDoc[] children = getChildren( parentNode );
438         for ( Object JavaDoc 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     /**
455      * Refreshes the viewer when a ATRemoved or OCRemoved event is fired.
456      *
457      * @param p
458      * the schema pool
459      * @param e
460      * the event
461      */

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 JavaDoc[] children = getChildren( parentNode );
486         for ( Object JavaDoc 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     /**
504      * Finds the corresponding Schema Wrapper in the Tree.
505      *
506      * @param schemaWrapper
507      * the Schema Wrapper to search
508      * @return
509      * the corresponding Schema Wrapper in the Tree
510      */

511     private ITreeNode findSchemaWrapperInTree( SchemaWrapper schemaWrapper )
512     {
513         Object JavaDoc[] schemaWrappers = getChildren( ( ITreeNode ) viewer.getInput() );
514         for ( Object JavaDoc sw : schemaWrappers )
515         {
516             if ( sw.equals( schemaWrapper ) )
517             {
518                 return ( ITreeNode ) sw;
519             }
520         }
521
522         return null;
523     }
524
525
526     /**
527      * Finds the parent node of a given element (AT or OT).
528      *
529      * @param element
530      * the element
531      * @return
532      * the parent node of a given element (AT or OT)
533      */

534     private ITreeNode findParentElement( SchemaElement element )
535     {
536         int group = Activator.getDefault().getPreferenceStore().getInt( PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING );
537
538         // Finding the associated SchemaWrapper
539
SchemaWrapper fakeSchemaWrapper = new SchemaWrapper( element.getOriginatingSchema(), null );
540         ITreeNode realSchemaWrapper = findSchemaWrapperInTree( fakeSchemaWrapper );
541         if ( realSchemaWrapper == null )
542         {
543             return null;
544         }
545
546         // Finding the correct node
547
ITreeNode parentNode = null;
548         if ( group == PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING_FOLDERS )
549         {
550             Object JavaDoc[] children = getChildren( realSchemaWrapper );
551             for ( Object JavaDoc 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     /* (non-Javadoc)
581      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
582      */

583     public void dispose()
584     {
585     }
586
587
588     /* (non-Javadoc)
589      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
590      */

591     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
592     {
593     }
594 }
595
Popular Tags