1 19 20 package org.netbeans.spi.palette; 21 22 import java.io.IOException ; 23 import org.netbeans.core.windows.Constants; 24 import org.netbeans.core.windows.SplitConstraint; 25 import org.netbeans.core.windows.WindowManagerImpl; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileSystem; 28 import org.openide.filesystems.MIMEResolver; 29 import org.openide.filesystems.Repository; 30 import org.openide.loaders.DataObject; 31 import org.openide.loaders.DataObjectNotFoundException; 32 import org.openide.nodes.Node; 33 import org.openide.util.lookup.AbstractLookup; 34 import org.openide.util.lookup.InstanceContent; 35 import org.openide.windows.Mode; 36 import org.openide.windows.TopComponent; 37 41 public class PaletteSwitchTest extends AbstractPaletteTestHid { 42 43 private String lookupPaletteRootName; 44 private FileObject lookupPaletteRootFolder; 45 46 static String mimePaletteRootName; 47 private static FileObject mimePaletteRootFolder; 48 private static final String MIME_TYPE_NAME = "junittest/x-paletteswitchtest"; 49 50 private FileObject dummyDocumentFile; 51 private final static String DUMMY_DOCUMENT_FILE_EXTENSION = "junitPaletteSwitchTest"; 52 53 static { 54 String [] layers = new String [] {"org/netbeans/spi/palette/mf-layer.xml"}; Object [] instances = new Object [] { new MyMimeResolver() }; 56 IDEInitializer.setup(layers,instances); 57 } 58 59 public PaletteSwitchTest(String testName) { 60 super(testName); 61 } 62 63 protected void setUp() throws Exception { 64 super.setUp(); 65 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 66 67 lookupPaletteRootName = "lookupPalette" + System.currentTimeMillis(); 68 lookupPaletteRootFolder = fs.getRoot().createFolder( lookupPaletteRootName ); 69 createDefaultPaletteContentInFolder( lookupPaletteRootFolder ); 70 71 if( null == mimePaletteRootName ) { 72 mimePaletteRootName = "mimePalette" + System.currentTimeMillis(); 73 mimePaletteRootFolder = fs.getRoot().createFolder( mimePaletteRootName ); 74 createDefaultPaletteContentInFolder( mimePaletteRootFolder ); 75 } 76 77 dummyDocumentFile = fs.getRoot().createData( "dummyDocumentFile" + System.currentTimeMillis(), DUMMY_DOCUMENT_FILE_EXTENSION ); 78 } 79 80 public void testNoLookupPalette() throws IOException { 81 TopComponent tc = createTopComponentWithPalette( null ); 82 PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); 83 84 PaletteController foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); 85 86 assertNull( foundPalette ); 87 } 88 89 public void testNoMimePalette() throws IOException { 90 PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); 91 92 PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( "unknown/mimetype" ); 93 94 assertNull( foundPalette ); 95 } 96 97 public void testLookupPalette() throws IOException { 98 PaletteController pc = PaletteFactory.createPalette( lookupPaletteRootName, new DummyActions() ); 99 100 TopComponent tc = createTopComponentWithPalette( pc ); 101 PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); 102 103 PaletteController foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); 104 105 assertNotNull( foundPalette ); 106 assertEquals( pc.getModel().getName(), foundPalette.getModel().getName() ); 107 } 108 109 public void testMimePalette() throws IOException { 110 TopComponent tc = createTopComponentWithPalette( null ); 111 tc.setActivatedNodes( new Node[] { DataObject.find( dummyDocumentFile ).getNodeDelegate() } ); 112 113 PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); 114 115 PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( MIME_TYPE_NAME ); 116 assertNotNull( foundPalette ); 117 assertEquals( mimePaletteRootName, foundPalette.getModel().getName() ); 118 119 foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); 120 assertNotNull( foundPalette ); 121 assertEquals( mimePaletteRootName, foundPalette.getModel().getName() ); 122 } 123 124 125 public void testLookupPaletteTakePrecendsOverMimePalette() throws IOException { 126 PaletteController pc = PaletteFactory.createPalette( lookupPaletteRootName, new DummyActions() ); 127 128 TopComponent tc = createTopComponentWithPalette( pc ); 129 tc.setActivatedNodes( new Node[] { DataObject.find( dummyDocumentFile ).getNodeDelegate() } ); 130 131 PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); 132 133 PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( MIME_TYPE_NAME ); 134 assertNotNull( foundPalette ); 135 assertEquals( mimePaletteRootName, foundPalette.getModel().getName() ); 136 137 foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); 138 assertNotNull( foundPalette ); 139 assertEquals( pc.getModel().getName(), foundPalette.getModel().getName() ); 140 } 141 142 private TopComponent createTopComponentWithPalette( PaletteController pc ) throws IOException { 143 TopComponent tc = new MyTopComponent( pc ); 144 145 Mode editorMode = WindowManagerImpl.getInstance().findMode( "unitTestEditorMode" ); 146 if( null == editorMode ) { 147 editorMode = WindowManagerImpl.getInstance().createMode( "unitTestEditorMode", Constants.MODE_KIND_EDITOR, Constants.MODE_STATE_JOINED, false, new SplitConstraint[0] ); 148 } 149 editorMode.dockInto(tc); 150 return tc; 151 } 152 153 private static class MyTopComponent extends TopComponent { 154 public MyTopComponent( PaletteController palette ) throws DataObjectNotFoundException { 155 this( new InstanceContent(), palette ); 156 } 157 158 private MyTopComponent( InstanceContent ic, PaletteController palette ) throws DataObjectNotFoundException { 159 super( new AbstractLookup( ic ) ); 160 if( null != palette ) 161 ic.add( palette ); 162 } 163 } 164 165 public static class MyMimeResolver extends MIMEResolver { 166 167 public String findMIMEType(FileObject fo) { 168 if( DUMMY_DOCUMENT_FILE_EXTENSION.equals( fo.getExt() ) ) 169 return MIME_TYPE_NAME; 170 return null; 171 } 172 } 173 174 175 } 176 | Popular Tags |