Download NetBeans!

20080516 Friday May 16, 2008

Drag/Drop Snippets into Palette

Changes to the NetBeans APIs since the release of 6.1 are continually updated here. So, if you're interested in the NetBeans APIs, bookmark that page and look at it every week or so to see the latest changes. (By the way, the list of NetBeans API changes in 6.1 are found here.)

One of the post-6.1 changes (available since about a week in development builds) is this one: "Allowing user dropping text into the palette to create new custom code clips." (Read about it here.) The author of the API, Stan Aubrecht, who also added this latest enhancement, described it in his blog yesterday.

In summary, this enhancement means two different things:

  • NetBeans users. If you are a user of NetBeans IDE, you are able, since last week's post-6.1 development builds, to drag and drop HTML snippets from the editor into the palette, to create a new custom code snippet. If you hold down the Ctrl key while you drag the snippet into the palette, the snippet will be copied from the editor to the palette; if you don't hold down the Ctrl key, the snippet will be cut from the editor and pasted into the palette. Click the image below to enlarge it, it shows you what you'll see when you drop the snippet into the palette, whether copied or cut:

    You need to drag the snippet onto an existing item or a category in the palette. Currently this works for HTML files, while other existing palettes will follow.

  • NetBeans developers. This is even more interesting (in my opinion). If you provide a palette (i.e., you are creating a module that adds a palette to NetBeans IDE or another NetBeans Platform application that has an editor and a palette), you can enable the palette to provide the same functionality as the above. Let's go through the steps that make this possible. Take the following steps:

    1. Download the Java Source File Palette Sample from the Plugin Portal.

    2. Install the module and open the sample from the New Project wizard's Samples | NetBeans Modules category.

    3. Tweak the class called JavaSourceFileLayerPaletteFactory. Everything that is in bold below is what you need to add, i.e., everything else is from the class you find in the sample:

      public class JavaSourceFileLayerPaletteFactory {
      
          public static final String JAVA_PALETTE_FOLDER = "JavaPalette";
          private static PaletteController palette = null;
      
          public JavaSourceFileLayerPaletteFactory() {
          }
      
          public static PaletteController createPalette() {
              try {
                  if (null == palette) {
                      //Add null for the filter, which is unused in this sample,
                      //but needs to be set if the DragAndDropHandler will be used:
                      palette = PaletteFactory.createPalette(JAVA_PALETTE_FOLDER, 
                         new MyActions(), null, new MyDragAndDropHandler());
                  }
                  return palette;
              } catch (IOException ex) {
                  Exceptions.printStackTrace(ex);
              }
              return null;
          }
      
          private static class MyDragAndDropHandler extends DragAndDropHandler {
      
              MyDragAndDropHandler() {
                  super(true);
              }
      
              //Maybe you don't like the default 'add to palette' implementation,
              //so you could create your own here:
              @Override
              public void customize(ExTransferable t, Lookup item) {
              }
      
          }
      
          private static class MyActions extends PaletteActions {
      
              //Add new buttons to the Palette Manager here:
              @Override
              public Action[] getImportActions() {
                  return null;
              }
      
              //Add new contextual menu items to the palette here:
              @Override
              public Action[] getCustomPaletteActions() {
                  return null;
              }
              
              //Add new contextual menu items to the categories here:
              @Override
              public Action[] getCustomCategoryActions(Lookup arg0) {
                  return null;
              }
              
              //Add new contextual menu items to the items here:
              @Override
              public Action[] getCustomItemActions(Lookup arg0) {
                  return null;
              }
              
              //Define the default action here:
              @Override
              public Action getPreferredAction(Lookup arg0) {
                  return null;
              }
      
          }
      
      }

      What this means is that you need to provide (at least) a default implementation of org.netbeans.spi.palette.DragAndDropHandler, as done above.

    4. Install the module. Now you can drag and drop code from the editor to which the palette applies (i.e., the Java editor, in the case of the sample above) and then automatically the "Add to Palette" dialog will appear, exactly as done for HTML files in the case of the NetBeans user scenario above.

I think this is an extremely cool enhancement and we'll see it implemented in the other palettes in the IDE too. But it's especially cool that it's possible (and so easy) to implement in your custom palettes too. Of all the NetBeans APIs, the Palette API is one of the ones that has seen the most cool enhancements over the past two years, in my opinion. Are there other things that could make it even better?

May 16 2008, 05:40:21 AM PDT Permalink