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.vcscore;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

import org.openide.util.*;

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

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

import org.netbeans.modules.vcscore.util.*;
import org.netbeans.modules.vcscore.cache.CacheHandler;
import org.netbeans.modules.vcscore.caching.VcsCache;
import org.netbeans.modules.vcscore.caching.RefreshCommandSupport;
import org.netbeans.modules.vcscore.caching.StatusFormat;
import org.netbeans.modules.vcscore.commands.VcsCommand;
import org.netbeans.modules.vcscore.commands.VcsDescribedCommand;
import org.openide.DialogDisplayer;

/** Recursively refresh directories.
 *
 * @author Michal Fadljevic
 */
public class RetrievingDialog extends JDialog implements Runnable, DirReaderListener {

    private JLabel      retrievingLabel;
    private JScrollPane listScrollPane;
    private JButton     stopButton;
    private JList       list;
    private DefaultListModel listData;

    private VcsFileSystem fileSystem=null;

    /** Directory for which recursive refresh started,
        e.g. "" or "src/org/netbeans" 
    */
    private String rootPath=null;

    /** Queue of the directories to be processed.
     */
    private Vector queue=new Vector(50);

    private boolean shouldStop=false;
    private boolean success = true;

    //-------------------------------------------
    static final long serialVersionUID =-6441709213287922213L;
    public RetrievingDialog(VcsFileSystem fileSystem, String rootPath,
                            Frame parent, boolean modal) {
        super (parent, modal);
        this.rootPath=rootPath;
        this.fileSystem=fileSystem;
        this.success = true;
        initComponents ();
        initAccessibility();
        pack ();
        HelpCtx.setHelpIDString (getRootPane (), RetrievingDialog.class.getName ());
    }

    //-------------------------------------------
    private void initComponents () {
        setBackground (new Color (192, 192, 192));
        setTitle ( g("CTL_Retrieving") ); // NOI18N
        addWindowListener (new WindowAdapter () {
                               public void windowClosing (WindowEvent evt) {
                                   closeDialog();
                               }
                           }
                          );
        getContentPane ().setLayout (new GridBagLayout ());
        GridBagConstraints gridBagConstraints1;

        retrievingLabel = new JLabel ();
        retrievingLabel.setText (g("CTL_Retrieving_directories")); // NOI18N
        gridBagConstraints1 = new GridBagConstraints ();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.gridwidth = 0;
        gridBagConstraints1.insets = new Insets (5, 5, 0, 0);
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.weightx = 0.2;
        gridBagConstraints1.weighty = 0.05;
        getContentPane ().add (retrievingLabel, gridBagConstraints1);

        listScrollPane = new JScrollPane ();
        listScrollPane.setPreferredSize (new Dimension(400, 70));
        listScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        list = new JList ();
        list.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
        listData=new DefaultListModel();
        list.setModel(listData);
        listScrollPane.add (list);

        listScrollPane.setViewportView (list);
        gridBagConstraints1 = new GridBagConstraints ();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.gridwidth = 4;
        gridBagConstraints1.gridheight = 4;
        gridBagConstraints1.fill = GridBagConstraints.BOTH;
        gridBagConstraints1.insets = new Insets (5, 5, 5, 5);
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.weighty = 0.9;
        getContentPane ().add (listScrollPane, gridBagConstraints1);

        stopButton = new JButton ();
        stopButton.setText (g("CTL_StopButtonLabel")); // NOI18N
        stopButton.setMnemonic(g("CTL_StopButtonLabel.mnemonic").charAt(0)); // NOI18N
        //stopButton.setLabel (g("CTL_StopButtonLabel")); // NOI18N
        gridBagConstraints1 = new GridBagConstraints ();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.insets = new Insets (0, 0, 5, 5);
        gridBagConstraints1.anchor = GridBagConstraints.EAST;
        gridBagConstraints1.weightx = 1.0;
        getContentPane ().add (stopButton, gridBagConstraints1);
        stopButton.addActionListener( new ActionListener(){
                                          public void actionPerformed(ActionEvent e) {
                                              stopButtonPressed(e);
                                          }
                                      }
                                    );
    }

    private void initAccessibility() {
        getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(RetrievingDialog.class, "ACSD_Retrieving"));
        retrievingLabel.setDisplayedMnemonic(NbBundle.getMessage(RetrievingDialog.class, "CTL_Retrieving_directories").charAt(0)); // NOI18N
        retrievingLabel.setLabelFor(list);
        list.getAccessibleContext().setAccessibleName(NbBundle.getMessage(RetrievingDialog.class, "ACSN_DirList"));
        list.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(RetrievingDialog.class, "ACSD_DirList"));
        stopButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(RetrievingDialog.class, "ACSD_StopButton"));
    }
    
    //-------------------------------------------
    private void stopButtonPressed(ActionEvent e){
        closeDialog();
    }


    //-------------------------------------------
    private void closeDialog() {
        shouldStop=true ;
        setVisible (false);
        dispose ();
    }


    //-------------------------------------------
    public void readDirFinished(String path, Collection rawData, boolean success){
        //VcsCacheDir[] sub = (VcsCacheDir[]) dir.getSubDirs();
        /*
        for(int i = 0; i < sub.length; i++) {
            String d = dir.getPath ()+"/"+sub[i].getName(); // NOI18N
            if (d.startsWith("/")) { // NOI18N
                d = d.substring(1);
            }
            if (success) queue.addElement(d);
            else {
                queue.removeAllElements();
                this.success = false;
            }
        }
         */

        // XXX use top level if (success) ...

        for(Iterator it = rawData.iterator(); it.hasNext(); ) {
            String fileName = StatusFormat.getFileName((String[]) it.next());
            if (fileName.endsWith("/")) { // NOI18N
                String d = path + "/" + fileName; // NOI18N
                if (success) {
                    queue.addElement(d.substring(0, d.length() - 1)); // remove the last slash
                } else {
                    queue.removeAllElements();
                    this.success = false;
                }
            }
            
        }
        this.success = this.success && success;

        ((VcsCache) CacheHandler.getInstance().getCache(fileSystem.getCacheIdStr())).readDirFinished(path, rawData, success, null);
        //fileSystem.getCache().readDirFinished(dir, rawData, success);
    }

    public void readDirFinishedRecursive(String path, VcsDirContainer rawData, boolean success) {
        // an empty method
    }


    //-------------------------------------------
    private void printMessage(String message){
        final String displayMessage=message;
        SwingUtilities.invokeLater( new Runnable() {
                                        public void run() {
                                            listData.addElement(displayMessage);
                                            int index=Math.max(0,listData.size()-1);
                                            list.setSelectedIndex(index);
                                            list.ensureIndexIsVisible(index);
                                            list.validate();
                                        }
                                    });
    }


    //-------------------------------------------
    public void run(){
        String message=null;

        queue.addElement(rootPath);
        boolean cancel = false;
        CommandSupport listSupport;
        if (fileSystem.isOffLine()) {
            listSupport = fileSystem.getCommandSupport(VcsCommand.NAME_REFRESH + VcsCommand.NAME_SUFFIX_OFFLINE);
            if (listSupport == null) {
                // TODO move to VcsUtilities, DLG_RefreshCommandDisabled is used from two packages
                if (org.openide.NotifyDescriptor.Confirmation.YES_OPTION.equals (
                    DialogDisplayer.getDefault ().notify (new org.openide.NotifyDescriptor.Confirmation (
                        org.openide.util.NbBundle.getMessage(RetrievingDialog.class,
                                                             "DLG_RefreshCommandDisabled"), // NOI18N
                        org.openide.NotifyDescriptor.Confirmation.YES_NO_OPTION)))) { // NOI18N
                    fileSystem.setOffLine(false);
                    listSupport = fileSystem.getCommandSupport(VcsCommand.NAME_REFRESH);
                }
            }
        } else {
            listSupport = fileSystem.getCommandSupport(VcsCommand.NAME_REFRESH);
        }
        if (listSupport == null) return ;
        Command cmd = listSupport.createCommand();
        if (!(cmd instanceof VcsDescribedCommand)) return ;
        VcsDescribedCommand list = (VcsDescribedCommand) cmd;
                //((VcsDescribedCommand) cmd).setFiles(new File[] { dir });
                //((VcsDescribedCommand) cmd).addDirReaderListener(new VcsCache.DirReaderListenerWithLock(locker));
                //VcsManager.showCustomizer(cmd);
                //cmd.execute();
        list.addDirReaderListener(this);
        show();
        while( queue.isEmpty()==false ){
            String path=(String)queue.remove(0);

            fileSystem.debug(g("MSG_Recursively_retrieving_directory",path)); // NOI18N
            String rootFolderLabel=g("MSG_Root_folder"); // NOI18N
            message=(path.equals("")?rootFolderLabel:path); // NOI18N
            printMessage(message);

            /*
            if (fileSystem.isOffLine()) {
                if (org.openide.NotifyDescriptor.Confirmation.YES_OPTION.equals (
                    org.openide.TopManager.getDefault ().notify (new org.openide.NotifyDescriptor.Confirmation (
                        org.openide.util.NbBundle.getMessage(org.netbeans.modules.vcscore.caching.VcsFSCache.class,
                                                             "DLG_RefreshCommandDisabled"), // NOI18N
                        org.openide.NotifyDescriptor.Confirmation.YES_NO_OPTION)))) { // NOI18N
                    fileSystem.setOffLine(false);
                } else {
                    //break ;
                }
            }
            VcsCommandExecutor reader= fileSystem.getVcsFactory ().getVcsDirReader (this, path);
            if (reader == null) {
                fileSystem.debug(org.openide.util.NbBundle.getMessage(org.netbeans.modules.vcscore.commands.CommandsPool.class, "MSG_Command_canceled")); // NOI18N
                queue.removeAllElements();
                cancel = true;
                continue;
            }
            fileSystem.getCommandsPool().startExecutor(reader, fileSystem);
            try {
                fileSystem.getCommandsPool().waitToFinish(reader);
                shouldStop = shouldStop || reader.getExitStatus() == VcsCommandExecutor.INTERRUPTED;
            } catch (InterruptedException iexc) {
                fileSystem.getCommandsPool().kill(reader);
                shouldStop = true;
            }
             */
            list.setDiskFiles(new java.io.File[] { fileSystem.getFile(path) });
            VcsManager.getDefault().showCustomizer(list);
            CommandTask reader = list.execute();
            try {
                reader.waitFinished(0);
                shouldStop = shouldStop || reader.getExitStatus() == reader.STATUS_INTERRUPTED;
            } catch (InterruptedException iexc) {
                reader.stop();
                shouldStop = true;
            }
            if (shouldStop) {
                message=g("MSG_Recursive_retrieving_interrupted_by_the_user"); // NOI18N
                printMessage(message);
                fileSystem.debug(message);
                return;
            }

        }

        stopButton.setText(g("CTL_CloseButtonLabel")); // NOI18N
        // The default button should not have mnemonics. We need to remove the
        // previously set mnemonic for Stop.
        stopButton.setMnemonic(0);
        getRootPane().setDefaultButton(stopButton);
        if (cancel) message=g("MSG_Recursive_retrieving_canceled"); // NOI18N
        else {
            if (success) message=g("MSG_Subtree_successfully_retrieved"); // NOI18N
            else message=g("MSG_Recursive_retrieving_failed"); // NOI18N
        }
        printMessage(message);
        fileSystem.debug(message);
    }


    //-------------------------------------------
    String g(String s) {
        return NbBundle.getBundle
               ("org.netbeans.modules.vcscore.Bundle").getString (s);
    }
    String  g(String s, Object obj) {
        return MessageFormat.format (g(s), new Object[] { obj });
    }
    String g(String s, Object obj1, Object obj2) {
        return MessageFormat.format (g(s), new Object[] { obj1, obj2 });
    }
    String g(String s, Object obj1, Object obj2, Object obj3) {
        return MessageFormat.format (g(s), new Object[] { obj1, obj2, obj3 });
    }
    //-------------------------------------------

}
... 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.