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

package org.netbeans.modules.vcscore;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.nodes.Node;
import org.openide.util.actions.SystemAction;

import org.netbeans.api.queries.SharabilityQuery;

import org.netbeans.api.vcs.VcsManager;
import org.netbeans.api.vcs.commands.Command;

import org.netbeans.spi.vcs.commands.CommandSupport;

import org.netbeans.modules.vcscore.actions.VersioningExplorerAction;
import org.netbeans.modules.vcscore.actions.AddToGroupAction;
import org.netbeans.modules.vcscore.actions.RefreshLocalFolderAction;
//import org.netbeans.modules.vcscore.commands.CommandExecutorSupport;
import org.netbeans.modules.vcscore.caching.FileStatusProvider;
import org.netbeans.modules.vcscore.caching.FileCacheProvider;
import org.netbeans.modules.vcscore.caching.VcsFSCache;

//import org.netbeans.modules.vcscore.cmdline.CommandLineVcsDirReader;
//import org.netbeans.modules.vcscore.cmdline.CommandLineVcsDirReaderRecursive;
//import org.netbeans.modules.vcscore.cmdline.AdditionalCommandDialog;
import org.netbeans.modules.vcscore.cmdline.ExecuteCommand;
import org.netbeans.modules.vcscore.cmdline.UserCommand;
import org.netbeans.modules.vcscore.cmdline.UserCommandSupport;
import org.netbeans.modules.vcscore.commands.VcsCommand;
import org.netbeans.modules.vcscore.commands.VcsCommandIO;
import org.netbeans.modules.vcscore.commands.VcsCommandExecutor;
import org.netbeans.modules.vcscore.commands.VcsDescribedCommand;

/**
 * This class provides a default implementation of VcsFactory.
 *
 * @author  Martin Entlicher
 */
public class DefaultVcsFactory extends Object implements VcsFactory {

    //private static Object fsActionAccessLock = new Object();
    //private static VcsAction fsAction = null;

    protected WeakReference fileSystem;
    
    /** Creates new DefaultVcsFactory */
    public DefaultVcsFactory(VcsFileSystem fileSystem) {
        this.fileSystem = new WeakReference(fileSystem);
    }

    /**
     * Get the provider of the VCS status information. The default provider
     * is the filesystem cache if implements FileStatusProvider or null otherwise.
     */
    public FileStatusProvider getFileStatusProvider() {
        VcsFileSystem fileSystem = (VcsFileSystem) this.fileSystem.get();
        Object cache = fileSystem.getCacheProvider();
        if (cache instanceof FileStatusProvider) return (FileStatusProvider) cache;
        else return null;
    }

    /**
     * Get the provider of the VCS cache. The default provider
     * is VcsFSCache.
     */
    public FileCacheProvider getFileCacheProvider() {
        VcsFileSystem fileSystem = (VcsFileSystem) this.fileSystem.get();
        return new VcsFSCache(fileSystem);
    }

    /**
     * Get the VCS directory reader.
     * @return an instance of CommandLineVcsDirReader or null when can not be created.
     *
    public VcsCommandExecutor getVcsDirReader(DirReaderListener listener, String path) {
        VcsCommand list;
        VcsFileSystem fileSystem = (VcsFileSystem) this.fileSystem.get();
        if (fileSystem.isOffLine()) {
            list = fileSystem.getCommand(VcsCommand.NAME_REFRESH_OFFLINE);
        } else {
            list = fileSystem.getCommand(VcsCommand.NAME_REFRESH);
        }
        if (list == null) return null;
        Hashtable vars = fileSystem.getVariablesAsHashtable();

        if (!java.io.File.separator.equals ("/") ) { // NOI18N
            String winPath = path.replace('/', java.io.File.separatorChar);
            vars.put("DIR", winPath); // NOI18N
        } else {
            vars.put("DIR", path); // NOI18N
        }

        String exec = (String) list.getProperty(VcsCommand.PROPERTY_EXEC);
        if (exec != null && !CommandExecutorSupport.promptForVariables(fileSystem, exec, vars, list, null)) return null;
        if (list instanceof UserCommand) {
            return new CommandLineVcsDirReader(listener, fileSystem, (UserCommand) list, vars);
        } else {
            return null;
        }
    }
    
    /**
     * Get the VCS directory reader that reads the whole directory structure.
     * @return an instance of CommandLineVcsDirReader or null when can not be created.
     *
    public VcsCommandExecutor getVcsDirReaderRecursive(DirReaderListener listener, String path) {
        VcsCommand list;
        VcsFileSystem fileSystem = (VcsFileSystem) this.fileSystem.get();
        if (fileSystem.isOffLine()) {
            list = fileSystem.getCommand(VcsCommand.NAME_REFRESH_RECURSIVELY_OFFLINE);
        } else {
            list = fileSystem.getCommand(VcsCommand.NAME_REFRESH_RECURSIVELY);
        }
        if (list == null) return null;
        Hashtable vars = fileSystem.getVariablesAsHashtable();

        if (!java.io.File.separator.equals ("/")) { // NOI18N
            String winPath = path.replace('/', java.io.File.separatorChar);
            vars.put("DIR", winPath); // NOI18N
        } else {
            vars.put("DIR", path); // NOI18N
        }

        String exec = (String) list.getProperty(VcsCommand.PROPERTY_EXEC);
        if (exec != null && !CommandExecutorSupport.promptForVariables(fileSystem, exec, vars, list, null)) return null;
        if (list instanceof UserCommand) {
            return new CommandLineVcsDirReaderRecursive(listener, fileSystem, (UserCommand) list, vars);
        } else {
            return null;
        }
    }
    
    /*
     * Get the VCS action.
     * @return instance of VcsAction
     *
    public VcsAction getVcsAction() {
        return new VcsAction (fileSystem);
    }
     */
    
    /*
     * Get the VCS action for a collection of FileObjects.
     * If the collection is null, it gets the FileObjects from
     * currently selected nodes.
     * @param fos the collection of FileObjects or null.
     * @return instance of VcsAction
     *
    public VcsAction getVcsAction(Collection fos) {
        return new VcsAction (fileSystem, fos);
    }
     */
    
    /**
     * Get the array of VCS actions for a collection of FileObjects.
     * If the collection is null, it gets the FileObjects from
     * currently selected nodes.
     * @param fos the collection of FileObjects or null.
     * @return the array of instances of VcsActions, one for each
     *         child of the root command node.
     */
    public SystemAction[] getActions(Collection fos) {
        ArrayList actions = new ArrayList();
        //return new SystemAction[] { getVcsAction() };
        //ArrayList actions = new ArrayList();
        VcsFileSystem fileSystem = (VcsFileSystem) this.fileSystem.get();
        /*
        Node commands = fileSystem.getCommands();
        Node[] commandRoots = commands.getChildren().getNodes();
        //System.out.println("DefaultVcsFactory.getActions(): commandRoots.length = "+commandRoots.length);
        //ArrayList commandsSubTrees = new ArrayList();
        int[] commandsSubTrees = new int[commandRoots.length];
        int numOfSubTrees = 0;
        for (int i = 0; i < commandRoots.length; i++) {
            VcsCommand cmd = (VcsCommand) commandRoots[i].getCookie(VcsCommand.class);
            //System.out.println("commandRoots["+i+"] = "+cmd);
            if (cmd != null &&
                VcsCommandIO.getIntegerPropertyAssumeZero(cmd, VcsCommand.PROPERTY_NUM_REVISIONS) == 0) {
                    //actions.add(new VcsAction(fileSystem, fos, i));
                    commandsSubTrees[numOfSubTrees++] = i;
                    //commandsSubTrees.add(new Integer(i));
            }
        }
        if (numOfSubTrees == 0) return new SystemAction[0];
        fsAction = (VcsAction) SystemAction.get(VcsAction.class);
        synchronized (fsActionAccessLock) {
            fsAction.setFileSystem(fileSystem);
            fsAction.setSelectedFileObjects(fos);
            Node[] commandNodesSubTrees = new Node[numOfSubTrees];
            for (int i = 0; i < numOfSubTrees; i++) {
                commandNodesSubTrees[i] = commandRoots[commandsSubTrees[i]];
            }
            fsAction.setCommandsSubTrees(commandNodesSubTrees);
        }
        actions.add(fsAction);
         */
        boolean sharable = true;
        if (fileSystem != null && !fileSystem.isProcessUnimportantFiles() && fos != null) {
            sharable = fos.size() == 0; // false if there are some files
            for (Iterator it = fos.iterator(); it.hasNext(); ) {
                FileObject fo = (FileObject) it.next();
                File file = FileUtil.toFile(fo);
                int sharability = SharabilityQuery.getSharability(file);
                if (sharability != SharabilityQuery.NOT_SHARABLE) {
                    sharable = true;
                    break;
                }
            }
        }
        actions.add(SystemAction.get(RefreshLocalFolderAction.class));
        if (sharable) {
            actions.add(SystemAction.get(VcsFSCommandsAction.class));
            if (fileSystem.getVersioningFileSystem() != null) {
                actions.add(SystemAction.get(VersioningExplorerAction.class));
            }
            actions.add(SystemAction.get(AddToGroupAction.class));
        }
        //System.out.println("action[0] = "+actions.get(0)+", action[1] = "+actions.get(1)+", equals = "+actions.get(0).equals(actions.get(1)));
        //return (SystemAction[]) actions.toArray(new SystemAction[actions.size()]);
        return (SystemAction[]) actions.toArray(new SystemAction[0]);
    }
    
    /**
     * Get the command executor for the command.
     * @param command the command to get the executor for
     * @param variables the Hashtable of (variable name, variable value) pairs
     * @return the command executor or null when no executor is found. If command is instance of {@link UserCommand}, an instance of {@link ExecuteCommand} is returned.
     * @deprecated This method is retained for compatibility only. XXX how to replace usages? It's called from 31 places.
     */
    public VcsCommandExecutor getCommandExecutor(VcsCommand command, Hashtable variables) {
        if (command instanceof UserCommand) {
            //if (VcsCommandIO.getBooleanProperty(command, UserCommand.PROPERTY_DISPLAY)) {
            //    AdditionalCommandDialog addCommand = new org.netbeans.modules.vcscore.cmdline.AdditionalCommandDialog(fileSystem, (UserCommand) command, variables, new javax.swing.JFrame(), false);
            //    return addCommand.createCommand();
            //} else {
            VcsFileSystem fileSystem = (VcsFileSystem) this.fileSystem.get();
            if (fileSystem == null) return null;
            UserCommand ucmd = (UserCommand) command;
            CommandSupport cmdSupp = fileSystem.getCommandSupport(command.getName());
            if (cmdSupp == null) {
                cmdSupp = new UserCommandSupport(ucmd, fileSystem);
            }
            Command cmd = cmdSupp.createCommand();
            if (cmd instanceof VcsDescribedCommand) ((VcsDescribedCommand) cmd).setAdditionalVariables(variables);
            //if (!VcsManager.showCustomizer(cmd)) return null;
            ExecuteCommand executor = new ExecuteCommand(fileSystem, (UserCommand) command, variables);
            if (cmd instanceof VcsDescribedCommand) {
                executor.setDescribedCommand((VcsDescribedCommand) cmd);
                ((VcsDescribedCommand) cmd).setExecutor(executor);
            }
            return executor;
            //}
        } else {
            return null;
        }
    }
    
}
... 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.