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.java.ui.nodes.elements;

import org.netbeans.jmi.javamodel.Attribute;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
import org.netbeans.modules.java.ui.nodes.editors.TypeEditor;
import org.openide.nodes.Children;
import org.openide.nodes.Sheet;
import org.openide.nodes.Node;
import org.openide.src.ElementProperties;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;

import javax.jmi.reflect.JmiException;
import java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.HashMap;

/** Node for a annotation type method.
 * @see org.netbeans.jmi.javamodel.Attribute
 * @author Jan Pokorsky
 */
public final class AnnotationTypeMethodNode extends ElementNode {
    
    private static final Map mapAttributeName;
    
    static {
        mapAttributeName = new HashMap();
        mapAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS);
        mapAttributeName.put(ElementProperties.PROP_NAME, ElementProperties.PROP_NAME);
        mapAttributeName.put("typeName", PROP_RETURN); // NOI18N
        mapAttributeName.put("defaultValueText", PROP_DEFAULT_VALUE); // NOI18N
    }
    
    /** Create a new method node.
    * @param element method element to represent
    * @param writeable true to be writable
    */
    public AnnotationTypeMethodNode(Attribute element, boolean writeable) {
        super(element, Children.LEAF, writeable, SourceOptions.PROP_ANNOTATION_TYPE_METHOD_FORMAT);
        setElementFormat0(getElementFormatProperty());
        superSetName(element.getName());
    }

    protected String resolveIconBase() {
        int modif = getMethod().getModifiers();
        if (Modifier.isStatic(modif)) {
            // static method...
            if (Modifier.isPrivate(modif))
                return METHOD_ST_PRIVATE;
            else if (Modifier.isProtected(modif))
                return METHOD_ST_PROTECTED;
            else if (Modifier.isPublic(modif))
                return METHOD_ST_PUBLIC;
            else
                return METHOD_ST_PACKAGE;
        }
        else {
            // non-static method...
            if (Modifier.isPrivate(modif))
                return METHOD_PRIVATE;
            else if (Modifier.isProtected(modif))
                return METHOD_PROTECTED;
            else if (Modifier.isPublic(modif))
                return METHOD_PUBLIC;
            else
                return METHOD_PACKAGE;
        }
    }

    protected ElementFormat getElementFormatProperty() {
        return getSourceOptions().getAnnTypeMethodElementFormat();
    }

    /* This method resolve the appropriate hint format for the type
    * of the element. It defines the short description.
    */
    protected ElementFormat getHintElementFormat() {
        return getSourceOptions().getAnnTypeMethodElementLongFormat();
    }

    protected Map getAttributeNameMap() {
        return mapAttributeName;
    }

    /* Creates property set for this node */
    protected Sheet createSheet () {
        Sheet sheet = Sheet.createDefault();
        Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
        ps.put(createModifiersProperty(writeable));
        ps.put(createNameProperty(getMethod()));
        ps.put(createReturnProperty(writeable));
        ps.put(createDefaultValueProperty(writeable));
        return sheet;
    }

    /** Create a property for the method default value.
    * @param canW false to force property to be read-only
    * @return the property
    */
    protected Node.Property createDefaultValueProperty(boolean canW) {
        return new ElementNode.ElementProp(PROP_DEFAULT_VALUE, String.class, canW) {

            public Object getValue () {
                String val = getMethod().getDefaultValueText(); 
                return val != null? val: ""; // NOI18N
            }
            
            /** Sets the value */
            public void setValue(final Object val) throws IllegalArgumentException,
                   IllegalAccessException, InvocationTargetException {
               
                super.setValue(val);
                if (!(val instanceof String)) throw new IllegalArgumentException();
                
                boolean fail = true;
                try {
                    JavaMetamodel.getDefaultRepository().beginTrans(true);
                    try {
                        getMethod().setDefaultValueText((String) val);
                        fail = false;
                    } finally {
                        JavaMetamodel.getDefaultRepository().endTrans(fail);
                    }
                } catch (JmiException e) {
                    IllegalArgumentException iae = new IllegalArgumentException();
                    iae.initCause(e);
                    ErrorManager.getDefault().annotate(iae, ErrorManager.USER, null,
                            NbBundle.getMessage(AnnotationTypeMethodNode.class, "MSG_InvalidDefaultVal"), null, null); // NOI18N
                    throw iae;
                }
            }
        };
    }

    /** Create a property for the method return value.
    * @param canW false to force property to be read-only
    * @return the property
    */
    private Node.Property createReturnProperty(boolean canW) {
        Node.Property np = createTypeProperty(PROP_RETURN, getMethod(), canW);
        np.setValue(TypeEditor.ANN_TYPE_EDITOR, Boolean.TRUE);
        return np;
    }
    
    private Attribute getMethod() {
        return (Attribute) this.element;
    }
}
... 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.