KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openidex > search > SearchType


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 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.openidex.search;
22
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import org.openide.ServiceType;
27 import org.openide.nodes.Node;
28 import org.openide.util.Lookup;
29
30
31 /**
32  * Search type is service which provides search functionality on set of nodes.
33  * It has to provide GUI presentation so user can have the possibility to
34  * set/modify criteria.
35  * It performs search according to that.
36  *
37  * @author Peter Zavadsky
38  */

39 public abstract class SearchType extends ServiceType implements Cloneable JavaDoc {
40
41     /** Serial version UID. */ // PENDING How to change this silly number? Can be done by using Utilities.translate
42
static final long serialVersionUID = 1L;
43     
44     /** Name of valid property. */
45     public static final String JavaDoc PROP_VALID = "valid"; // NOI18N
46

47     /** Name of object changed property. */
48     protected static final String JavaDoc PROP_OBJECT_CHANGED = "org.openidex.search.objectChanged"; // NOI18N
49

50     /** Property valid. */
51     private boolean valid;
52
53     
54     /** Class types of object on which this search type is able to search. */
55     private Class JavaDoc[] searchTypeClasses;
56     
57
58     /**
59      * Gets class types of objects this search type can search (test) on.
60      * The classes are used for associating search types working on the same
61      * object types to create <code>SearchGroup</code>.
62      * <em>Note: </em> the order of classes declares also priority.
63      */

64     public synchronized final Class JavaDoc[] getSearchTypeClasses() {
65         if (searchTypeClasses == null) {
66             searchTypeClasses = createSearchTypeClasses();
67         }
68         return searchTypeClasses;
69     }
70
71     /**
72      * Actually creates array of class types of objects this search type can search.
73      * <em>Note: </em> the order of classes declares also priority.
74      */

75     protected abstract Class JavaDoc[] createSearchTypeClasses();
76     
77
78     /**
79      * Accepts search root nodes. Subclasses have a chance to exclude some of
80      * the non interesting node systems. E.g. CVS search type can exclude non
81      * CVS node systems.
82      */

83     protected Node[] acceptSearchRootNodes(Node[] roots) {
84         return roots;
85     }
86
87     /**
88      * Accepts search object to the search. Subclasses have a chance to exclude
89      * the non interesting objects from the search. E.g. Java search type will
90      * exclude non Java data objects.
91      * <em>Note:</em> the search object instance is of the class type
92      * returned by SearchKey.getSearchObjectType method. So there is no necessity
93      * to do additional check for that search type.
94      * @return <code>true</code> */

95     protected boolean acceptSearchObject(Object JavaDoc searchObject) {
96         return true;
97     }
98
99     /**
100      * Prepares search object. Dummy implementation.
101      */

102     protected void prepareSearchObject(Object JavaDoc searchObject) {}
103     
104     /**
105      * Checks whether an object matches the criteria defined in this search
106      * type.
107      *
108      * @param searchObject object to be tested
109      * @return <code>true</code> if the object matches the criteria,
110      * <code>false</code> it it does not
111      */

112     protected abstract boolean testObject(Object JavaDoc searchObject);
113
114     /**
115      * Creates nodes representing matches found within the specified object.
116      * <p>
117      * This is a dummy implementation, subclasses should provide a real
118      * implementation.
119      *
120      * @param resultObject object to create the nodes for
121      * @return <code>null</code> (subclasses should return the created nodes)
122      */

123     public Node[] getDetails(Object JavaDoc resultObject) {
124         return null;
125     }
126     
127     /**
128      * Creates nodes representing matches found withing an object
129      * represented by the specified node.
130      * <p>
131      * This is a dummy implementation, subclasses should provide a real
132      * implementation. The typical implementation is that the node is validated,
133      * an object is extracted from it and passed to method
134      * {@link #getDetails(Object)}.
135      *
136      * @param node node representing object with matches
137      * @return <code>null</code> (subclasses should return the created nodes)
138      * @see #getDetails(Object)
139      */

140     public Node[] getDetails(Node node) {
141         return null;
142     }
143
144     /**
145      * Checks that this search type is able to search the specified set
146      * of nodes.
147      * <p>
148      * This method is usually implemented such that it returns <code>true</code>
149      * if it is possible to search at least one of the nodes.
150      *
151      * @param nodes nodes to be searched
152      * @return <code>true</code> if this search type is able to search
153      * the nodes, <code>false</code> otherwise
154      */

155     public abstract boolean enabled(Node[] nodes);
156     
157     /** Now the custonized criterion changed validity state. */
158     public final void setValid(boolean state) {
159         boolean old = valid;
160         valid = state;
161         firePropertyChange(PROP_VALID, old ? Boolean.TRUE : Boolean.FALSE, state ? Boolean.TRUE : Boolean.FALSE);
162     }
163
164     /** @return true if the criterion is currently valid. */
165     public final boolean isValid() {
166         return valid;
167     }
168     
169     /** Clones seach type. */
170     public Object JavaDoc clone() {
171         try {
172             return super.clone();
173         } catch (CloneNotSupportedException JavaDoc ex) {
174             throw new RuntimeException JavaDoc("SearchType must be cloneable."); // NOI18N
175
}
176     }
177
178
179     /**
180      * Enumeration of all SearchTypes in the system.
181      *
182      * @return enumeration of SearchType instances
183      * @deprecated Please use {@link Lookup} instead.
184      */

185     public static Enumeration JavaDoc enumerateSearchTypes () {
186         return Collections.enumeration(Lookup.getDefault().lookup(new Lookup.Template(SearchType.class)).allInstances());
187     }
188     
189 }
190
Popular Tags