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

import java.io.File;
import org.openide.awt.JMenuPlus;
import org.netbeans.modules.javacvs.events.CommandDisplayerAdapter;
import org.openide.util.*;
import java.lang.ref.*;
import java.util.*;
import org.netbeans.modules.vcscore.util.WeakList;
import org.netbeans.modules.vcscore.versioning.*;
import org.netbeans.modules.cvsclient.FsCommandFactory;
import org.netbeans.modules.cvsclient.NbJavaCvsFileSystem;
import org.netbeans.modules.cvsclient.commands.diff.DiffCommandDisplayer;
import org.netbeans.modules.cvsclient.commands.annotate.AnnotateDisplayer;
import org.openide.filesystems.FileObject;
import org.netbeans.modules.javacvs.commands.ClientProvider;
import org.netbeans.lib.cvsclient.command.*;

import org.netbeans.modules.javacvs.commands.FileSystemCommand;
import org.netbeans.modules.cvsclient.commands.ErrorLogPanel;
import org.netbeans.modules.cvsclient.commands.update.*;
import org.netbeans.modules.javacvs.commands.CvsUpdate;
import org.netbeans.modules.javacvs.commands.CvsDiff;
import org.netbeans.modules.javacvs.commands.CvsAnnotate;
import org.openide.nodes.Node;
import java.awt.event.ActionEvent;
import org.openide.util.actions.NodeAction;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import org.openide.awt.JInlineMenu;

/**
 * Node action for the revision explorer in javaCvs module
 * @author  mkleint
 * @version 
 */
public class JavaCvsVersioningAction extends NodeAction  {

    
    protected WeakReference fileSystem = new WeakReference(null);
    protected WeakReference fileObject = new WeakReference(null);
    protected Collection selectedRevisionItems = null;
    
    /** Creates new RevisionAction 
     * Gets revision actions from filesystem and acts on the givn file object.
     * @param fileSystem the file system to get the actions from
     * @param fo the file object to act on
     */
    public JavaCvsVersioningAction() {
    }

    public String getName(){
        return NbBundle.getBundle(JavaCvsVersioningAction.class).
                     getString("CTL_Revision_Action"); // NOI18N
    }

    //-------------------------------------------

    //-------------------------------------------
    public boolean enable(Node[] nodes){
        //D.deb("enable()"); // NOI18N
        return (nodes.length > 0 && nodes.length < 3);
    }

    //-------------------------------------------
    public HelpCtx getHelpCtx(){
        //D.deb("getHelpCtx()"); // NOI18N
        return null;
    }
    
        //-------------------------------------------
    public void performAction(Node[] nodes){
        //D.deb("performAction()"); // NOI18N
    }
    
    private JMenuItem createItem(String name, boolean switchable){
        JMenuItem item=null ;
        item=new JMenuItem(g(name));
        item.setActionCommand(name);
        item.addActionListener(this);
//        item.addMenuKeyListener(this);
        assignHelp (item, name);
        return item;
    }
    
    
    public JMenuItem getPopupPresenter() {
        Node[] nodes = getActivatedNodes();
        JInlineMenu inlineMenu = new JInlineMenu();
        LinkedList menu = new LinkedList();
        HelpCtx.setHelpIDString (inlineMenu, JavaCvsVersioningAction.class.getName ());
        JMenuItem item=null;
        boolean isOnOne = (nodes.length == 1);
        boolean isOnTwo = (nodes.length == 2);
        if (isOnOne) {
            item=createItem("JavaCvsVersioningAction.update", false); // NOI18N
            menu.add(item);
            item=createItem("JavaCvsVersioningAction.diff", false); // NOI18N
            menu.add(item);
            item=createItem("JavaCvsVersioningAction.merge", false); // NOI18N
            menu.add(item);
            item=createItem("JavaCvsVersioningAction.annotate", false); // NOI18N
            menu.add(item);
        }
        if (isOnTwo) {
            item=createItem("JavaCvsVersioningAction.merge2Rev", false); // NOI18N
            menu.add(item);
            item=createItem("JavaCvsVersioningAction.diff2Rev", false); // NOI18N
            menu.add(item);
        }
        inlineMenu.setMenuItems((JMenuItem[]) menu.toArray(new JMenuItem[menu.size()]));
        return inlineMenu;
    }
    
    
    public void setFileSystem(NbJavaCvsFileSystem fileSystem) {
        this.fileSystem = new WeakReference(fileSystem);
    }
    
    public void setFileObject(FileObject fileObject) {
        this.fileObject = new WeakReference(fileObject);
    }
    
    public void setSelectedRevisionItems(Collection items) {
        if (items == null) {
            this.selectedRevisionItems = null;
            return ;
        }
        this.selectedRevisionItems = new WeakList(items);
    }    

    public void actionPerformed(final ActionEvent e){
        String cmd = e.getActionCommand();
        //D.deb("cmd="+cmd); // NOI18N
        //Node[] nodes = getActivatedNodes();
        NbJavaCvsFileSystem fileSystem = (NbJavaCvsFileSystem) this.fileSystem.get();
        FileObject fo = (FileObject) this.fileObject.get();
        if (fileSystem == null || fo == null) return ;
        RevisionItem[] items = (RevisionItem[]) selectedRevisionItems.toArray(new RevisionItem[0]);
/*        
        if (nodes.length == 0) {
            //E.err("internal error nodes.length<1 TODO");
            return ;
        } else {
            for (int i = 0; i < nodes.length; i++) {
                if (nodes[i] instanceof RevisionNode) {
                    NbJavaCvsFileSystem nodeFS = (NbJavaCvsFileSystem) ((RevisionNode) nodes[i]).getFileSystem();
                    FileObject nodeFO = ((RevisionNode) nodes[i]).getFileObject();
                    if (fileSystem == null) fileSystem = nodeFS;
                    else if (fileSystem != nodeFS) return ;
                    if (fo == null) fo = nodeFO;
                    else if (fo != nodeFO) return ;
                }
            }
            if (fileSystem == null) return ;
        }
        */
        FileObject[] files = new FileObject[] {fo};
        if (cmd.equals("JavaCvsVersioningAction.update")) { // NOI18N
            RevisionItem selItem = items[0];
            CvsUpdate.UpdateImpl command = (CvsUpdate.UpdateImpl)fileSystem.createUpdate();
            command.setFileObjects(files);
            command.setUpdateByRevision(getBranchOrRevision(selItem));
            
            command.addDisplayerListener(new ErrorLogPanel(command.getOuterClassInstance()));
            fileSystem.prepareCommand(command.getOuterClassInstance());
            command.startCommand();
        }
        if (cmd.equals("JavaCvsVersioningAction.diff")) { // NOI18N
            RevisionItem selItem = items[0];
            CvsDiff.DiffImpl command = (CvsDiff.DiffImpl)fileSystem.createDiff();
            command.setFileObjects(files);
            command.setRevision1(getBranchOrRevision(selItem));
            command.addDisplayerListener(new ErrorLogPanel(command.getOuterClassInstance()));
            command.addDisplayerListener(new DiffCommandDisplayer((CvsDiff)command.getOuterClassInstance(), true));
            fileSystem.prepareCommand(command.getOuterClassInstance());
            command.startCommand();
        }
        if (cmd.equals("JavaCvsVersioningAction.annotate")) { // NOI18N
            RevisionItem selItem = items[0];
            CvsAnnotate.AnnotateImpl command = (CvsAnnotate.AnnotateImpl)fileSystem.createAnnotate();
            command.setFileObjects(files);
            command.setAnnotateByRevision(getBranchOrRevision(selItem));
            command.addDisplayerListener(new ErrorLogPanel(command.getOuterClassInstance()));
            command.addDisplayerListener(new AnnotateDisplayer((CvsAnnotate)command.getOuterClassInstance()));
            fileSystem.prepareCommand(command.getOuterClassInstance());
            command.startCommand();
        }        
        if (cmd.equals("JavaCvsVersioningAction.merge")) { // NOI18N
            RevisionItem selItem = items[0];
            CvsUpdate.UpdateImpl command = (CvsUpdate.UpdateImpl)fileSystem.createUpdate();
            command.setFileObjects(files);
            command.setMergeRevision1(getBranchOrRevision(selItem));
            command.addDisplayerListener(new ErrorLogPanel(command.getOuterClassInstance()));
            fileSystem.prepareCommand(command.getOuterClassInstance());
            command.startCommand();
        }        
        if (cmd.equals("JavaCvsVersioningAction.merge2Rev")) { // NOI18N
            RevisionItem item1 = items[0];
            RevisionItem item2 = items[1];
            CvsUpdate.UpdateImpl command = (CvsUpdate.UpdateImpl)fileSystem.createUpdate();
            command.setFileObjects(files);
            command.setMergeRevision1(getBranchOrRevision(item1));
            command.setMergeRevision2(getBranchOrRevision(item2));
            command.addDisplayerListener(new ErrorLogPanel(command.getOuterClassInstance()));
            fileSystem.prepareCommand(command.getOuterClassInstance());
            command.startCommand();
        }        
        if (cmd.equals("JavaCvsVersioningAction.diff2Rev")) { // NOI18N
            RevisionItem item1 = items[0];
            RevisionItem item2 = items[1];
            CvsDiff.DiffImpl command = (CvsDiff.DiffImpl)fileSystem.createDiff();
            command.setFileObjects(files);
            command.setRevision1(getBranchOrRevision(item1));
            command.setRevision2(getBranchOrRevision(item2));
            CvsDiff diff = (CvsDiff)command.getOuterClassInstance();
            diff.setIncludeStatusAndCheckout(true);
            command.addDisplayerListener(new ErrorLogPanel(command.getOuterClassInstance()));
            command.addDisplayerListener(new DiffCommandDisplayer((CvsDiff)command.getOuterClassInstance(), true));
            fileSystem.prepareCommand(command.getOuterClassInstance());
            command.startCommand();
        }
    }
    
    private String getBranchOrRevision(RevisionItem item) {
        if (item.isBranch()) {
            String[] tags = item.getTagNames();
            if (tags != null && tags.length > 0) {
                return tags[0];
            }
        } 
        return item.getRevision();
    }
    
    
    private String g(String s) {
        return NbBundle.getBundle
        ("org.netbeans.modules.cvsclient.versioning.Bundle").getString (s); // NOI18N
    }
    
    private void assignHelp (JMenuItem item, String commandName) {
        HelpCtx.setHelpIDString (item, JavaCvsVersioningAction.class.getName () + "." + commandName); // NOI18N
    }
}
... 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.