1 19 20 package org.netbeans.spi.palette; 21 22 import java.awt.datatransfer.Transferable ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.BeanInfo ; 25 import java.io.IOException ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.Action ; 28 import javax.swing.JPanel ; 29 import org.netbeans.modules.palette.Category; 30 import org.netbeans.modules.palette.Item; 31 import org.netbeans.modules.palette.Model; 32 import org.openide.nodes.Node; 33 import org.openide.util.Lookup; 34 35 39 public class ItemTest extends AbstractPaletteTestHid { 40 41 public ItemTest(String testName) { 42 super(testName); 43 } 44 45 48 public void testGetName() throws IOException { 49 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 50 Model model = pc.getModel(); 51 Category[] categories = model.getCategories(); 52 53 for( int i=0; i<itemNames.length; i++ ) { 54 Item[] items = categories[i].getItems(); 55 assertEquals( itemNames[i].length, items.length ); 56 for( int j=0; j<items.length; j++ ) { 57 Node node = getItemNode( categoryNames[i], itemNames[i][j] ); 58 assertEquals( node.getName(), items[j].getName() ); 59 } 60 } 61 } 62 63 66 public void testGetDisplayName() throws IOException { 67 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 68 Model model = pc.getModel(); 69 Category[] categories = model.getCategories(); 70 71 for( int i=0; i<itemNames.length; i++ ) { 72 Item[] items = categories[i].getItems(); 73 assertEquals( itemNames[i].length, items.length ); 74 for( int j=0; j<items.length; j++ ) { 75 Node node = getItemNode( categoryNames[i], itemNames[i][j] ); 76 assertEquals( node.getDisplayName(), items[j].getDisplayName() ); 77 } 78 } 79 } 80 81 84 public void testGetShortDescription() throws IOException { 85 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 86 Model model = pc.getModel(); 87 Category[] categories = model.getCategories(); 88 89 for( int i=0; i<itemNames.length; i++ ) { 90 Item[] items = categories[i].getItems(); 91 assertEquals( itemNames[i].length, items.length ); 92 for( int j=0; j<items.length; j++ ) { 93 Node node = getItemNode( categoryNames[i], itemNames[i][j] ); 94 assertEquals( node.getShortDescription(), items[j].getShortDescription() ); 95 } 96 } 97 } 98 99 102 public void testGetIcon() throws IOException { 103 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 104 Model model = pc.getModel(); 105 Category[] categories = model.getCategories(); 106 107 for( int i=0; i<itemNames.length; i++ ) { 108 Item[] items = categories[i].getItems(); 109 assertEquals( itemNames[i].length, items.length ); 110 for( int j=0; j<items.length; j++ ) { 111 Node node = getItemNode( categoryNames[i], itemNames[i][j] ); 112 assertEquals( node.getIcon( BeanInfo.ICON_COLOR_16x16 ), items[i].getIcon( BeanInfo.ICON_COLOR_16x16 ) ); 113 assertEquals( node.getIcon( BeanInfo.ICON_COLOR_32x32 ), items[i].getIcon( BeanInfo.ICON_COLOR_32x32 ) ); 114 } 115 } 116 } 117 118 121 public void testGetActions() throws IOException { 122 PaletteActions actions = new DummyActions(); 123 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 124 Model model = pc.getModel(); 125 126 Category[] categories = model.getCategories(); 127 128 for( int i=0; i<categories.length; i++ ) { 129 Item[] items = categories[i].getItems(); 130 for( int m=0; m<items.length; m++ ) { 131 Action [] itemActions = items[m].getActions(); 132 133 Action [] providedActions = actions.getCustomItemActions( items[m].getLookup() ); 134 135 for( int k=0; k<providedActions.length; k++ ) { 136 if( null == providedActions[k] ) 137 continue; 138 boolean found = false; 139 for( int j=0; j<itemActions.length; j++ ) { 140 if( null == itemActions[j] ) 141 continue; 142 if( itemActions[j].equals( providedActions[k] ) ) { 143 found = true; 144 break; 145 } 146 } 147 assertTrue( "Action " + providedActions[k].getValue( Action.NAME ) + " not found in palette actions.", found ); 148 } 149 } 150 } 151 } 152 153 156 public void testInvokePreferredAction() throws IOException { 157 DummyActions actions = new DummyActions(); 158 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 159 Model model = pc.getModel(); 160 Category[] categories = model.getCategories(); 161 162 for( int i=0; i<itemNames.length; i++ ) { 163 Item[] items = categories[i].getItems(); 164 assertEquals( itemNames[i].length, items.length ); 165 for( int j=0; j<items.length; j++ ) { 166 MyPreferredAction a = new MyPreferredAction(); 167 actions.setPreferredAction( a ); 168 items[j].invokePreferredAction( new ActionEvent ( new JPanel (), 0, "junittest") ); 169 assertEquals( 1, a.getActionInvocations() ); 170 } 171 } 172 } 173 174 private static class MyPreferredAction extends AbstractAction { 175 private int actionInvocations = 0; 176 177 public MyPreferredAction() { 178 super( "JunitAction" ); 179 } 180 181 public void actionPerformed(ActionEvent e) { 182 actionInvocations++; 183 } 184 185 public int getActionInvocations() { 186 return actionInvocations; 187 } 188 } 189 190 193 public void testGetLookup() throws IOException { 194 PaletteActions actions = new DummyActions(); 195 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 196 Model model = pc.getModel(); 197 198 Category[] categories = model.getCategories(); 199 200 for( int i=0; i<categories.length; i++ ) { 201 Item[] items = categories[i].getItems(); 202 for( int j=0; j<items.length; j++ ) { 203 Lookup lkp = items[j].getLookup(); 204 assertNotNull( lkp ); 205 Node node = (Node)lkp.lookup( Node.class ); 206 assertEquals( itemNames[i][j], node.getName() ); 207 } 208 } 209 } 210 211 214 public void testDrag() throws Exception { 215 PaletteActions actions = new DummyActions(); 216 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 217 Model model = pc.getModel(); 218 219 Category[] categories = model.getCategories(); 220 221 for( int i=0; i<categories.length; i++ ) { 222 Item[] items = categories[i].getItems(); 223 for( int j=0; j<items.length; j++ ) { 224 Transferable t = items[j].drag(); 225 assertNotNull( t ); 226 assertTrue( t.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) ); 227 Lookup lookup = (Lookup)t.getTransferData( PaletteController.ITEM_DATA_FLAVOR ); 228 assertNotNull( lookup ); 229 Node node = (Node)lookup.lookup( Node.class ); 230 assertEquals( itemNames[i][j], node.getName() ); 231 } 232 } 233 } 234 235 238 public void testCut() throws Exception { 239 PaletteActions actions = new DummyActions(); 240 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 241 Model model = pc.getModel(); 242 243 Category[] categories = model.getCategories(); 244 245 for( int i=0; i<categories.length; i++ ) { 246 Item[] items = categories[i].getItems(); 247 for( int j=0; j<items.length; j++ ) { 248 Transferable t = items[j].cut(); 249 assertNotNull( t ); 250 assertTrue( t.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) ); 251 Lookup lookup = (Lookup)t.getTransferData( PaletteController.ITEM_DATA_FLAVOR ); 252 assertNotNull( lookup ); 253 Node node = (Node)lookup.lookup( Node.class ); 254 assertEquals( itemNames[i][j], node.getName() ); 255 } 256 } 257 } 258 } 259 | Popular Tags |