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.project;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.modules.search.SearchPanel;
import org.netbeans.modules.search.SearchPerformer;
import org.openide.filesystems.FileObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;

import org.openide.nodes.Node;
import org.openide.util.actions.CallableSystemAction;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.lookup.Lookups;
import org.openidex.search.FileObjectFilter;
import org.openidex.search.SearchInfo;
import org.openidex.search.SearchInfoFactory;


/**
 * Action which searchs on filesystems.
 *
 * @author  Petr Kuzel
 * @author  Marian Petras
 */
public final class ProjectsSearchAction extends CallableSystemAction {

    static final long serialVersionUID = 4554342565076372610L;
    
    private PropertyChangeListener openProjectsListener;

    void link() {        
        if (openProjectsListener == null) {
            openProjectsListener = new PropertyChangeListener() {
                        public void propertyChange(PropertyChangeEvent e) {
                            if (e.getPropertyName().equals(
                                    OpenProjects.PROPERTY_OPEN_PROJECTS)) {
                                updateState();
                            }
                        }
                    };
            OpenProjects.getDefault()
                    .addPropertyChangeListener(openProjectsListener);
        }
        updateState();
    }
    
    void unlink() {
        if (openProjectsListener != null) {
            OpenProjects.getDefault()
                    .removePropertyChangeListener(openProjectsListener);
            openProjectsListener = null;
        }
        setEnabled(false);
    }
    
    private synchronized void updateState() {
        setEnabled(OpenProjects.getDefault().getOpenProjects().length != 0);
    }

    protected String iconResource() {
        return "org/openide/resources/actions/find.gif";                //NOI18N
    }
    
    public String getName() {
        return NbBundle.getMessage(ProjectsSearchAction.class,
                                   "LBL_SearchProjects");               //NOI18N
    }

    public HelpCtx getHelpCtx() {
        return new HelpCtx(ProjectsSearchAction.class);
    }

    /** Where to search */
    private Node[] getNodesToSearch() {
        Project[] openProjects = OpenProjects.getDefault().getOpenProjects();
        
        if (openProjects.length == 0) {
            
            /*
             * We cannot prevent this situation. The action may be invoked
             * between moment the last project had been removed and the removal
             * notice was distributed to the open projects listener (and this
             * action disabled). This may happen if the the last project
             * is being removed in another thread than this action was
             * invoked from.
             */
            return new Node[0];
        }
        
        List rootFolders = new ArrayList();
        for (int i = 0; i < openProjects.length; i++) {
            Sources sources = ProjectUtils.getSources(openProjects[i]);
            SourceGroup[] sourceGroups
                    = sources.getSourceGroups(Sources.TYPE_GENERIC);
            for (int j = 0; j < sourceGroups.length; j++) {
                rootFolders.add(sourceGroups[j].getRootFolder());
            }
        }

        if (rootFolders.isEmpty()) {
            return new Node[0];
        }

        SearchInfo searchInfo = SearchInfoFactory.createSearchInfo(
                (FileObject[])
                        rootFolders.toArray(new FileObject[rootFolders.size()]),
                true,           //recursive
                new FileObjectFilter[] {SearchInfoFactory.VISIBILITY_FILTER,
                                        SearchInfoFactory.SHARABILITY_FILTER});
        
        Node nodeToSearch = new AbstractNode(
                Children.LEAF,
                Lookups.singleton(searchInfo));
        nodeToSearch.setValue(
                SearchPanel.PROP_DIALOG_TITLE,
                NbBundle.getMessage(ProjectsSearchAction.class,
                                    "LBL_Title_SearchProjects"));       //NOI18N
        return new Node[] {nodeToSearch};
    }

    /** Perform this action. */
    public void performAction() {
        SearchPerformer.getDefault().performAction(getNodesToSearch());
    }
    
    /**
     */
    protected boolean asynchronous() {
        return false;
    }

}
... 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.