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 java.lang.reflect.InvocationTargetException;
import java.beans.PropertyEditor;

import org.openide.nodes.*;
import org.openide.ErrorManager;
import org.openide.src.JavaDoc;
import org.netbeans.modules.javacore.internalapi.JMIElementCookie;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.netbeans.modules.java.ui.nodes.FilterSourceNodeFactory;
import org.netbeans.jmi.javamodel.*;

import javax.jmi.reflect.JmiException;


/** This FilterFactory descendant adds the JavaDoc property to every Method, Constructor, Field and
 * class node create for JavaDataObjects
 *
 * @author Petr Hrebejk
 */

public class JavaDocPropertySupportFactory extends FilterSourceNodeFactory {
    
    private static final String PROP_JAVADOCCOMMENT = "javadocComment"; // NOI18N
        
    public Node createMethodNode(Method element) {
        Node node = super.createMethodNode( element );
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createConstructorNode(Constructor element) {
        Node node = super.createConstructorNode( element );
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createFieldNode(Field element) {
        Node node = super.createFieldNode( element );
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createClassNode(final JavaClass element) {
        Node node = super.createClassNode( element );
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createAnnotationTypeMethodNode(Attribute element) {
        Node node = super.createAnnotationTypeMethodNode(element);
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createAnnotationTypeNode(AnnotationType element) {
        Node node = super.createAnnotationTypeNode(element);
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createEnumConstantNode(EnumConstant element) {
        Node node = super.createEnumConstantNode(element);
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }

    public Node createEnumNode(JavaEnum element) {
        Node node = super.createEnumNode(element);
        JavaDocPropertySupportFactory.addJavaDocProperty( node );
        return node;
    }
    // UTILITY FUNCTIONS ------------------------------------------------------------------------------

    /** Create a node property for javadoc comment for class element
    * @param canW if false, property will be read-only
    * @return the property
    */ 
    private static Node.Property createJavaDocProperty(boolean canW, final Node node ) {
        JavaDocPropertySupport prop = new JavaDocPropertySupport(PROP_JAVADOCCOMMENT, JavaDoc.class, canW) {

            private AutoCommenter.Element acElement;
            
            private AutoCommenter.Element getCommentable() throws InvocationTargetException {
                if (acElement == null) {
                    JMIElementCookie jc = (JMIElementCookie) node.getCookie(JMIElementCookie.class);
                    if (jc == null)
                      throw new IllegalStateException("missing JMIElementCookie: " + node); // NOI18N
                    ClassMember el = (ClassMember) jc.getElement();
                    try {
                        JavaMetamodel.getDefaultRepository().beginTrans(false);
                        try {
                            acElement = AutoCommenter.createAutoCommenterElement(el);
                        } finally {
                            JavaMetamodel.getDefaultRepository().endTrans();
                        }
                    } catch (JmiException e) {
                        throw new InvocationTargetException(e);
                    }
                }
                return acElement;
            }
            
            public Object getValue() throws
                    IllegalAccessException, InvocationTargetException {
                
                return getCommentable().getJavaDoc();
            }

            // Sets the value
            public void setValue(Object val) throws IllegalArgumentException, 
                    IllegalAccessException, InvocationTargetException {
            
                super.setValue(val);
                if (!(val instanceof JavaDoc))
                    throw new IllegalArgumentException("val: " + val); //NOI18N

                try {
                    JavaMetamodel.getDefaultRepository().beginTrans(true);
                    boolean fail = true;
                    try {
                        getCommentable().setJavaDoc((JavaDoc) val);
                        fail = false;
                    } finally {
                        JavaMetamodel.getDefaultRepository().endTrans(fail);
                    }
                } catch (JmiException e) {
                    throw new InvocationTargetException(e);
                }
            }
            
            public PropertyEditor getPropertyEditor() {
                try {
                    AutoCommenter.Element commentable = getCommentable();
                    return new JavaDocEditor(commentable);
                } catch (InvocationTargetException e) {
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, e.getCause());
                    return null;
                }
            }
        
        };

        return prop;
    }




    /** Adds JavaDoc property to node
     */

    private static void addJavaDocProperty(final Node node) {

        // PENDING: THIS IS A WORKAROUND FOR ADDING NEW PROPERTIES ONLY
        //          TO JAVA NODES NOT TO CLAZZ NODES

        Node.PropertySet [] ps = node.getPropertySets();

        for ( int i = 0; i < ps.length; i++ ) {
            if ( ps[i].getName().equals( Sheet.PROPERTIES ) && ps[i] instanceof Sheet.Set ) {
                ((Sheet.Set)ps[i]).put(createJavaDocProperty( true, node ));
            }
        }
    }


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