# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: D:\projects\nb.m7\core\palette # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: test/unit/src/org/netbeans/spi/palette/mf-layer.xml *** D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\mf-layer.xml No Base Revision --- D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\mf-layer.xml Locally New *************** *** 1,0 **** --- 1,33 ---- + + + + + + + + + + + + + + + + Index: src/org/netbeans/spi/palette/PaletteSwitch.java *** D:\projects\nb.m7\core\palette\src\org\netbeans\spi\palette\PaletteSwitch.java Base (1.1) --- D:\projects\nb.m7\core\palette\src\org\netbeans\spi\palette\PaletteSwitch.java Locally Modified (Based On 1.1) *************** *** 26,32 **** --- 26,37 ---- import java.util.Iterator; import java.util.Set; import javax.swing.SwingUtilities; + import org.netbeans.api.editor.mimelookup.MimeLookup; + import org.netbeans.api.editor.mimelookup.MimePath; import org.netbeans.modules.palette.ui.PalettePanel; + import org.openide.filesystems.FileObject; + import org.openide.loaders.DataObject; + import org.openide.loaders.DataShadow; import org.openide.nodes.Node; import org.openide.util.Lookup; import org.openide.util.LookupEvent; *************** *** 44,54 **** */ final class PaletteSwitch implements Runnable, LookupListener { ! public static final String PROP_PALETTE_CONTENTS = "component_palette_contents"; private static PaletteSwitch theInstance; --- 49,59 ---- */ final class PaletteSwitch implements Runnable, LookupListener { ! static final String PROP_PALETTE_CONTENTS = "component_palette_contents"; //NOI18N + /** template for finding all PaletteController instances in lookup */ + private static final Lookup.Template CONTROLLER_TEMPLATE = new Lookup.Template( PaletteController.class ); + private static PaletteSwitch theInstance; private PropertyChangeListener registryListener; *************** *** 166,181 **** return isMaximized && currentPaletteStillAvailable; } ! private PaletteController getPaletteFromTopComponent( TopComponent tc, boolean mustBeShowing ) { if( null == tc || (!tc.isShowing() && mustBeShowing) ) return null; ! return (PaletteController)tc.getLookup().lookup( PaletteController.class ); } private void showHidePaletteTopComponent( PaletteController prevPalette, PaletteController newPalette ) { if( prevPalette == newPalette && null != newPalette ) --- 174,219 ---- return isMaximized && currentPaletteStillAvailable; } ! PaletteController getPaletteFromTopComponent( TopComponent tc, boolean mustBeShowing ) { if( null == tc || (!tc.isShowing() && mustBeShowing) ) return null; ! PaletteController pc = (PaletteController)tc.getLookup().lookup( PaletteController.class ); ! if( null == pc && WindowManager.getDefault().isEditorTopComponent( tc ) ) { ! //check if there's any palette assigned to TopComponent's mime type ! Node[] activeNodes = tc.getActivatedNodes(); ! if( null != activeNodes && activeNodes.length > 0 ) { ! DataObject dob = activeNodes[0].getLookup().lookup( DataObject.class ); ! if( null != dob ) { ! while( dob instanceof DataShadow ) { ! dob = ((DataShadow)dob).getOriginal(); } + FileObject fo = dob.getPrimaryFile(); + if( !fo.isVirtual() ) { + String mimeType = fo.getMIMEType(); + pc = getPaletteFromMimeType( mimeType ); + } + } + } + } + return pc; + } + /** + * Finds appropriate PaletteController for given mime type. + * + * @param mimeType Mime type to check for associated palette content. + * + * @return PaletteController that is associated with the given mime type and that should + * be displayed in the Common Palette when an editor window with the given mime type is activated. + * @since 1.10 + */ + PaletteController getPaletteFromMimeType( String mimeType ) { + MimePath path = MimePath.get( mimeType ); + Lookup lkp = MimeLookup.getLookup( path ); + return lkp.lookup( PaletteController.class ); + } + private void showHidePaletteTopComponent( PaletteController prevPalette, PaletteController newPalette ) { if( prevPalette == newPalette && null != newPalette ) return; Index: manifest.mf *** D:\projects\nb.m7\core\palette\manifest.mf Base (1.10) --- D:\projects\nb.m7\core\palette\manifest.mf Locally Modified (Based On 1.10) *************** *** 1,10 **** Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.spi.palette/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/palette/resources/Bundle.properties ! OpenIDE-Module-Specification-Version: 1.7 OpenIDE-Module-Layer: org/netbeans/modules/palette/resources/layer.xml --- 1,11 ---- Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.spi.palette/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/palette/resources/Bundle.properties ! OpenIDE-Module-Specification-Version: 1.10 OpenIDE-Module-Layer: org/netbeans/modules/palette/resources/layer.xml + OpenIDE-Module-Install: org/netbeans/spi/palette/PaletteModule.class Index: test/unit/src/org/netbeans/spi/palette/AbstractPaletteTestHid.java *** D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\AbstractPaletteTestHid.java Base (1.4) --- D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\AbstractPaletteTestHid.java Locally Modified (Based On 1.4) *************** *** 18,25 **** */ package org.netbeans.spi.palette; import junit.framework.*; - import org.netbeans.modules.palette.DefaultSettings; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileSystem; --- 18,28 ---- */ package org.netbeans.spi.palette; + import java.io.IOException; import junit.framework.*; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; *************** *** 29,35 **** import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; import org.openide.nodes.Node; - import org.openide.util.NbPreferences; /** --- 29,34 ---- *************** *** 64,84 **** if( null == myDummyLoader ) myDummyLoader = new DummyItemLoader(); ! categoryNames = new String[10]; ! itemNames = new String[categoryNames.length][10]; ! for( int i=0; iorg.netbeans.spi.palette + org.netbeans.modules.editor.mimelookup + + + + 1 + 1.6 + + + org.openide.awt *************** *** 62,67 **** --- 71,84 ---- + org.openide.modules + + + + 7.2 + + + org.openide.nodes *************** *** 98,114 **** unit ! org.netbeans.spi.palette ! ! org.netbeans.core org.openide.modules --- 115,136 ---- unit ! org.netbeans.core ! ! ! org.netbeans.core.windows ! org.netbeans.modules.editor.mimelookup.impl ! + org.netbeans.spi.palette + + + + org.openide.modules Index: test/unit/src/org/netbeans/spi/palette/IDEInitializer.java *** D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\IDEInitializer.java No Base Revision --- D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\IDEInitializer.java Locally New *************** *** 1,0 **** --- 1,136 ---- + /* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + package org.netbeans.spi.palette; + + import java.beans.PropertyVetoException; + import java.io.File; + import java.io.IOException; + import java.net.URL; + import java.net.URLStreamHandler; + import java.net.URLStreamHandlerFactory; + import java.util.Enumeration; + import junit.framework.Assert; + import org.netbeans.junit.Manager; + import org.openide.filesystems.FileObject; + import org.openide.filesystems.FileSystem; + import org.openide.filesystems.FileUtil; + import org.openide.filesystems.LocalFileSystem; + import org.openide.filesystems.MultiFileSystem; + import org.openide.filesystems.Repository; + import org.openide.filesystems.XMLFileSystem; + import org.openide.util.Lookup; + import org.openide.util.lookup.Lookups; + import org.openide.util.lookup.ProxyLookup; + + + /** + * Inspired by org.netbeans.api.project.TestUtil. + * + * @author Miloslav Metelka, Jan Lahoda + */ + public class IDEInitializer extends ProxyLookup { + + public static IDEInitializer DEFAULT_LOOKUP = null; + private static FileSystem lfs; + + static { + IDEInitializer.class.getClassLoader ().setDefaultAssertionStatus (true); + System.setProperty ("org.openide.util.Lookup", IDEInitializer.class.getName ()); + Assert.assertEquals (IDEInitializer.class, Lookup.getDefault ().getClass ()); + } + + public IDEInitializer () { + Assert.assertNull (DEFAULT_LOOKUP); + DEFAULT_LOOKUP = this; + URL.setURLStreamHandlerFactory (new MyURLHandlerFactory ()); + } + + /** + * Set the global default lookup with the specified content. + * + * @param layers xml-layer URLs to be present in the system filesystem. + * @param instances object instances to be present in the default lookup. + */ + public static void setup ( + String[] layers, + Object[] instances + ) { + ClassLoader classLoader = IDEInitializer.class.getClassLoader (); + File workDir = new File (Manager.getWorkDirPath ()); + URL[] urls = new URL [layers.length]; + int i, k = urls.length; + for (i = 0; i < k; i++) + urls [i] = classLoader.getResource (layers [i]); + + // 1) create repository + XMLFileSystem systemFS = new XMLFileSystem (); + lfs = FileUtil.createMemoryFileSystem(); + try { + systemFS.setXmlUrls (urls); + } catch (Exception ex) { + ex.printStackTrace (); + } + MyFileSystem myFileSystem = new MyFileSystem ( + new FileSystem [] {lfs, systemFS} + ); + Repository repository = new Repository (myFileSystem); + + Object[] lookupContent = new Object [instances.length + 1]; + lookupContent [0] = repository; + System.arraycopy (instances, 0, lookupContent, 1, instances.length); + + DEFAULT_LOOKUP.setLookups (new Lookup[] { + Lookups.fixed (lookupContent), + Lookups.metaInfServices (classLoader), + Lookups.singleton (classLoader), + }); + Assert.assertEquals (myFileSystem, Repository.getDefault ().getDefaultFileSystem ()); + } + + public static void cleanWorkDir () { + try { + Enumeration en = lfs.getRoot ().getChildren (false); + while (en.hasMoreElements ()) + ((FileObject) en.nextElement ()).delete (); + } catch (IOException ex) { + ex.printStackTrace (); + } + } + + private static class MyFileSystem extends MultiFileSystem { + public MyFileSystem (FileSystem[] fileSystems) { + super (fileSystems); + try { + setSystemName ("TestFS"); + } catch (PropertyVetoException ex) { + ex.printStackTrace(); + } + } + } + + private static class MyURLHandlerFactory implements URLStreamHandlerFactory { + public URLStreamHandler createURLStreamHandler(String protocol) { + if (protocol.equals ("nbfs")) { + return FileUtil.nbfsURLStreamHandler (); + } + return null; + } + } + } Index: test/unit/src/org/netbeans/spi/palette/MyLayerPaletteFactory.java *** D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\MyLayerPaletteFactory.java No Base Revision --- D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\MyLayerPaletteFactory.java Locally New *************** *** 1,0 **** --- 1,44 ---- + /* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + package org.netbeans.spi.palette; + + /** + * + * @author S. Aubrecht + */ + public class MyLayerPaletteFactory { + + private static PaletteController palette; + + /** Creates a new instance of MyLayerPaletteFactory */ + public MyLayerPaletteFactory() { + } + + public static PaletteController createPalette() { + try { + if( null == palette ) + palette = PaletteFactory.createPalette( PaletteSwitchTest.mimePaletteRootName, new DummyActions() ); + return palette; + } catch( Exception e ) { + e.printStackTrace(); + } + return null; + } + } Index: api/doc/changes/apichanges.xml *** D:\projects\nb.m7\core\palette\api\doc\changes\apichanges.xml Base (1.14) --- D:\projects\nb.m7\core\palette\api\doc\changes\apichanges.xml Locally Modified (Based On 1.14) *************** *** 211,216 **** --- 211,243 ---- + + + + Allow associating palette content with document mime type. + + + + + +

+ The previous version of palette API mandated that editor TopComponent had to insert a + PaletteController instance into its Lookup if it wants to associate the palette with it. + Now it is possible to associate the palette also with an existing + editor without the need to change its implementation, e.g. to add code snippets + palette to java source editor. +

+

+ If the mime type of active editor window has an associated instance of PaletteController + in the XML layer system then palette window opens and displays the specified palette contents. + The PaletteController from TopComponent's Lookup takes precedens over the PaletteController + found from mime type lookup in the XML layer (if any) for backwards compatibility. +

+

The new API is fully backwards compatible and there are no implications for existing + palette providers.

+
+ +
Index: src/org/netbeans/spi/palette/PaletteModule.java *** D:\projects\nb.m7\core\palette\src\org\netbeans\spi\palette\PaletteModule.java No Base Revision --- D:\projects\nb.m7\core\palette\src\org\netbeans\spi\palette\PaletteModule.java Locally New *************** *** 1,0 **** --- 1,47 ---- + /* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + package org.netbeans.spi.palette; + + import org.openide.modules.ModuleInstall; + import org.openide.windows.WindowManager; + + /** + * + * @author S. Aubrecht + * @since 1.10 + */ + public class PaletteModule extends ModuleInstall { + + /** Creates a new instance of ModuleInstall */ + public PaletteModule() { + } + + @Override + public void restored() { + super.restored(); + WindowManager.getDefault().invokeWhenUIReady( new Runnable() { + public void run() { + //start listening to activated TopComponents and Nodes + //to see if palette window should be displayed + PaletteSwitch.getDefault().startListening(); + } + }); + } + } Index: test/unit/src/org/netbeans/spi/palette/PaletteSwitchTest.java *** D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\PaletteSwitchTest.java No Base Revision --- D:\projects\nb.m7\core\palette\test\unit\src\org\netbeans\spi\palette\PaletteSwitchTest.java Locally New *************** *** 1,0 **** --- 1,175 ---- + /* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + package org.netbeans.spi.palette; + + import java.io.IOException; + import org.netbeans.core.windows.Constants; + import org.netbeans.core.windows.SplitConstraint; + import org.netbeans.core.windows.WindowManagerImpl; + import org.openide.filesystems.FileObject; + import org.openide.filesystems.FileSystem; + import org.openide.filesystems.MIMEResolver; + import org.openide.filesystems.Repository; + import org.openide.loaders.DataObject; + import org.openide.loaders.DataObjectNotFoundException; + import org.openide.nodes.Node; + import org.openide.util.lookup.AbstractLookup; + import org.openide.util.lookup.InstanceContent; + import org.openide.windows.Mode; + import org.openide.windows.TopComponent; + /** + * + * @author S. Aubrecht + */ + public class PaletteSwitchTest extends AbstractPaletteTestHid { + + private String lookupPaletteRootName; + private FileObject lookupPaletteRootFolder; + + static String mimePaletteRootName; + private static FileObject mimePaletteRootFolder; + private static final String MIME_TYPE_NAME = "junittest/x-paletteswitchtest"; + + private FileObject dummyDocumentFile; + private final static String DUMMY_DOCUMENT_FILE_EXTENSION = "junitPaletteSwitchTest"; + + static { + String[] layers = new String[] {"org/netbeans/spi/palette/mf-layer.xml"};//NOI18N + Object[] instances = new Object[] { new MyMimeResolver() }; + IDEInitializer.setup(layers,instances); + } + + public PaletteSwitchTest(String testName) { + super(testName); + } + + protected void setUp() throws Exception { + super.setUp(); + FileSystem fs = Repository.getDefault().getDefaultFileSystem(); + + lookupPaletteRootName = "lookupPalette" + System.currentTimeMillis(); + lookupPaletteRootFolder = fs.getRoot().createFolder( lookupPaletteRootName ); + createDefaultPaletteContentInFolder( lookupPaletteRootFolder ); + + if( null == mimePaletteRootName ) { + mimePaletteRootName = "mimePalette" + System.currentTimeMillis(); + mimePaletteRootFolder = fs.getRoot().createFolder( mimePaletteRootName ); + createDefaultPaletteContentInFolder( mimePaletteRootFolder ); + } + + dummyDocumentFile = fs.getRoot().createData( "dummyDocumentFile" + System.currentTimeMillis(), DUMMY_DOCUMENT_FILE_EXTENSION ); + } + + public void testNoLookupPalette() throws IOException { + TopComponent tc = createTopComponentWithPalette( null ); + PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); + + PaletteController foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); + + assertNull( foundPalette ); + } + + public void testNoMimePalette() throws IOException { + PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); + + PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( "unknown/mimetype" ); + + assertNull( foundPalette ); + } + + public void testLookupPalette() throws IOException { + PaletteController pc = PaletteFactory.createPalette( lookupPaletteRootName, new DummyActions() ); + + TopComponent tc = createTopComponentWithPalette( pc ); + PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); + + PaletteController foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); + + assertNotNull( foundPalette ); + assertEquals( pc.getModel().getName(), foundPalette.getModel().getName() ); + } + + public void testMimePalette() throws IOException { + TopComponent tc = createTopComponentWithPalette( null ); + tc.setActivatedNodes( new Node[] { DataObject.find( dummyDocumentFile ).getNodeDelegate() } ); + + PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); + + PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( MIME_TYPE_NAME ); + assertNotNull( foundPalette ); + assertEquals( mimePaletteRootName, foundPalette.getModel().getName() ); + + foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); + assertNotNull( foundPalette ); + assertEquals( mimePaletteRootName, foundPalette.getModel().getName() ); + } + + + public void testLookupPaletteTakePrecendsOverMimePalette() throws IOException { + PaletteController pc = PaletteFactory.createPalette( lookupPaletteRootName, new DummyActions() ); + + TopComponent tc = createTopComponentWithPalette( pc ); + tc.setActivatedNodes( new Node[] { DataObject.find( dummyDocumentFile ).getNodeDelegate() } ); + + PaletteSwitch paletteSwitch = PaletteSwitch.getDefault(); + + PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( MIME_TYPE_NAME ); + assertNotNull( foundPalette ); + assertEquals( mimePaletteRootName, foundPalette.getModel().getName() ); + + foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false ); + assertNotNull( foundPalette ); + assertEquals( pc.getModel().getName(), foundPalette.getModel().getName() ); + } + + private TopComponent createTopComponentWithPalette( PaletteController pc ) throws IOException { + TopComponent tc = new MyTopComponent( pc ); + + Mode editorMode = WindowManagerImpl.getInstance().findMode( "unitTestEditorMode" ); + if( null == editorMode ) { + editorMode = WindowManagerImpl.getInstance().createMode( "unitTestEditorMode", Constants.MODE_KIND_EDITOR, Constants.MODE_STATE_JOINED, false, new SplitConstraint[0] ); + } + editorMode.dockInto(tc); + return tc; + } + + private static class MyTopComponent extends TopComponent { + public MyTopComponent( PaletteController palette ) throws DataObjectNotFoundException { + this( new InstanceContent(), palette ); + } + + private MyTopComponent( InstanceContent ic, PaletteController palette ) throws DataObjectNotFoundException { + super( new AbstractLookup( ic ) ); + if( null != palette ) + ic.add( palette ); + } + } + + public static class MyMimeResolver extends MIMEResolver { + + public String findMIMEType(FileObject fo) { + if( DUMMY_DOCUMENT_FILE_EXTENSION.equals( fo.getExt() ) ) + return MIME_TYPE_NAME; + return null; + } + } + + + }