1 19 20 package org.openide.nodes; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.*; 24 import java.util.*; 25 import javax.swing.AbstractAction ; 26 import javax.swing.Action ; 27 28 import junit.textui.TestRunner; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 32 import org.openide.nodes.*; 33 import org.openide.util.actions.SystemAction; 34 35 38 public class NodeTest extends NbTestCase { 39 40 public NodeTest(String name) { 41 super(name); 42 } 43 44 public static void main(String [] args) { 45 TestRunner.run(new NbTestSuite(NodeTest.class)); 46 } 47 48 public void testGetActions () throws Exception { 49 final SystemAction[] arr1 = { 50 SystemAction.get (PropertiesAction.class) 51 }; 52 final SystemAction[] arr2 = { 53 }; 54 55 AbstractNode an = new AbstractNode (Children.LEAF) { 56 public SystemAction[] getActions () { 57 return arr1; 58 } 59 60 public SystemAction[] getContextActions () { 61 return arr2; 62 } 63 }; 64 65 66 assertEquals ("getActions(false) properly delegates to getActions()", arr1, an.getActions (false)); 67 assertEquals ("getActions(true) properly delegates to getContextActions()", arr2, an.getActions (true)); 68 69 } 70 71 public void testPreferredAction() throws Exception { 72 final SystemAction a1 = SystemAction.get(PropertiesAction.class); 73 final Action a2 = new AbstractAction () { 74 public void actionPerformed(ActionEvent ev) {} 75 }; 76 final SystemAction a3 = SystemAction.get(OpenAction.class); 77 final Action a4 = new AbstractAction () { 78 public void actionPerformed(ActionEvent ev) {} 79 }; 80 Node n1 = new AbstractNode(Children.LEAF) { 82 { 83 setDefaultAction(a1); 84 } 85 }; 86 Node n2 = new AbstractNode(Children.LEAF) { 87 public SystemAction getDefaultAction() { 88 return a1; 89 } 90 }; 91 Node n4 = new AbstractNode(Children.LEAF) { 93 public Action getPreferredAction() { 94 return a1; 95 } 96 }; 97 Node n5 = new AbstractNode(Children.LEAF) { 99 { 100 setDefaultAction (a1); 101 } 102 103 public SystemAction getDefaultAction () { 104 return super.getDefaultAction (); 105 } 106 }; 107 Node n6 = new AbstractNode(Children.LEAF) { 108 public Action getPreferredAction() { 109 return a2; 110 } 111 }; 112 Node n7 = new AbstractNode(Children.LEAF) { 114 { 115 setDefaultAction(a1); 116 } 117 public SystemAction getDefaultAction() { 118 return a3; 119 } 120 }; 121 assertEquals(a1, n1.getDefaultAction()); 122 assertEquals(a1, n1.getPreferredAction()); 123 assertEquals(a1, n2.getDefaultAction()); 124 assertEquals(a1, n2.getPreferredAction()); 125 assertEquals(a1, n4.getDefaultAction()); 126 assertEquals(a1, n4.getPreferredAction()); 127 assertEquals(a1, n5.getPreferredAction()); 128 assertEquals(a1, n5.getDefaultAction()); 129 assertEquals(null, n6.getDefaultAction()); 130 assertEquals(a2, n6.getPreferredAction()); 131 assertEquals(a3, n7.getDefaultAction()); 132 assertEquals(a3, n7.getPreferredAction()); 133 } 134 135 public void testShortDescriptionCanBeSetToNull () { 136 class PCL extends NodeAdapter { 137 public int cnt; 138 139 public void propertyChange (PropertyChangeEvent ev) { 140 if (Node.PROP_SHORT_DESCRIPTION.equals (ev.getPropertyName ())) { 141 cnt++; 142 } 143 } 144 } 145 146 AbstractNode an = new AbstractNode (Children.LEAF); 147 an.setDisplayName ("My name"); 148 149 PCL pcl = new PCL (); 150 an.addNodeListener (pcl); 151 assertEquals ("My name", an.getShortDescription ()); 152 153 an.setShortDescription ("Ahoj"); 154 assertEquals ("Ahoj", an.getShortDescription ()); 155 assertEquals ("One change", 1, pcl.cnt); 156 157 an.setShortDescription (null); 158 assertEquals ("My name", an.getShortDescription ()); 159 assertEquals ("Second change", 2, pcl.cnt); 160 } 161 162 163 public static final class PropertiesAction extends OpenAction { 164 165 } 166 } 167 | Popular Tags |