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

import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.netbeans.modules.refactoring.ui.RefactoringPanel;
import org.netbeans.api.mdr.MDRepository;
import org.netbeans.modules.java.JavaEditor;
import org.netbeans.modules.refactoring.classpath.RefactoringClassPathImplementation;

import org.netbeans.spi.java.classpath.ClassPathFactory;
import org.openide.cookies.EditorCookie;

import org.openide.loaders.DataObject;
import org.openide.loaders.DataShadow;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.actions.NodeAction;
import org.openide.windows.TopComponent;

/** Generic superclass for refactoring actions.
 * Subclasses should implement createRefactoringUI and enabled methods.
 * 
 * @author Martin Matula
 */
public abstract class AbstractRefactoringAction extends NodeAction implements RefactoringAction, Runnable {
    private Node[] activatedNodes;
    private int caretPosition;
    private static DelegateTextAction delegate = new DelegateTextAction();

    /** Creates a new instance of AbstractRefactoringAction */
    public AbstractRefactoringAction(String name, Icon icon) {
        setName(name);
        setIcon(icon);
    }
    
    public final String getName() {
        return (String) getValue(Action.NAME);
    }
    
    protected void setName(String name) {
        putValue(Action.NAME, name);
    }
    
    protected void setMnemonic(char m) {
        putValue(Action.MNEMONIC_KEY, new Integer(m));
    }
    
    public final void performAction(Node[] n) {
        processContext(n, delegate.getTextComponent());
        run();
    }
    
    protected abstract boolean enabled(Node[] activatedNodes);
    
    /** Should be implemented by subclasses to provide instance of RefactoringUI
     * implementation for the implemented refactoring. The method should initialize
     * the RefactoringUI implementation with the provided array of nodes and textComponent.
     * @param nodes Array of activated nodes that the refactoring should be performed
     * on.
     * @param selectedElement org.netbeans.jmi.javamodel.Element at cursor position if the action is invoked in editor or null.
     * @return Implementation of RefactoringUI for the refactoring this action is
     * supposed to invoke.
     */
    protected abstract RefactoringUI createRefactoringUI(Node[] nodes, Element selectedElement);

    public final void processContext(Node[] activatedNodes, JTextComponent textComponent) {
        this.activatedNodes = activatedNodes;

        caretPosition=getCaretPosition(textComponent);
        boolean e = enabled(activatedNodes);
        //if (e != isEnabled()) {
            setEnabled(e);
        //}
    }
    
    private final int getCaretPosition(JTextComponent textComponent) {
        if (activatedNodes == null)
            return -1;
        EditorCookie ec = (EditorCookie) activatedNodes[0].getCookie(EditorCookie.class);
        if (ec != null) {
            TopComponent activetc = TopComponent.getRegistry().getActivated();
            if (activetc instanceof JavaEditor.JavaEditorComponent) {
                return textComponent.getCaretPosition();
            }
        }
        return -1;
    }

    public final void run() {
        Element selectedElement=null;
        MDRepository rep = JavaMetamodel.getDefaultRepository();
        RefactoringUI ui;
        
        rep.beginTrans(false);
        try {
//            JavaMetamodel.getManager().setClassPath(
//              ClassPathFactory.createClassPath(RefactoringClassPathImplementation.getDefault())
//            );
            if (caretPosition != -1) {
                DataObject dobj = (DataObject)activatedNodes[0].getCookie(DataObject.class);
                while (dobj instanceof DataShadow) {
                    dobj = ((DataShadow) dobj).getOriginal();
                }
                selectedElement = JavaMetamodel.getManager().getElementByOffset(dobj.getPrimaryFile(),caretPosition);
            }
            ui=createRefactoringUI(activatedNodes, selectedElement);
        } finally {
            rep.endTrans();
        }
        new RefactoringPanel(ui);
    }
    
    public boolean enable(Node[]a) {
        return enabled(a);
    }
    
    public org.openide.util.HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }
    
    protected void firePropertyChange(String name, Object oldValue, Object newValue) {
        if (PROP_ENABLED.equals(name)) {
            if (newValue != null) {
                super.firePropertyChange(name, oldValue, newValue);
            }
        } else {
            super.firePropertyChange(name, oldValue, newValue);
        }
    }

    protected boolean asynchronous() {
        return false;
    }
    
    private static final class DelegateTextAction extends TextAction {
        
        public DelegateTextAction() {
            super("");
        }
        
        public void actionPerformed(ActionEvent a) {
        }
        
        public JTextComponent getTextComponent() {
            return getFocusedComponent();
        }
    };

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