Tomasz Zarna, IBM KSL since 2 years, 1 year eclipse committer in Compare, Team, CVS
The Compare Framework allows to compare alternate states of a file system, or in the simplest case: two files or two different versions of the same file. In some cases we’re comparing 3 files at the same time but I’ll talk about it in a minute. What does the framework provide? It gives us the API to define the input to a comparison because we need a way to point what to compare. It gives as extensions for associating viewers with input types because sometimes we want to compare different types of inputs in a different way (using different viewers). And finally it gives us containers to display the result in the UI, so we don’t need to the UI part from the scratch. Diff engine: For tree or range based differences , API and action: Compare with Each Other, Local History, Apply Patch
Here is a quick view on the compare architecture. As I said we usually compare two states of a file, named „left” and „right” here. If there is a third one it’s treated as a common ancestor of the previous two. Comparison result is displayed in compare viewers, which can support specific model providers like JDT or EMF. There are many clients of the compare framework, some of the are shown at the top.
What do we need to create the simplest compare editor? First we will need a CompareItem that represents a resource we are going to compare. Then we will need a CompareEditorInput class that manages the panes of the comparison. Diff nodes are used as the compare result of the differencing engine. With CompareConfiguration we can set details of the comparison, like which of the panes is editable or set lables for panes.
Let’s take a look at the code. As mentioned before we need to subclass the CompareEditorInput. In the simplest example we will only implement the prepareInput method. Inside we will create 3 CompareItems, set lables for compare panes and create the DiffNode. In the action class all we need to do is open a compare editor and pass our compare input as an argument.
Now let’s have a look at the CompareItem class. It implements 3 interfaces, but because we want to keep it as simple as possible the implementation is rather straightforward. Definition of the popupMenus extension point is also trivial.
And here is the result!
Here you can see how structure viewers can be used. This is a result of comparing two zip archives. There is a change in the Three.java file. We can see where is it located by looking at the „Zip Archive Compare” pane, on the right in the „Java Structure Compare” pane we can see what parts of the java files has been changed. Below we can see what exactly happened.
Some of you have probably noticed that when comparing Java files, the Compare Editor doesn’t give us many Java Editor editor as it could. We’re working on making these diffs less visible, by adding more Java features to Compare Editor. Some of the bugs are even 7 years old, but that’s a good thing because if the bug is older more people will be happy when it’s finally fixed :)