X

Geertjan's Blog

  • November 4, 2010

Diff Viewer for Maven Based Desktop Applications

Geertjan Wielenga
Product Manager
There are many reasons why you might want a diff window in your desktop application. Maybe you have files that need to be diffed, maybe you want to compare databases to each other, etc. Fortunately, the NetBeans Platform provides an out of the box Diff Window, together with programmatic access to it via the NetBeans Diff API.

Since we're using Maven, the trickiest part is setting up the dependencies correctly. Once that's done, we have a lot of functionality out of the box. Start by adding the dependencies below to the POM of the module where you want to provide a diff window:

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-diff</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-fold</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-settings</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-util</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-text</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-lib2</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-lib</artifactId>
<version>RELEASE691</version>
</dependency>

You also need to set dependencies in the application module's POM:

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-plain-lib</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-editor-plain</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-editor-errorstripe</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-options-editor</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-editor-actions</artifactId>
<version>RELEASE691</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-errorstripe-api</artifactId>
<version>RELEASE691</version>
</dependency>

Now use the Window template to create a new TopComponent in the module where you want to have a diff window. Put the window in any mode you like. In my case, I'm going to put it into the "output" mode, with "Diff" as the class name prefix.

OK. Now you're ready to start using the Diff API.

Add this to the end of the constructor generated by the Window template:

setLayout(new BorderLayout());
DiffView view = Diff.getDefault().createDiff(sourceA, sourceB);
add(view.getComponent(), BorderLayout.CENTER);

What's "sourceA" and "sourceB" in the second line above? That's up to you. It could be whatever it is that you're trying to diff. As a simple starting point, create two text files ("abc1.txt" and "abc2.txt"), somewhere on disk, e.g., in your home directory, with the content as follows:

In "abc1.txt":

aaaa
bb
cccc111

In "abc2.txt":

aaaa
bbb
cccc1234

Then change the snippet in the constructor to:

setLayout(new BorderLayout());
File fileA = new File("/home/geertjan/abc1.txt");
File fileB = new File("/home/geertjan/abc2.txt");
StreamSource source1 = StreamSource.createSource(
"name1", "title1", "text/plain", fileA);
StreamSource source2 = StreamSource.createSource(
"name2", "title2", "text/plain", fileB);
DiffView view;
try {
view = Diff.getDefault().createDiff(source1, source2);
add(view.getComponent(), BorderLayout.CENTER);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}

Of course, change the paths to correct them for your file locations.

Clean and build the parent project and then run the application module. The result should be as follows:

Join the discussion

Comments ( 1 )
  • opticyclic Sunday, March 13, 2011

    I can't get this to work in openSuse or Windows 7:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException

    at org.netbeans.api.queries.FileEncodingQuery.getEncoding(FileEncodingQuery.java:93)

    at org.netbeans.api.diff.StreamSource$Impl.<init>(StreamSource.java:187)

    at org.netbeans.api.diff.StreamSource.createSource(StreamSource.java:146)

    at diff.NewOkCancelDialog.<init>(NewOkCancelDialog.java:45)

    at diff.NewOkCancelDialog$4.run(NewOkCancelDialog.java:152)

    It seems that the URLMapper is always returning null for some reason (I have changed fileA/B to my own valid ones!).

    Could you extend this so that it wasn't just diff, but merge as well?


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.