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-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.tree.children;

import java.beans.IntrospectionException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Collection;
import java.util.Collections;

import org.openide.nodes.Children;
import org.openide.nodes.Node;

import org.netbeans.tax.*;
import org.netbeans.tax.traversal.TreeNodeFilter;

import org.netbeans.modules.xml.tree.nodes.*;
import org.netbeans.modules.xml.tree.settings.NodeViewType;
import org.netbeans.modules.xml.tree.settings.TreeEditSettings;
import org.openide.util.WeakListener;

/**
 * Manages children according to content of passed TreeObjectList.
 *
 * @author  Libor Kramolis
 * @author  Petr Kuzel
 * @version 0.1
 */
public class ObjectListChildren extends Children.Keys {

    /** */
    private final TreeObjectList objectList;  // may be null

    /** */
    private final Class[] groupTypes;

    /** */
    private final boolean layer;

    /** */
    private NodeViewType nodeViewType;

    /** */
    private final ObjectListListener olListener;
    /** */
    private final NodeViewTypeListener nvtListener;
    private PropertyChangeListener weakNVTListener;


    //
    // init
    //

    /** */
    public ObjectListChildren (TreeObjectList objectList, Class[] groupTypes, boolean layer) {
        super();

        this.objectList   = objectList;
        this.nodeViewType = null;
        this.groupTypes   = groupTypes;
        this.layer        = layer;

        this.olListener   = new ObjectListListener();
        this.nvtListener  = new NodeViewTypeListener();
    }

    /** */
    public ObjectListChildren (TreeObjectList objectList, Class[] groupTypes) {
        this (objectList, groupTypes, false);
    }

    /** */
    public ObjectListChildren (TreeObjectList objectList) {
        this (objectList, null);
    }
    
    //
    // from Children.Keys
    //

    /**
     */
    protected void addNotify () {
        if ( this.objectList != null ) {
            this.objectList.addPropertyChangeListener (olListener);
        }

        setKeys();
    }

    /*
     * Removes listener
     */
    protected void removeNotify () {
        setKeys (Collections.EMPTY_SET);

        if ( this.objectList != null ) {
            this.objectList.removePropertyChangeListener (olListener);
        }
        
        super.removeNotify();
    }

        
    /**
     */
    protected final Node[] createNodes (Object key) {
        Node node = createNode (key);

        if (node == null) { //??? impossible ???
            return new Node[0];
        }
        return new Node [] { node };
    }


    //
    // itself
    //

    /** */
    private void setNodeViewTypeImpl (NodeViewType newNodeViewType) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("@@@ ObjectListChildren::setNodeViewTypeImpl: this = " + this); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("@@@                   ::setNodeViewTypeImpl: parent = " + getNode()); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("@@@                   ::setNodeViewTypeImpl: newNodeViewType = " + newNodeViewType);//, new RuntimeException()); // NOI18N
        if ( newNodeViewType == null ) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("@@@                   ::setNodeViewTypeImpl: newNodeViewType == NULL", new RuntimeException()); // NOI18N
        }

        if ( this.nodeViewType != null && weakNVTListener != null) {
            this.nodeViewType.removePropertyChangeListener (weakNVTListener);
        }

        this.nodeViewType = newNodeViewType;

        weakNVTListener = WeakListener.propertyChange(nvtListener, this.nodeViewType);
        this.nodeViewType.addPropertyChangeListener (weakNVTListener);
    }

    /** */
    public final void setNodeViewType (NodeViewType nodeViewType) {
        if ( Util.equals (this.nodeViewType, nodeViewType) ) {
            return;
        }

        setNodeViewTypeImpl (nodeViewType);

        refreshKeys();
    }

    /** */
    public final NodeViewType getNodeViewType () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("$$$ ObjectListChildren::getNodeViewType: this = " + this); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("$$$                   ::getNodeViewTypeImpl: parent = " + getNode()); // NOI18N

        if ( nodeViewType == null ) {
            Node parent = getNode();
            if ( parent instanceof NodeViewType.Provider ) {
                setNodeViewTypeImpl (((NodeViewType.Provider)parent).getNodeViewType());
            } else {
                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("$$$                   ::getNodeViewType: !!! parent is NOT instance of NodeViewType.Provider : " + parent); // NOI18N
            }
        }
        if (nodeViewType == null) {
            return new NodeViewType (NodeViewType.LINEAR_DATA);
        }

        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("$$$                   ::getNodeViewType: nodeViewType = " + nodeViewType); // NOI18N

        return nodeViewType;
    }

    /**
     */
    public TreeObjectList getObjectList () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("ObjectListChildren::getObjectList: " + objectList); // NOI18N

        return objectList;
    }


    /*
     * Recreates all keys and set them.
     */
    protected final void refreshKeys () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("ObjectListChildren::refreshKeys: isInitialized = " + isInitialized()); // NOI18N

//          if (!!! isInitialized())
//              return;

        setKeys();
    }
    
    /**
     */
    private void setKeys () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("ObjectListChildren::setKeys"); // NOI18N
        
        setKeys (createKeys());
    }

    /**
     */
    protected synchronized Collection createKeys () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n\nObjectListChildren::createKeys: groupTypes = " + groupTypes); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("                  ::createKeys: isGroupTypes = " + getNodeViewType().isGroupTypes()); // NOI18N

        Collection keys = new LinkedList();

        if ( ( groupTypes != null ) && ( layer == false ) && ( getNodeViewType().isGroupTypes() == true ) ) {
            keys.addAll (createTypeKeys());
        } else {
            keys.addAll (createFilterKeys());
        }

        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("                  ::createKeys: keys = " + keys); // NOI18N

        return keys;
    }

    /**
     */
    private Collection createFilterKeys () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren::createFilterKeys"); // NOI18N

        Collection keys = new LinkedList();
        Iterator it;

        if (getObjectList() == null)
            return keys;
        
        if ( layer == false ) {
            it = getObjectList().iterator();
        } else {
            it = getChildNodes (groupTypes[0]).iterator();
        }

        TreeNodeFilter filter = getNodeViewType().getFilter();
        boolean showWS = getNodeViewType().isShowIgnorableWhitespaces();

        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("                  ::createFilterKeys: showWS = " + showWS); // NOI18N

        while (it.hasNext()) {
            TreeObject treeObj = (TreeObject)it.next();

            if ( ( treeObj instanceof TreeNode ) &&
                 ( filter.acceptNode ((TreeNode)treeObj) != TreeNodeFilter.FILTER_ACCEPT ) ) {
                continue;
            }

            if ( showWS == false ) {
                if ( treeObj instanceof TreeText ) {
                    if ( ((TreeText)treeObj).onlyWhiteSpaces() ) {
                        continue;
                    }
                }
            }

            if ( treeObj instanceof TreeAttribute ) {
                if ( ((TreeAttribute)treeObj).isSpecified() == false ) {
                    continue;
                }
            }
            
            keys.add (new InstanceKey (treeObj));
        }

        return keys;
    }

    /**
     */
    private boolean hasChildNodes (Class type) {
	Iterator it = getObjectList().iterator();
	while (it.hasNext()) {
            Object obj = it.next();
            if ( obj instanceof TreeChild ) {
                TreeChild child = (TreeChild)obj;
                if (child.getClass().isAssignableFrom (type)) {
                    return true;
                } else if ( child instanceof TreeParentNode ) {
                    if ( ((TreeParentNode)child).hasChildNodes (type, true) ) {
                        return true;
                    }
                }

            } else {
                //???
            }
	}
        return false;
    }

    /**
     */
    private Collection getChildNodes (Class type) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren::getChildNodes: objectList = " + getObjectList()); // NOI18N
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("                  ::getChildNodes:       type = " + type); // NOI18N

	Collection allChildNodes = new LinkedList();
	Iterator it = getObjectList().iterator();
	while (it.hasNext()) {
            Object obj = it.next();

            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("                  ::getChildNodes:       next_obj = " + obj); // NOI18N

            if ( obj instanceof TreeChild ) {
                TreeChild child = (TreeChild)obj;
                if (child.getClass().isAssignableFrom (type)) {
                    allChildNodes.add (child);
                } else if ( child instanceof TreeParentNode ) {
                    Collection col = ((TreeParentNode)child).getChildNodes (type, true);

                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("                  ::getChildNodes:       child is parent : " + col); // NOI18N

                    allChildNodes.addAll (col);
                }
            } else {
                //???
            }
	}
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("ObjectListChildren::getChildNodes: allChildNodes = " + allChildNodes); // NOI18N

	return allChildNodes;
    }

    /**
     */
    private Collection createTypeKeys () {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren::createTypeKeys"); // NOI18N

        Collection keys = new LinkedList();

        for ( int i = 0; i < groupTypes.length; i++ ) {
            if ( hasChildNodes (groupTypes[i]) ) {
                keys.add (groupTypes[i]);
            }
        }

        return keys;
    }

    /**
     */
    protected Node createNode (Object key) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n\nObjectListChildren::createNode: key = " + key); // NOI18N

        Node node = null;

        if ( key instanceof InstanceKey ) {
            node = createFilterNode (((InstanceKey)key).getInstance());
        } else if ( key instanceof Class ) {
            node = createTypeNode ((Class)key);
        }

        return node;
    }

    /**
     */
    private Node createFilterNode (TreeObject treeObject) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren::createFilterNode: treeObject = " + treeObject); // NOI18N

        Node node = null;

        boolean bShowAttributes = TreeEditSettings.getDefault().getShowAttributeNodes();
        
        if( bShowAttributes )
        { 
            try {
                    if ( treeObject instanceof TreeObjectList ) {
                        node = createObjectListNode ((TreeObjectList)treeObject);
                    } else {
                        node = NodeFactory.createObjectNode (treeObject);
                    }
            } catch (IntrospectionException ex) {
                //let it be
            }
        } 
        else
        { 
            if ( ! (treeObject instanceof TreeObjectList) ) {
                node = NodeFactory.createObjectNode (treeObject);
            }
        } 
        
        return node;        
    }

    /**
     */
    private Node createObjectListNode (TreeObjectList treeObjectList) throws IntrospectionException {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("ObjectListChildren::createObjectListNode: treeObjectList = " + treeObjectList); // NOI18N

        Node node = null;
        TreeNode ownerNode = treeObjectList.getContentManager().getOwnerNode();

        if ( (ownerNode instanceof TreeElement)
             && (treeObjectList instanceof TreeNamedObjectMap) ) {
            node = new ElementAttributesNode ((TreeNamedObjectMap)treeObjectList);
        } else if ( ownerNode instanceof TreeDocumentType ) {
            node = new ExternalDTDNode (treeObjectList);
        } else {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\tMISSION IMPOSSIBLE -- createObjectListNode : what is it? : " + treeObjectList, new RuntimeException()); // NOI18N
        }

        return node;
    }

    /**
     */
    private Node createTypeNode (Class type) {
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren::createTypeNode: type = " + type); // NOI18N

        TreeObjectList objectList = getObjectList();
        Node node = null;

  	try {
            if ( type.isAssignableFrom (TreeElementDecl.class) ) {
                node = new ElementDeclLayerNode (objectList);
            } else if ( type.isAssignableFrom (TreeAttlistDecl.class) ) {
                node = new AttlistDeclLayerNode (objectList);
            } else if ( type.isAssignableFrom (TreeEntityDecl.class) ) {
                node = new EntityDeclLayerNode (objectList);
            } else if ( type.isAssignableFrom (TreeNotationDecl.class) ) {
                node = new NotationDeclLayerNode (objectList);
            } else {
		if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\tMISSION IMPOSSIBLE: what is it? : " + type, new RuntimeException()); // NOI18N
	    }
  	} catch (IntrospectionException ex) {
  	    //let it be
  	}
        
        return node;
    }


    //
    // NodeViewTypeListener
    //

    /**
     *
     */
    private class NodeViewTypeListener implements PropertyChangeListener {

        /**
         */
        public void propertyChange (PropertyChangeEvent pche) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren [ " + this + " ] ::NodeViewTypeListener::propertyChange: propertyName = '" + pche.getPropertyName() + "'"); // NOI18N

            if ( ( NodeViewType.PROP_GROUP_TYPES.equals (pche.getPropertyName()) ) ||
                 ( NodeViewType.PROP_SHOW_IGNORABLE_WHITESPACES.equals (pche.getPropertyName()) ) ||
                 ( NodeViewType.PROP_FILTER.equals (pche.getPropertyName()) ) ) {
                ObjectListChildren.this.refreshKeys();
            }
        }

    } // end: class NodeViewTypeListener


    //
    // ObjectListListener
    //

    /**
     *
     */
    private class ObjectListListener implements PropertyChangeListener {

        /**
         */
        public void propertyChange (PropertyChangeEvent pche) {
            if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\nObjectListChildren [ " + this + " ] ::ObjectListListener::propertyChange: propertyName = '" + pche.getPropertyName() + "'"); // NOI18N

            if ( ( TreeObjectList.PROP_CONTENT_INSERT.equals (pche.getPropertyName()) ) ||
                 ( TreeObjectList.PROP_CONTENT_REMOVE.equals (pche.getPropertyName()) ) ||
                 ( TreeObjectList.PROP_CONTENT_ORDER.equals (pche.getPropertyName()) ) ) {
                ObjectListChildren.this.refreshKeys();
            }
        }

    } // end: class ObjectListListener

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