Want to drag and drop Java code snippets into the palette, in the same way as can be done for HTML files? If so, create a new module and add a class with the content below and you're done. You'll be able to select a piece of Java code, drag it into the palette (Ctrl-Shift-8 to open it), where you'll be able to set a name, tooltip, and icons for the snippet, and then you'll be able to drag it out of the palette into any Java files you like.
The palette content is persisted across restarts of the IDE.
package org.netbeans.modules.javasourcefilepalette;
import java.io.IOException;
import javax.swing.Action;
import org.netbeans.api.editor.mimelookup.MimeRegistration;
import org.netbeans.spi.palette.DragAndDropHandler;
import org.netbeans.spi.palette.PaletteActions;
import org.netbeans.spi.palette.PaletteController;
import org.netbeans.spi.palette.PaletteFactory;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.datatransfer.ExTransferable;
public class JavaSourceFileLayerPaletteFactory {
private static PaletteController palette = null;
@MimeRegistration(mimeType = "text/x-java", service = PaletteController.class)
public static PaletteController createPalette() {
try {
if (null == palette) {
return PaletteFactory.createPalette(
//Folder:
"JavaPalette",
//Palette Actions:
new PaletteActions() {
@Override public Action[] getImportActions() {return null;}
@Override public Action[] getCustomPaletteActions() {return null;}
@Override public Action[] getCustomCategoryActions(Lookup lkp) {return null;}
@Override public Action[] getCustomItemActions(Lookup lkp) {return null;}
@Override public Action getPreferredAction(Lookup lkp) {return null;}
},
//Palette Filter:
null,
//Drag and Drop Handler:
new DragAndDropHandler(true) {
@Override public void customize(ExTransferable et, Lookup lkp) {}
});
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
}
In my layer file, I have this content:
<folder name="JavaPalette">
<folder name="Snippets"/>
</folder>
That's all. Run the module. Open a Java source file and the palette will automatically open. Drag some code into the palette and a dialog will pop up asking for some details like display name and icons. Then the snippet will be in the palette and you'll be able to drag and drop it anywhere you like. Use the Palette Manager, which is automatically integrated, to add new categories and show/hide palette items.
Related blog entry, for which the above is a big simplification: Drag/Drop Snippets into Palette
.