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.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.openide.actions.FindAction;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.actions.ActionPerformer;
import org.openide.util.actions.SystemAction;
import org.openide.windows.TopComponent;
import org.openidex.search.SearchInfo;
import org.openidex.search.SearchType;


/**
 * FindAction performer providing search dialog.
 * This classes is hooked on search action.
 *
 * @author  Petr Kuzel
 * @see SearchHook
 * @see org.openide.actions.FindAction
 */
public class SearchPerformer implements ActionPerformer {
    
    /** singleton of SearchPerfomer */
    private static SearchPerformer searchPerformer;

    /**
     * Gets singleton of SearchPerfomer.
     *
     * @return  singleton of this class
     */
    public static synchronized SearchPerformer getDefault() {
        if (searchPerformer == null) {
            searchPerformer = new SearchPerformer();
        }
        return searchPerformer;
    }
    
    /**
     * Collects a list of search types that are able to search the specified
     * nodes.
     *
     * @param  nodes  nodes to be searched
     * @return  list of SearchTypes being able to search
     *          the specified nodes, or an empty list if nodes
     *          is null or empty; or if no applicable search type
     *          is found for the specified set of nodes
     * @see  Utils#getSearchTypes()
     * @see  SearchType#enabled
     */
    static List getTypes(Node[] nodes) {
        if (nodes == null || nodes.length == 0) {
            return Collections.EMPTY_LIST;
        }
        Iterator it = Utils.getSearchTypes().iterator();
        if (!it.hasNext()) {
            return Collections.EMPTY_LIST;
        }
        List result = new ArrayList(5);
        do {
            SearchType searchType = (SearchType) it.next();
            if (searchType.enabled(nodes) && !result.contains(searchType)) {
                result.add(searchType);
            }
        } while (it.hasNext());
        return result;
    }

    /**
     * Decides whether searching should be enabled with respect to a set
     * of selected nodes.
     * Searching is enabled if searching instructions
     * (SearchInfo object) are available for all selected nodes
     * and at least one registered search type is able to search all the
     * selected nodes.
     *
     * @param  nodes  selected nodes
     * @return  true if searching the selected nodes should be
     *          enabled; false otherwise
     * @see  SearchInfo
     * @see  SearchType
     */
    public boolean enabled(Node[] nodes) {
        if (nodes == null || nodes.length == 0) {
            return false;
        }

        for (int i = 0; i < nodes.length; i++) {
            if (!canSearch(nodes[i])) {
                return false;
            }
        }

        Iterator it = Utils.getSearchTypes().iterator();
        
        while (it.hasNext()) {
            SearchType searchType = (SearchType) it.next();

            if (searchType.enabled(nodes)) {
                return true;
            }
        }

        return false;
    }

    /**
     */
    private static boolean canSearch(Node node) {
        Lookup nodeLookup = node.getLookup();
        
        /* 1st try - is the SearchInfo object in the node's lookup? */
        SearchInfo searchInfo = (SearchInfo)
                                nodeLookup.lookup(SearchInfo.class);
        if (searchInfo != null) {
            return searchInfo.canSearch();
        }
    
        /* 2nd try - does the node represent a DataObject.Container? */
        return nodeLookup.lookup(DataObject.Container.class) != null;
    }

    /**
     * Performs search on nodes. Displays CriteriaView with predefined search types set.
     *
     * @param nodes currently selected nodes
     * @see CriteriaView
     */
    public void performAction(Node[] nodes) {

        /* Get a list of applicable search types: */
        List searchTypeList = getTypes(nodes);
        if (searchTypeList.isEmpty()) {
            return;
        }
        
        /* Clone the list (deep copy): */
        List clonedSearchTypeList = new ArrayList(searchTypeList.size());
        for (Iterator it = searchTypeList.iterator(); it.hasNext(); ) {
            clonedSearchTypeList.add(((SearchType) it.next()).clone());
        }
        
        SearchPanel searchPanel = new SearchPanel(clonedSearchTypeList);
        
        /* a node may bear title of the Find dialog: */
        if (nodes.length == 1) {
            Object title = nodes[0].getValue(SearchPanel.PROP_DIALOG_TITLE);
            if (title != null && title instanceof String) {
                searchPanel.setTitle((String) title);
            }
        }

        searchPanel.showDialog();
        if (searchPanel.getReturnStatus() != SearchPanel.RET_OK) {
            return;
        }
        
        ResultView resultView = ResultView.getInstance();
        resultView.rememberInput(nodes,
                                 Utils.cloneSearchTypes(clonedSearchTypeList));
        resultView.open();
        resultView.requestActive();
        
        Manager.getInstance().scheduleSearchTask(
                new SearchTask(nodes,
                               clonedSearchTypeList,
                               searchPanel.getCustomizedSearchTypes()));
    }
    
    /**
     * Starts searching the currently selected set of nodes.
     *
     * @see ResultModel
     */
    public void performAction(SystemAction action) {
        if (action instanceof FindAction) {
            performAction(TopComponent.getRegistry().getCurrentNodes());
        } else {
            assert false;
        }
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.