diff -r 9152bc6fef39 spi.palette/src/org/netbeans/spi/palette/DragAndDropHandler.java --- a/spi.palette/src/org/netbeans/spi/palette/DragAndDropHandler.java Tue Apr 22 10:39:01 2008 +0200 +++ b/spi.palette/src/org/netbeans/spi/palette/DragAndDropHandler.java Thu Apr 24 16:17:19 2008 +0200 @@ -45,9 +45,12 @@ import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; +import java.io.Reader; import java.util.logging.Level; import java.util.logging.Logger; +import javax.swing.SwingUtilities; import org.netbeans.modules.palette.DefaultModel; +import org.netbeans.modules.palette.ui.TextImporter; import org.openide.nodes.Index; import org.openide.nodes.Node; import org.openide.util.Lookup; @@ -66,7 +69,23 @@ */ public abstract class DragAndDropHandler { + private boolean isTextDnDEnabled; + private static DragAndDropHandler defaultHandler; + + public DragAndDropHandler() { + this( false ); + } + + /** + * Subclass this class and use this c'tor with true parameter to support text drag and drop + * into the palette to create new custom code clips. Subclassed instance must be then provided + * to PaletteFactory when creating PaletteController. + * @param textDnDEnabled True to allow text to be dropped into the palette window. + */ + protected DragAndDropHandler( boolean textDnDEnabled ) { + this.isTextDnDEnabled = textDnDEnabled; + } static DragAndDropHandler getDefault() { if( null == defaultHandler ) @@ -97,7 +116,7 @@ return true; } } - return false; + return (isTextDnDEnabled && DataFlavor.selectBestTextFlavor(flavors) != null); } /** @@ -157,6 +176,9 @@ } } return true; + } + if( isTextDnDEnabled && null != DataFlavor.selectBestTextFlavor(item.getTransferDataFlavors()) ) { + return importTextIntoPalette( targetCategory, item, dropIndex ); } } catch( IOException ioE ) { Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, ioE ); @@ -237,6 +259,34 @@ return true; } + private boolean importTextIntoPalette( Lookup targetCategory, Transferable item, int dropIndex ) + throws IOException, UnsupportedFlavorException { + + DataFlavor flavor = DataFlavor.selectBestTextFlavor( item.getTransferDataFlavors() ); + if( null == flavor ) + return false; + + String textToImport = extractText( item, flavor ); + SwingUtilities.invokeLater( new TextImporter( textToImport, targetCategory, dropIndex ) ); + return true; + } + + private String extractText( Transferable t, DataFlavor flavor ) + throws IOException, UnsupportedFlavorException { + + Reader reader = flavor.getReaderForText(t); + if( null == reader ) + return null; + StringBuffer res = new StringBuffer(); + char[] buffer = new char[4*1024]; + int len; + while( (len=reader.read( buffer )) > 0 ) { + res.append(buffer, 0, len); + } + + return res.toString(); + } + private static final class DefaultDragAndDropHandler extends DragAndDropHandler { public void customize(ExTransferable t, Lookup item) { //do nothing