KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30
31 import org.apache.directory.ldapstudio.schemas.Messages;
32 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
33 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
34 import org.apache.directory.ldapstudio.schemas.model.SchemaElement;
35 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
36 import org.eclipse.jface.viewers.IStructuredContentProvider;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.ui.PlatformUI;
39
40
41 /**
42  * Content provider for the search view.
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class SearchViewContentProvider implements IStructuredContentProvider/*, PoolListener*/
48 {
49     /** The object classes Map */
50     private Map JavaDoc<String JavaDoc, ObjectClass> objectClassTable;
51
52     /** The attribute types Map*/
53     private Map JavaDoc<String JavaDoc, AttributeType> attributeTypeTable;
54
55
56     /**
57      * Creates a new instance of SearchViewContentProvider.
58      */

59     public SearchViewContentProvider()
60     {
61         SchemaPool schemaPool = SchemaPool.getInstance();
62         objectClassTable = schemaPool.getObjectClassesAsMap();
63         attributeTypeTable = schemaPool.getAttributeTypesAsMap();
64     }
65
66
67     /* (non-Javadoc)
68      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
69      */

70     public Object JavaDoc[] getElements( Object JavaDoc parent )
71     {
72         if ( parent instanceof String JavaDoc )
73         {
74             String JavaDoc searchText = ( String JavaDoc ) parent;
75
76             //reset the view title
77
SearchView view = ( SearchView ) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
78                 .findView( SearchView.ID );
79             view.setPartName( Messages.getString( "SearchContentProvider.Search" ) ); //$NON-NLS-1$
80

81             if ( searchText.length() > 0 )
82             {
83                 String JavaDoc searchRegexp = searchText + ".*"; //$NON-NLS-1$
84
Pattern JavaDoc pattern = Pattern.compile( searchRegexp, Pattern.CASE_INSENSITIVE );
85                 List JavaDoc<SchemaElement> resultsList = new ArrayList JavaDoc<SchemaElement>();
86
87                 Collection JavaDoc<ObjectClass> OCs = objectClassTable.values();
88                 Collection JavaDoc<AttributeType> ATs = attributeTypeTable.values();
89
90                 List JavaDoc<SchemaElement> allList = new ArrayList JavaDoc<SchemaElement>();
91                 allList.addAll( OCs );
92                 allList.addAll( ATs );
93
94                 //search for all matching elements
95
for ( SchemaElement element : allList )
96                 {
97
98                     if ( SearchView.currentSearchScope.equals( SearchView.SEARCH_NAME )
99                         || SearchView.currentSearchScope.equals( SearchView.SEARCH_ALL ) )
100                     {
101                         String JavaDoc[] names = element.getNames();
102                         for ( String JavaDoc name : names )
103                         {
104                             Matcher JavaDoc m = pattern.matcher( name );
105                             if ( m.matches() )
106                             {
107                                 if ( !resultsList.contains( element ) )
108                                 {
109                                     resultsList.add( element );
110                                 }
111                                 break;
112                             }
113                         }
114                     }
115
116                     if ( SearchView.currentSearchScope.equals( SearchView.SEARCH_OID )
117                         || SearchView.currentSearchScope.equals( SearchView.SEARCH_ALL ) )
118                     {
119                         String JavaDoc oid = element.getOid();
120                         Matcher JavaDoc m = pattern.matcher( oid );
121                         if ( m.matches() )
122                         {
123                             if ( !resultsList.contains( element ) )
124                             {
125                                 resultsList.add( element );
126                             }
127
128                         }
129                     }
130
131                     if ( SearchView.currentSearchScope.equals( SearchView.SEARCH_DESC )
132                         || SearchView.currentSearchScope.equals( SearchView.SEARCH_ALL ) )
133                     {
134                         String JavaDoc desc = element.getDescription();
135                         if ( desc == null )
136                             continue;
137                         Matcher JavaDoc m = pattern.matcher( desc );
138                         if ( m.matches() )
139                         {
140                             if ( !resultsList.contains( element ) )
141                             {
142                                 resultsList.add( element );
143                             }
144                         }
145                     }
146                 }
147
148                 //change the number of results in the view
149
view
150                     .setPartName( Messages.getString( "SearchContentProvider.Search_(" ) + resultsList.size() + Messages.getString( "SearchContentProvider.Results)" ) ); //$NON-NLS-1$ //$NON-NLS-2$
151

152                 //returns the result list
153
return resultsList.toArray();
154             }
155         }
156
157         return new Object JavaDoc[0];
158     }
159
160
161     /* (non-Javadoc)
162      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
163      */

164     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
165     {
166     }
167
168
169     /* (non-Javadoc)
170      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
171      */

172     public void dispose()
173     {
174     }
175 }
176
Popular Tags