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.javacvs.commands;



import org.openide.util.NbBundle;
import java.io.*;
import java.util.*;
import java.beans.*;

import org.netbeans.modules.javacvs.customizers.RemoveParamInput;
import org.netbeans.modules.javacvs.*;
import org.netbeans.modules.javacvs.events.*;
import org.netbeans.modules.javacvs.commands.*;
import org.netbeans.lib.cvsclient.event.*;
import org.netbeans.lib.cvsclient.admin.*;
import org.netbeans.lib.cvsclient.command.remove.*;
import org.netbeans.lib.cvsclient.command.*;
import org.netbeans.modules.javacvs.JavaCvsFileSystem;
import org.netbeans.modules.javacvs.caching.*;
import org.netbeans.modules.vcscore.VcsAttributes;
import org.netbeans.modules.vcscore.cache.*;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;


/** This class implements the cvs remove command.
 *
 * @author mkleint
 */
public class CvsRemove extends CacheUpdatingFsCommand {
    
    RemoveCommand command;
    /**
     * If true, will delete the file in working dir before it gets removed.
     */
    private boolean deleteBeforeRemove;

    private boolean ignoreLocallyExistingFiles;
    
    private boolean recursive = true;
    
    private FsRemove removeImpl;
    
    /** Creates new CvsRemove instance.
     */    
    public CvsRemove() {
        super();
        removeImpl = new RemoveImpl();
    }
    
    /** Creates new CvsRemove instance.
     */
    public CvsRemove(File[] files, ClientProvider fs) {
        super(fs);
        setFiles(files);
        removeImpl = new RemoveImpl();
    }

    public CvsCommand getImpl() {
        return removeImpl;
    }    
    
    public FsRemove getRemoveImpl() {
        return removeImpl;
    }
    
    public void setRemoveImpl(FsRemove remImpl) {
        removeImpl = remImpl;
    }   
    
    /**
     * return the library command that is considered main command.
     * that one is used for loading from
     */
    protected Class getMainCvsCommand() {
        return RemoveCommand.class;
    }
    
    
    public String getName() {
        return NbBundle.getBundle(CvsRemove.class).getString("CvsRemove.name"); // NOI18N
    }
    
    
    protected void initCommand(boolean commandIsRunning) {
        // here the the commands is initiated just right before running the command
        clearCommandList();
        command = new RemoveCommand();
        setCommandArguments(command);
        command.setFiles(getFiles());
        toDoCommands.addElement(command);
        
        super.initCommand(commandIsRunning);

        if (commandIsRunning && isDeleteBeforeRemove()) {
            File[] files = getFiles();
            deleteAssociatedUnimportantFiles(files);
        }
        setFullEntriesUpdate(true);
        prepareCache(getFiles(), isRecursive());
    }
    
    private void deleteAssociatedUnimportantFiles(File[] files) {
        for (int i = 0; i < files.length; i++) {
            FileObject[] fos = FileUtil.fromFile(files[i]);
            for (int j = 0; j < fos.length;j++) {
                org.openide.filesystems.FileSystem nativeFs = (org.openide.filesystems.FileSystem) fos[j].getAttribute(VcsAttributes.VCS_NATIVE_FS);
                String nativePath = (String) fos[j].getAttribute(VcsAttributes.VCS_NATIVE_PACKAGE_NAME_EXT);
                if (nativePath != null && nativeFs instanceof JavaCvsFileSystem) {
                    FileObject fo = nativeFs.findResource(nativePath);
                    if (fo == null) continue;
                    Set objectFiles;
                    try {
                        DataObject dobj = DataObject.find(fo);
                        objectFiles = dobj.files();
                    } catch (DataObjectNotFoundException donfex) {
                        objectFiles = Collections.EMPTY_SET;
                    }
                    for (Iterator it = objectFiles.iterator(); it.hasNext(); ) {
                        FileObject foToDelete = (FileObject) it.next();
                        if (!((JavaCvsFileSystem) nativeFs).isImportant(foToDelete.getPath())) {
                            try {
                                foToDelete.delete();
                            } catch (IOException ioex) {
                                // What can we do if we can not delete the file?
                            }
                        }
                    }
                }
            }
        }
    }
    
    protected void finishedCommand() {
        // update cache..
        fireUpdateCache();        
        super.finishedCommand();
    }
    
     
    /**
     * Called when file status information has been received
     */
    public void fileInfoGenerated(FileInfoEvent e) {
        FileInfoContainer fInfo = e.getInfoContainer();
        if (fInfo != null) {
            if (fInfo instanceof RemoveInformation) {
                RemoveInformation info = (RemoveInformation)fInfo;
                updateCache(info);
            }
        }
        super.fileInfoGenerated(e);
    }
    

    /**
     * Returns true if the local files will be deleted automatically.
     */
    public boolean isDeleteBeforeRemove()
    {
        return deleteBeforeRemove;
    }

    /**
     * Sets whether the local files will be deleted before.
     */
    public void setDeleteBeforeRemove(boolean deleteBeforeRemove)
    {
        this.deleteBeforeRemove = deleteBeforeRemove;
    }

    /**
     * Returns true to indicate that locally existing files are treated as they
     * would not exist.
     * This is a extension to the standard cvs-behaviour!
     */
    public boolean isIgnoreLocallyExistingFiles()
    {
        return ignoreLocallyExistingFiles;
    }

    /** Sets whether locally existing files will be treated as they were deleted
     * before.
     * This is a extension to the standard cvs-behaviour!
     */
    public void setIgnoreLocallyExistingFiles(boolean ignoreLocallyExistingFiles)
    {
        this.ignoreLocallyExistingFiles = ignoreLocallyExistingFiles;
    }

    /** Sets recursive (-R switch) or local (-l switch) behaviour.
     */    
    public void setRecursive(boolean recursive) {
        this.recursive = recursive;
    }
    
    /** Returns recursive (-R switch) or local (-l switch) behaviour.
     */    
    public boolean isRecursive() {
        return recursive;
    }   
    
    public java.lang.Object clone() throws java.lang.CloneNotSupportedException {
        CvsRemove retValue;
        
        retValue = (CvsRemove)super.clone();
        retValue.setRemoveImpl(new RemoveImpl());
        return retValue;
    }
    
    /**
     * Overrides the default behaviour, since we need to iterate the cache file objects 
     * present and remove the ones that don't have the adequate Entry in the Entries file.
     * That are basically Locally added files that were removed.
     */
    protected void updateFromEntries(File dir, Entry[] entries, CvsCacheDir cacheDir) {
//        super.updateFromEntries(dir, entries, cacheDir);
        CacheFile[] files = cacheDir.getFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                CacheFile file = files[i];
                if (file.getStatus().equals(JavaCvsStatusManager.LOCALLY_ADDED)) { //NOI18N
                    //now we have the cache file that was just locally added??
                    File realFile = new File(dir.getAbsolutePath(), file.getName());
                    boolean isInEntries = false;
                    for (int j = 0; j < entries.length; j++) {
                        if (entries[j].getName().equals(file.getName())) {
                            isInEntries = true;
                            break;
                        }
                    }
                    if (!realFile.exists() && !isInEntries) {
                        cacheDir.removeFile(file.getName(), false);
                    }
                    
                }
            }
        }
    }
    
    public class RemoveImpl extends FsRemove implements FileSystemCommandImpl {
        
        public RemoveImpl() {
        }

        public FileSystemCommand getOuterClassInstance() {
            return CvsRemove.this;
        }        
       
        public void removeCommandErrorListener(org.netbeans.modules.javacvs.events.CommandErrorListener commErrListener) {
            CvsRemove.this.removeCommandErrorListener(commErrListener);
        }
        
        /** Sets whether locally existing files will be treated as they were deleted
         * before.
         * This is a extension to the standard cvs-behaviour!
         */
        public void setIgnoreLocallyExistingFiles(boolean ignoreLocallyExistingFiles) {
            CvsRemove.this.setIgnoreLocallyExistingFiles(ignoreLocallyExistingFiles);
        }
        
        public void addCommandErrorListener(org.netbeans.modules.javacvs.events.CommandErrorListener commErrListener) {
            CvsRemove.this.addCommandErrorListener(commErrListener);
        }
        
        /** Clones the instance of the command.
         * @return Returns the cloned object.
         * @throws CloneNotSupportedException if clone is not supported by subclasses, it throws CloneNotSupportedException
         */
        public Object clone() throws CloneNotSupportedException {
            return CvsRemove.this.clone();
        }
        
        /** Sets recursive (-R switch) or local (-l switch) behaviour.
         */
        public void setRecursive(boolean recursive) {
            CvsRemove.this.setRecursive(recursive);
        }
        
        /** Starts the thread with the command - executes the run() method.
         */
        public void startCommand() {
            CvsRemove.this.startCommand();
        }
        
        /**
         * Returns true to indicate that locally existing files are treated as they
         * would not exist.
         * This is a extension to the standard cvs-behaviour!
         */
        public boolean isIgnoreLocallyExistingFiles() {
            return CvsRemove.this.isIgnoreLocallyExistingFiles();
        }
        
        /** Removes a CommandDisplayerListener from the FilesystemCommand.
         *  All the listeners are removed once the command ends.
         *
         * @param listener the listener to remove
         */
        public void removeDisplayerListener(CommandDisplayerListener listener) {
            CvsRemove.this.removeDisplayerListener(listener);
        }
        
        /**
         * Returns true if the local files will be deleted automatically.
         */
        public boolean isDeleteBeforeRemove() {
            return CvsRemove.this.isDeleteBeforeRemove();
        }
        
        /** Add a CommandDisplayerListener to the FilesystemCommand.
         * 
All UI communication and display of output should be handled via the CommandDisplayerListeners. * * @param listener The listener to add. * */ public void addDisplayerListener(CommandDisplayerListener listener) { CvsRemove.this.addDisplayerListener(listener); } /** Returns recursive (-R switch) or local (-l switch) behaviour. */ public boolean isRecursive() { return CvsRemove.this.isRecursive(); } /** * Sets whether the local files will be deleted before. */ public void setDeleteBeforeRemove(boolean deleteBeforeRemove) { CvsRemove.this.setDeleteBeforeRemove(deleteBeforeRemove); } public void setFileObjects(FileObject[] fileObjects) { CvsRemove.this.setFileObjects(fileObjects); } public FsGlobalOptions getGlobalOptions() { FsGlobalOptions retValue; retValue = new FsGlobalOptionsImpl(CvsRemove.this.getGlobalOptions()); return retValue; } } }
... 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.