Wednesday September 19, 2007
Enabling Undo/Redo Functionality on the NetBeans Platform
I've been looking through a draft of Heiko Böck's "NetBeans 6 - Rich-Client-Entwicklung mit Java" (which you can already preorder from Amazon, right here). Lots of interesting bits of code in there, quite a lot that I hadn't seen before. Some of them involve the UndoRedo.Manager. The basics are quite simple—declare the manager, override the getUndoRedo() method in the TopComponent or MultiviewElement and then, as shown in the highlighted line in the constructor below, assign the manager as an UndoableEditListener to the component's Document object:
private UndoRedo.Manager manager = new UndoRedo.Manager();
private DemoTopComponent() {
initComponents();
setName(NbBundle.getMessage(DemoTopComponent.class, "CTL_DemoTopComponent"));
setToolTipText(NbBundle.getMessage(DemoTopComponent.class, "HINT_DemoTopComponent"));
jEditorPane1.getDocument().addUndoableEditListener(manager);
}
@Override
public UndoRedo getUndoRedo() {
return manager;
}
Once you've done that, the IDE's (or your own application's) Undo and Redo functionality is enabled for your component—the menu items and toolbar buttons are enabled when you expect them to be, i.e., after making changes to the component's document, such as in the JEditorPane in the example below, where changes to the "Failed New Year's Resolutions" list result in the undo/redo actions being enabled/disabled:
I know there are more complex scenarios, and that those scenarios are often a cause of frustration, but at least, in this simple scenario, I'm happy to say that things work really nicely and exactly as I would expect, and clearly for very little coding.
In other news. Recently, the NetBeans Web team made a small but significant improvement—they've provided a constant URL pointing to the latest NetBeans weekly newsletter. In the past, each week's newsletter had a different URL, based on the day the newsletter was published. So, in the left sidebar of this blog, where I have a link to the latest newsletter, I used to have to change that link each week. Now there's just a standard URL that will always bring you to the latest newsletter. Why not add it to your blog or correspondence, or wherever, to publicize this extremely useful resource, that will always keep you up to date with the latest and coolest info coming out of the NetBeans project? Here it is: http://www.netbeans.org/community/news/newsletter/latest.html.
Sep 19 2007, 07:24:21 AM PDT Permalink


