Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

FAQ How do I create a compare editor?



Compare dialogs are typically used in simple contexts that ask the user to select from a list of available editions. For richer comparisons, a compare editor is typically used. The advantage of using an editor is that the user can take as long as needed to browse, modify, and merge the contents.


Compare editors display a tree of DiffNode elements, where each node represents a logical entity, such as a file or programming-language element. These nodes represent either a two-way or a three-way comparison, where the optional third element is the common ancestor of the two elements being compared. Each DiffNode references a left- and right-side element and, possibly, a third element representing the common ancestor. As with compare dialogs, these compare elements should implement ITypedElement and IStreamContentAccessor. You can construct these node trees manually or use the supplied Differencer class to help you construct it.


The DiffNode tree is computed by a CompareEditorInput subclass that is passed as an input to the editor. The subclass must implement the prepareInput method to return the tree represented by the DiffNode. The following example illustrates a compare editor input that uses the CompareItem class described in FAQ_How_do_I_create_a_Compare_dialog?

   class CompareInput extends CompareEditorInput {
      public CompareInput() {
         super(new CompareConfiguration());
      }
      protected Object prepareInput(IProgressMonitor pm) {
         CompareItem ancestor = 
            new CompareItem("Common", "contents");
         CompareItem left = 
            new CompareItem("Left", "new contents");
         CompareItem right = 
            new CompareItem("Right", "old contents");
         return new DiffNode(null, Differencer.CONFLICTING, 
            ancestor, left, right);
      }
   }

Once you have a compare editor input, opening a compare editor on that input is trivial. Here is an example action that opens a compare editor, using the preceding input:

   public class CompareEditorAction implements 
    IWorkbenchWindowActionDelegate {
      public void run(IAction action) {
         CompareUI.openCompareEditor(new CompareInput());
      }
   }


If you want to support merging as well as comparing, two extra steps are involved. First, you need to specify which of the elements is editable. This is done by the CompareConfiguration object that is passed to the CompareEditorInput constructor. Use the setLeftEditable and setRightEditable methods to specify which of the comparison panes should support modification. Second, your editor input class should override the save method to perform the save of the editor contents.


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top