KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2003 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.BorderFactory JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.border.Border JavaDoc;
32 import org.openide.util.Lookup;
33 import org.openidex.search.SearchType;
34
35 /**
36  *
37  * @author Marian Petras
38  */

39 final class Utils {
40
41     /**
42      * result of lookup for registered search types
43      *
44      * @see #getSearchTypes
45      */

46     private static Lookup.Result result;
47
48     private Utils() { }
49     
50     /**
51      * Finds all registered instances of class <code>SearchType</code>.
52      * <p>
53      * When this method is called for the first time, a lookup is performed
54      * and its result stored. Subsequent calls return the remembered result.
55      *
56      * @return result of lookup for instances of class <code>SearchType</code>
57      * @see SearchType
58      */

59     private static Lookup.Result getSearchTypes0() {
60         if (result == null) {
61             result = Lookup.getDefault().lookup(
62                     new Lookup.Template(SearchType.class));
63         }
64         return result;
65     }
66     
67     /**
68      * Returns a list of all registered search types.
69      *
70      * @return all instances of {@link SearchType} available via
71      * {@link Lookup}
72      */

73     static Collection JavaDoc getSearchTypes() {
74         return getSearchTypes0().allInstances();
75     }
76     
77     /**
78      * Returns a subclass of <code>SearchType</code>, having the specified name.
79      * A search is performed through all registered instances of
80      * <code>SearchType</code> (in a {@link Lookup Lookup}).
81      *
82      * @param className class name of the requested search type
83      * @return subclass of <code>SearchType</code>, having the specified name;
84      * or <code>null</code> is none was found
85      * @see SearchType
86      */

87     static Class JavaDoc searchTypeForName(String JavaDoc className) {
88         Set JavaDoc allClasses = getSearchTypes0().allClasses();
89         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext(); ) {
90             Class JavaDoc c = (Class JavaDoc) i.next();
91             if (c.getName().equals(className)) {
92                 return c;
93             }
94         }
95         return null;
96     }
97     
98     /**
99      * Sorts search criteria by search types. Constructs a table which contains
100      * pairs
101      * <blockquote>
102      * (search type, saved criteria for the search type)
103      * </blockquote>
104      *
105      * @param criteria criteria to sort
106      * @return map with search type class names as keys and collections of
107      * search criteria as values; or <code>null</code> if no search
108      * criterion is saved
109      */

110     static Map JavaDoc sortCriteriaBySearchType(SearchCriterion[] criteria) {
111         
112         if (criteria == null || criteria.length == 0) {
113             return null;
114         }
115         
116         Map JavaDoc map = new HashMap JavaDoc(6, 0.75f);
117         for (int i = 0; i < criteria.length; i++) {
118             SearchCriterion c = criteria[i];
119             String JavaDoc className = c.searchTypeClassName;
120             Collection JavaDoc criteriaOfType;
121             Object JavaDoc o = map.get(className);
122             if (o == null) {
123                 criteriaOfType = new ArrayList JavaDoc(4);
124                 criteriaOfType.add(c);
125                 map.put(className, criteriaOfType);
126             } else {
127                 criteriaOfType = (Collection JavaDoc) o;
128                 criteriaOfType.add(c);
129             }
130         }
131         return map;
132     }
133     
134     /**
135      * Returns a border for explorer views.
136      *
137      * @return border to be used around explorer views
138      * (<code>BeanTreeView</code>, <code>TreeTableView</code>,
139      * <code>ListView</code>).
140      */

141     static final Border JavaDoc getExplorerViewBorder() {
142         Border JavaDoc border;
143         border = (Border JavaDoc) UIManager.get("Nb.ScrollPane.border"); //NOI18N
144
if (border == null) {
145             border = BorderFactory.createEtchedBorder();
146         }
147         return border;
148     }
149     
150     /**
151      * Clones a list of <code>SearchType</code>s.
152      *
153      * @param searchTypes list of search types to be cloned
154      * @return deep copy of the given list of <code>SearchTypes</code>s
155      */

156     static List JavaDoc cloneSearchTypes(List JavaDoc searchTypes) {
157         List JavaDoc clonedSearchTypes = new ArrayList JavaDoc(searchTypes.size());
158         for (Iterator JavaDoc it = searchTypes.iterator(); it.hasNext(); ) {
159             clonedSearchTypes.add(((SearchType) it.next()).clone());
160         }
161         return clonedSearchTypes;
162     }
163     
164     
165 }
166
Popular Tags