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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.javadoc.comments;

import org.openide.ErrorManager;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.HelpCtx;
import org.openide.util.actions.CookieAction;
import org.netbeans.modules.javacore.internalapi.JMIElementCookie;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.netbeans.jmi.javamodel.*;

import javax.jmi.reflect.JmiException;

/**
 * Corrects selected java source (or descendant) Javadoc.
 *
 * @author   Mauro Botelho
 */
public class CorrectJavaDocAction extends CookieAction {
        
    static final long serialVersionUID =4989490116568783623L;
        
    /** Human presentable name of the action. This should be
     * presented as an item in a menu.
     * @return the name of the action
     */
    public String getName () {
        return NbBundle.getBundle( CorrectJavaDocAction.class ).getString("CTL_CORRECTJAVADOC_MenuItem");   //NOI18N
    }
    
    /** Cookie classes contains one class returned by cookie () method.
     */
    protected final Class[] cookieClasses () {
        return new Class[] { JMIElementCookie.class };
    }
    
    /** All must be DataFolders or JavaDataObjects
     */
    protected int mode () {
        return MODE_ALL;
    }
    
    /** Help context where to find more about the action.
     * @return the help context for this action
     */
    public HelpCtx getHelpCtx () {
        return new HelpCtx (CorrectJavaDocAction.class);
    }
    
    /** Enable this action only if it is really possible to correct the javadoc
     * for this element.
     */
    protected boolean enable( Node[] activatedNodes ) {
        if (activatedNodes.length != 1) {
            return false;
        }
        JMIElementCookie elCookie = (JMIElementCookie) activatedNodes[0].getCookie(JMIElementCookie.class);
        if( elCookie == null)   //for situation getCookie returns null value
            return false;
        
        Element element = elCookie.getElement();
        if (!(element instanceof ClassMember)) 
            return false;
        
        try {
            JavaMetamodel.getDefaultRepository().beginTrans(false);
            try {
                AutoCommenter.Element jdElement = AutoCommenter.createAutoCommenterElement((ClassMember) element);
                if( jdElement == null ) //it is for example static initializer
                    return false;
        
                return jdElement.isCorrectable();
            } finally {
                JavaMetamodel.getDefaultRepository().endTrans();
            }
        } catch (JmiException e) {
            ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
        }
        return false;
    }

    /** This method is called by one of the "invokers" as a result of
     * some user's action that should lead to actual "performing" of the action.
     * This default implementation calls the assigned actionPerformer if it
     * is not null otherwise the action is ignored.
     */
    public void performAction ( Node[] nodes ) {
        JMIElementCookie c = (JMIElementCookie) nodes[0].getCookie(JMIElementCookie.class);
        Element element;
        if(c == null || !((element = c.getElement()) instanceof ClassMember))
            return; 
        
        try {
            JavaMetamodel.getDefaultRepository().beginTrans(true);
            boolean fail = true;
            try {
                AutoCommenter.Element jdElement = AutoCommenter.createAutoCommenterElement((ClassMember) element);
                if (jdElement.isCorrectable()) {
                    jdElement.autoCorrect();
                }
                fail = false;
            } finally {
                JavaMetamodel.getDefaultRepository().endTrans(fail);
            }
        } catch (JmiException e) {
            ErrorManager.getDefault().notify(e);
        } catch (org.openide.src.SourceException e) {
            ErrorManager.getDefault().notify(e);
        }
    }

    /**
     * no reason to run this in the AWT-event thread, moreover it touches MDR
     * @return true
     */ 
    protected boolean asynchronous() {
        return true;
    }
}    
... 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.