Thursday May 15, 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?

Zipping A Suite's NBM Files for Distribution

One of the many nice people I met at JavaOne was Mark Stephens, from IDR Solutions. Mark and a colleague did a very interesting BOF about how they created their JPedal viewer plugin for each of the main IDEs (i.e., IntelliJ, NetBeans IDE, and Eclipse). He highlighted various things that were cool and not so cool about creating plugins for each of the IDEs. One of the things he told me was that the "Build ZIP Distribution" menu item on the project node of module suite projects is confusing. He had expected to be able to choose that menu item and then get a ZIP file containing all the NBMs in the suite. Instead, the menu item resulted in an application being created, i.e., including a launcher and a bin folder and so on.

However, Mark's usecase was different: his suite contained a number of different modules which he wanted to upload to the Plugin Portal. However, one can only upload one file to a new Plugin Portal entry. Either one uploads one NBM file or one ZIP file. Mark, having multiple NBM files in his plugin, thus chose "Build ZIP Distribution" on the suite project and was disappointed/confused to discover that he was then prompted to make the suite an application after which he still didn't get his NBMs zipped up into a suite. As pointed out above, the purpose of that menu item is different to what Mark had expected. Therefore, I created a plugin today that provides this functionality:

Get it here:

http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=9296

I think everything is explained on that page. The menu item is only shown for module suite projects. The ZIP file is created via an Ant script that is created in the tmp directory and then removed after the target has completed. The Ant target simply runs the "nbms" target, which every module suite project has, thus creating all the NBM files into the build/updates folder and then picks up all the NBM files in that folder and ZIPs them up. (The only problem is that I can't put the menu item higher in the contextual menu, because ideally it would appear right beneath the existing "Build ZIP Distribution" item, but currently that's not possible.) If you are a NetBeans module developer and are interested in how all this is done, click here and you will get the Action's source file. (Click here to see the related layer file.) Comments/critiques/feedback on this functionality is/are welcome.

Today on NetBeans Zone. Two NetBeans Keyboard Shortcuts That Will Change Your Life (Forever)

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
« May 2008 »
SunMonTueWedThuFriSat
    
2
5
7
16
20
22
25
28
       
Today