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

import org.openide.DialogDisplayer;

import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
import org.netbeans.lib.cvsclient.command.status.StatusCommand;
import java.util.Iterator;
import java.util.LinkedList;
import java.text.MessageFormat;

import org.netbeans.modules.javacvs.events.CommandDisplayerAdapter;
import org.netbeans.modules.javacvs.commands.CvsDiff;
import org.netbeans.lib.cvsclient.command.PipedFileInformation;
import org.netbeans.lib.cvsclient.command.FileInfoContainer;
import org.netbeans.lib.cvsclient.command.Command;
import org.netbeans.modules.cvsclient.IndependantClient;
import org.netbeans.modules.cvsclient.NbJavaCvsFileSystem;
import java.io.File;
import org.netbeans.lib.cvsclient.command.diff.DiffCommand;
import org.netbeans.lib.cvsclient.command.diff.DiffInformation;
import org.netbeans.lib.cvsclient.command.status.StatusInformation;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;
import javax.swing.SwingUtilities;

/**
 *
 * @author  Milos Kleint
 */
public class DiffCommandDisplayer extends CommandDisplayerAdapter {
    
    private static final String BINARY="-kb"; //NOI18N

    private CvsDiff command;
    private DiffCommand diffCommand;
    private boolean isDir = false;
    private File[] files;
    private java.util.HashMap standaloneFiles;
    private LinkedList resultList;
    private LinkedList updInfoList;
    private Command currentCommand;
    private boolean includeUpdateCommand = false;
    private boolean binaryFilesWarningShown;
    private boolean noDiffFilesWarningShown;
    private java.util.HashMap binaryFiles;
    private java.util.Set noDiffFiles;
    
    
    /** Creates new StatusDisplayer */
    public DiffCommandDisplayer(CvsDiff comm) {
        command = comm;
    }

    /** Creates new StatusDisplayer */
    public DiffCommandDisplayer(CvsDiff comm, boolean includeUpdateInfo) {
        command = comm;
        setIncludeUpdateCommand(includeUpdateInfo);
    }

    /**
     * this method is called after library's commands execution
     */
    public void showAfterEachExecute() {
        if (currentCommand instanceof UpdateCommand) {
            return;
        }        
        if (currentCommand instanceof StatusCommand)
            return;
        if (isDir) {
/*TODO            LogTreeInfoPanel treePanel = (LogTreeInfoPanel)FsCommandFactory.findOpenDisplayer(
                    files[0], LogTreeInfoPanel.class, null);
            if (treePanel == null) {
                treePanel = new LogTreeInfoPanel(files[0], command);
            } else {
                treePanel.setCommand(command);
            }
            treePanel.setDataToDisplay(resultList);
            treePanel.displayOutputData();
 */
        } else {
            if (files.length != standaloneFiles.size()) {
                for (int i = 0; i < files.length; i++) {
                    if (standaloneFiles.get(files[i]) == null) {
                        noDiffFiles.add(files[i]);
                    }
                }
            }
        }
        
    }
    
    /**
     * this method is called before any of the library's commands 
     * that is stored in the queue in FileSystemCommand, is run.
     * @param currentCommand shows the command that will be executed.
     */
    public void showBeforeEachExecute(Command currentCommand) {
        this.currentCommand = currentCommand;
        if (currentCommand instanceof UpdateCommand) {
            return;
        }
        if (currentCommand instanceof StatusCommand)
            return;
        resultList = new LinkedList();
        diffCommand = (DiffCommand)currentCommand;
        files = command.getFiles();
        if (files[0].isDirectory()) {
            isDir = true;
        } else {
            isDir = false;
            standaloneFiles = new java.util.HashMap();
        }
    }
    
    /**
     * this one is called when the command's execution  fails for any reason.
     */
    public void showExecutionFailed(Exception exception) {
    }
    
    /**
     * When the library command's builder generates a FileInfoContainer object, the
     * Displayer is notified.
     * @param info - the generated information object
     */
    public void showFileInfoGenerated(FileInfoContainer info) {
        if (info == null) return;
        if (info instanceof PipedFileInformation) {
            updInfoList.add(info);
            return;
        }
        if (info instanceof StatusInformation) {
            StatusInformation statusInfo = (StatusInformation) info;
            String stickyTag = statusInfo.getStickyOptions();
            if (stickyTag != null && BINARY.equals(stickyTag))
                this.binaryFiles.put (statusInfo.getFile(),statusInfo.getFile());
            return;
        }
        if (isDir) {
            resultList.add(info);
        } else {
           final File fl = info.getFile();
           if (this.binaryFiles.get (fl) == null) {
              DiffInformation dInfo = (DiffInformation)info;
              if (dInfo.getFirstChange() != null) {
                  DiffDisplayer panel = new DiffDisplayer(diffCommand);
                  panel.setFileSystemCommand(command);
                  PipedFileInformation pInfo = getUpdInfo(dInfo);
                  panel.setData(dInfo, diffCommand, pInfo);                  
                  panel.displayOutputData();
                  standaloneFiles.put(dInfo.getFile(), dInfo.getFile());
              }
           }
        }
    }
    
    /**
     * This is the last method to be called in the displayer. 
     * Is called when the execution finishes. Any filan touchups can be made here.
     */
    public void showFinishedCommand() {
        Iterator it = binaryFiles.keySet().iterator();
        while (it.hasNext()) {
            noDiffFiles.remove(it.next());
        }
        int binLength = binaryFiles.size();
        int noDiffLength = noDiffFiles.size();
        if (binLength == 0 && noDiffLength == 0) return;
        try {
            Thread.sleep(3000);
        } catch (Exception ex) {
        }
        if (binLength == 1 && noDiffLength == 0) {
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    DialogDisplayer dialDisp = DialogDisplayer.getDefault();
                    Object message = null;
                    File fl = (File)binaryFiles.keySet().iterator().next();
                    message = MessageFormat.format(NbBundle.getBundle(DiffCommandDisplayer.class).getString("MSG_BinaryFile"),new Object[]{fl.getAbsolutePath()}); //NOI18N
                    NotifyDescriptor nd = new NotifyDescriptor.Message(message,NotifyDescriptor.INFORMATION_MESSAGE);
                    dialDisp.notify(nd);
                }
            });
            return;
        }
        if (binLength == 0 && noDiffLength == 1) {
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    DialogDisplayer dialDisp = DialogDisplayer.getDefault();
                    Object message = null;
                    File fl = (File)noDiffFiles.iterator().next();
                    message = MessageFormat.format(NbBundle.getBundle(DiffCommandDisplayer.class).getString("MSG_NoDiffFile"),new Object[]{fl.getAbsolutePath()}); //NOI18N
                    NotifyDescriptor nd = new NotifyDescriptor.Message(message,NotifyDescriptor.INFORMATION_MESSAGE);
                    dialDisp.notify(nd);
                }
            });
            return;
        }
        
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                DialogDisplayer dialDisp = DialogDisplayer.getDefault();
                Object message = null;
                message = new BinaryFilesWarning(DiffCommandDisplayer.this.binaryFiles.keySet(), DiffCommandDisplayer.this.noDiffFiles);
                NotifyDescriptor nd = new NotifyDescriptor.Message(message,NotifyDescriptor.INFORMATION_MESSAGE);
                dialDisp.notify(nd);
            }
        });
    }
    
    
    /**
     * This method is the first one that is called during execution.
     * Here any initial setup of the displayer can be made.
     */
    public void showStartCommand() {
        updInfoList = new LinkedList();
        resultList = new LinkedList();
        if (this.binaryFiles == null)
            this.binaryFiles = new java.util.HashMap ();
        else
            this.binaryFiles.clear();
        this.binaryFilesWarningShown = false;
        this.noDiffFilesWarningShown = false;
        noDiffFiles = new java.util.HashSet();
        
//        if (!command.getClientProvider() instanceof IndependantClient) return;
        IndependantClient provider = (IndependantClient)command.getClientProvider();
        if (provider.getDisplayType() != NbJavaCvsFileSystem.DISP_TYPE_SIMPLE
            || isIncludeUpdateCommand()) {
                command.addCheckoutCommands();
        }
        
    }
    
    private PipedFileInformation getUpdInfo(DiffInformation info) {
        Iterator it = updInfoList.iterator();
        PipedFileInformation toReturn = null;
        while (it.hasNext()) {
            PipedFileInformation pInfo = (PipedFileInformation)it.next();
            if (pInfo.getFile().equals(info.getFile())) {
                toReturn = pInfo;
                break;
            }
        }
        return toReturn;
    }
 
   
     /**
     * 
     */
    public void setIncludeUpdateCommand(boolean include) {
        includeUpdateCommand = include;
    }
    
    public boolean isIncludeUpdateCommand() {
        return includeUpdateCommand;
    }
    
}
... 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.