1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Obeo.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.emf
.compare
.provider
.spec
;
13 import static com
.google
.common
.base
.Predicates
.not
;
15 import com
.google
.common
.base
.Predicate
;
17 import org
.eclipse
.emf
.common
.notify
.AdapterFactory
;
18 import org
.eclipse
.emf
.compare
.Comparison
;
19 import org
.eclipse
.emf
.compare
.MatchResource
;
20 import org
.eclipse
.emf
.compare
.ResourceAttachmentChange
;
21 import org
.eclipse
.emf
.compare
.provider
.IItemDescriptionProvider
;
22 import org
.eclipse
.emf
.compare
.provider
.IItemStyledLabelProvider
;
23 import org
.eclipse
.emf
.compare
.provider
.MatchResourceItemProvider
;
24 import org
.eclipse
.emf
.compare
.provider
.utils
.ComposedStyledString
;
25 import org
.eclipse
.emf
.compare
.provider
.utils
.IStyledString
;
26 import org
.eclipse
.emf
.ecore
.resource
.Resource
;
27 import org
.eclipse
.emf
.edit
.provider
.AdapterFactoryItemDelegator
;
30 * Specialized {@link MatchResourceItemProvider} returning nice output for {@link #getText(Object)} and
31 * {@link #getImage(Object)}.
33 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
35 public class MatchResourceItemProviderSpec
extends MatchResourceItemProvider
implements IItemStyledLabelProvider
, IItemDescriptionProvider
{
37 /** The item delegator for matched resources. */
38 private final AdapterFactoryItemDelegator itemDelegator
;
41 * Constructor calling super {@link #MatchResourceItemProviderSpec(AdapterFactory)}.
43 * @param adapterFactory
46 public MatchResourceItemProviderSpec(AdapterFactory adapterFactory
) {
47 super(adapterFactory
);
48 itemDelegator
= new AdapterFactoryItemDelegator(getRootAdapterFactory());
52 * Predicate to check if the URI of the current attachment change is equal to one (at least) of the URIs
53 * of the resources matched by the given <code>MatchResource</code>.
55 * @param matchResource
57 * @return The predicate.
59 private static Predicate
<ResourceAttachmentChange
> uriEqualToOneAtLeast(final MatchResource matchResource
) {
60 return new Predicate
<ResourceAttachmentChange
>() {
61 public boolean apply(ResourceAttachmentChange difference
) {
62 final String diffResourceURI
= difference
.getResourceURI();
63 return diffResourceURI
!= null
64 && (diffResourceURI
.equals(matchResource
.getLeftURI())
65 || diffResourceURI
.equals(matchResource
.getRightURI()) || diffResourceURI
66 .equals(matchResource
.getOriginURI()));
72 * Predicate to check if the URI of the current attachment change is different from all the URIs of the
73 * resources matched by the given <code>MatchResource</code>.
75 * @param matchResource
77 * @return The predicate.
80 public static final Predicate
<ResourceAttachmentChange
> uriDifferentFromAll(
81 final MatchResource matchResource
) {
82 return not(uriEqualToOneAtLeast(matchResource
));
88 * @see org.eclipse.emf.compare.provider.MatchResourceItemProvider#getText(java.lang.Object)
91 public String
getText(Object object
) {
92 final MatchResource matchResource
= (MatchResource
)object
;
93 final String leftURI
= matchResource
.getLeftURI();
94 final String rightURI
= matchResource
.getRightURI();
96 final String commonBase
= getCommonBase(leftURI
, rightURI
);
98 String text
= ""; //$NON-NLS-1$
99 if (leftURI
!= null) {
100 text
+= leftURI
.substring(commonBase
.length());
102 text
+= " <-> "; //$NON-NLS-1$
103 if (rightURI
!= null) {
104 text
+= rightURI
.substring(commonBase
.length());
106 if (matchResource
.eContainer() instanceof Comparison
107 && ((Comparison
)matchResource
.eContainer()).isThreeWay()) {
108 final String originURI
= matchResource
.getOriginURI();
109 if (originURI
!= null) {
110 text
+= " (" + originURI
.substring(commonBase
.length()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
119 * @see org.eclipse.emf.compare.provider.MatchResourceItemProvider#getImage(java.lang.Object)
122 public Object
getImage(Object object
) {
123 final MatchResource matchResource
= (MatchResource
)object
;
124 Resource resource
= matchResource
.getLeft();
126 if (resource
== null) {
127 resource
= matchResource
.getRight();
128 if (resource
== null) {
129 resource
= matchResource
.getOrigin();
133 if (resource
!= null) {
134 image
= itemDelegator
.getImage(resource
);
136 image
= super.getImage(object
);
139 image
= super.getImage(object
);
145 * Returns the longest common starting substring of the two given strings.
148 * First of the two strings for which we need the common starting substring.
150 * Second of the two strings for which we need the common starting substring.
151 * @return The longest common starting substring of the two given strings.
153 public String
getCommonBase(String left
, String right
) {
154 if (left
== null || right
== null) {
155 return ""; //$NON-NLS-1$
158 final char[] leftChars
= left
.toCharArray();
159 final char[] rightChars
= right
.toCharArray();
161 final StringBuilder buffer
= new StringBuilder();
162 StringBuilder fragmentBuffer
= new StringBuilder();
163 for (int i
= 0; i
< Math
.min(leftChars
.length
, rightChars
.length
); i
++) {
164 if (leftChars
[i
] == rightChars
[i
]) {
165 fragmentBuffer
.append(leftChars
[i
]);
167 if (leftChars
[i
] == '\\' || leftChars
[i
] == '/') {
168 buffer
.append(fragmentBuffer
);
169 fragmentBuffer
= new StringBuilder();
176 return buffer
.toString();
179 /* Missing override : only for EMF 2.10 and later. Do not tag. */
183 * @see org.eclipse.emf.compare.provider.IItemStyledLabelProvider#getStyledText(java.lang.Object)
185 public IStyledString
.IComposedStyledString
getStyledText(Object object
) {
186 return new ComposedStyledString(getText(object
));
192 * @see org.eclipse.emf.compare.provider.IItemDescriptionProvider#getDescription(java.lang.Object)
194 public String
getDescription(Object object
) {
195 return getText(object
);