This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 133840
Collapse All | Expand All

(-)a/spi.palette/src/org/netbeans/spi/palette/DragAndDropHandler.java (-1 / +51 lines)
Lines 45-53 Link Here
45
import java.awt.datatransfer.Transferable;
45
import java.awt.datatransfer.Transferable;
46
import java.awt.datatransfer.UnsupportedFlavorException;
46
import java.awt.datatransfer.UnsupportedFlavorException;
47
import java.io.IOException;
47
import java.io.IOException;
48
import java.io.Reader;
48
import java.util.logging.Level;
49
import java.util.logging.Level;
49
import java.util.logging.Logger;
50
import java.util.logging.Logger;
51
import javax.swing.SwingUtilities;
50
import org.netbeans.modules.palette.DefaultModel;
52
import org.netbeans.modules.palette.DefaultModel;
53
import org.netbeans.modules.palette.ui.TextImporter;
51
import org.openide.nodes.Index;
54
import org.openide.nodes.Index;
52
import org.openide.nodes.Node;
55
import org.openide.nodes.Node;
53
import org.openide.util.Lookup;
56
import org.openide.util.Lookup;
Lines 66-72 Link Here
66
 */
69
 */
67
public abstract class DragAndDropHandler {
70
public abstract class DragAndDropHandler {
68
71
72
    private boolean isTextDnDEnabled;
73
    
69
    private static DragAndDropHandler defaultHandler;
74
    private static DragAndDropHandler defaultHandler;
75
    
76
    public DragAndDropHandler() {
77
        this( false );
78
    }
79
    
80
    /**
81
     * Subclass this class and use this c'tor with <code>true</code> parameter to support text drag and drop
82
     * into the palette to create new custom code clips. Subclassed instance must be then provided
83
     * to <code>PaletteFactory</code> when creating <code>PaletteController</code>.
84
     * @param textDnDEnabled True to allow text to be dropped into the palette window.
85
     */
86
    protected DragAndDropHandler( boolean textDnDEnabled ) {
87
        this.isTextDnDEnabled = textDnDEnabled;
88
    }
70
    
89
    
71
    static DragAndDropHandler getDefault() {
90
    static DragAndDropHandler getDefault() {
72
        if( null == defaultHandler )
91
        if( null == defaultHandler )
Lines 97-103 Link Here
97
                return true;
116
                return true;
98
            }
117
            }
99
        }
118
        }
100
        return false;
119
        return (isTextDnDEnabled && DataFlavor.selectBestTextFlavor(flavors) != null);
101
    }
120
    }
102
    
121
    
103
    /**
122
    /**
Lines 157-162 Link Here
157
                    }
176
                    }
158
                }
177
                }
159
                return true;
178
                return true;
179
            }
180
            if( isTextDnDEnabled && null != DataFlavor.selectBestTextFlavor(item.getTransferDataFlavors()) ) {
181
                return importTextIntoPalette( targetCategory, item, dropIndex );
160
            }
182
            }
161
        } catch( IOException ioE ) {
183
        } catch( IOException ioE ) {
162
            Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, ioE );
184
            Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, ioE );
Lines 237-242 Link Here
237
        return true;
259
        return true;
238
    }
260
    }
239
    
261
    
262
    private boolean importTextIntoPalette( Lookup targetCategory, Transferable item, int dropIndex ) 
263
            throws IOException, UnsupportedFlavorException {
264
        
265
        DataFlavor flavor = DataFlavor.selectBestTextFlavor( item.getTransferDataFlavors() );
266
        if( null == flavor )
267
            return false;
268
        
269
        String textToImport = extractText( item, flavor );
270
        SwingUtilities.invokeLater( new TextImporter( textToImport, targetCategory, dropIndex ) );
271
        return true;
272
    }
273
    
274
    private String extractText( Transferable t, DataFlavor flavor ) 
275
            throws IOException, UnsupportedFlavorException {
276
        
277
        Reader reader = flavor.getReaderForText(t);
278
        if( null == reader )
279
            return null;
280
        StringBuffer res = new StringBuffer();
281
        char[] buffer = new char[4*1024];
282
        int len;
283
        while( (len=reader.read( buffer )) > 0 ) {
284
            res.append(buffer, 0, len);
285
        }
286
        
287
        return res.toString();
288
    }
289
    
240
    private static final class DefaultDragAndDropHandler extends DragAndDropHandler {
290
    private static final class DefaultDragAndDropHandler extends DragAndDropHandler {
241
        public void customize(ExTransferable t, Lookup item) {
291
        public void customize(ExTransferable t, Lookup item) {
242
            //do nothing
292
            //do nothing

Return to bug 133840