# How To Open Compare Dialog

This is only valid since release 2.1M4.

In this page you will learn how to open a dialog displaying the result of a comparison.

## Preparing the input

The first thing to do is to choose an EMF Compare sub-implementation of the class of CompareEditorInput.

Two implementations are provided:

- ComparisonEditorInput, that should be use when you want to display a pre-computed Comparison (the results of EMFCompare).
- ComparisonScopeEditorInput, that should be use when you want to open the compare editor or dialog and to let it perform the comparison.

Both are available from the org.eclipse.emf.compare.ide.ui plug-in, in the package org.eclipse.emf.compare.ide.ui.internal.editor. This is still provisional API so we may break it any time.

## Preparing the configuration

When instantiating an EMF Compare specific implementation of CompareEditorInput, you have to give it at least three parameters:

- An EMFCompareConfiguration. This is a refined configuration of the standard CompareConfiguration so you just instantiate it like this:

    EMFCompareConfiguration configuration = new EMFCompareConfiguration(new CompareConfiguration());

- An EMFCompareEditingDomain. It is not an implementation of EditingDomain from EMF. It shares similar concepts (it has a command stack, it can create some commands) but is much simpler. You can create it through the factory method:

    // ancestor may be null
    ICompareEditingDomain editingDomain = EMFCompareEditingDomain.create(left, right, ancestor);

You can even give your own command(s) stack(s) if you need to know about executed merge commands.

- An AdapterFactory used by the framework to adapt EObjects to be nicely displayed. Most of the time, a ComposedAdapterFactory with the global registry is sufficient as created in the example below:

    AdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

Depending on the choosen sub-implementation of CompareEditorInput, you may need to provide additional parameters.

### ComparisonEditorInput

You must provide a Comparison object, the result of the comparison computation of EMFCompare.

    EMFCompare comparator = EMFCompare.builder().build();
    Comparison comparison = comparator.compare(EMFCompare.createDefaultScope(left, right, ancestor));

### ComparisonScopeEditorInput

You must provide the comparator (instance of EMFCompare) and the scope of the comparison.

    EMFCompare comparator = EMFCompare.builder().build();
    IComparisonScope scope = EMFCompare.createDefaultScope(left, right, ancestor);

## Opening the compare UI

Then, you can call the black magic method from Eclipse Compare framework. You have two choices. You may either open the compare UI wihtin a modal dialog or within an editor. Just call one of the two following methods:

- CompareUI.openCompareEditorOnPage(CompareEditorInput, IWorkbenchPage), to open an editor.
- CompareUI.openCompareDialog(CompareEditorInput), to open a modal dialog.

## End-to-end examples

### With pre-computed comparison

    public void compare(Notifier left, Notifier right, Notifier ancestor) {
        EMFCompare comparator = EMFCompare.builder().build();
        Comparison comparison = comparator.compare(EMFCompare.createDefaultScope(left, right, ancestor));

        EMFCompareConfiguration configuration = new EMFCompareConfiguration(new CompareConfiguration());
        ICompareEditingDomain editingDomain = EMFCompareEditingDomain.create(left, right, ancestor);
        AdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
        CompareEditorInput input = new ComparisonEditorInput(configuration, comparison, editingDomain, adapterFactory);

        CompareUI.openCompareDialog(input); // or CompareUI.openCompareEditor(input);
    }

### With a comparison scope

    public void compare(Notifier left, Notifier right, Notifier ancestor) {
        EMFCompare comparator = EMFCompare.builder().build();
        IComparisonScope scope = EMFCompare.createDefaultScope(left, right, ancestor));

        EMFCompareConfiguration configuration = new EMFCompareConfiguration(new CompareConfiguration());
        ICompareEditingDomain editingDomain = EMFCompareEditingDomain.create(left, right, ancestor);
        AdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
        CompareEditorInput input = new ComparisonScopeEditorInput(configuration, editingDomain, adapterFactory, comparator, scope);

        CompareUI.openCompareDialog(input); // or CompareUI.openCompareEditor(input);
    }