KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
25 import java.util.Collections 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.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 /**
53  * This class implements the content provider for the Schema Elements View.
54  *
55  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
56  * @version $Rev$, $Date$
57  */

58 public class SchemaElementsViewContentProvider implements IStructuredContentProvider, ITreeContentProvider,
59     PoolListener
60 {
61     /** The Schema Pool holding all schemas */
62     private SchemaPool schemaPool;
63
64     /** The associated viewer */
65     private TreeViewer viewer;
66
67     /** The preferences store */
68     IPreferenceStore store;
69
70     /** The FirstName Sorter */
71     private FirstNameSorter firstNameSorter;
72
73     /** The OID Sorter */
74     private OidSorter oidSorter;
75
76
77     /**
78      * Creates a new instance of SchemaElementsContentProvider.
79      *
80      * @param schemaPool
81      * the associated Schema Pool
82      */

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

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

107     public Object JavaDoc[] getChildren( Object JavaDoc parentElement )
108     {
109         if ( parentElement instanceof SchemaElementsViewRoot )
110         {
111             SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) parentElement;
112
113             List JavaDoc<ITreeNode> children = new ArrayList JavaDoc<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                 // ATTRIBUTE TYPES
126
List JavaDoc<AttributeType> attributeTypes = schemaPool.getAttributeTypes();
127                 for ( AttributeType attributeType : attributeTypes )
128                 {
129                     root.addChild( new AttributeTypeWrapper( attributeType, root ) );
130                 }
131
132                 // OBJECT CLASSES
133
List JavaDoc<ObjectClass> objectClasses = schemaPool.getObjectClasses();
134                 for ( ObjectClass objectClass : objectClasses )
135                 {
136                     root.addChild( new ObjectClassWrapper( objectClass, root ) );
137                 }
138             }
139
140             List JavaDoc<AttributeTypeWrapper> atList = new ArrayList JavaDoc<AttributeTypeWrapper>();
141             List JavaDoc<ObjectClassWrapper> ocList = new ArrayList JavaDoc<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                 // Sort by
155
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                 // Sort Order
167
if ( sortOrder == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER_DESCENDING )
168                 {
169                     Collections.reverse( atList );
170                     Collections.reverse( ocList );
171                 }
172
173                 // Group
174
children.addAll( atList );
175                 children.addAll( ocList );
176             }
177             else if ( group == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING_OCFIRST )
178             {
179                 // Sort by
180
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                 // Sort Order
192
if ( sortOrder == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER_DESCENDING )
193                 {
194                     Collections.reverse( atList );
195                     Collections.reverse( ocList );
196                 }
197
198                 // Group
199
children.addAll( ocList );
200                 children.addAll( atList );
201             }
202             else if ( group == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING_MIXED )
203             {
204                 // Group
205
children.addAll( atList );
206                 children.addAll( ocList );
207
208                 // Sort by
209
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                 // Sort order
219
if ( sortOrder == PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER_DESCENDING )
220                 {
221                     Collections.reverse( children );
222                 }
223             }
224             return children.toArray();
225         }
226
227         // Default
228
return new Object JavaDoc[0];
229     }
230
231
232     /* (non-Javadoc)
233      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
234      */

235     public Object JavaDoc getParent( Object JavaDoc element )
236     {
237         if ( element instanceof ITreeNode )
238         {
239             return ( ( ITreeNode ) element ).getParent();
240         }
241
242         return null;
243     }
244
245
246     /* (non-Javadoc)
247      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
248      */

249     public boolean hasChildren( Object JavaDoc element )
250     {
251         if ( element instanceof SchemaElementsViewRoot )
252         {
253             return true;
254         }
255
256         return false;
257     }
258
259
260     /* (non-Javadoc)
261      * @see org.apache.directory.ldapstudio.schemas.model.PoolListener#poolChanged(org.apache.directory.ldapstudio.schemas.model.SchemaPool, org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent)
262      */

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     /**
301      * Refreshes the viewer when a SchemaAdded event is fired.
302      *
303      * @param p
304      * the schema pool
305      * @param e
306      * the event
307      */

308     private void schemaAdded( SchemaPool p, LDAPModelEvent e )
309     {
310         SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) viewer.getInput();
311         Schema schema = ( Schema ) e.getNewValue();
312
313         // ATTRIBUTE TYPES
314
AttributeType[] attributeTypes = schema.getAttributeTypesAsArray();
315         for ( AttributeType attributeType : attributeTypes )
316         {
317             root.addChild( new AttributeTypeWrapper( attributeType, root ) );
318         }
319
320         // OBJECT CLASSES
321
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     /**
332      * Refreshes the viewer when a SchemaRemoved event is fired.
333      *
334      * @param p
335      * the schema pool
336      * @param e
337      * the event
338      */

339     private void schemaRemoved( SchemaPool p, LDAPModelEvent e )
340     {
341         Schema schema = ( Schema ) e.getOldValue();
342
343         // ATTRIBUTE TYPES
344
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         // OBJECT CLASSES
355
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     /**
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         SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) viewer.getInput();
380         Object JavaDoc 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     /**
396      * Refreshes the viewer when a ATModified or OCModified event is fired.
397      *
398      * @param p
399      * the schema pool
400      * @param e
401      * the event
402      */

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     /**
414      * Refreshes the viewer when a ATRemoved or OCRemoved event is fired.
415      *
416      * @param p
417      * the schema pool
418      * @param e
419      * the event
420      */

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     /**
433      * Gets the Wrapper associated with the given Schema Element.
434      *
435      * @param schemaElement
436      * the Schema Element to search from
437      * @return
438      * the associated wrapper, null if no wrapper is found
439      */

440     private ITreeNode getWrapper( SchemaElement schemaElement )
441     {
442         SchemaElementsViewRoot root = ( SchemaElementsViewRoot ) viewer.getInput();
443
444         if ( schemaElement instanceof AttributeType )
445         {
446             List JavaDoc<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 JavaDoc<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     /* (non-Javadoc)
472      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
473      */

474     public void dispose()
475     {
476     }
477
478
479     /* (non-Javadoc)
480      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
481      */

482     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
483     {
484     }
485 }
486
Popular Tags