Thursday October 08, 2009
NetBeans Diff API
If you're creating some kind of text-based IDE or editor on the NetBeans Platform, integrating a Diff Viewer is possible via the NetBeans Diff API. It's quite new, since NetBeans IDE 6.7.
Here's a simple action that shows how the Diff API works (in its simplest scenario). For example, in real life, you wouldn't hardcode the files, but somehow identify them programmatically:
public final class DiffAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
StreamSource local = StreamSource.createSource("name1",
"title1", "text/html", new File("/home/geertjan/file1.html"));
StreamSource remote = StreamSource.createSource("name2",
"title2", "text/html", new File("/home/geertjan/file2.html"));
diff(local, remote);
}
public void diff(final StreamSource local, final StreamSource remote) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
DiffView view = Diff.getDefault().createDiff(local, remote);
TopComponent tc = new TopComponent();
tc.setDisplayName("Diff Viewer");
tc.setLayout(new BorderLayout());
tc.add(view.getComponent(), BorderLayout.CENTER);
tc.open();
tc.requestActive();
} catch (IOException ex) {
}
}
});
}
}
The code above gives you a whole new window, exactly as shown below, with a visual diff of the two files you sent to the Diff API:
And this is how I register the above action in the layer. Take note especially of the line in bold, which is new in 6.8 and ensures that the action will be invoked asynchronously (and a spinning cursor automatically appears during processing):
<folder name="Actions">
<folder name="Tools">
<file name="org-demo-diffimpl-DiffAction.instance">
<attr name="delegate" newvalue="org.demo.diffimpl.DiffAction"/>
<attr name="displayName" bundlevalue="org.demo.diffimpl.Bundle#CTL_DiffAction"/>
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
<attr name="noIconInMenu" boolvalue="false"/>
<attr name="asynchronous" boolvalue="true"/>
</file>
</folder>
</folder>
<folder name="Menu">
<folder name="Tools">
<file name="org-demo-diffimpl-DiffAction.shadow">
<attr name="originalFile" stringvalue="Actions/Tools/org-demo-diffimpl-DiffAction.instance"/>
<attr name="position" intvalue="0"/>
</file>
</folder>
</folder>
Another good thing is that the sample code shown in the Use Cases section of the NetBeans Diff API javadoc is very clear and so you can begin using this component in your own applications right away.
Oct 08 2009, 08:21:30 AM PDT Permalink
It sounds interesting.
Just look at :
(1)
my enhancement requests:
http://www.netbeans.org/issues/show_bug.cgi?id=155472
and
http://www.netbeans.org/issues/show_bug.cgi?id=159823
(2)
the idea behind is:
http://www.jroller.com/dmdevito/entry/the_netbeans_missing_distributions
Posted by Dominique De Vito on October 08, 2009 at 09:47 AM PDT #
It's a bit worrisome to see all those singletons floating around the netbeans api. Are there any plans to move towards something like osgi?
Posted by ocean on October 08, 2009 at 11:23 AM PDT #
@Dominique: Could you add a little more detail to your comment? How would moving to OSGi help and what singletons are you referring to? Don't get me wrong, I like OSGi as you might be able to see from my blog, but I simply didn't understand your comment :-).
Toni
Posted by Toni Epple on October 09, 2009 at 01:29 AM PDT #
Hi, I'm trying to use de Diff API as it says in the javadoc, the use case. But gives me two NullPointerExceptions in these classes:
org.netbeans.modules.diff.builtin.visualizer.editable.DecoratedEditorPane.paintComponent(DecoratedEditorPane.java:128)
org.netbeans.modules.diff.builtin.visualizer.editable.LineNumbersActionsBar.paintComponent(LineNumbersActionsBar.java:333)
It's something about the Editor settings being Null. I don't have it in an Action, only in a method inside the TopComponent, I don't know if there is a difference in that.
I will thank you any help you can give me
Posted by Walter on October 27, 2009 at 11:05 AM PDT #


