001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openidex.search;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import org.openide.filesystems.FileObject;
047: import org.openide.loaders.DataFolder;
048: import org.openide.loaders.DataObject;
049: import org.openide.loaders.DataObjectNotFoundException;
050: import org.openide.nodes.AbstractNode;
051: import org.openide.nodes.Children;
052: import org.openide.nodes.Node;
053:
054: /**
055: * Search group which perform search on file objects. It is a
056: * convenience and the default implementation of <code>SearchGroup</code>
057: * abstract class.
058: *
059: * @author Peter Zavadsky
060: * @author Marian Petras
061: * @see org.openidex.search.SearchGroup
062: */
063: public class FileObjectSearchGroup extends SearchGroup {
064:
065: /**
066: * {@inheritDoc} If the specified search type does not support searching
067: * in <code>FileObject</code>s, the group is left unmodified, too.
068: *
069: * @see SearchType#getSearchTypeClasses()
070: */
071: @Override
072: protected void add(SearchType searchType) {
073: boolean ok = false;
074: for (Class clazz : searchType.getSearchTypeClasses()) {
075: if (clazz == FileObject.class) {
076: ok = true;
077: break;
078: }
079: }
080: if (ok) {
081: super .add(searchType);
082: }
083: }
084:
085: /**
086: * Actuall search implementation. Fires PROP_FOUND notifications.
087: * Implements superclass abstract method. */
088: public void doSearch() {
089: FileObject[] rootFolders = getFileFolders();
090:
091: if (rootFolders == null) {
092: return;
093: }
094: for (FileObject rootFolder : rootFolders) {
095: if (!scanFolder(rootFolder)) {
096: return;
097: }
098: }
099: }
100:
101: /** Gets data folder roots on which to search. */
102: private FileObject[] getFileFolders() {
103: Node[] nodes = normalizeNodes(searchRoots
104: .toArray(new Node[searchRoots.size()]));
105:
106: List<FileObject> children = new ArrayList<FileObject>(
107: nodes.length);
108:
109: for (Node node : nodes) {
110: DataFolder dataFolder = node.getCookie(DataFolder.class);
111: if (dataFolder != null) {
112: children.add(dataFolder.getPrimaryFile());
113: }
114: }
115:
116: return children.toArray(new FileObject[children.size()]);
117: }
118:
119: /** Scans data folder recursivelly.
120: * @return <code>true</code> if scanned entire folder successfully
121: * or <code>false</code> if scanning was stopped. */
122: private boolean scanFolder(FileObject folder) {
123: for (FileObject child : folder.getChildren()) {
124: // Test if the search was stopped.
125: if (stopped) {
126: stopped = true;
127: return false;
128: }
129:
130: if (child.isFolder()) {
131: if (!scanFolder(child)) {
132: return false;
133: }
134: } else {
135: processSearchObject(child);
136: }
137: }
138:
139: return true;
140: }
141:
142: /** Gets node for found object. Implements superclass method.
143: * @return node delegate for found data object or <code>null</code>
144: * if the object is not of <code>DataObjectType</code> */
145: public Node getNodeForFoundObject(final Object object) {
146: if (!(object instanceof FileObject)) {
147: return null;
148: }
149: try {
150: return DataObject.find((FileObject) object)
151: .getNodeDelegate();
152: } catch (DataObjectNotFoundException dnfe) {
153: return new AbstractNode(Children.LEAF) {
154: @Override
155: public String getName() {
156: return ((FileObject) object).getName();
157: }
158: };
159: }
160: }
161:
162: /** Removes kids from node array. Helper method. */
163: private static Node[] normalizeNodes(Node[] nodes) {
164:
165: List<Node> ret = new ArrayList<Node>();
166:
167: for (Node node : nodes) {
168: if (!hasParent(node, nodes)) {
169: ret.add(node);
170: }
171: }
172:
173: return ret.toArray(new Node[ret.size()]);
174: }
175:
176: /** Tests if the node has parent. Helper method. */
177: private static boolean hasParent(Node node, Node[] nodes) {
178: for (Node parent = node.getParentNode(); parent != null; parent = parent
179: .getParentNode()) {
180: for (Node n : nodes) {
181: if (n.equals(parent)) {
182: return true;
183: }
184: }
185: }
186: return false;
187: }
188:
189: }
|