Less than 50 Lines of Code to Create a Java Palette in NetBeans

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 .

Comments:

Hi!

I have am NEW to NetBeans. I am going through yous screen cast on the Top 10 Netbeans APIs. I would like to download these screen casts. Where can I download a copy of each one.

I am very much interested in developing applications using NetBeans.I hope you will reply soon.

Cheers!

Posted by Patrick Guasura on July 11, 2012 at 10:31 AM PDT #

I took a look at the code (but not tested it) and I think that it should be

if (null == palette) {
palette = ...
...
...
return palette;

or you can just remove the palette field.

Posted by Enrico Scantamburlo on July 15, 2012 at 04:01 PM PDT #

Great tutorial! I used it to build a java palette that contains all the macros in netbeans with a tooltip description for each (Visual Macros). That way I don't have to remember them. It just basically drops the abbreviation for the macro onto the document and uses the robot class to type the TAB key to expand it.
The only problem is for user defined macros - the tab doesn't get pressed to expand. Any idea how or (where to look in the APIs) to override the editor drop to always include a keypress after dropped?

Thanks man!
Ian

Posted by guest on December 16, 2012 at 09:41 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today