1 19 20 package org.openide.nodes; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.*; 25 26 import junit.textui.TestRunner; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.junit.NbTestSuite; 29 30 31 32 35 public class SheetTest extends NbTestCase { 36 37 public SheetTest(String name) { 38 super(name); 39 } 40 41 public static void main(String [] args) { 42 TestRunner.run(new NbTestSuite(SheetTest.class)); 43 } 44 45 public void testSheetEvents() { 46 47 AbstractNode node = new AbstractNode( Children.LEAF ); 48 49 Sheet sheet = node.getSheet(); 50 51 SheetListener sl = new SheetListener(); 52 TestNodeListener tnl = new TestNodeListener(); 53 54 node.addNodeListener( tnl ); 55 node.addPropertyChangeListener( sl ); 56 57 Sheet.Set ss = new Sheet.Set(); 58 ss.setName("Karel"); 59 sheet.put( ss ); 60 61 tnl.assertEvents( "NodePropertySets change", new PropertyChangeEvent [] { 62 new PropertyChangeEvent ( node, Node.PROP_PROPERTY_SETS, null, null ) 63 } ); 64 65 sl.assertEvents( "No events", new PropertyChangeEvent [] {} ); 66 67 PropertySupport.Name prop = new PropertySupport.Name (node); 68 ss.put (prop); 69 70 tnl.assertEvents( "NodePropertySets change again", new PropertyChangeEvent [] { 71 new PropertyChangeEvent ( node, Node.PROP_PROPERTY_SETS, null, null ) 72 } ); 73 74 sl.assertEvents( "No events fired", new PropertyChangeEvent [] {} ); 75 76 sheet.remove(ss.getName()); 77 78 tnl.assertEvents( "NodePropertySets change", new PropertyChangeEvent [] { 79 new PropertyChangeEvent ( node, Node.PROP_PROPERTY_SETS, null, null ) 80 } ); 81 82 sl.assertEvents( "No events", new PropertyChangeEvent [] {} ); 83 84 ss.remove (prop.getName()); 85 86 tnl.assertEvents( "No change in Node, as the set is removed", new PropertyChangeEvent [] {} ); 87 sl.assertEvents( "No events fired", new PropertyChangeEvent [] {} ); 88 } 89 90 91 private static class SheetListener implements PropertyChangeListener { 92 93 List events = new ArrayList(); 94 95 public void propertyChange(java.beans.PropertyChangeEvent evt) { 96 events.add( evt ); 97 } 98 99 public void assertEvents( String message, PropertyChangeEvent [] pevents ) { 100 101 if ( events.size() != pevents.length ) { 102 fail( message ); 103 } 104 105 int i = 0; 106 for( Iterator it = events.iterator(); it.hasNext(); i++ ) { 107 PropertyChangeEvent pche = (PropertyChangeEvent )it.next(); 108 assertEquals( message + " [" + i + "] ", pevents[i].getSource(), pche.getSource ()); 109 assertEquals( message + " [" + i + "] ", pevents[i].getPropertyName(), pche.getPropertyName()); 110 assertEquals( message + " [" + i + "] ", pevents[i].getOldValue (), pche.getOldValue()); 111 assertEquals( message + " [" + i + "] ", pevents[i].getNewValue(), pche.getNewValue ()); 112 assertEquals( message + " [" + i + "] ", pevents[i].getPropagationId(), pche.getPropagationId()); 113 } 114 115 events.clear(); 116 } 117 118 } 119 120 private static class TestNodeListener extends SheetListener implements NodeListener { 121 122 public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) { 123 } 124 125 public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) { 126 } 127 128 public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) { 129 } 130 131 public void nodeDestroyed(org.openide.nodes.NodeEvent ev) { 132 } 133 134 } 135 136 } 137 | Popular Tags |