1 19 20 package org.netbeans.spi.palette; 21 22 import java.beans.BeanInfo ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import org.netbeans.junit.MockServices; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.modules.palette.Category; 30 import org.netbeans.modules.palette.Item; 31 import org.openide.filesystems.FileLock; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.Repository; 35 import org.openide.loaders.DataObject; 36 import org.openide.nodes.Node; 37 import org.openide.text.ActiveEditorDrop; 38 import org.openide.util.Lookup; 39 import org.openide.util.NbBundle; 40 41 45 public class PaletteItemTest extends NbTestCase { 46 47 private static final String PALETTE_ROOT = "FooPalette"; 48 private static final String ITEMS_FOLDER = PALETTE_ROOT + "/FooCategory"; 49 private static final String ITEM_FILE = "FooItem.xml"; 50 private static final String CLASS_NAME = "org.netbeans.spi.palette.FooItem"; 51 private static final String BODY = "<fooTag att=''></fooTag>"; 52 private static final String ICON16 = "org/netbeans/spi/palette/FooItem16.gif"; 53 private static final String ICON32 = "org/netbeans/spi/palette/FooItem32.gif"; 54 private static final String BUNDLE_NAME = "org.netbeans.spi.palette.Bundle"; 55 private static final String NAME_KEY = "NAME_foo-FooItem"; 56 private static final String TOOLTIP_KEY = "HINT_foo-FooItem"; 57 58 59 private FileObject itemsFolder; 60 61 private Lookup selectedNode; 62 63 public PaletteItemTest(String testName) { 64 super(testName); 65 } 66 67 protected void setUp() throws Exception { 68 MockServices.setServices(new Class [] {RepositoryImpl.class}); 69 itemsFolder = createItemsFolder(); 70 assertNotNull(itemsFolder); 71 } 72 73 protected void tearDown() throws Exception { 74 FileObject[] ch = itemsFolder.getChildren(); 75 for (int i = 0; i < ch.length; i++) 76 ch[i].delete(); 77 FileObject paletteRoot = itemsFolder.getParent(); 78 itemsFolder.delete(); 79 paletteRoot.delete(); 80 } 81 82 public void testReadItemWithActiveEditorDrop() throws Exception { 83 FileObject itemFile = createItemFileWithActiveEditorDrop(); 84 verifyPaletteItem(itemFile); 85 } 86 87 public void testReadItemWithBody() throws Exception { 88 FileObject itemFile = createItemFileWithBody(); 89 verifyPaletteItem(itemFile); 90 } 91 92 public void testReadItemWithInlineDescription() throws Exception { 93 FileObject itemFile = createItemFileWithInLineDescription(); 94 verifyPaletteItem(itemFile); 95 } 96 97 private void verifyPaletteItem(FileObject itemFile) throws Exception { 98 assertNotNull(itemFile); 99 100 Node itemNode = DataObject.find(itemFile).getNodeDelegate(); 101 102 assertEquals("Item loaded with a wrong display name.", NbBundle.getBundle(BUNDLE_NAME).getString(NAME_KEY), itemNode.getDisplayName()); 103 assertEquals("Item loaded with a wrong description.", NbBundle.getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY), itemNode.getShortDescription()); 104 assertNotNull("Item loaded with no small icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_16x16)); 105 assertNotNull("Item loaded with no big icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_32x32)); 106 107 Object o = itemNode.getLookup().lookup(ActiveEditorDrop.class); 108 assertNotNull("Item does not contain ActiveEditorDrop implementation in its lookup.", o); 109 } 110 111 public void testFindItem() throws Exception { 112 FileObject itemFile = createItemFileWithActiveEditorDrop(); 113 assertNotNull(itemFile); 114 115 PaletteController pc = PaletteFactory.createPalette(PALETTE_ROOT, new DummyActions()); 117 118 Node root = (Node)pc.getRoot().lookup(Node.class); 120 Node[] cats = root.getChildren().getNodes(true); 121 Node foundNode = null; 122 Node foundCat = null; 123 assertEquals("Too many categories", 1, cats.length); 124 Node[] items = cats[0].getChildren().getNodes(true); 125 assertEquals("Too many items", 1, items.length); 126 127 assertTrue("Item not found.", 128 DataObject.find(itemFile).getNodeDelegate().getLookup().lookup(ActiveEditorDrop.class) == 129 items[0].getLookup().lookup(ActiveEditorDrop.class) 130 ); 131 132 } 133 134 public void testSelectItem() throws Exception { 135 FileObject itemFile = createItemFileWithActiveEditorDrop(); 136 assertNotNull(itemFile); 137 138 PaletteController pc = PaletteFactory.createPalette(PALETTE_ROOT, new DummyActions()); 140 pc.addPropertyChangeListener(new PaletteListener(pc)); 141 142 Category modelCat = pc.getModel().getCategories()[0]; 144 Item modelItem = modelCat.getItems()[0]; 145 pc.getModel().setSelectedItem(modelCat.getLookup(), modelItem.getLookup()); 146 147 assertNotNull("Selected item does not contain ActiveEditorDrop implementation", 148 selectedNode.lookup(ActiveEditorDrop.class)); 149 } 150 151 152 154 private class PaletteListener implements PropertyChangeListener { 155 PaletteController pc; 156 PaletteListener(PaletteController pc) { this.pc = pc; } 157 public void propertyChange(PropertyChangeEvent evt) { 158 assertEquals("Property " + PaletteController.PROP_SELECTED_ITEM + " was expected.", 159 PaletteController.PROP_SELECTED_ITEM, evt.getPropertyName()); 160 161 selectedNode = pc.getSelectedItem(); 162 } 163 }; 164 165 166 private FileObject createItemsFolder() throws IOException { 167 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 168 FileObject fooCategory = FileUtil.createFolder(root, ITEMS_FOLDER); 169 return fooCategory; 170 } 171 172 private FileObject createItemFileWithActiveEditorDrop() throws Exception { 173 FileObject fo = itemsFolder.createData(ITEM_FILE); 174 FileLock lock = fo.lock(); 175 try { 176 OutputStreamWriter writer = new OutputStreamWriter (fo.getOutputStream(lock), "UTF-8"); 177 try { 178 writer.write("<?xml version='1.0' encoding='UTF-8'?>"); 179 writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>"); 180 writer.write("<editor_palette_item version='1.0'>"); 181 writer.write("<class name='" + CLASS_NAME + "' />"); 182 writer.write("<icon16 urlvalue='" + ICON16 + "' />"); 183 writer.write("<icon32 urlvalue='" + ICON32 + "' />"); 184 writer.write("<description localizing-bundle='" + BUNDLE_NAME + "' display-name-key='" + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY + "' />"); 185 writer.write("</editor_palette_item>"); 186 } finally { 187 writer.close(); 188 } 189 } finally { 190 lock.releaseLock(); 191 } 192 return fo; 193 } 194 195 private FileObject createItemFileWithBody() throws Exception { 196 FileObject fo = itemsFolder.createData(ITEM_FILE); 197 FileLock lock = fo.lock(); 198 try { 199 OutputStreamWriter writer = new OutputStreamWriter (fo.getOutputStream(lock), "UTF-8"); 200 try { 201 writer.write("<?xml version='1.0' encoding='UTF-8'?>"); 202 writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>"); 203 writer.write("<editor_palette_item version='1.0'>"); 204 writer.write("<body><![CDATA[" + BODY + "]]></body>"); 205 writer.write("<icon16 urlvalue='" + ICON16 + "' />"); 206 writer.write("<icon32 urlvalue='" + ICON32 + "' />"); 207 writer.write("<description localizing-bundle='" + BUNDLE_NAME + "' display-name-key='" + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY + "' />"); 208 writer.write("</editor_palette_item>"); 209 } finally { 210 writer.close(); 211 } 212 } finally { 213 lock.releaseLock(); 214 } 215 return fo; 216 } 217 218 private FileObject createItemFileWithInLineDescription() throws Exception { 219 FileObject fo = itemsFolder.createData(ITEM_FILE); 220 FileLock lock = fo.lock(); 221 try { 222 OutputStreamWriter writer = new OutputStreamWriter (fo.getOutputStream(lock), "UTF-8"); 223 try { 224 writer.write("<?xml version='1.0' encoding='UTF-8'?>"); 225 writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.1//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_1.dtd'>"); 226 writer.write("<editor_palette_item version='1.1'>"); 227 writer.write("<body><![CDATA[" + BODY + "]]></body>"); 228 writer.write("<icon16 urlvalue='" + ICON16 + "' />"); 229 writer.write("<icon32 urlvalue='" + ICON32 + "' />"); 230 writer.write("<inline-description> <display-name>" 231 +NbBundle.getBundle(BUNDLE_NAME).getString(NAME_KEY)+"</display-name> <tooltip>" 232 +NbBundle.getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY)+"</tooltip> </inline-description>"); 233 writer.write("</editor_palette_item>"); 234 } finally { 235 writer.close(); 236 } 237 } finally { 238 lock.releaseLock(); 239 } 240 return fo; 241 } 242 243 } 244 | Popular Tags |