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.refactoring.ui;

import java.io.IOException;
import java.util.ArrayList;
import javax.swing.Action;
import org.netbeans.modules.refactoring.api.ui.AbstractRefactoringAction;
import org.openide.actions.ToolsAction;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.nodes.Node.Handle;

/** Filter node that adds Refactoring action to Java element nodes.
 *
 * @author Martin Matula
 */
public class RefactoringFilterNode extends FilterNode {
    private Action refactoringAction = null;
    private Action whereUsedAction = null;
    
    public RefactoringFilterNode(Node original) {
        super(original);
    }
    
    public Node cloneNode() {
        return new RefactoringFilterNode(getOriginal());
    }
    
    public Handle getHandle() {
        Handle origHandle = getOriginal().getHandle();
        // Simplest behavior: just store the original node and try to recreate
        // a filter based on that.
        if (origHandle != null)
            return new RefactoringFilterHandle(origHandle);
        else
            return null; // cannot persist original, do not persist filter
    }

    private static class RefactoringFilterHandle implements Handle {
        private static final long serialVersionUID = 1L;
        private Handle origHandle; // the only serializable state
        public RefactoringFilterHandle(Handle origHandle) {
            this.origHandle = origHandle;
        }
        public Node getNode() throws IOException {
            return new RefactoringFilterNode(origHandle.getNode());
        }
    }
    
    public boolean equals(Object o) {
        return this == o ||
               getOriginal().equals(o) ||
               (o != null && o.equals(getOriginal()));
    }

    public int hashCode() {
        return getOriginal().hashCode();
    }

    public Action[] getActions(boolean context) {
        Action[] orig = super.getActions(context);
        ArrayList ret = new ArrayList(orig.length + 1);
        
        if (refactoringAction == null) {
            refactoringAction = new RefactoringSubMenuAction(false);
            whereUsedAction = (WhereUsedAction) WhereUsedAction.findObject(WhereUsedAction.class, true);
        }
        
        for (int i = 0; i < orig.length; i++) {
            if (orig[i] instanceof ToolsAction) {
                ret.add(whereUsedAction);
                ret.add(refactoringAction);
            }
            if (!(orig[i] instanceof org.openide.actions.RenameAction)) {
                ret.add(orig[i]);
            }
        }

        if (!ret.contains(refactoringAction)) {
            ret.add(refactoringAction);
        }
        
        return (Action[]) ret.toArray(new Action[ret.size()]);
    }    
    
}
... 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.