KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > dialogs > ObjectClassSelectionDialogContentProvider


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.dialogs;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30
31 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
32 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
34 import org.eclipse.jface.viewers.IStructuredContentProvider;
35 import org.eclipse.jface.viewers.Viewer;
36
37
38 /**
39  * This class is the Content Provider for the Object Class Selection Dialog.
40  *
41  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
42  * @version $Rev$, $Date$
43  */

44 public class ObjectClassSelectionDialogContentProvider implements IStructuredContentProvider
45 {
46     /** The Schema Pool */
47     private SchemaPool schemaPool;
48
49     /** The hidden Object Classes */
50     private List JavaDoc<ObjectClass> hiddenObjectClasses;
51
52
53     /**
54      * Creates a new instance of ObjectClassSelectionDialogContentProvider.
55      */

56     public ObjectClassSelectionDialogContentProvider( List JavaDoc<ObjectClass> hiddenObjectClasses )
57     {
58         schemaPool = SchemaPool.getInstance();
59         this.hiddenObjectClasses = hiddenObjectClasses;
60     }
61
62
63     /* (non-Javadoc)
64      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
65      */

66     public Object JavaDoc[] getElements( Object JavaDoc inputElement )
67     {
68         if ( inputElement instanceof String JavaDoc )
69         {
70             ArrayList JavaDoc<ObjectClassWrapper> results = new ArrayList JavaDoc<ObjectClassWrapper>();
71
72             String JavaDoc searchText = ( String JavaDoc ) inputElement;
73
74             String JavaDoc searchRegexp;
75             if ( searchText.length() == 0 )
76             {
77                 searchRegexp = ".*"; //$NON-NLS-1$
78
}
79             else
80             {
81                 searchRegexp = searchText + ".*"; //$NON-NLS-1$
82
}
83             Pattern JavaDoc pattern = Pattern.compile( searchRegexp, Pattern.CASE_INSENSITIVE );
84
85             List JavaDoc<ObjectClass> ocList = schemaPool.getObjectClasses();
86
87             // Sorting the list
88
Collections.sort( ocList, new Comparator JavaDoc<ObjectClass>()
89             {
90                 public int compare( ObjectClass oc1, ObjectClass oc2 )
91                 {
92                     if ( ( oc1.getNames() == null || oc1.getNames().length == 0 )
93                         && ( oc2.getNames() == null || oc2.getNames().length == 0 ) )
94                     {
95                         return 0;
96                     }
97                     else if ( ( oc1.getNames() == null || oc1.getNames().length == 0 )
98                         && ( oc2.getNames() != null && oc2.getNames().length > 0 ) )
99                     {
100                         return "".compareToIgnoreCase( oc2.getNames()[0] ); //$NON-NLS-1$
101
}
102                     else if ( ( oc1.getNames() != null && oc1.getNames().length > 0 )
103                         && ( oc2.getNames() == null || oc2.getNames().length == 0 ) )
104                     {
105                         return oc1.getNames()[0].compareToIgnoreCase( "" ); //$NON-NLS-1$
106
}
107                     else
108                     {
109                         return oc1.getNames()[0].compareToIgnoreCase( oc2.getNames()[0] );
110                     }
111                 }
112             } );
113
114             // Searching for all matching elements
115
for ( ObjectClass oc : ocList )
116             {
117                 for ( String JavaDoc name : oc.getNames() )
118                 {
119                     Matcher JavaDoc m = pattern.matcher( name );
120                     if ( m.matches() )
121                     {
122                         if ( !hiddenObjectClasses.contains( oc ) )
123                         {
124                             ObjectClassWrapper ocw = new ObjectClassWrapper( oc, null );
125                             if ( !results.contains( ocw ) )
126                             {
127                                 results.add( new ObjectClassWrapper( oc, null ) );
128                             }
129                         }
130                         break;
131                     }
132                 }
133                 Matcher JavaDoc m = pattern.matcher( oc.getOid() );
134                 if ( m.matches() )
135                 {
136                     if ( !hiddenObjectClasses.contains( oc ) )
137                     {
138                         ObjectClassWrapper ocw = new ObjectClassWrapper( oc, null );
139                         if ( !results.contains( ocw ) )
140                         {
141                             results.add( new ObjectClassWrapper( oc, null ) );
142                         }
143                     }
144                 }
145             }
146
147             // Returns the results
148
return results.toArray();
149         }
150
151         // Default
152
return new Object JavaDoc[0];
153     }
154
155
156     /* (non-Javadoc)
157      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
158      */

159     public void dispose()
160     {
161     }
162
163
164     /* (non-Javadoc)
165      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
166      */

167     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
168     {
169     }
170 }
171
Popular Tags