Skip to content

Commit

Permalink
Issue #499: (v3) Overrides Papyrus Compare MergeViewerItemProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Ernesto Posse <eposse.gmail.com>
  • Loading branch information
Ernesto Posse committed Jun 24, 2024
1 parent ba9052e commit 1b6694d
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.emf.common;bundle-version="2.19.0",
com.google.guava;bundle-version="27.1.0",
org.eclipse.emf.compare;bundle-version="3.5.3",
org.eclipse.swt;bundle-version="3.114.100"
org.eclipse.swt;bundle-version="3.114.100",
org.eclipse.papyrus.compare.diagram.ide.ui;bundle-version="2.8.1",
org.eclipse.emf.compare.ide.ui;bundle-version="4.4.3",
org.eclipse.emf.compare.edit;bundle-version="4.3.1",
org.eclipse.uml2.uml;bundle-version="5.5.0",
org.eclipse.papyrus.uml.tools;bundle-version="4.3.0",
org.eclipse.papyrus.infra.core;bundle-version="4.4.0"
Bundle-RequiredExecutionEnvironment: JavaSE-11
Automatic-Module-Name: com.zeligsoft.domain.dds4ccm.ui.compare
Bundle-ActivationPolicy: lazy
8 changes: 8 additions & 0 deletions bundles/com.zeligsoft.domain.dds4ccm.ui.compare/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@
ranking="20">
</factory>
</extension>
<extension
point="org.eclipse.emf.compare.rcp.ui.contentMergeViewerCustomization">
<contentCustomization
context="com.zeligsoft.domain.dds4ccm.ui.compare.contentmergeviewer.provider.DDS4CCMContextTester"
mergeViewerItemProvider="com.zeligsoft.domain.dds4ccm.ui.compare.contentmergeviewer.provider.DDS4CCMMergeViewerItemProvider"
ranking="30">
</contentCustomization>
</extension>

</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
*/
package com.zeligsoft.domain.dds4ccm.ui.compare.contentmergeviewer.provider;

import java.util.Map;
import java.util.WeakHashMap;

import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.adapterfactory.context.AbstractContextTester;

import com.zeligsoft.domain.dds4ccm.ui.compare.internal.context.DDS4CCMContextUtils;

/**
*
*/
public class DDS4CCMContextTester extends AbstractContextTester {

/**
* A weak cache of comparisons that have been already been tested.
*/
private final Map<Comparison, Boolean> cache = new WeakHashMap<>();

/**
* {@inheritDoc}
*/
public boolean apply(Map<Object, Object> context) {
Comparison comparison = getComparison(context);
if (comparison != null) {
Boolean result = cache.get(comparison);
if (result == null) {
result = Boolean.valueOf(DDS4CCMContextUtils.isDDS4CCMContext(comparison));
cache.put(comparison, result);
}
return result.booleanValue();
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
*/
package com.zeligsoft.domain.dds4ccm.ui.compare.contentmergeviewer.provider;

import java.util.List;

import org.eclipse.emf.compare.ide.ui.internal.contentmergeviewer.provider.CompareAccessorMergeViewerItemProvider;
import org.eclipse.emf.compare.rcp.ui.contentmergeviewer.accessor.ICompareAccessor;
import org.eclipse.emf.compare.rcp.ui.mergeviewer.item.provider.IMergeViewerItemProviderConfiguration;

/**
*
*/
public class DDS4CCMMergeViewerItemProvider extends CompareAccessorMergeViewerItemProvider {

// @Override
// public List<Object> getMergeViewerItems(Object object,
// final IMergeViewerItemProviderConfiguration configuration) {
// final List<Object> mergeViewerItems = super.getMergeViewerItems(object, configuration);
//
// return mergeViewerItems;
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
*
*/
package com.zeligsoft.domain.dds4ccm.ui.compare.internal.context;

import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.Iterables.any;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.MatchResource;
import org.eclipse.emf.compare.scope.IComparisonScope2;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.uml2.uml.Element;

/**
*
*/
public final class DDS4CCMContextUtils {

/** The value ".di". */
private static final String DI_FILE_EXTENSION = "." + DiModel.DI_FILE_EXTENSION; //$NON-NLS-1$

/**
* Hidden constructor for this utility class.
*/
private DDS4CCMContextUtils() {
// prevent instantiation
}

/** Predicate specifying whether the given URI string ends with the Papyrus DI file extension. */
private static final Predicate<String> ENDS_WITH_PAPYRUS_EXTENSION = new Predicate<String>() {
public boolean apply(String input) {
if (input != null) {
return input.endsWith(DI_FILE_EXTENSION);
}
return false;
}
};

/** Transforms an URI into the platform string representation. */
private static final Function<URI, String> URI_TO_STRING = new Function<URI, String>() {
public String apply(URI input) {
String uriString = null;
if (input != null) {
uriString = input.toPlatformString(true);
if (uriString == null) {
uriString = input.toString();
}
}
return uriString;
}
};

/**
* Predicate testing whether a {@link Resource} is a UML resource.
*/
private static final Predicate<Resource> IS_UML_RESOURCE = new Predicate<Resource>() {
public boolean apply(Resource input) {
if (input == null) {
// Null is not an UML resource
return false;
}

URI uri = input.getURI();
return (uri != null) && UmlModel.UML_FILE_EXTENSION.equals(uri.fileExtension())
&& !input.getContents().isEmpty() //
&& input.getContents().get(0) instanceof Element;
}
};

/**
* Determines whether the comparison concerns Papyrus models.
*
* @param comparison
* the {@link Comparison} to check.
* @return {@code true} if the comparison concerns Papyrus models, {@code false} otherwise.
*/
public static boolean isDDS4CCMContext(Comparison comparison) {
final IComparisonScope2 comparisonScope = (IComparisonScope2)EcoreUtil
.getAdapter(comparison.eAdapters(), IComparisonScope2.class);
if (comparisonScope != null) {
return containsPapyrusURI(transform(comparisonScope.getAllInvolvedResourceURIs(), URI_TO_STRING));
}
// Fallback if the scope is not available. This way of determining the Papyrus context is not as
// accurate since the MatchResources are already minimized.
return containsPapyrusURI(comparison.getMatchedResources());
}

/**
* Specifies whether the given matched resources contain a Papyrus resource.
*
* @param matchedResources
* the list of matched resources to check.
* @return <code>true</code> if the matched resources contain a Papyrus resources, <code>false</code>
* otherwise.
*/
private static boolean containsPapyrusURI(List<MatchResource> matchedResources) {
final Builder<String> setBuilder = ImmutableSet.builder();
for (MatchResource res : matchedResources) {
for (String uri : ImmutableSet.of(res.getLeftURI(), res.getRightURI(), res.getOriginURI())) {
if (uri != null) {
setBuilder.add(uri);
}
}
}
return containsPapyrusURI(setBuilder.build());
}

/**
* Specifies whether the given URI strings contain a Papyrus resource.
*
* @param uris
* the set of uris to check.
* @return <code>true</code> if uris contain a Papyrus resources, <code>false</code> otherwise.
*/
private static boolean containsPapyrusURI(Collection<String> uris) {
return any(uris, ENDS_WITH_PAPYRUS_EXTENSION);
}

/**
* Obtains a predicate testing whether a resource is a UML resource.
*
* @return the UML resource predicate
*/
public static Predicate<Resource> isUMLResource() {
return IS_UML_RESOURCE;
}

}

0 comments on commit 1b6694d

Please sign in to comment.