/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openidex.search; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.netbeans.api.queries.SharabilityQuery; import org.netbeans.api.queries.VisibilityQuery; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.loaders.DataFolder; import org.openide.nodes.Node; /** * Factory for creating SearchInfo objects. * * @author Marian Petras */ public final class SearchInfoFactory { /** */ private SearchInfoFactory() {} /** * Creates a SearchInfo object for a given folder. * The returned SearchInfo object's method * {@link SearchInfo#canSearch()} always returns true * and iterates through DataObjects found in the given * folder. Non-sharable and/or non-visible DataObjects * may be skipped, according to enabled filters. * * @param folder folder which should be searched for * DataObjects * @param recursive whether the folder's subfolders should be taken * into account * @param checkSharability whether the sharability filter should be used * @param checkVisibility whether the visibility filter should be used * @return SearchInfo object which iterates through * DataObjects found in the specified folder * and (optionally) its subfolders * @see org.netbeans.api.queries.SharabilityQuery * @see org.netbeans.api.queries.VisibilityQuery */ public static SearchInfo createSearchInfo( FileObject folder, boolean recursive, boolean checkSharability, boolean checkVisibility) { if (!folder.isFolder()) { throw new IllegalArgumentException("folder expected"); //NOI18N } DataFolder dataFolder = DataFolder.findFolder(folder); if (checkSharability || checkVisibility) { List filterList = new ArrayList(2); if (checkVisibility) { filterList.add(VisibilityFilter.INSTANCE); } if (checkSharability) { filterList.add(SharabilityFilter.INSTANCE); } return new SimpleSearchInfo(dataFolder, recursive, filterList); } else { return new SimpleSearchInfo(dataFolder, recursive, null); } } /** * Creates a SearchInfo object for given folders. * The returned SearchInfo object's method * {@link SearchInfo#canSearch()} always returns true * and iterates through DataObjects found in the given * folders. Non-sharable and/or non-visible DataObjects * may be skipped, according to enabled filters. * * @param folders folders which should be searched for * DataObjects * @param recursive whether the folders' subfolders should be taken * into account * @param checkSharability whether the sharability filter should be used * @param checkVisibility whether the visibility filter should be used * @return SearchInfo object which iterates through * DataObjects found in the specified folders * and (optionally) their subfolders * @see org.netbeans.api.queries.SharabilityQuery * @see org.netbeans.api.queries.VisibilityQuery */ public static SearchInfo createSearchInfo( final FileObject[] folders, final boolean recursive, final boolean checkSharability, final boolean checkVisibility) { if (folders.length == 0) { return SimpleSearchInfo.EMPTY_SEARCH_INFO; } if (folders.length == 1) { return createSearchInfo(folders[0], recursive, checkSharability, checkVisibility); } for (int i = 0; i < folders.length; i++) { if (!folders[i].isFolder()) { throw new IllegalArgumentException( "folder expected (index " + i + ')'); //NOI18N } } SearchInfo[] nested = new SearchInfo[folders.length]; for (int i = 0; i < folders.length; i++) { nested[i] = createSearchInfo(folders[i], recursive, checkSharability, checkVisibility); } return new CompoundSearchInfo(nested); } /** * Creates a SearchInfo object combining * SearchInfo objects returned by the node's subnodes. * * Method {@link SearchInfo#canSearch()} of the resulting * SearchInfo objects returns true if and only if * at least one of the nodes is searchable * (its method canSearch() returns true). * The iterator iterates through all DataObjects returned * by the subnode's SearchInfo iterators. * * @param node node to create SearchInfo for * @return SearchInfo object representing combination * of SearchInfo objects of the node's subnodes */ public static SearchInfo createSearchInfoBySubnodes(Node node) { return new SubnodesSearchInfo(node); } }