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.modules.cvsclient;


import org.openide.util.NbBundle;
import javax.swing.DefaultComboBoxModel;
import org.netbeans.modules.vcscore.util.*;
import org.netbeans.modules.javacvs.*;
import org.netbeans.modules.javacvs.commands.*;
import org.netbeans.modules.cvsclient.commands.checkout.ModulesListPanel;
import org.netbeans.lib.cvsclient.command.GlobalOptions;
import org.netbeans.lib.cvsclient.util.DefaultIgnoreFileFilter;
import org.netbeans.lib.cvsclient.util.IgnoreFileFilter;
import org.openide.filesystems.FileObject;
import javax.swing.*;
import javax.accessibility.*;

import javax.swing.tree.*;
import java.awt.Component;
import javax.swing.event.*;
import java.io.File;
import java.io.*;
import java.util.Comparator;
import java.util.Arrays;
import java.util.LinkedList;
import org.netbeans.modules.cvsclient.customizer.NbJavaCvsCustomizer;
import org.netbeans.modules.cvsclient.commands.checkout.NbCheckoutParamInput;
import org.netbeans.modules.vcscore.cache.CacheHandler;
import org.netbeans.modules.javacvs.caching.JavaCvsCache;
import org.netbeans.modules.javacvs.caching.CvsFsCache;
import org.netbeans.modules.cvsclient.caching.NbCvsFsCache;


import org.openide.util.*;
import org.openide.*;

/**
 *  Starting panel for execution of filesystem independant cvs commands.
 * @author  mkleint
 */
public class ToolsCvsExecutionPanel extends javax.swing.JPanel implements TreeSelectionListener {
    
    private Object[] commandValues;
    
    /*validate values if they are accpetable
     */
    private final int VALID_WRK = 1;
    private final int VALID_CVSROOT = 2;
    private int validateValue = 0;
    
    private FsCommandFactory commandFactory;
    
    /** Creates new form ToolsCvsExecutionPanel */
    public ToolsCvsExecutionPanel() {
        initComponents();
        initAccessibility();
        commandFactory = (FsCommandFactory)FsCommandFactory.getInstance();
        helpTextArea.setFont(javax.swing.UIManager.getFont("Label.font")); // NOI18N
        helpTextArea.setDisabledTextColor(javax.swing.UIManager.getColor("Label.foreground")); // NOI18N
        helpTextArea.setText(NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.helpTextArea.text")); // NOI18N
        lblCurrentDir.setDisplayedMnemonic(NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.lblCurrentDir.mnemonic").charAt(0)); // NOI18N
        lblCurrentDir.setLabelFor(cbCurrentDir);
        lblFiles.setDisplayedMnemonic(NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.lblFiles.mnemonic").charAt(0)); // NOI18N
        lblFiles.setLabelFor(trFiles);
        lblCommand.setDisplayedMnemonic(NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.lblCommand.mnemonic").charAt(0)); // NOI18N
        lblCommand.setLabelFor(cbCommand);
        lblCvsRoot.setDisplayedMnemonic(NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.lblCvsRoot.mnemonic").charAt(0)); // NOI18N
        lblCvsRoot.setLabelFor(txCvsRoot);
        btnCurrentDir.setMnemonic(NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.btnCurrentDir.mnemonic").charAt(0)); // NOI18N
        
        final String UPDATE = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Update"); // NOI18N
        final String COMMIT = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Commit"); // NOI18N
        final String DIFF = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Diff"); // NOI18N
        final String STATUS = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Status"); // NOI18N
        final String LOG = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Log"); // NOI18N
        final String CHECKOUT = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Checkout"); // NOI18N
        final String ADD = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Add"); // NOI18N
        final String REMOVE = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Remove"); // NOI18N
        final String IMPORT = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Import"); // NOI18N
        final String EXPORT = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Export"); // NOI18N
        final String ANNOTATE = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.Annotate"); // NOI18N
        final String LISTMODULES = NbBundle.getBundle(ToolsCvsExecutionPanel.class).getString("ToolsCvsExecutionPanel.ListModules"); // NOI18N
        commandValues = new Object[] {
            UPDATE, COMMIT, DIFF, STATUS, LOG, ANNOTATE, CHECKOUT, IMPORT, EXPORT, ADD, REMOVE, LISTMODULES};
            cbCommand.setModel(new DefaultComboBoxModel(commandValues));
            cbCurrentDir.setModel(new DefaultComboBoxModel());
            addTreeListeners();
            createTree(""); // NOI18N
            DefaultTreeCellRenderer  def = new MyTreeCellRenderer();
            trFiles.setCellRenderer(def);
            trFiles.putClientProperty("JTree.lineStyle", "Angled"); // NOI18N
            
            // init cache..
            // do this, just for the case the cache wasn't init'ed by some of the filesystems..
            CacheHandler handler = CacheHandler.getInstance();
            JavaCvsCache cache = (JavaCvsCache)handler.getCache(JavaCvsCache.JAVA_CACHE_NAME);
            if (cache == null) {
                cache = NbCvsFsCache.getCacheObject();
                handler.registerCacheType(JavaCvsCache.JAVA_CACHE_NAME, cache);
            }
//            spFiles.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        helpTextArea = new javax.swing.JTextArea();
        lblCurrentDir = new javax.swing.JLabel();
        cbCurrentDir = new javax.swing.JComboBox();
        btnCurrentDir = new javax.swing.JButton();
        lblCvsRoot = new javax.swing.JLabel();
        txCvsRoot = new javax.swing.JTextField();
        lblFiles = new javax.swing.JLabel();
        spFiles = new javax.swing.JScrollPane();
        trFiles = new javax.swing.JTree();
        commandPanel = new javax.swing.JPanel();
        lblCommand = new javax.swing.JLabel();
        cbCommand = new javax.swing.JComboBox();

        setLayout(new java.awt.GridBagLayout());

        helpTextArea.setEditable(false);
        helpTextArea.setLineWrap(true);
        helpTextArea.setWrapStyleWord(true);
        helpTextArea.setEnabled(false);
        helpTextArea.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 5, 11);
        add(helpTextArea, gridBagConstraints);

        lblCurrentDir.setLabelFor(cbCurrentDir);
        lblCurrentDir.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/Bundle").getString("ToolsCvsExecutionPanel.lblCurrentDir.label"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
        add(lblCurrentDir, gridBagConstraints);

        cbCurrentDir.setEditable(true);
        cbCurrentDir.setMaximumRowCount(10);
        cbCurrentDir.setPreferredSize(new java.awt.Dimension(300, 25));
        cbCurrentDir.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ToolsCvsExecutionPanel.this.cbCurrentDirFocusLost(evt);
            }
        });
        cbCurrentDir.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ToolsCvsExecutionPanel.this.cbCurrentDirItemStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
        add(cbCurrentDir, gridBagConstraints);

        btnCurrentDir.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/Bundle").getString("ToolsCvsExecutionPanel.btnCurrentDir.text"));
        btnCurrentDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ToolsCvsExecutionPanel.this.btnCurrentDirActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 5, 0, 11);
        add(btnCurrentDir, gridBagConstraints);

        lblCvsRoot.setLabelFor(txCvsRoot);
        lblCvsRoot.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/Bundle").getString("ToolsCvsExecutionPanel.lblCvsRoot.text"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 0);
        add(lblCvsRoot, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 0);
        add(txCvsRoot, gridBagConstraints);

        lblFiles.setLabelFor(trFiles);
        lblFiles.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/Bundle").getString("ToolsCvsExecutionPanel.lblFiles.text"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 2, 11);
        add(lblFiles, gridBagConstraints);

        spFiles.setMinimumSize(new java.awt.Dimension(300, 150));
        spFiles.setPreferredSize(new java.awt.Dimension(300, 150));
        spFiles.setViewportView(trFiles);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 11);
        add(spFiles, gridBagConstraints);

        commandPanel.setLayout(new java.awt.GridBagLayout());

        lblCommand.setLabelFor(cbCommand);
        lblCommand.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/Bundle").getString("ToolsCvsExecutionPanel.lblCommand.text"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        commandPanel.add(lblCommand, gridBagConstraints);

        cbCommand.setMaximumRowCount(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
        commandPanel.add(cbCommand, gridBagConstraints);
        cbCommand.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/Bundle").getString("ACSD_cbCommand"));

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 11, 11);
        add(commandPanel, gridBagConstraints);

    }//GEN-END:initComponents
    
   private void cbCurrentDirItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbCurrentDirItemStateChanged
       // Add your handling code here:
       String item = (String)cbCurrentDir.getSelectedItem();
       createTree(item);
       findCvsRoot(item);
       
    }//GEN-LAST:event_cbCurrentDirItemStateChanged
   
   private void cbCurrentDirFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cbCurrentDirFocusLost
 /*      // Add your handling code here:
        DefaultComboBoxModel model = (DefaultComboBoxModel)cbCurrentDir.getModel();
        String item = (String)cbCurrentDir.getEditor().getItem();
       System.out.println("FocusLost");
       System.out.println("selItem=" + item);
        model.insertElementAt(item, 0);
        cbCurrentDir.setSelectedIndex(0);
  */
    }//GEN-LAST:event_cbCurrentDirFocusLost
   
    private void btnCurrentDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCurrentDirActionPerformed
        // Add your handling code here:
        String selected = (String)cbCurrentDir.getSelectedItem();
        File dir = null;
        if (selected == null || selected.trim().equals(""))
        {
            try
            {
                File defDir = new File(System.getProperty("user.home"));  // NOI18N
                if (Utilities.isUnix())
                    dir = defDir;
                else if (Utilities.isWindows())
                {
                    do
                    {
                        defDir = defDir.getParentFile();
                    }
                    while (defDir != null && defDir.getParentFile() != null && defDir.getParentFile().getParentFile() != null);
                    if (defDir != null)
                        dir = defDir;
                }
            }
            catch (Exception ex)
            {
                dir = null;
            }
        }
        else
            dir = new File(selected);
        if (dir == null) {
            dir = new File(""); // NOI18N
        }
        ChooseDirDialog chooseDir=new ChooseDirDialog(new JFrame(), dir);
        NbJavaCvsCustomizer.centerWindow(chooseDir);
        HelpCtx.setHelpIDString(chooseDir.getRootPane(), ToolsCvsExecutionPanel.class.getName());
        chooseDir.show();
        selected = chooseDir.getSelectedDir();
        if( selected==null ){
            //D.deb("no directory selected"); // NOI18N
            return ;
        }
        addToCombo(selected);
        createTree(selected);
        findCvsRoot(selected);
    }//GEN-LAST:event_btnCurrentDirActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCurrentDir;
    private javax.swing.JTree trFiles;
    private javax.swing.JComboBox cbCommand;
    private javax.swing.JLabel lblFiles;
    private javax.swing.JPanel commandPanel;
    private javax.swing.JLabel lblCvsRoot;
    private javax.swing.JTextArea helpTextArea;
    private javax.swing.JLabel lblCommand;
    private javax.swing.JTextField txCvsRoot;
    private javax.swing.JComboBox cbCurrentDir;
    private javax.swing.JScrollPane spFiles;
    private javax.swing.JLabel lblCurrentDir;
    // End of variables declaration//GEN-END:variables
    
    private static final java.util.ResourceBundle bundle = NbBundle.getBundle(ToolsCvsExecutionPanel.class);
    
    private void initAccessibility() {
        
        AccessibleContext context = this.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ToolsCvsExecutionPanel.helpTextArea.text"));
        
        context = btnCurrentDir.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_ToolsCvsExecutionPanel.btnCurrentDir"));
        
        context = txCvsRoot.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_ToolsCvsExecutionPanel.txCvsRoot"));
        
        context = trFiles.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_ToolsCvsExecutionPanel.trFiles"));
        
    }
    
    private void addToCombo(String fileString) {
        DefaultComboBoxModel model = (DefaultComboBoxModel)cbCurrentDir.getModel();
        int size = model.getSize();
        for (int index = 0; index < size; index++) {
            String one = (String)model.getElementAt(index);
            if (fileString.equals(one)) {
                return;
            }
        }
        model.insertElementAt(fileString, 0);
        cbCurrentDir.setSelectedIndex(0);
    }
    
    
    private void addTreeListeners() {
        trFiles.addTreeSelectionListener(this);
        trFiles.addTreeWillExpandListener(new TreeWillExpandListener() {
            public void treeWillExpand(TreeExpansionEvent evt) {
                TreePath path = evt.getPath();
                final MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        folderTreeNodes(node);
                    }
                });
            }
            public void treeWillCollapse(TreeExpansionEvent evt) {
                TreePath path = evt.getPath();
                final MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                node.setWasRead(false);
            }
        });
    }
    
    private void createTree(String rootString) {
        trFiles.setModel(new DefaultTreeModel(new MyTreeNode()));
        File rootDir = new File(rootString);
        if (rootDir.isDirectory()) {
            final MyTreeNode root = new MyTreeNode(rootDir);
            //            Runnable treeBuild = new Runnable() {
            //                public void run() {
            //recursiveTreeNodes(root);
            folderTreeNodes(root);
            //                }
            //            };
            //            new Thread(treeBuild, "Mount Panel Tree Build").start();
            trFiles.setModel(new DefaultTreeModel(root));
        }
        trFiles.setSelectionRow(0);
    }
    
    private void folderTreeNodes(final MyTreeNode parent) {
        boolean hasChild = false;
        if (parent.isWasRead()) return;
        synchronized (trFiles) {
            parent.removeAllChildren();
            parent.setWasRead(true);
            File parentFile = (File) parent.getUserObject();
            if (parentFile == null) return;
            MyTreeNode child;
            File childFile;
            File[] list = parentFile.listFiles();
            Arrays.sort(list, new MyComparator());
            for (int index = 0; index < list.length; index++) {
                if (list[index].exists()) {
                    childFile = list[index];
                    if (!childFile.getName().equals("CVS")) { //CVS dirs go out.. // NOI18N
                        hasChild = true;
                        child = new MyTreeNode(new File(childFile.getAbsolutePath()));
                        child.setAllowsChildren(childFile.isDirectory());
                        parent.add(child);
                    }
                }
            }
            if (!hasChild) {
                parent.setAllowsChildren(false);
                trFiles.collapsePath(new TreePath(parent.getPath()));
            }
        }
        ((DefaultTreeModel) trFiles.getModel()).nodeStructureChanged(parent);
        if (hasChild) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    trFiles.scrollPathToVisible(new TreePath(((MyTreeNode) parent.getLastChild()).getPath()));
                    trFiles.scrollPathToVisible(new TreePath(((MyTreeNode) parent.getFirstChild()).getPath()));
                }
            });
        }
    }
    
    public void valueChanged(javax.swing.event.TreeSelectionEvent p1) {
/*        TreePath path = trFiles.getSelectionPath();
        if (path != null) {
            MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
            File selFile = (File) node.getUserObject();
            MyTreeNode rootNode = (MyTreeNode) node.getRoot();
            File rootFile = (File) rootNode.getUserObject();
            if (rootFile.getAbsolutePath().equals(selFile.getAbsolutePath())) {
                return;
            }
            String toWrite = selFile.getAbsolutePath().substring(rootFile.getAbsolutePath().length());
            if (toWrite.startsWith(File.separator)) {
                toWrite = toWrite.substring(File.separator.length());
            }
        } else {
        }
 */
    }
    
    public void executeCommand() {
        Object selected = cbCurrentDir.getEditor().getItem();
        addToCombo((String)selected);
        TreePath[] paths = trFiles.getSelectionPaths();
        LinkedList list = new LinkedList();
        if (paths != null) {
            for (int i = 0; i < paths.length; i++) {
                TreePath path = paths[i];
                Object fobj = path.getLastPathComponent();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)fobj;
                fobj = node.getUserObject();
                if (fobj instanceof File) {
                    list.add(fobj);
                }
            }
        }
        if (list.size() == 0) {
            String item = (String)cbCurrentDir.getSelectedItem();
            list.add(new File(item));
        }
        File[] files = new File[list.size()];
        files = (File[])list.toArray(files);
        JavaCvsSettings settings = (JavaCvsSettings)SharedClassObject.findObject(
        JavaCvsSettings.class, true);
        IndependantClient client = new IndependantClient(settings.getHome());
        GlobalOptions opt = new GlobalOptions();
        opt.setCVSRoot(txCvsRoot.getText());
        client.setGlobalOptions(opt);
        client.setCvsRootString(txCvsRoot.getText());
        client.setUiMode(settings.getUiMode());
        client.setDisplayType(settings.getDisplayType());
        String localPath = (String)cbCurrentDir.getSelectedItem();
        client.setLocalPath(localPath);
        // load ignore list..
        java.util.List ignoreList = NbJavaCvsFileSystem.constructIgnoreList(settings.getHome(),
        new java.io.File(localPath, "CVSROOT")); // NOI18N
        IgnoreFileFilter ignoreFilter = new DefaultIgnoreFileFilter(ignoreList);
        client.setIgnoreFilter(ignoreFilter);
        FileSystemCommand command = createCommand(client, files);
        if (command != null) {
            command.setFiles(files);
            commandFactory.addIndependantRuntime(command);
            if (command.getClass().equals(CvsCheckout.class)) {
                commandFactory.showCustomizerAndRun(command, new NbCheckoutParamInput(), false);
                return;
            }
            commandFactory.showCustomizerAndRun(command, false, false);
        } else {
            //TODO error.
        }
    }
    
    private FileSystemCommand createCommand(IndependantClient client, File[] files) {
        int index = cbCommand.getSelectedIndex();
        FsCommandFactory fact = (FsCommandFactory)FsCommandFactory.getInstance();
        switch (index) {
            case 0 : {
                CvsUpdate command = (CvsUpdate)fact.createCommand(CvsUpdate.class,
                true, files, client);
                return command;
            }
            case 1 : {
                CvsCommit command = (CvsCommit)fact.createCommand(CvsCommit.class,
                true, files, client);
                return command;
            }
            case 2 : {
                CvsDiff command = (CvsDiff)fact.createCommand(CvsDiff.class,
                true, files, client);
                return command;
            }
            case 3 : {
                CvsStatus command = (CvsStatus)fact.createCommand(CvsStatus.class,
                true, files, client);
                return command;
            }
            case 4 : {
                CvsLog command = (CvsLog)fact.createCommand(CvsLog.class,
                true, files, client);
                return command;
            }
            case 5 : {
                CvsAnnotate annot = (CvsAnnotate)fact.createCommand(CvsAnnotate.class,
                true, files, client);
                return annot;
            }
            
            case 6 : {
                String[] modules = new String[files.length];
                String root = (String)cbCurrentDir.getSelectedItem();
                for (int i = 0; i < files.length; i++) {
                    String module = files[i].getAbsolutePath().substring(root.length());
                    module = module.replace('\\','/');
                    if (module.length() == 0) {
                        module = "."; // NOI18N
                    } else if (module.charAt(0) == '/') {
                        module = module.substring(1);
                    }
                    modules[i] = module;
                }
                CvsCheckout command = (CvsCheckout)fact.createCommand(CvsCheckout.class,
                true, new FileObject[] {}, client);
                command.setModules(modules);
                command.setLocalPath(root);
                return command;
            }
            case 7 : {
                // import
                String root = (String)cbCurrentDir.getSelectedItem();
                if (files == null || files.length == 0) return null;
                String localPath = files[0].getAbsolutePath();
                client.setLocalPath(localPath);
                
                String start = localPath;
                start = start.substring(root.length());
                start = start.replace('\\', '/');
                if (start.startsWith("/")) { // NOI18N
                    start = start.substring(1);
                }
                String module = start;
                CvsImport myStat = (CvsImport)fact.createCommand(CvsImport.class,
                true, new FileObject[] {}, client);
                myStat.setModule(module);
                return myStat;
            }
            case 8 : {
                // export
                String[] modules = new String[files.length];
                String root = (String)cbCurrentDir.getSelectedItem();
                for (int i = 0; i < files.length; i++) {
                    String module = files[i].getAbsolutePath().substring(root.length());
                    module = module.replace('\\','/');
                    if (module.length() == 0) {
                        module = "."; // NOI18N
                    } else if (module.charAt(0) == '/') {
                        module = module.substring(1);
                    }
                    modules[i] = module;
                }
                client.setLocalPath(root);
                CvsExport myStat = (CvsExport)fact.createCommand(CvsExport.class,
                true, new FileObject[] {}, client);
                myStat.setModules(modules);
                return myStat;
            }
            
            case 9 : {
                CvsAdd command = (CvsAdd)fact.createCommand(CvsAdd.class,
                true, files, client);
                return command;
            }
            case 10 : {
                CvsRemove command = (CvsRemove)fact.createCommand(CvsRemove.class,
                true, files, client);
                return command;
            }
            case 11 : {
                CvsCompleteModList command = new CvsCompleteModList(client);
                command.addDisplayerListener(new ModulesListPanel());
                return command;
            }
        }
        return null;
    }
    
    private void findCvsRoot(String dirName) {
        File dir = new File(dirName);
        if (dir.exists()) {
            final String rootName = "CVS" + File.separator + "Root"; // NOI18N
            File rootFile = new File(dirName, rootName);
            if (rootFile.exists()) {
                BufferedReader buff = null;
                try {
                    buff = new BufferedReader(new InputStreamReader(new FileInputStream(rootFile)));
                    String line = buff.readLine();
                    txCvsRoot.setText(line);
                } catch (IOException exc) { //doesn't matter - nothing will be filled in
                    txCvsRoot.setText(""); // NOI18N
                }
                finally {
                    if (buff != null) {
                        try {
                            buff.close();
                        } catch (IOException eIO) {}
                    }
                }
                return;
            }
        }
        txCvsRoot.setText(""); // NOI18N
        return;
    }
    
    /** Informs you about if all necceseary values needed for executing command is set
     * @return true if user set CVS Working dir and CVSROOT variable
     * otherwise false
     */
    public boolean isValid() {
        validateValue = 0;
        if (txCvsRoot.getText().equals("") || txCvsRoot.getText().length() < 13 ) validateValue = validateValue | VALID_CVSROOT;  //length 13 'cause it must contains :pserver:@:/
        String localPath = (String)cbCurrentDir.getSelectedItem();
        if (localPath == null ) {
            validateValue = validateValue | VALID_WRK; 
        } else {
            File wrkdir = new File(localPath);
            if (!wrkdir.isDirectory() || !wrkdir.canWrite()) validateValue = validateValue | VALID_WRK;
        }
        return (validateValue == 0);
    }    
    
    private class MyTreeNode extends DefaultMutableTreeNode {
        
        static final long serialVersionUID = 2478352100056378993L;
        private boolean wasRead = false;
        
        public MyTreeNode() {
            super();
        }
        
        public MyTreeNode(Object userObject) {
            super(userObject);
        }
        
        public boolean isLeaf() {
            return !getAllowsChildren();
        }
        
        public void setWasRead(boolean read) {
            wasRead = read;
        }
        public boolean isWasRead() {
            return wasRead;
        }
    }
    
    private class MyComparator implements Comparator {
        
        public MyComparator() {
        }
        
        public boolean equals(Object obj) {
            if (obj instanceof MyComparator) {
                MyComparator obj2 = (MyComparator)obj;
                return true;
            }
            return false;
        }
        
        public int compare(java.lang.Object obj1,java.lang.Object obj2) {
            if (!(obj1 instanceof File) ||
            !(obj2 instanceof File)) return 0;
            File rev1 = (File)obj1;
            File rev2 = (File)obj2;
            int result = 0;
            if ((rev1.isDirectory() && !rev2.isDirectory())) {
                return  - 1;
            }
            if (!rev1.isDirectory() && rev2.isDirectory()) {
                return 1;
            }
            result = rev1.getName().compareTo(rev2.getName());
            return result;
        }
    }
    
    private class MyTreeCellRenderer extends DefaultTreeCellRenderer {
        String DEFAULT_FOLDER = "/org/openide/loaders/defaultFolder.gif"; // NOI18N
        String DEFAULT_OPEN_FOLDER = "/org/openide/loaders/defaultFolderOpen.gif"; // NOI18N
        String DEFAULT_FILE = "/org/openide/resources/defaultNode.gif"; // NOI18N
        static final long serialVersionUID = -1075722862531035018L;
        
        
        /** Finds the component that is capable of drawing the cell in a tree.
         * @param value value can be either Node or a value produced by models (like
         *   NodeTreeModel, etc.)
         * @return component to draw the value
         */
        public Component getTreeCellRendererComponent(
        JTree tree, Object value,
        boolean sel, boolean expanded,
        boolean leaf, int row, boolean hasFocus
        ) {
            Component comp = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                MyTreeNode node = (MyTreeNode)value;
                if (node != null) {
                    Object userObj = node.getUserObject();
                    if (userObj != null) {
                        File file = (File) userObj;
                        label.setText(file.getName());
                        if (!file.isDirectory()) {
                            java.net.URL url1 = this.getClass().getResource(DEFAULT_FILE);
                            label.setIcon(new ImageIcon(url1));
                            
                        } else {
                            File cvsSubDir = new File(file.getAbsolutePath(), "CVS"); // NOI18N
                            if (!cvsSubDir.exists()) {
                                label.setText(file.getName() + " [Local]"); // NOI18N
                            }
                            if (!expanded) {
                                java.net.URL url1 = this.getClass().getResource(DEFAULT_FOLDER);
                                label.setIcon(new ImageIcon(url1));
                            } else {
                                java.net.URL url2 = this.getClass().getResource(DEFAULT_OPEN_FOLDER);
                                label.setIcon(new ImageIcon(url2));
                                //                  label.setIcon(new ImageIcon(new java.net.URL("org.netbeans.openide.resources.defaultFolder.gif")));
                            }
                            
                        }
                    }
                }
            }
            return comp;
        }
        
    }
    
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.