Moving the implementation to IDE.UI. For CVS, we depend on
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.edit / src / org / eclipse / emf / compare / provider / spec / MatchResourceItemProviderSpec.java
blobfacbf985c01bf4994cfd88463bc215faaeb43d31
1 /*******************************************************************************
2 * Copyright (c) 2012 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
7 *
8 * Contributors:
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.provider.spec;
13 import org.eclipse.emf.common.notify.AdapterFactory;
14 import org.eclipse.emf.compare.MatchResource;
15 import org.eclipse.emf.compare.provider.MatchResourceItemProvider;
17 /**
18 * Specialized {@link MatchResourceItemProvider} returning nice output for {@link #getText(Object)} and
19 * {@link #getImage(Object)}.
21 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
23 public class MatchResourceItemProviderSpec extends MatchResourceItemProvider {
25 /**
26 * Constructor calling super {@link #MatchResourceItemProviderSpec(AdapterFactory)}.
28 * @param adapterFactory
29 * the adapter factory
31 public MatchResourceItemProviderSpec(AdapterFactory adapterFactory) {
32 super(adapterFactory);
35 /**
36 * {@inheritDoc}
38 * @see org.eclipse.emf.compare.provider.MatchResourceItemProvider#getText(java.lang.Object)
40 @Override
41 public String getText(Object object) {
42 final MatchResource matchResource = (MatchResource)object;
43 final String leftURI = matchResource.getLeftURI();
44 final String rightURI = matchResource.getRightURI();
46 final String commonBase = getCommonBase(leftURI, rightURI);
48 String text = ""; //$NON-NLS-1$
49 if (leftURI != null) {
50 text += leftURI.substring(commonBase.length());
52 text += " <-> "; //$NON-NLS-1$
53 if (rightURI != null) {
54 text += rightURI.substring(commonBase.length());
56 // TODO is that really useful info?
57 // if (matchResource.eContainer() instanceof Comparison
58 // && ((Comparison)matchResource.eContainer()).isThreeWay()) {
59 // final String originURI = matchResource.getOriginURI();
60 // text += " (" + originURI + ")";
61 // }
62 return text;
65 /**
66 * Returns the longest common starting substring of the two given strings.
68 * @param left
69 * First of the two strings for which we need the common starting substring.
70 * @param right
71 * Second of the two strings for which we need the common starting substring.
72 * @return The longest common starting substring of the two given strings.
74 public String getCommonBase(String left, String right) {
75 if (left == null || right == null) {
76 return ""; //$NON-NLS-1$
79 final char[] leftChars = left.toCharArray();
80 final char[] rightChars = right.toCharArray();
82 final StringBuilder buffer = new StringBuilder();
83 for (int i = 0; i < Math.min(leftChars.length, rightChars.length); i++) {
84 if (leftChars[i] == rightChars[i]) {
85 buffer.append(leftChars[i]);
86 } else {
87 break;
91 return buffer.toString();