KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.openidex.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.openide.filesystems.FileObject;
25 import org.openide.loaders.DataFolder;
26 import org.openide.loaders.DataObject;
27 import org.openide.loaders.DataObjectNotFoundException;
28 import org.openide.nodes.AbstractNode;
29 import org.openide.nodes.Children;
30 import org.openide.nodes.Node;
31
32 /**
33  * Search group which perform search on file objects. It is a
34  * convenience and the default implementation of <code>SearchGroup</code>
35  * abstract class.
36  *
37  * @author Peter Zavadsky
38  * @author Marian Petras
39  * @see org.openidex.search.SearchGroup
40  */

41 public class FileObjectSearchGroup extends SearchGroup {
42
43     /**
44      * {@inheritDoc} If the specified search type does not support searching
45      * in <code>FileObject</code>s, the group is left unmodified, too.
46      *
47      * @see SearchType#getSearchTypeClasses()
48      */

49     protected void add(SearchType searchType) {
50         boolean ok = false;
51         Class JavaDoc[] classes = searchType.getSearchTypeClasses();
52         for (int i = 0; i < classes.length; i++) {
53             if (classes[i] == FileObject.class) {
54                 ok = true;
55                 break;
56             }
57         }
58         if (ok) {
59             super.add(searchType);
60         }
61     }
62
63     /**
64      * Actuall search implementation. Fires PROP_FOUND notifications.
65      * Implements superclass abstract method. */

66     public void doSearch() {
67         FileObject[] rootFolders = getFileFolders();
68         
69         if (rootFolders == null) {
70             return;
71         }
72         for(int i = 0; i < rootFolders.length; i++) {
73             if (!scanFolder(rootFolders[i])) {
74                 return;
75             }
76         }
77     }
78     
79     /** Gets data folder roots on which to search. */
80     private FileObject[] getFileFolders() {
81         Node[] nodes = normalizeNodes((Node[])searchRoots.toArray(new Node[searchRoots.size()]));
82
83         List JavaDoc children = new ArrayList JavaDoc(nodes.length);
84
85         for (int i = 0; i < nodes.length; i++) {
86             DataFolder dataFolder = (DataFolder) nodes[i].getCookie(DataFolder.class);
87             if (dataFolder != null) {
88                 children.add(dataFolder.getPrimaryFile());
89             }
90         }
91
92         return (FileObject[])children.toArray(new FileObject[children.size()]);
93     }
94     
95     /** Scans data folder recursivelly.
96      * @return <code>true</code> if scanned entire folder successfully
97      * or <code>false</code> if scanning was stopped. */

98     private boolean scanFolder(FileObject folder) {
99         FileObject[] children = folder.getChildren();
100
101         for (int i = 0; i < children.length; i++) {
102             // Test if the search was stopped.
103
if (stopped) {
104                 stopped = true;
105                 return false;
106             }
107             
108             if (children[i].isFolder()) {
109                 if (!scanFolder(children[i])) {
110                     return false;
111                 }
112             } else {
113                 processSearchObject(children[i]);
114             }
115         }
116
117         return true;
118     }
119
120
121     /** Gets node for found object. Implements superclass method.
122      * @return node delegate for found data object or <code>null</code>
123      * if the object is not of <code>DataObjectType</code> */

124     public Node getNodeForFoundObject(final Object JavaDoc object) {
125         if (!(object instanceof FileObject)) {
126             return null;
127         }
128         try {
129             return DataObject.find((FileObject) object).getNodeDelegate();
130         } catch (DataObjectNotFoundException dnfe) {
131             return new AbstractNode(Children.LEAF) {
132                 public String JavaDoc getName() {
133                     return ((FileObject) object).getName();
134                 }
135             };
136         }
137     }
138       
139     
140
141     /** Removes kids from node array. Helper method. */
142     private static Node[] normalizeNodes(Node[] nodes) {
143
144         List JavaDoc ret = new ArrayList JavaDoc();
145
146         for (int i = 0; i<nodes.length; i++) {
147             if (!hasParent(nodes[i], nodes)) {
148                 ret.add(nodes[i]);
149             }
150         }
151
152         return (Node[]) ret.toArray(new Node[ret.size()]);
153     }
154
155     /** Tests if the node has parent. Helper method. */
156     private static boolean hasParent(Node node, Node[] nodes) {
157         for (Node parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) {
158             for (int i = 0; i<nodes.length; i++) {
159                 if (nodes[i].equals(parent)) {
160                     return true;
161                 }
162             }
163         }
164         return false;
165     }
166     
167 }
168
Popular Tags