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.api.nodes2looks;

import java.beans.PropertyChangeListener;
import junit.framework.*;
import junit.textui.TestRunner;
import java.util.*;
import java.util.Arrays;
import org.netbeans.spi.looks.*;
import org.openide.nodes.*;

import org.netbeans.junit.*;
import org.openide.modules.ModuleInfo;
import org.openide.util.Lookup;

import org.openide.util.LookupListener;
import org.openide.util.lookup.*;

/** Tests whether notification to NodeListener is fired under Mutex.writeAccess
 *
 * @author Jiri Rechtacek
 */
public class IsLeafTest extends NbTestCase {
    public IsLeafTest(String name) {
        super(name);
    }
    
    protected void setUp() throws Exception {
        super.setUp();
        TestUtil.setUpRegistryToDefault();
    }
        
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite (IsLeafTest.class));
    }

    public void testChangeLeafToNonLeafRepObjects () {
        
        Children ch = new Children.Array ();
        Object repLeaf = new AbstractNode (Children.LEAF);
        Object repRoot = new AbstractNode (ch);
        Look look = Nodes.nodeLook();
        
        Node lnLeaf = Nodes.node( repLeaf, look );
        Node lnRoot = Nodes.node( repRoot, look );
        
        Node n = (Node)repLeaf;
        assertTrue ("NODE - Leaf is Leaf.", lnLeaf.isLeaf () );
        assertTrue ("NODE - Root is not Leaf.", !lnRoot.isLeaf () );
        
        assertTrue ("LOOK - Leaf is Leaf.", look.isLeaf ( repLeaf, Lookup.EMPTY ) );
        assertTrue ("LOOK - Root is not Leaf.", !look.isLeaf ( repRoot, Lookup.EMPTY ) );
        
        assertEquals ("Leaf has no children.", 0, look.getChildObjects (repLeaf, Lookup.EMPTY).size());
        assertEquals ("Root has no children.", 0, look.getChildObjects (repRoot, Lookup.EMPTY).size());
        
        ch.add (new Node[] { new AbstractNode (Children.LEAF) } );
        
        assertTrue ("NODE - Leaf is Leaf.", lnLeaf.isLeaf () );
        assertTrue ("NODE - Root is not Leaf.", !lnRoot.isLeaf () );
        
        assertTrue ("LOOK - Leaf is Leaf.", look.isLeaf ( repLeaf, Lookup.EMPTY ) );
        assertTrue ("LOOK - Root is not Leaf.", !look.isLeaf ( repRoot, Lookup.EMPTY ) );
        
        assertEquals ("Leaf has no children.", 0, look.getChildObjects (repLeaf, Lookup.EMPTY).size());
        assertEquals ("Root has children.", 1, look.getChildObjects (repRoot, Lookup.EMPTY).size());
        
    }

 
    public void testChangeLeafToNonLeaf () {
        
        
        javax.swing.JTextField rep = new javax.swing.JTextField( TestIsLeafLook.LEAF );
        
        TestIsLeafLook look = new TestIsLeafLook ("LeafTestLook");
        
        
        
        Node node = Nodes.node (rep, look);
        
        Listener l = new Listener ();
        node.addNodeListener (l);
        
        // initially is leaf
        assertTrue ("Look is leaf.", look.isLeaf ( rep, Lookup.EMPTY ));
        assertTrue ("LookNode is leaf.", node.isLeaf ());
        
        if (look.getChildObjects (rep, Lookup.EMPTY ) != null)
            assertEquals ("Look has no child objects.", 0, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
        l.assertEvents ("No property change", 0);
        
        // set children
        rep.setText ( TestIsLeafLook.PARENT );
        
        look.refreshChildren( rep );
        assertTrue ("Look is not leaf.", !look.isLeaf (rep, Lookup.EMPTY ));
        assertTrue ("LookNode is not leaf.", !node.isLeaf ());
        
        if (look.getChildObjects ( rep, Lookup.EMPTY ) != null)
            assertEquals ("Look has one child object.", 1, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
        else
            fail ("Look has one child object.");
        l.assertEvents ("One property change", 1);
        
        // check vasted events
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        l.assertEvents ("No property change", 0);
        
        // no children
        rep.setText (TestIsLeafLook.LEAF);
        look.refreshChildren ( rep );
        assertTrue ("Look is leaf.", look.isLeaf ( rep, Lookup.EMPTY ));
        assertTrue ("LookNode is leaf.", node.isLeaf ());
        if (look.getChildObjects ( rep, Lookup.EMPTY ) != null)
            assertEquals ("Look has no child objects.", 0, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
        l.assertEvents ("One property change", 1);
        
        // check vasted events
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        l.assertEvents ("No property change", 0);
        
        // set children
        rep.setText (TestIsLeafLook.PARENT);
        look.refreshChildren ( rep );
        look.refreshChildren ( rep );
        assertTrue ("Look is not leaf.", !look.isLeaf ( rep, Lookup.EMPTY ));
        assertTrue ("LookNode is not leaf.", !node.isLeaf ());
        if (look.getChildObjects ( rep, Lookup.EMPTY ) != null)
            assertEquals ("Look has one child object.", 1, look.getChildObjects ( rep, Lookup.EMPTY ).size ());
        else
            fail ("Look has one child object.");
        l.assertEvents ("One property change", 1);
    
    }
    
    private static class Listener extends Object implements NodeListener {
        private int leafs = 0;
        
        public void assertEvents (String txt, int eventsCount) {
            if (leafs != -1) 
                assertEquals (txt + " leafs", eventsCount, leafs);
            leafs = 0;
        }
        
        public void propertyChange (java.beans.PropertyChangeEvent evt) {
            if (Node.PROP_LEAF == evt.getPropertyName()) {
                leafs++;
            }
        }
        public void childrenAdded(NodeMemberEvent ev) {}
        public void childrenRemoved(NodeMemberEvent ev) {}
        public void childrenReordered(NodeReorderEvent ev) {}
        public void objectDestroyed(NodeEvent ev) {}
      
        public void nodeDestroyed(NodeEvent ev) {
        }
        
    } // end of Listener
    
    // substitute with public fireChildrenChange() method
    
    
    private static class TestIsLeafLook extends Look {
        
        final static String LEAF = "Leaf";
        final static String PARENT = "Parent";
                
        TestIsLeafLook( String name ) {
            super( name );
        }

        public List getChildObjects( Object ro, Lookup env ) {
            String s = ((javax.swing.JTextField)ro).getText ();
            if (LEAF.equals (s)) {
                return null;
            } else {
               return Arrays.asList (new Object[] {"Child"});
            }
        }

        public boolean isLeaf ( Object ro, Lookup env ) {
            return getChildObjects (ro, env) == null ? true : getChildObjects (ro, env).size () == 0;
        }

        public void refreshChildren( Object o ) {
            fireChange( o, Look.GET_CHILD_OBJECTS );
        }
    };
    
}

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