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

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.EventListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

import org.netbeans.spi.queries.VisibilityQueryImplementation;

import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.WeakListeners;
import org.openide.util.WeakSet;

/**
 * VCS visibility query implementation based on VCS filesystems.
 * Files are visible if they are not ignored by the VCS filesystem.
 *
 * @author Martin Entlicher
 */
public class VcsVisibilityQueryImpl implements VisibilityQueryImplementation, PropertyChangeListener {
    
    private EventListenerList listeners = new EventListenerList();
    private WeakSet listenedVFSs = new WeakSet();
    
    /** Create VcsVisibilityQueryImpl */
    public VcsVisibilityQueryImpl() {}
    
    public boolean isVisible(FileObject fo) {
        VcsFileSystem vcsFS = (VcsFileSystem) fo.getAttribute(VcsAttributes.VCS_NATIVE_FS);
        if (vcsFS == null) return true;
        synchronized (listenedVFSs) {
            if (!listenedVFSs.contains(vcsFS)) {
                listenedVFSs.add(vcsFS);
                vcsFS.addPropertyChangeListener(WeakListeners.propertyChange(this, vcsFS));
            }
        }
        File file = FileUtil.toFile(fo);
        return vcsFS.getFileFilter().accept(file.getParentFile(), file.getName());
    }
    
    public void addChangeListener(ChangeListener l) {
        listeners.add(ChangeListener.class, l);
    }
    
    public void removeChangeListener(ChangeListener l) {
        listeners.remove(ChangeListener.class, l);
    }
    
    private void fireChangeEvent() {
        ChangeEvent che = new ChangeEvent(this);
        EventListener[] ls = listeners.getListeners(ChangeListener.class);
        for (int i = ls.length - 1; i >= 0; i -= 2) {
            ((ChangeListener) ls[i]).stateChanged(che);
        }
    }
    
    public void propertyChange(PropertyChangeEvent evt) {
        if (VcsFileSystem.PROP_FILE_FILTER.equals(evt.getPropertyName())) {
            fireChangeEvent();
        }
    }
    
}
... 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.