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

package org.netbeans.core.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Enumeration;
import java.util.Set;
import java.util.LinkedHashSet;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;

import org.openide.awt.Actions;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.Repository;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.Presenter;

/**
 * An action which refreshes all filesystems in the repository.
 * Any file objects in memory are checked against the modification times on disk.
 * Useful to invoke after having done changes in some external program.
 * @author Jesse Glick
 * @see "#31047"
 */
public final class RefreshAllFilesystemsAction extends AbstractAction implements HelpCtx.Provider, Presenter.Popup {
    
    public RefreshAllFilesystemsAction() {
        super(NbBundle.getMessage(RefreshAllFilesystemsAction.class, "LAB_refresh_all_filesystems"));
    }
    
    public HelpCtx getHelpCtx() {
        return new HelpCtx(RefreshAllFilesystemsAction.class);
    }
    
    public void actionPerformed(ActionEvent ev) {
        // Cf. ant/src-bridge/org/apache/tools/ant/module/bridge/impl/BridgeImpl.java:
        // need to somehow get a handle on the MasterFileSystem instance to refresh it:
        FileSystem[] allFileSystems = getFileSystems();
        for (int i = 0; i < allFileSystems.length; i++) {
            FileSystem fs = allFileSystems[i];
            fs.refresh(false);                    
        }                                
        // Also do system filesystem:
        Repository.getDefault().getDefaultFileSystem().refresh(false);
    }
    
    //copy - paste programming
    //http://ant.netbeans.org/source/browse/ant/src-bridge/org/apache/tools/ant/module/bridge/impl/BridgeImpl.java.diff?r1=1.15&r2=1.16
    //http:/java.netbeans.org/source/browse/java/javacore/src/org/netbeans/modules/javacore/Util.java    
    //http://core.netbeans.org/source/browse/core/ui/src/org/netbeans/core/ui/MenuWarmUpTask.java
    //http://core.netbeans.org/source/browse/core/src/org/netbeans/core/actions/RefreshAllFilesystemsAction.java
    //http://java.netbeans.org/source/browse/java/api/src/org/netbeans/api/java/classpath/ClassPath.java
        
    private static FileSystem[] fileSystems;

    private static FileSystem[] getFileSystems() {
        if (fileSystems != null) {
            return fileSystems;
        }
        File[] roots = File.listRoots();
        Set allRoots = new LinkedHashSet();
        assert roots != null && roots.length > 0 : "Could not list file roots"; // NOI18N
        
        for (int i = 0; i < roots.length; i++) {
            File root = roots[i];
            FileObject random = FileUtil.toFileObject(root);
            if (random == null) continue;
            
            FileSystem fs;
            try {
                fs = random.getFileSystem();
                allRoots.add(fs);
                
                /*Because there is MasterFileSystem impl. that provides conversion to FileObject for all File.listRoots
                (except floppy drives and empty CD). Then there is useless to convert all roots into FileObjects including
                net drives that might cause performance regression.
                */
                
                if (fs != null) {
                    break;
                }
            } catch (FileStateInvalidException e) {
                throw new AssertionError(e);
            }
        }
        FileSystem[] retVal = new FileSystem [allRoots.size()];
        allRoots.toArray(retVal);
        assert retVal.length > 0 : "Could not get any filesystem"; // NOI18N
        
        return fileSystems = retVal;
    }
    
    /** Provides different label for action in popup menu.
    */
    public JMenuItem getPopupPresenter() {
        JMenuItem item = new JMenuItem();
        String text = NbBundle.getMessage(RefreshAllFilesystemsAction.class, "LAB_refresh_all_filesystems_popup");
        item.setAction(this);
        item.setText(Actions.cutAmpersand(text));
        item.setMnemonic(KeyEvent.VK_R);
        return item;
    }
    
}
... 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.