Extreme Java When vanilla Java is not enough


From Swing to NetBeans Platform (part 3)

In my previous posts, I put an old Swing application to run as a NetBeans Platform Application and start converting JDialogs to TopComponent sensitive to global selection. Now, it's time to remove a panel from the main editor.

The old MapEditor is a "demigod class" that is the centre of all logic of the editor (I guess that's why they abandoned the project). Everything is there: layer management, tileset choosing, among others. It's over 2000 lines!

The first feature I will refactor is the layer management. Just like the Tileset Manager, I created a TopComponent sensitive to MapEditor selection (available from the main editor). The difference is I'm using the NB's Outline view to reproduce the old table layer style, and the layer node implements CheckableNode's contract (with "checked" meaning "visible").

Another interesting behaviour is the toolbar below the layer table. I used a JToolBar with this recipe to add its buttons:

for (Object obj : Lookups.forPath("...")
    .lookupAll(Object.class)) {
  if (obj instanceof JSeparator) {
  if (obj instanceof Action) {
    tbrBottom.add((Action) obj);

Using this snippet with NB's actions registered on layer.xml, it was easy to make an extensible toolbar with buttons synchronized to the Outline's selection.

As a last tip, I associated an InstanceContent to the layer TopComponent's lookup. This IC adds the global selection (i.e. the map shown in the Outline view) to the TC lookup. The resultChanged method is:

Collection<? extends TiledMap> c = result.allInstances();
if (!c.isEmpty()) {
  TiledMap map = c.iterator().next();
  em.setRootContext(new RootNode(map));
} else {
  ic.set(Collections.EMPTY_LIST, null);

Using this snippet retains the global selection when user put the focus on layer TC. Without this workaround, it loses the selection, like this example.

Filed under: NetBeans No Comments