Difference between revisions of "EMF Compare/How To Open Compare Dialog With Comparison"

From Eclipsepedia

Jump to: navigation, search
(Preparing the configuration)
(Preparing the input)
Line 15: Line 15:
  
 
<pre>
 
<pre>
 +
package org.eclipse.emf.compare.ide.ui.internal.util;
 +
 +
import static com.google.common.base.Preconditions.checkNotNull;
 +
 +
import java.lang.reflect.InvocationTargetException;
 +
 +
import org.eclipse.compare.CompareConfiguration;
 +
import org.eclipse.compare.CompareEditorInput;
 +
import org.eclipse.compare.structuremergeviewer.IDiffElement;
 +
import org.eclipse.core.runtime.IProgressMonitor;
 +
import org.eclipse.emf.common.notify.AdapterFactory;
 +
import org.eclipse.emf.compare.Comparison;
 +
import org.eclipse.emf.compare.ide.ui.internal.EMFCompareConstants;
 +
import org.eclipse.emf.compare.ide.ui.internal.structuremergeviewer.EMFCompareStructureMergeViewer;
 +
import org.eclipse.jface.viewers.Viewer;
 +
import org.eclipse.swt.widgets.Composite;
 +
 
/**
 
/**
 
  * A custom implementation of the editor input.
 
  * A custom implementation of the editor input.
Line 29: Line 46:
 
* @param configuration
 
* @param configuration
 
*            the compare configuration
 
*            the compare configuration
* @param comparison
+
* @param adapterFactory
*            the comparison
+
*            an adapter factory (can be new as simple as ComposedAdapterFactory(
 +
ComposedAdapterFactory.Descriptor.Registry.INSTANCE)).
 
*/
 
*/
public CustomCompareEditorInput(CompareConfiguration configuration) {
+
public EMFCompareEditorInput(CompareConfiguration configuration, AdapterFactory adapterFactory) {
super(configuration);
+
super(checkNotNull(configuration));
 
this.comparison = (Comparison)configuration.getProperty(EMFCompareConstants.COMPARE_RESULT);
 
this.comparison = (Comparison)configuration.getProperty(EMFCompareConstants.COMPARE_RESULT);
this.adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);  
+
this.adapterFactory = checkNotNull(adapterFactory);
 
}
 
}
  
Line 57: Line 75:
 
public Viewer createDiffViewer(Composite parent) {
 
public Viewer createDiffViewer(Composite parent) {
 
return new EMFCompareStructureMergeViewer(parent, getCompareConfiguration());
 
return new EMFCompareStructureMergeViewer(parent, getCompareConfiguration());
};
+
}
 
}
 
}
 
</pre>
 
</pre>

Revision as of 05:00, 4 October 2012

This is only valid for the 2.0 release stream.

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 implement your own class of CompareEditorInput.

You must override two methods :

  • prepareInput(IProgressMonitor), that must return the Comparison object (result from the EMFCompare computation) adapted as an IDiffElement
  • createDiffViewer(Composite), that must return a new instance of EMFCompareStructureMergeViewer.

Here is a sample implementation that can be reused as is :

package org.eclipse.emf.compare.ide.ui.internal.util;

import static com.google.common.base.Preconditions.checkNotNull;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.ide.ui.internal.EMFCompareConstants;
import org.eclipse.emf.compare.ide.ui.internal.structuremergeviewer.EMFCompareStructureMergeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;

/**
 * A custom implementation of the editor input.
 */
class EMFCompareEditorInput extends CompareEditorInput {

	private final Comparison comparison;

	private final AdapterFactory adapterFactory;

	/**
	 * Constructor.
	 * 
	 * @param configuration
	 *            the compare configuration
	 * @param adapterFactory
	 *            an adapter factory (can be new as simple as ComposedAdapterFactory(
				ComposedAdapterFactory.Descriptor.Registry.INSTANCE)).
	 */
	public EMFCompareEditorInput(CompareConfiguration configuration, AdapterFactory adapterFactory) {
		super(checkNotNull(configuration));
		this.comparison = (Comparison)configuration.getProperty(EMFCompareConstants.COMPARE_RESULT);
		this.adapterFactory = checkNotNull(adapterFactory);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#prepareInput(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {
		return adapterFactory.adapt(this.comparison, IDiffElement.class);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#createDiffViewer(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public Viewer createDiffViewer(Composite parent) {
		return new EMFCompareStructureMergeViewer(parent, getCompareConfiguration());
	}
}

Preparing the configuration

When instantiating an EMFCompareEditorInput, you have to give him a CompareConfiguration. This is a standard CompareConfiguration (no specific implementation needed). But you need to fill two EMFCompare specific properties:

  • EMFCompareConstants.COMPARE_RESULT, the comparison itself.
  • EMFCompareConstants.EDITING_DOMAIN, 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.

Here is a code sample on how to create a CompareConfiguration suitable for EMFCompareEditorInput. The left, right and ancestor variables are the notifiers (EObject, Resource or ResourceSet) that have been given to EMFCompare to compute the Comparison (stored in the comparison variable).

final CompareConfiguration configuration = new CompareConfiguration();
configuration.setProperty(EMFCompareConstants.COMPARE_RESULT, comparison);
configuration.setProperty(EMFCompareConstants.EDITING_DOMAIN, new EMFCompareEditingDomain(comparison, left, right, ancestor));

Wiring it all together

Then, you can call the black magic method from Eclipse Compare framework: CompareUI.openCompareDialog(CompareEditorInput).

final Notifier left = ..., right = ..., ancestor = ...;
final Comparison comparison =  ...;

final CompareConfiguration configuration = new CompareConfiguration();
configuration.setProperty(EMFCompareConstants.COMPARE_RESULT, comparison);
configuration.setProperty(EMFCompareConstants.EDITING_DOMAIN, new EMFCompareEditingDomain(comparison, left, right, ancestor));

final CompareEditorInput input = new EMFCompareEditorInput(configuration);
input.setTitle("My own compare dialog");

configuration.setContainer(input);
CompareUI.openCompareDialog(input);