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;

import org.openide.nodes.Node;
import org.openide.nodes.Children;
import org.openide.nodes.FilterNode;
import org.openide.nodes.AbstractNode;
import org.openide.util.actions.SystemAction;
import org.openide.util.NbBundle;
import org.openide.util.WeakListeners;
import org.openide.actions.*;
import org.openide.cookies.OpenCookie;
import org.openide.ErrorManager;
import org.openide.loaders.DataObject;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.modules.java.ui.nodes.elements.*;
import org.netbeans.modules.java.JavaDataObject;
import org.netbeans.modules.javacore.internalapi.JavaMetamodel;

import javax.jmi.reflect.JmiException;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import org.netbeans.modules.javacore.internalapi.JMIElementCookie;

/**
 * Default implementation of {@link SourceNodeFactory}
 * XXX clipboard and new actions are disabled yet
 * @author Jan Pokorsky
 */
public final class JavaSourceNodeFactory extends FilterSourceNodeFactory {
    
    private static final JavaSourceNodeFactory DEFAULT = new JavaSourceNodeFactory();
    
    /** Create nodes for tree
     * see org.netbeans.modules.java.JavaElementNodeFactory
     */ 
    private boolean tree = false;
    
    public static SourceNodeFactory getDefault() {
        return DEFAULT;
    }
    
    public Node createFieldNode(Field element) {
        FieldNode n = new FieldNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
        n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
        return new CompatibleNode(n);
    }

    public Node createConstructorNode(Constructor element) {
        ConstructorNode n = new ConstructorNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
        n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
        return new CompatibleNode(n);
    }

    public Node createMethodNode(Method element) {
        MethodNode n = new MethodNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
        n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
        return new CompatibleNode(n);
    }

    public Node createInitializerNode(Initializer element) {
        InitializerNode n = new InitializerNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
        n.setActions(INITIALIZER_ACTIONS, SystemAction.get(OpenAction.class));
        return new CompatibleNode(n);
    }

    public Node createEnumNode(JavaEnum element) {
        SourceNodeFactory snf;
        EnumNode n;
        if (tree) {
            snf = SourceNodes.getBrowserFactory();
            EnumChildren children = new EnumChildren(snf, element);
            EnumFilter filter = new EnumFilter();
            n = new EnumNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element));

            n.setElementFormat(new ElementFormat (
                                   NbBundle.getBundle(JavaDataObject.class).getString("CTL_Class_name_format") // NOI18N
                               ));

            // filter out inner classes
            filter.setOrder(new int[] {
                                 EnumFilter.CONSTANTS,
                                 ClassElementFilter.FIELD,
                                 ClassElementFilter.CONSTRUCTOR,
                                 ClassElementFilter.METHOD,
                             });
            children.setFilter(filter);
        } else {
            snf = SourceNodes.getExplorerFactory();
            n = new EnumNode(element, Categories.createEnumChildren(element, snf, true), !JavaMetamodel.getManager().isElementGuarded(element));
        }
        n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
        return new CompatibleNode(n);
    }

    public Node createEnumConstantNode(EnumConstant element) {
        EnumConstantNode n = new EnumConstantNode(element, Children.LEAF, !JavaMetamodel.getManager().isElementGuarded(element));
        n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
        return n;
    }

    public Node createAnnotationTypeNode(AnnotationType element) {
        SourceNodeFactory snf;
        AnnotationTypeNode n;
        
        if (tree) {
            snf = SourceNodes.getBrowserFactory();
            AnnotationTypeChildren children = new AnnotationTypeChildren(snf, element);
            AnnotationTypeFilter filter = new AnnotationTypeFilter();
            n = new AnnotationTypeNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element));
            n.setElementFormat(new ElementFormat(
                    NbBundle.getBundle (JavaDataObject.class).getString("CTL_Class_name_format") // NOI18N
            ));

            // filter out inner classes
            filter.setOrder(new int[] {
                ClassElementFilter.FIELD,
                ClassElementFilter.METHOD,
            });
        } else {
            snf = SourceNodes.getExplorerFactory();
            n = new AnnotationTypeNode(
                    element, Categories.createAnnotationTypeChildren(element, snf, true), 
                    !JavaMetamodel.getManager().isElementGuarded(element));
        }
        n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
        return n;
    }

    public Node createAnnotationTypeMethodNode(Attribute element) {
        ElementNode n = new AnnotationTypeMethodNode(element, !JavaMetamodel.getManager().isElementGuarded(element));
        n.setActions(DEFAULT_ACTIONS, SystemAction.get(OpenAction.class));
        return n;
    }

    public Node createClassNode(JavaClass element) {
        if (element == null) {
            // XXX hack for beans module; see JavaElementNodeFactory.FACTORY_GETTER_NODE and org.netbeans.modules.beans.PatternsBrowserFactory.createClassNode
            return super.createClassNode(element);
        }
        
        ClassNode n ;
        if (tree) {
            SourceNodeFactory snf = SourceNodes.getBrowserFactory();
            ClassChildren children = new ClassChildren(snf, element);
            ClassElementFilter filter = new ClassElementFilter();
            n = new ClassNode(element, children, !JavaMetamodel.getManager().isElementGuarded(element));

            n.setElementFormat(new ElementFormat (
                                   NbBundle.getBundle (JavaDataObject.class).getString("CTL_Class_name_format") // NOI18N
                               ));

            // filter out inner classes
            filter.setOrder (new int[] {
                                 ClassElementFilter.FIELD,
                                 ClassElementFilter.CONSTRUCTOR,
                                 ClassElementFilter.METHOD,
                             });
            children.setFilter (filter);
        } else {
            SourceNodeFactory snf = SourceNodes.getExplorerFactory();
            n = new ClassNode(element, Categories.createClassChildren(element, snf, true), !JavaMetamodel.getManager().isElementGuarded(element));
        }
        
        n.setActions(CLASS_ACTIONS, SystemAction.get(OpenAction.class));
        return new CompatibleNode(n);
    }

    public Node createErrorNode() {
        AbstractNode n = new AbstractNode(Children.LEAF);
        n.setName(NbBundle.getMessage(ElementNode.class, "Error")); // NOI18N
        n.setIconBase(IconStrings.ERROR);
        return n;
    }

    public Node createWaitNode() {
        AbstractNode n = new AbstractNode(Children.LEAF);
        n.setName(NbBundle.getMessage(ElementNode.class, "Wait")); // NOI18N
        n.setIconBase(IconStrings.WAIT);
        return n;
    }

    /** If true generate nodes for tree.
    */
    public void setGenerateForTree (boolean tree) {
        this.tree = tree;
    }

    /** Returns true if generate nodes for tree.
    * @return true if generate nodes for tree.
    */
    public boolean getGenerateForTree () {
        return tree;
    }


    /** Array of the actions of the java methods, constructors and fields. */
    private static final SystemAction[] DEFAULT_ACTIONS = new SystemAction[] {
                SystemAction.get(OpenAction.class),
                null,
//                SystemAction.get(CutAction.class),
//                SystemAction.get(CopyAction.class),
                null,
                SystemAction.get(DeleteAction.class),
//                SystemAction.get(RenameAction.class),
                null,
                SystemAction.get(ToolsAction.class),
                SystemAction.get(PropertiesAction.class)
            };

    /** Array of the actions of the java intializers. */
    private static final SystemAction[] INITIALIZER_ACTIONS = new SystemAction[] {
                SystemAction.get(OpenAction.class),
                null,
//                SystemAction.get(CutAction.class),
//                SystemAction.get(CopyAction.class),
                null,
                SystemAction.get(DeleteAction.class),
                null,
                SystemAction.get(ToolsAction.class),
                SystemAction.get(PropertiesAction.class)
            };

    /** Array of the actions of the java classes. */
    private static final SystemAction[] CLASS_ACTIONS = new SystemAction[] {
                SystemAction.get(OpenAction.class),
                null,
//                SystemAction.get(CutAction.class),
//                SystemAction.get(CopyAction.class),
//                SystemAction.get(PasteAction.class),
//                null,
                SystemAction.get(DeleteAction.class),
//                SystemAction.get(RenameAction.class),
                null,
                SystemAction.get(NewAction.class),
                null,
                SystemAction.get(ToolsAction.class),
                SystemAction.get(PropertiesAction.class)
            };
    
    /** supplies cookies to ensure proper functionality of code that uses the old source hierarchy api */
    private static final class CompatibleNode extends FilterNode implements PropertyChangeListener {
        
        private org.openide.src.Element oldEl = null;
        
        public CompatibleNode(Node original) {
            super(original);
        }

        public Node.Cookie getCookie(Class type) {
            Node.Cookie nc = super.getCookie(type);
            if (nc == null) {
                org.openide.src.Element el = getOldElement();
                nc = el != null? el.getCookie(type): null;
            }
            return nc;
        }
        
        /** gets the source hierarchy element or null */
        private org.openide.src.Element getOldElement() {
            synchronized (this) {
                if (oldEl != null) {
                    return oldEl;
                }
            }
            
            // XXX #45361 workaround
//            Element elJMI = (Element) this.getOriginal().getLookup().lookup(Element.class);
            JMIElementCookie elCookie = (JMIElementCookie) this.getOriginal().
                    getCookie(JMIElementCookie.class);
            assert elCookie != null;
            Element elJMI = elCookie.getElement();
            org.openide.src.Element el = null;
            if (elJMI instanceof ClassMember) {
                try {
                    JavaMetamodel.getDefaultRepository().beginTrans(false);
                    try {
                        el = BridgeUtils.getElementForCookieSet((ClassMember) elJMI);
                    } finally {
                        JavaMetamodel.getDefaultRepository().endTrans();
                    }
                } catch (JmiException ex) {
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
                }
            }
            
            synchronized (this) {
                if (oldEl != null || el == null) {
                    return oldEl;
                }
                oldEl = el;
                oldEl.addPropertyChangeListener(WeakListeners.propertyChange(this, oldEl));
            }
            return oldEl; 
        }

        public void propertyChange(PropertyChangeEvent evt) {
            if (Node.PROP_COOKIE.equals(evt.getPropertyName())) {
                fireCookieChange();
            }
        }
    }
}
... 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.