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 java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.HashMap;

import org.openide.nodes.*;
import org.openide.src.ElementProperties;
import org.netbeans.jmi.javamodel.Constructor;
import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.ClassDefinition;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;

import javax.jmi.reflect.JmiException;

/** Node representing a constructor.
* @see org.openide.src.ConstructorElement
* @author Petr Hamernik, Jan Pokorsky
*/
public final class ConstructorNode extends ElementNode {

    private static final Map mapAttributeName;
    
    static {
        mapAttributeName = new HashMap();
        mapAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS);
        mapAttributeName.put(PROP_PARAMETERS, PROP_PARAMETERS);
        mapAttributeName.put("exceptionNames", PROP_EXCEPTIONS); // NOI18N
    }
    
    /** Create a new constructor node.
    * @param element constructor element to represent
    * @param writeable true to be writable
    */
    public ConstructorNode(Constructor element, boolean writeable) {
        super(element, Children.LEAF, writeable, SourceOptions.PROP_CONSTRUCTOR_FORMAT);
        setElementFormat0(getElementFormatProperty());
    }

    /* Resolve the current icon base.
    * @return icon base string.
    */
    protected String resolveIconBase() {
        int modif = getConstructor().getModifiers();
        if (Modifier.isPrivate(modif))
            return CONSTRUCTOR_PRIVATE;
        else if (Modifier.isProtected(modif))
            return CONSTRUCTOR_PROTECTED;
        else if (Modifier.isPublic(modif))
            return CONSTRUCTOR_PUBLIC;
        else
            return CONSTRUCTOR_PACKAGE;
    }

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

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

    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());
        ps.put(createParametersProperty(false));
        ps.put(createExceptionsProperty(writeable));
        return sheet;
    }

    /** Indicate that this node cannot be renamed.
    * An constructor must have the same name like class
    * @return false
    */
    public boolean canRename() {
        return false;
    }
    
    private Node.Property createNameProperty() {
        
        return new ElementNode.ElementProp(ElementProperties.PROP_NAME, String.class, false) {
            public Object getValue() throws
                    IllegalAccessException, InvocationTargetException {

                try {
                    JavaMetamodel.getDefaultRepository().beginTrans(false);
                    try {
                        String name;
                        ClassDefinition cd = getConstructor().getDeclaringClass();
                        name = cd instanceof JavaClass? ((JavaClass) cd).getSimpleName(): cd.getName();
                        return name;
                    } finally {
                        JavaMetamodel.getDefaultRepository().endTrans();
                    }
                } catch (JmiException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
    }

    /** Create a node property for constructor parameters.
    * @param canW false to force property to be read-only
    * @return the property
    */
    private Node.Property createParametersProperty(boolean canW) {
        // this should be read-olny -> refactoring job
        Node.Property p = createParametersProperty(getConstructor(), canW);
        p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
        return p;
    }

    /** Create a node property for constructor exceptions.
    * @param canW false to force property to be read-only
    * @return the property
    */
    private Node.Property createExceptionsProperty(boolean canW) {
        Node.Property p = createExceptionsProperty(getConstructor(), canW);
        p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
        return p;
    }
    
    private Constructor getConstructor() {
        return (Constructor) 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.