KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > editors > attributeType > ATESuperiorComboContentProvider


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 package org.apache.directory.ldapstudio.schemas.view.editors.attributeType;
21
22
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
28 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
29 import org.apache.directory.ldapstudio.schemas.view.editors.NonExistingAttributeType;
30 import org.eclipse.jface.viewers.IStructuredContentProvider;
31 import org.eclipse.jface.viewers.Viewer;
32
33
34 /**
35  * This class implements the Content Provider for the Superior Combo of the Attribute Type Editor.
36  *
37  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
38  * @version $Rev$, $Date$
39  */

40 public class ATESuperiorComboContentProvider implements IStructuredContentProvider
41 {
42     /** The Schema Pool */
43     private SchemaPool schemaPool;
44
45
46     /**
47      * Creates a new instance of ATESuperiorComboContentProvider.
48      */

49     public ATESuperiorComboContentProvider()
50     {
51         schemaPool = SchemaPool.getInstance();
52     }
53
54
55     /* (non-Javadoc)
56      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
57      */

58     public Object JavaDoc[] getElements( Object JavaDoc inputElement )
59     {
60         if ( inputElement instanceof ATESuperiorComboInput )
61         {
62             ATESuperiorComboInput input = ( ATESuperiorComboInput ) inputElement;
63
64             if ( input.getChildren().isEmpty() )
65             {
66                 AttributeType editorAT = input.getAttributeType();
67
68                 // Creating the '(None)' item
69
input.addChild( new NonExistingAttributeType( NonExistingAttributeType.NONE ) );
70
71                 // Creating Children
72
List JavaDoc<AttributeType> ats = schemaPool.getAttributeTypes();
73                 for ( AttributeType at : ats )
74                 {
75                     if ( !isSubType( at, editorAT ) )
76                     {
77                         input.addChild( at );
78                     }
79                 }
80             }
81
82             // Getting Children
83
List JavaDoc<Object JavaDoc> children = input.getChildren();
84
85             // Sorting Children
86
Collections.sort( children, new Comparator JavaDoc<Object JavaDoc>()
87             {
88                 public int compare( Object JavaDoc o1, Object JavaDoc o2 )
89                 {
90                     if ( o1 instanceof AttributeType && o2 instanceof AttributeType )
91                     {
92                         return ( ( AttributeType ) o1 ).getNames()[0].compareToIgnoreCase( ( ( AttributeType ) o2 )
93                             .getNames()[0] );
94                     }
95                     else if ( o1 instanceof AttributeType && o2 instanceof NonExistingAttributeType )
96                     {
97                         return ( ( AttributeType ) o1 ).getNames()[0]
98                             .compareToIgnoreCase( ( ( NonExistingAttributeType ) o2 ).getName() );
99                     }
100                     else if ( o1 instanceof NonExistingAttributeType && o2 instanceof AttributeType )
101                     {
102                         return ( ( NonExistingAttributeType ) o1 ).getName().compareToIgnoreCase(
103                             ( ( AttributeType ) o2 ).getNames()[0] );
104                     }
105                     else if ( o1 instanceof NonExistingAttributeType && o2 instanceof NonExistingAttributeType )
106                     {
107                         return ( ( NonExistingAttributeType ) o1 ).getName().compareToIgnoreCase(
108                             ( ( NonExistingAttributeType ) o2 ).getName() );
109                     }
110
111                     return 0;
112                 }
113             } );
114
115             return children.toArray();
116         }
117
118         // Default
119
return new Object JavaDoc[0];
120     }
121
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
125      */

126     public void dispose()
127     {
128     }
129
130
131     /* (non-Javadoc)
132      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
133      */

134     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
135     {
136     }
137
138
139     /**
140      * Checks id an Attribute Type is a sub type of another Attribute Type.
141      *
142      * @param at1
143      * the first Attribute Type
144      * @param at2
145      * the second Attribute Type
146      * @return
147      * true if at1 is a sub type of at2
148      */

149     private boolean isSubType( AttributeType at1, AttributeType at2 )
150     {
151         if ( at1.equals( at2 ) )
152         {
153             return true;
154         }
155         else
156         {
157             String JavaDoc sup = at1.getSuperior();
158
159             if ( sup == null )
160             {
161                 return false;
162             }
163             else
164             {
165                 AttributeType supAT = schemaPool.getAttributeType( sup );
166                 if ( supAT == null )
167                 {
168                     return false;
169                 }
170                 else
171                 {
172                     return isSubType( supAT, at2 );
173                 }
174             }
175         }
176     }
177 }
178
Popular Tags