1 19 20 package org.netbeans.spi.palette; 21 import java.awt.datatransfer.Transferable ; 22 import java.awt.datatransfer.UnsupportedFlavorException ; 23 import java.awt.dnd.DnDConstants ; 24 import java.beans.BeanInfo ; 25 import java.io.IOException ; 26 import javax.swing.Action ; 27 import org.netbeans.modules.palette.Category; 28 import org.netbeans.modules.palette.Item; 29 import org.netbeans.modules.palette.Model; 30 import org.openide.nodes.Node; 31 import org.openide.util.Lookup; 32 import org.openide.util.datatransfer.ExTransferable; 33 37 public class CategoryTest extends AbstractPaletteTestHid { 38 39 public CategoryTest(String testName) { 40 super(testName); 41 } 42 43 46 public void testGetName() throws IOException { 47 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 48 Model model = pc.getModel(); 49 Category[] categories = model.getCategories(); 50 51 assertEquals( categoryNames.length, categories.length ); 52 53 for( int i=0; i<categoryNames.length; i++ ) { 54 Node catNode = getCategoryNode( categoryNames[i] ); 55 assertEquals( catNode.getName(), categories[i].getName() ); 56 } 57 } 58 59 62 public void testGetDisplayName() throws IOException { 63 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 64 Model model = pc.getModel(); 65 Category[] categories = model.getCategories(); 66 67 assertEquals( categoryNames.length, categories.length ); 68 69 for( int i=0; i<categoryNames.length; i++ ) { 70 Node catNode = getCategoryNode( categoryNames[i] ); 71 assertEquals( catNode.getDisplayName(), categories[i].getDisplayName() ); 72 } 73 } 74 75 78 public void testGetShortDescription() throws IOException { 79 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 80 Model model = pc.getModel(); 81 Category[] categories = model.getCategories(); 82 83 assertEquals( categoryNames.length, categories.length ); 84 85 for( int i=0; i<categoryNames.length; i++ ) { 86 Node catNode = getCategoryNode( categoryNames[i] ); 87 assertEquals( catNode.getShortDescription(), categories[i].getShortDescription() ); 88 } 89 } 90 91 94 public void testGetIcon() throws IOException { 95 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), new DummyActions() ); 96 Model model = pc.getModel(); 97 Category[] categories = model.getCategories(); 98 99 assertEquals( categoryNames.length, categories.length ); 100 101 for( int i=0; i<categoryNames.length; i++ ) { 102 Node catNode = getCategoryNode( categoryNames[i] ); 103 assertEquals( catNode.getIcon( BeanInfo.ICON_COLOR_16x16 ), categories[i].getIcon( BeanInfo.ICON_COLOR_16x16 ) ); 104 assertEquals( catNode.getIcon( BeanInfo.ICON_COLOR_32x32 ), categories[i].getIcon( BeanInfo.ICON_COLOR_32x32 ) ); 105 } 106 } 107 108 111 public void testGetActions() throws IOException { 112 PaletteActions actions = new DummyActions(); 113 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 114 Model model = pc.getModel(); 115 116 Category[] categories = model.getCategories(); 117 118 assertEquals( categoryNames.length, categories.length ); 119 120 for( int i=0; i<categoryNames.length; i++ ) { 121 Action [] catActions = categories[i].getActions(); 122 123 Action [] providedActions = actions.getCustomCategoryActions( categories[i].getLookup() ); 124 125 for( int k=0; k<providedActions.length; k++ ) { 126 if( null == providedActions[k] ) 127 continue; 128 boolean found = false; 129 for( int j=0; j<catActions.length; j++ ) { 130 if( null == catActions[j] ) 131 continue; 132 if( catActions[j].equals( providedActions[k] ) ) { 133 found = true; 134 break; 135 } 136 } 137 assertTrue( "Action " + providedActions[k].getValue( Action.NAME ) + " not found in palette actions.", found ); 138 } 139 } 140 } 141 142 145 public void testGetItems() throws IOException { 146 PaletteActions actions = new DummyActions(); 147 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 148 Model model = pc.getModel(); 149 150 Category[] categories = model.getCategories(); 151 152 assertEquals( categoryNames.length, categories.length ); 153 154 for( int i=0; i<categories.length; i++ ) { 155 Item[] items = categories[i].getItems(); 156 assertEquals( itemNames[i].length, items.length ); 157 for( int j=0; j<items.length; j++ ) { 158 assertEquals( itemNames[i][j], items[j].getName() ); 159 } 160 } 161 } 162 163 166 public void testGetTransferable() throws IOException { 167 PaletteActions actions = new DummyActions(); 168 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 169 Model model = pc.getModel(); 170 171 Category[] categories = model.getCategories(); 172 173 assertEquals( categoryNames.length, categories.length ); 174 175 for( int i=0; i<categories.length; i++ ) { 176 Transferable t = categories[i].getTransferable(); 177 assertNotNull( t ); 178 } 179 } 180 181 184 public void testGetLookup() throws IOException { 185 PaletteActions actions = new DummyActions(); 186 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 187 Model model = pc.getModel(); 188 189 Category[] categories = model.getCategories(); 190 191 assertEquals( categoryNames.length, categories.length ); 192 193 for( int i=0; i<categories.length; i++ ) { 194 Lookup lkp = categories[i].getLookup(); 195 assertNotNull( lkp ); 196 Node node = (Node)lkp.lookup( Node.class ); 197 assertEquals( categoryNames[i], node.getName() ); 198 } 199 } 200 201 204 public void testMoveItemBefore() throws IOException { 205 PaletteActions actions = new DummyActions(); 206 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 207 Model model = pc.getModel(); 208 209 Category[] categories = model.getCategories(); 210 211 Category cat = categories[0]; 212 Item[] itemsBeforeMove = cat.getItems(); 213 214 Item source = itemsBeforeMove[0]; 215 Item target = itemsBeforeMove[itemsBeforeMove.length-1]; 216 217 cat.dropItem( createTransferable( source ), DnDConstants.ACTION_COPY_OR_MOVE, target, true ); 218 219 Item[] itemsAfterMove = cat.getItems(); 220 221 assertEquals( itemsBeforeMove.length, itemsAfterMove.length ); 222 assertEquals( source.getName(), itemsAfterMove[itemsAfterMove.length-1-1].getName() ); 223 assertEquals( itemsBeforeMove[1].getName(), itemsAfterMove[0].getName() ); 224 assertEquals( target.getName(), itemsAfterMove[itemsAfterMove.length-1].getName() ); 225 } 226 227 230 public void testMoveItemAfter() throws IOException { 231 PaletteActions actions = new DummyActions(); 232 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 233 Model model = pc.getModel(); 234 235 Category[] categories = model.getCategories(); 236 237 Category cat = categories[0]; 238 Item[] itemsBeforeMove = cat.getItems(); 239 240 Item source = itemsBeforeMove[0]; 241 Item target = itemsBeforeMove[itemsBeforeMove.length-1]; 242 243 cat.dropItem( createTransferable( source ), DnDConstants.ACTION_COPY_OR_MOVE, target, false ); 244 245 Item[] itemsAfterMove = cat.getItems(); 246 247 assertEquals( itemsBeforeMove.length, itemsAfterMove.length ); 248 assertEquals( source.getName(), itemsAfterMove[itemsAfterMove.length-1].getName() ); 249 assertEquals( itemsBeforeMove[1].getName(), itemsAfterMove[0].getName() ); 250 assertEquals( target.getName(), itemsAfterMove[itemsAfterMove.length-1-1].getName() ); 251 } 252 253 public void testDropItemBefore() throws IOException { 254 PaletteActions actions = new DummyActions(); 255 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 256 Model model = pc.getModel(); 257 258 Category[] categories = model.getCategories(); 259 260 Category srcCat = categories[0]; 261 Item[] srcItemsBefore = srcCat.getItems(); 262 Item dropItem = srcItemsBefore[0]; 263 264 Category tgtCat = categories[1]; 265 Item[] tgtItemsBefore = tgtCat.getItems(); 266 Item target = tgtItemsBefore[5]; 267 268 tgtCat.dropItem( dropItem.cut(), DnDConstants.ACTION_COPY_OR_MOVE, target, true ); 269 270 pc.refresh(); 272 categories = model.getCategories(); 273 srcCat = categories[0]; 274 tgtCat = categories[1]; 275 276 Item[] srcItemsAfter = srcCat.getItems(); 277 Item[] tgtItemsAfter = tgtCat.getItems(); 278 279 assertEquals( srcItemsBefore.length, srcItemsAfter.length+1 ); 280 for( int i=0; i<srcItemsAfter.length; i++ ) { 281 assertEquals( srcItemsBefore[i+1].getName(), srcItemsAfter[i].getName() ); 282 } 283 284 assertEquals( tgtItemsBefore.length, tgtItemsAfter.length-1 ); 285 assertEquals( target.getName(), tgtItemsAfter[5+1].getName() ); 286 assertEquals( dropItem.getName(), tgtItemsAfter[5].getName() ); 287 } 288 289 public void testDropItemAfter() throws IOException { 290 PaletteActions actions = new DummyActions(); 291 PaletteController pc = PaletteFactory.createPalette( getRootFolderName(), actions ); 292 Model model = pc.getModel(); 293 294 Category[] categories = model.getCategories(); 295 296 Category srcCat = categories[0]; 297 Item[] srcItemsBefore = srcCat.getItems(); 298 Item dropItem = srcItemsBefore[0]; 299 300 Category tgtCat = categories[1]; 301 Item[] tgtItemsBefore = tgtCat.getItems(); 302 Item target = tgtItemsBefore[5]; 303 304 tgtCat.dropItem( dropItem.cut(), DnDConstants.ACTION_COPY_OR_MOVE, target, false ); 305 306 pc.refresh(); 308 categories = model.getCategories(); 309 srcCat = categories[0]; 310 tgtCat = categories[1]; 311 312 Item[] srcItemsAfter = srcCat.getItems(); 313 Item[] tgtItemsAfter = tgtCat.getItems(); 314 315 assertEquals( srcItemsBefore.length, srcItemsAfter.length+1 ); 316 for( int i=0; i<srcItemsAfter.length; i++ ) { 317 assertEquals( srcItemsBefore[i+1].getName(), srcItemsAfter[i].getName() ); 318 } 319 320 assertEquals( tgtItemsBefore.length, tgtItemsAfter.length-1 ); 321 assertEquals( target.getName(), tgtItemsAfter[5].getName() ); 322 assertEquals( dropItem.getName(), tgtItemsAfter[5+1].getName() ); 323 } 324 325 private Transferable createTransferable( final Item item ) { 326 return new ExTransferable.Single( PaletteController.ITEM_DATA_FLAVOR ) { 327 protected Object getData() throws IOException , UnsupportedFlavorException { 328 return item.getLookup(); 329 } 330 }; 331 } 332 } 333 | Popular Tags |