alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

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


package org.netbeans.modules.search;


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.openide.nodes.Node;
import org.openide.util.WeakListeners;
import org.openidex.search.SearchGroup;
import org.openidex.search.SearchType;


/**
 * Task performing search.
 *
 * @author  Peter Zavadsky
 * @author  Marian Petras
 */
final class SearchTask implements Runnable {

    /** nodes to search */
    private final Node[] nodes;
    /** */
    private final SearchType[] customizedSearchTypes;
    /** */
    private final List searchTypeList;
    /** ResultModel result model. */
    private ResultModel resultModel;
    /** SearchGroup to search on. */
    private SearchGroup searchGroup;
    /**
     * listener which listens for the search group's notifications of found
     * objects
     */
    private PropertyChangeListener searchGroupListener;
    /** attribute used by class Manager */
    private boolean notifyWhenFinished = true;
    /** */
    private volatile boolean interrupted = false;
    /** */
    private volatile boolean finished = false;
    
    
    /**
     * Creates a new SearchTask.
     *
     * @param 
     * @param 
     * @param 
     */
    public SearchTask(final Node[] nodes,
                      final List searchTypeList,
                      final SearchType[] customizedSearchTypes) {
        this.nodes = nodes;
        this.searchTypeList = searchTypeList;
        this.customizedSearchTypes = customizedSearchTypes;
    }

    
    /** Runs the search task. */
    public void run() {
        /* Start the actual search: */
        ensureResultModelExists();
        if (searchGroup == null) {
            return;
        }

        //Set of search types to be used able to search on the same object type.
        searchGroup.addPropertyChangeListener(WeakListeners.propertyChange(
            searchGroupListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if (SearchGroup.PROP_FOUND.equals(evt.getPropertyName())) {
                        matchingObjectFound(evt.getNewValue());
                    }
                }
            }, searchGroup)
        );

        searchGroup.setSearchRootNodes(nodes);
        try {
            searchGroup.search();
        } catch (RuntimeException ex) {
            resultModel.searchException(ex);
        }
        finished = true;
    }

    /**
     */
    ResultModel getResultModel() {
        ensureResultModelExists();
        return resultModel;
    }
    
    /**
     */
    private void ensureResultModelExists() {
        if (resultModel == null) {
            SearchGroup[] groups
                    = SearchGroup.createSearchGroups(customizedSearchTypes);
            searchGroup = (groups.length != 0) ? groups[0] : null;
            resultModel = new ResultModel(searchTypeList, searchGroup);
        }
    }

    /**
     * Called when a matching object is found by the SearchGroup.
     * Notifies the result model of the found object and stops searching
     * if number of the found objects reached the limit.
     *
     * @param  object  found matching object
     */
    private void matchingObjectFound(Object object) {
        boolean canContinue = resultModel.objectFound(object);
        if (!canContinue) {
            searchGroup.stopSearch();
        }
    }
    
    /**
     * Stops this search task.
     * This method also sets a value of attribute
     * notifyWhenFinished. This method may be called multiple
     * times (even if this task is already stopped) to change the value
     * of the attribute.
     *
     * @param  notifyWhenFinished  new value of attribute
     *                             notifyWhenFinished
     */
    void stop(boolean notifyWhenFinished) {
        if (notifyWhenFinished == false) {     //allow only change true -> false
            this.notifyWhenFinished = notifyWhenFinished;
        }
        stop();
    }
    
    /**
     * Stops this search task.
     *
     * @see  #stop(boolean)
     */
    void stop() {
        if (!finished) {
            interrupted = true;
        }
        if (searchGroup != null) {
            searchGroup.stopSearch();
        }
    }
    
    /**
     * Returns value of attribute notifyWhenFinished.
     *
     * @return  current value of the attribute
     */
    boolean notifyWhenFinished() {
        return notifyWhenFinished;
    }
    
    /**
     * Was this search task interrupted?
     *
     * @return  true if this method has been interrupted
     *          by calling {@link #stop()} or {@link #stop(boolean)}
     *          during the search; false otherwise
     */
    boolean wasInterrupted() {
        return interrupted;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.