KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.schemas.Activator;
28 import org.apache.directory.ldapstudio.schemas.PluginConstants;
29 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
30 import org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent;
31 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
32 import org.apache.directory.ldapstudio.schemas.model.PoolListener;
33 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
34 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
35 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode;
36 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
37 import org.eclipse.jface.viewers.IStructuredContentProvider;
38 import org.eclipse.jface.viewers.ITreeContentProvider;
39 import org.eclipse.jface.viewers.TreeViewer;
40 import org.eclipse.jface.viewers.Viewer;
41
42
43 /**
44  * This class implements the Content Provider for the Schemas View.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class HierarchyViewContentProvider implements IStructuredContentProvider, ITreeContentProvider, PoolListener
50 {
51     /** The Schema Pool */
52     private SchemaPool schemaPool;
53
54     /** The associated viewer */
55     private TreeViewer viewer;
56
57     List JavaDoc<AttributeType> attributeTypes;
58     List JavaDoc<ObjectClass> objectClasses;
59
60
61     /**
62      * Default constructor
63      */

64     public HierarchyViewContentProvider( TreeViewer viewer )
65     {
66         this.viewer = viewer;
67         schemaPool = SchemaPool.getInstance();
68         schemaPool.addListener( this );
69         attributeTypes = schemaPool.getAttributeTypes();
70         objectClasses = schemaPool.getObjectClasses();
71     }
72
73
74     /* (non-Javadoc)
75      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
76      */

77     public Object JavaDoc[] getElements( Object JavaDoc inputElement )
78     {
79         return getChildren( inputElement );
80     }
81
82
83     /* (non-Javadoc)
84      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
85      */

86     public Object JavaDoc[] getChildren( Object JavaDoc parentElement )
87     {
88         int mode = Activator.getDefault().getDialogSettings().getInt( PluginConstants.PREFS_HIERARCHY_VIEW_MODE );
89         List JavaDoc<ITreeNode> children = null;
90
91         if ( parentElement instanceof ObjectClass )
92         {
93             ObjectClass oc = ( ObjectClass ) parentElement;
94
95             ObjectClassWrapper ocw = new ObjectClassWrapper( oc, null );
96             children = new ArrayList JavaDoc<ITreeNode>();
97             children.add( ocw );
98         }
99         else if ( parentElement instanceof AttributeType )
100         {
101             AttributeType at = ( AttributeType ) parentElement;
102
103             AttributeTypeWrapper atw = new AttributeTypeWrapper( at, null );
104             children = new ArrayList JavaDoc<ITreeNode>();
105             children.add( atw );
106         }
107         else if ( parentElement instanceof ObjectClassWrapper )
108         {
109             ObjectClassWrapper ocw = ( ObjectClassWrapper ) parentElement;
110             ObjectClass oc = ocw.getMyObjectClass();
111
112             if ( mode == PluginConstants.PREFS_HIERARCHY_VIEW_MODE_SUPERTYPE )
113             {
114                 String JavaDoc[] superiors = oc.getSuperiors();
115                 for ( String JavaDoc superior : superiors )
116                 {
117                     if ( superior != null || "".equals( superior ) ) //$NON-NLS-1$
118
{
119                         ObjectClass supOC = schemaPool.getObjectClass( superior );
120                         if ( supOC != null )
121                         {
122                             ObjectClassWrapper supOCW = new ObjectClassWrapper( supOC, ocw );
123                             ocw.addChild( supOCW );
124                         }
125                     }
126                 }
127             }
128             else if ( mode == PluginConstants.PREFS_HIERARCHY_VIEW_MODE_SUBTYPE )
129             {
130                 for ( ObjectClass objectClass : objectClasses )
131                 {
132                     String JavaDoc[] superiors = objectClass.getSuperiors();
133                     for ( String JavaDoc superior : superiors )
134                     {
135                         if ( superior != null || "".equals( superior ) ) //$NON-NLS-1$
136
{
137                             ObjectClass supOC = schemaPool.getObjectClass( superior );
138                             if ( supOC != null && oc.equals( supOC ) )
139                             {
140                                 ObjectClassWrapper supOCW = new ObjectClassWrapper( objectClass, ocw );
141                                 ocw.addChild( supOCW );
142                             }
143                         }
144                     }
145                 }
146             }
147
148             children = ocw.getChildren();
149         }
150         else if ( parentElement instanceof AttributeTypeWrapper )
151         {
152             AttributeTypeWrapper atw = ( AttributeTypeWrapper ) parentElement;
153             AttributeType at = atw.getMyAttributeType();
154
155             if ( mode == PluginConstants.PREFS_HIERARCHY_VIEW_MODE_SUPERTYPE )
156             {
157                 String JavaDoc superior = at.getSuperior();
158                 if ( superior != null || "".equals( superior ) ) //$NON-NLS-1$
159
{
160                     AttributeType supAT = schemaPool.getAttributeType( superior );
161                     if ( supAT != null )
162                     {
163                         AttributeTypeWrapper supATW = new AttributeTypeWrapper( supAT, atw );
164                         atw.addChild( supATW );
165                     }
166                 }
167             }
168             else if ( mode == PluginConstants.PREFS_HIERARCHY_VIEW_MODE_SUBTYPE )
169             {
170                 for ( AttributeType attributeType : attributeTypes )
171                 {
172                     String JavaDoc superior = attributeType.getSuperior();
173                     if ( superior != null && !"".equals( superior ) ) //$NON-NLS-1$
174
{
175                         AttributeType supAT = schemaPool.getAttributeType( superior );
176                         if ( supAT != null && at.equals( supAT ) )
177                         {
178                             AttributeTypeWrapper supATW = new AttributeTypeWrapper( attributeType, atw );
179                             atw.addChild( supATW );
180                         }
181                     }
182                 }
183             }
184
185             children = atw.getChildren();
186         }
187
188         if ( children == null )
189         {
190             return new Object JavaDoc[0];
191         }
192         else
193         {
194             return children.toArray();
195         }
196     }
197
198
199     /* (non-Javadoc)
200      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
201      */

202     public Object JavaDoc getParent( Object JavaDoc element )
203     {
204         if ( element instanceof ITreeNode )
205         {
206             return ( ( ITreeNode ) element ).getParent();
207         }
208
209         // Default
210
return null;
211     }
212
213
214     /* (non-Javadoc)
215      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
216      */

217     public boolean hasChildren( Object JavaDoc element )
218     {
219         if ( element instanceof ITreeNode )
220         {
221             return getChildren( element ).length > 0;
222         }
223
224         // Default
225
return false;
226     }
227
228
229     /* (non-Javadoc)
230      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
231      */

232     public void dispose()
233     {
234     }
235
236
237     /* (non-Javadoc)
238      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
239      */

240     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
241     {
242     }
243
244
245     /* (non-Javadoc)
246      * @see org.apache.directory.ldapstudio.schemas.model.PoolListener#poolChanged(org.apache.directory.ldapstudio.schemas.model.SchemaPool, org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent)
247      */

248     public void poolChanged( SchemaPool p, LDAPModelEvent e )
249     {
250         viewer.refresh();
251         viewer.expandAll();
252     }
253 }
254
Popular Tags