Download NetBeans!

20070605 Tuesday June 05, 2007

Prague Writers Festival

Attended an evening of the Prague Writers Festival. Three writers got interviewed, one by one, by various interesting or less interesting interviewers. Funny moments: First question for Writer 1 (James Meek): "So, this is your first book..." Response from Writer 1: "Well, actually, this is my third book..." Later, in the same interview: "Your book has quite a lot of violence in it..." Response: "Not really. There's just a little bit of castration and cannabilism. But, apart from that, not really." His book is apparently about horses. It opens with horses falling from trees, according to the interview. And then, somehow, it focuses on a Russian sect that worships voluntary(!) castration. Then the second writer was interviewed, by perennially annoyingly vague and pretentious Michael March. The second writer turned out to be Peter Stephan Jungk, who is, originally, from Vienna, where I used to live (for close to 3 years). I talked to him afterwards, and there was a lot of identification, a lot that we recognized (and knew) about each other's experieces in Vienna. The beauty, the stoic yet plastic nobility, the bombast, the superficiality, the pride, the stupidity of such an opulent and massive capital for an empire that is now a vastly shrunk bit player. But, despite the recognition, he had a clear aversion to, and fear of, aloneness and loneliness. Ended up having a discussion with some random person about how people's toes tend to contradict one's expectations. People wearing sandles, exposing their toes (those vulnerable appendages that cannot disguise their true nature) are often so different to the face to which they are (indirectly) attached. Very interesting, if somewhat obscure, conversation, provoked by me, so I can't complain. (Funny moment in that conversation was when it turned out he aspired to be a waiter and I laughed and said he was unique for 'aspiring to mediocrity' and then we discussed, briefly, a short story in which the hero aspires to be a waiter but fails multiple times for obscure reasons.) Then, the break having ended, the final interview, with Dutch writer Arnon Grunberg, which is why I went to the evening in the first place. Famous in the Netherlands for his elusiveness and writing style. Just like Jungk, he did a great reading. (Memorable for his "I want the snow!" which reminded me, for its contrast, with Rimbaud's cries of "I want the sun!") The questions he got were pretty banal and, in retrospect, the first interview was the most edifying. Because of Grunberg's presence, there were several Dutch people in the audience. Easily identifiable—lanky, myopic, with a smooth-faced naievety that is easily mistaken for openness, honesty, and optimism. That which has put the Dutch in the forefront of campaigns against injustice, but simultaneously brought the world catastrophic blunders with cataclysmic consequences, like Srebrenica. (By the way, I found out yesterday that it turns out that the Dutch government wasn't "abandoned by the UN" (as the Dutch government claimed aftewards). Instead, they'd TOLD the UN not to intervene, up to 7 times (i.e., literally, by Kok and van Mierlo, overriding desperate requests from troops on the ground), because they feared that their 30 or so 'voluntary hostages' would have been put in danger if any pressure had been put on the Serbs.)

Jun 05 2007, 02:40:59 PM PDT Permalink

NetBeans DragAndDropHandler Class and the "Enclose In" Feature

I received an interesting bit of code from Stan Aubrecht, the NetBeans drag-and-drop king, yesterday. It is an implementation of the NetBeans API org.netbeans.spi.palette.DragAndDropHandler class. It lets you drag a piece of code from the editor into the palette:

private static class MyHandler extends DragAndDropHandler {

   public void customize(ExTransferable t, Lookup item) {
   }

   @Override
   public boolean canDrop(Lookup targetCategory, DataFlavor[] flavors, int dndAction) {
       for( DataFlavor flavor : flavors ) {
           if( DataFlavor.stringFlavor.equals( flavor ) ) {
               //some text is being dragged over the palette so let's allow the drop
               //(we may want to check that the text is dragged over some specific 
               //'customizable' category)
               return true;
           }
       }
       return super.canDrop(targetCategory, flavors, dndAction);
   }

   @Override
   public boolean doDrop(Lookup targetCategory, Transferable item, int dndAction, int dropIndex) {
       if( item.isDataFlavorSupported( DataFlavor.stringFlavor ) ) {
           try {
               String draggedText = (String)item.getTransferData( DataFlavor.stringFlavor );
               //show some add-to-palette dialog window to select item name and icon
               //create a new item
               //add it to targetCategory
               return true;
           } catch( IOException e ) {
               Exceptions.printStackTrace( e );
           } catch( UnsupportedFlavorException e ) {
               Exceptions.printStackTrace( e );
           }
       }
       return super.doDrop(targetCategory, item, dndAction, dropIndex);
   }
}

To use the DragAndDropHandler, you need to add it as an argument to the PaletteFactory.createPalette method, as shown in the highlighted bit here:

public static PaletteController createPalette() {
    try {
        if (null == palette)
            palette = PaletteFactory.createPalette(JAVA_PALETTE_FOLDER, new MyActions(), null, new MyHandler());
        return palette;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}

"That's all well and good," you might think, "but, so what?" Well, if you read the code on page 352 and 353 of Rich Client Programming: Plugging into the NetBeans Platform, you'll find a piece of code that programmatically creates an XML file that conforms to the NetBeans Editor Palette Item DTD. If you create that XML file in the appropriate folder in the user directory, it will appear in the palette. So, once the text from the editor is dropped in the palette, you need a small dialog box to appear, where the user can specify the palette item's display name, tooltip, and icons. And then you're done, because the code above handles all the rest. In other words, the code above handles the drag from the editor and the drop in the palette.

One thing I learned was that you need to make sure that the user copies the text from the editor, instead of cutting, otherwise the snippet will be in the palette, but not in the editor (anymore)! That's easy to implement, all you need to do is make sure that the DndAction brings the correct int, as shown in the highlighted condition within the if statement below:

@Override
public boolean canDrop(Lookup targetCategory, DataFlavor[] flavors, int dndAction) {
    for( DataFlavor flavor : flavors ) {
        if( DataFlavor.stringFlavor.equals( flavor ) && dndAction == 1 ) {
            //some text is being dragged over the palette so let's allow the drop
            //(we may want to check that the text is dragged over some specific
            //'customizable' category)

            return true;
        }
    }
    return super.canDrop(targetCategory, flavors, dndAction);
}

So, above, the drop will not succeed unless a copy action is done. A copy action is done when the user holds down the Ctrl key prior to moving the text to the palette. Otherwise, it is a cut action (which returns a "2" instead of a "1" for the DndAction).

Once I have this whole scenario completely worked out, including the icons (which is what I'm working on now), I'll make it available somehow. The combination of MIME-type registration and the new NetBeans Editor Palette Item DTD make this scenario possible. By the way, if you compare what I'm doing here to the scenario outlined in Palette API and 6.0, Testing the Waters... (Part 3) , then it is interesting to outline the difference. And the difference is that in that scenario, the user had to right-click in the editor and choose a menu item. When the menu item was chosen, the small customizer appeared. In contrast to that, the current scenario uses the DragAndDropHandler class, which enables the text in the editor to be dragged, and thus allows for a smoother experience for the user (i.e., no right-clicking necessary and not necessary to select a menu item, because all you're doing is dragging). That's the only difference. In that scenario, I was using the latest NetBeans Editor Palette Item DTD already, hence I was able to let the user specify their own display name, tooltip, and icons, which wasn't possible in the earlier version of the DTD, since there they had to be defined in a properties file, within the module.

One other interesting thing... today, for the first time, I noticed the feature that I was so happy about at NetBeans Day in San Francisco. In fact, in my report on that day (here), my very first paragraph described the new "enclose in" functionality, allowing me to select components in the GUI Builder and then do a kind of 'surround with' a selected container, such as a JPanel. To see what I mean look at it here:

So, now I just need to select the container and then my selected components are immediately grouped within that container. Extremely handy. Great to have it available for real now.

Jun 05 2007, 08:52:36 AM PDT Permalink