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

package org.netbeans.modules.cvsclient;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import java.beans.PropertyChangeSupport;
//import java.lang.ref.Reference;
//import java.lang.ref.WeakReference;

//import org.openide.filesystems.FileSystem;
import org.openide.nodes.Node;
import org.openide.nodes.Index;
import org.openide.util.WeakListener;

/*
import org.netbeans.modules.vcscore.VcsFileSystem;
import org.netbeans.modules.vcscore.commands.CommandListener;
import org.netbeans.modules.vcscore.commands.CommandsPool;
import org.netbeans.modules.vcscore.commands.VcsCommandExecutor;
 */
import org.netbeans.modules.vcscore.runtime.*;

/**
 * The provider of JavaCvs commands for the representation on the Runtime Tab.
 *
 * @author  Martin Entlicher
 */
public class JavaCvsRuntimeCommandsProvider extends RuntimeCommandsProvider {
    
    private NbJavaCvsFileSystem fs;
    private RuntimeCommandsListener rcl = new RuntimeCommandsListener();
    private int numOfCommandsToKeep = RuntimeFolderNode.DEFAULT_NUM_OF_FINISHED_CMDS_TO_COLLECT;
    //private List runningCommands = new ArrayList();//Collections.synchronizedList(new ArrayList());
    private List finishedCommands = new ArrayList();//Collections.synchronizedList(new ArrayList());
    private List commands = new ArrayList();
    private Map runtimeCommandsForExecutors = new HashMap();//new Hashtable();
    //private CommandsPool cpool = CommandsPool.getInstance();
    
    public JavaCvsRuntimeCommandsProvider(NbJavaCvsFileSystem fs) {
        this.fs = fs;
        //cpool.addCommandListener(rcl);
        if (fs == null) {
            numOfCommandsToKeep = RuntimeFolderNode.DEFAULT_NUM_OF_FINISHED_CMDS_TO_COLLECT;
            register();
        } else {
            fs.addPropertyChangeListener(WeakListener.propertyChange(rcl, fs));
            numOfCommandsToKeep = fs.getNumberOfFinishedCmdsToCollect();
        }
    }
    
    protected Node createNodeDelegate() {
        RuntimeFolderNode fsRuntime = new RuntimeFolderNode(new RuntimeFolderChildren(this));
        if (fs == null) {
            String indName = org.openide.util.NbBundle.getBundle(JavaCvsRuntimeCommandsProvider.class).getString("FsCommandFactory.IndependantRuntimeFolder");
            fsRuntime.setName(indName);
            fsRuntime.setDisplayName(indName);
            fsRuntime.setIconBase("org/netbeans/modules/cvsclient/JavaCvsFileSystemIcon"); //NOI18N
        } else {
            fsRuntime.setName(fs.getSystemName());
            fsRuntime.setDisplayName(fs.getDisplayName());
            java.beans.BeanDescriptor bd = getFsBeanDescriptor(fs);
            if (bd != null) {
                String str = (String)bd.getValue(org.netbeans.modules.vcscore.VcsFileSystem.VCS_FILESYSTEM_ICON_BASE);
                if (str != null) {
                    fsRuntime.setIconBase(str);
                }
            }
        }
        fsRuntime.setNumOfFinishedCmdsToCollect(numOfCommandsToKeep);
        fsRuntime.addPropertyChangeListener(WeakListener.propertyChange(rcl, fsRuntime));
        //attachListeners(fsRuntime);
        return fsRuntime;
    }
    
    private static java.beans.BeanDescriptor getFsBeanDescriptor(NbJavaCvsFileSystem fs) {
        java.beans.BeanInfo info;
        try {
            info = org.openide.util.Utilities.getBeanInfo(fs.getClass());
        } catch (java.beans.IntrospectionException intrExc) {
            return null;
        }
        if (info != null) {
            return info.getBeanDescriptor();
        }
        return null;
    }
    
    public RuntimeCommand[] children() {
        //ArrayList children = new ArrayList(commands);
        return (RuntimeCommand[]) commands.toArray(new RuntimeCommand[commands.size()]);
    }
    
    public synchronized boolean cutCommandsToMaxToKeep() {
        boolean cutted = false;
        int current = commands.size();// + finishedCommands.size();
        while (current > numOfCommandsToKeep && finishedCommands.size() > 0) {
            commands.remove(finishedCommands.remove(0));
            current--;
            cutted = true;
        }
        return cutted;
    }
    
    public void updateCommand(RuntimeCommand cmd) {
        if (!commands.contains(cmd)) {
            commands.add(cmd);
        }
        if (cmd.getState() == RuntimeCommand.STATE_DONE || cmd.getState() == RuntimeCommand.STATE_CANCELLED) {
            if (!finishedCommands.contains(cmd)) finishedCommands.add(cmd);
        }
        cutCommandsToMaxToKeep();
        firePropertyChange(PROP_CHILDREN, null, null);
    }
    
    private class RuntimeCommandsListener extends Object implements PropertyChangeListener {
        
        /**
         * Called when the command is just to be preprocessed.
         *
        public void commandPreprocessing(VcsCommandExecutor vce) {
            VcsFileSystem fs = cpool.getFileSystemForExecutor(vce);
            if (VcsRuntimeCommandsProvider.this.fs == fs ||
                VcsRuntimeCommandsProvider.this.equals(RuntimeCommandsProvider.findProvider(fs))) {
                
                VcsRuntimeCommand cmd = new VcsRuntimeCommand(vce);
                cmd.setState(RuntimeCommand.STATE_WAITING);
                synchronized (VcsRuntimeCommandsProvider.this) {
                    commands.add(cmd);
                    runtimeCommandsForExecutors.put(vce, cmd);
                    cutCommandsToMaxToKeep();
                }
                firePropertyChange(PROP_CHILDREN, null, null);
            }
        }
        
        /**
         * This method is called when the command is just to be started.
         *
        public void commandStarted(VcsCommandExecutor vce) {
            VcsFileSystem fs = cpool.getFileSystemForExecutor(vce);
            if (VcsRuntimeCommandsProvider.this.fs == fs ||
                VcsRuntimeCommandsProvider.this.equals(RuntimeCommandsProvider.findProvider(fs))) {
                
                VcsRuntimeCommand cmd = (VcsRuntimeCommand) runtimeCommandsForExecutors.get(vce);
                if (cmd == null) {
                    cmd = new VcsRuntimeCommand(vce);
                    synchronized (VcsRuntimeCommandsProvider.this) {
                        commands.add(cmd);
                        runtimeCommandsForExecutors.put(vce, cmd);
                        cutCommandsToMaxToKeep();
                    }
                    firePropertyChange(PROP_CHILDREN, null, null);
                }
                cmd.setState(RuntimeCommand.STATE_RUNNING);
            }
        }
        
        /**
         * This method is called when the command is done.
         *
        public void commandDone(VcsCommandExecutor vce) {
            VcsFileSystem fs = cpool.getFileSystemForExecutor(vce);
            if (VcsRuntimeCommandsProvider.this.fs == fs ||
                VcsRuntimeCommandsProvider.this.equals(RuntimeCommandsProvider.findProvider(fs))) {
                
                RuntimeCommand cmd = (RuntimeCommand) runtimeCommandsForExecutors.get(vce);
                if (cmd != null) {
                    synchronized (VcsRuntimeCommandsProvider.this) {
                        //commands.remove(cmd);
                        runtimeCommandsForExecutors.remove(vce);
                        finishedCommands.add(cmd);
                        cutCommandsToMaxToKeep();
                    }
                    cmd.setState(RuntimeCommand.STATE_DONE);
                    firePropertyChange(PROP_CHILDREN, null, null);
                }
            }
        }
         */
        
        public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
            Object source = propertyChangeEvent.getSource();
            String propertyName = propertyChangeEvent.getPropertyName();
            if (source == fs) { // The change is comming from the FileSystem
                if (RuntimeFolderNode.PROPERTY_NUM_OF_FINISHED_CMDS_TO_COLLECT.equals(propertyName)) {
                    int newNumOfCommandsToKeep = fs.getNumberOfFinishedCmdsToCollect();
                    if (numOfCommandsToKeep == newNumOfCommandsToKeep) return ;
                    numOfCommandsToKeep = newNumOfCommandsToKeep;
                    RuntimeFolderNode node = (RuntimeFolderNode) getExistingNodeDelegate();
                    if (node != null) {
                        node.setNumOfFinishedCmdsToCollect(numOfCommandsToKeep);
                    }
                    if (cutCommandsToMaxToKeep()) firePropertyChange(PROP_CHILDREN, null, null);
                } else if (NbJavaCvsFileSystem.PROP_DISPLAY_NAME.equals(propertyName)) {
                    RuntimeFolderNode node = (RuntimeFolderNode) getExistingNodeDelegate();
                    if (node != null) {
                        node.setDisplayName(fs.getDisplayName());
                    }
                }
            } else if (source instanceof RuntimeFolderNode) { // The change is comming from the Node
                RuntimeFolderNode node = (RuntimeFolderNode) source;
                if (RuntimeFolderNode.PROPERTY_NUM_OF_FINISHED_CMDS_TO_COLLECT.equals(propertyName)) {
                    int newNumOfCommandsToKeep = node.getNumOfFinishedCmdsToCollect();
                    if (numOfCommandsToKeep == newNumOfCommandsToKeep) return ;
                    numOfCommandsToKeep = newNumOfCommandsToKeep;
                    if (cutCommandsToMaxToKeep()) firePropertyChange(PROP_CHILDREN, null, 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.