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
.ide
.ui
.internal
.contentmergeviewer
.text
;
13 import static org
.eclipse
.emf
.compare
.ide
.utils
.ResourceUtil
.saveAllResources
;
14 import static org
.eclipse
.emf
.compare
.ide
.utils
.ResourceUtil
.saveResource
;
16 import com
.google
.common
.collect
.ImmutableMap
;
18 import org
.eclipse
.compare
.ITypedElement
;
19 import org
.eclipse
.compare
.contentmergeviewer
.IMergeViewerContentProvider
;
20 import org
.eclipse
.compare
.structuremergeviewer
.ICompareInput
;
21 import org
.eclipse
.emf
.common
.util
.EList
;
22 import org
.eclipse
.emf
.compare
.Match
;
23 import org
.eclipse
.emf
.compare
.ide
.ui
.internal
.configuration
.EMFCompareConfiguration
;
24 import org
.eclipse
.emf
.ecore
.EObject
;
25 import org
.eclipse
.emf
.ecore
.resource
.Resource
;
26 import org
.eclipse
.emf
.ecore
.resource
.ResourceSet
;
27 import org
.eclipse
.jface
.viewers
.Viewer
;
28 import org
.eclipse
.swt
.graphics
.Image
;
31 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
33 public class EMFCompareTextMergeViewerContentProvider
implements IMergeViewerContentProvider
{
35 private EMFCompareConfiguration fCompareConfiguration
;
37 private String fAncestorError
;
39 private String fLeftError
;
41 private String fRightError
;
43 public EMFCompareTextMergeViewerContentProvider(EMFCompareConfiguration cc
) {
44 fCompareConfiguration
= cc
;
47 private boolean hasError() {
48 return fAncestorError
!= null || fLeftError
!= null || fRightError
!= null;
51 public void dispose() {
52 // empty default implementation
55 public void inputChanged(Viewer v
, Object o1
, Object o2
) {
56 // we are not interested since we have no state
61 public void setAncestorError(String errorMessage
) {
62 fAncestorError
= errorMessage
;
65 public String
getAncestorLabel(Object element
) {
66 if (fAncestorError
!= null) {
67 return fAncestorError
;
69 return fCompareConfiguration
.getAncestorLabel(element
);
72 public Image
getAncestorImage(Object element
) {
73 if (fAncestorError
!= null) {
76 return fCompareConfiguration
.getAncestorImage(element
);
79 public Object
getAncestorContent(Object element
) {
80 if (element
instanceof ICompareInput
) {
81 ITypedElement ancestor
= ((ICompareInput
)element
).getAncestor();
87 public boolean showAncestor(Object element
) {
88 if (element
instanceof ICompareInput
) {
89 return true; // fix for #45239: Show ancestor for incoming and outgoing changes
91 // return (((ICompareInput)element).getKind() & Differencer.DIRECTION_MASK) ==
92 // Differencer.CONFLICTING;
98 public void setLeftError(String errorMessage
) {
99 fLeftError
= errorMessage
;
102 public String
getLeftLabel(Object element
) {
103 if (fLeftError
!= null) {
106 return fCompareConfiguration
.getLeftLabel(element
);
109 public Image
getLeftImage(Object element
) {
110 if (fLeftError
!= null) {
113 return fCompareConfiguration
.getLeftImage(element
);
116 public Object
getLeftContent(Object element
) {
117 if (element
instanceof ICompareInput
) {
118 ITypedElement left
= ((ICompareInput
)element
).getLeft();
124 public boolean isLeftEditable(Object element
) {
128 return fCompareConfiguration
.isLeftEditable();
131 public void saveLeftContent(Object element
, byte[] bytes
) {
132 EList
<Match
> matches
= fCompareConfiguration
.getComparison().getMatches();
133 EObject leftEObject
= null;
134 for (Match match
: matches
) {
135 leftEObject
= match
.getLeft();
136 if (leftEObject
!= null) {
140 if (leftEObject
!= null) {
141 Resource eResource
= leftEObject
.eResource();
142 if (eResource
!= null) {
143 ResourceSet resourceSet
= eResource
.getResourceSet();
144 if (resourceSet
!= null) {
145 saveAllResources(resourceSet
, ImmutableMap
.of(Resource
.OPTION_SAVE_ONLY_IF_CHANGED
,
146 Resource
.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER
));
148 saveResource(eResource
, ImmutableMap
.of(Resource
.OPTION_SAVE_ONLY_IF_CHANGED
,
149 Resource
.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER
));
157 public void setRightError(String errorMessage
) {
158 fRightError
= errorMessage
;
161 public String
getRightLabel(Object element
) {
162 if (fRightError
!= null) {
165 return fCompareConfiguration
.getRightLabel(element
);
168 public Image
getRightImage(Object element
) {
169 if (fRightError
!= null) {
172 return fCompareConfiguration
.getRightImage(element
);
175 public Object
getRightContent(Object element
) {
176 if (element
instanceof ICompareInput
) {
177 ITypedElement right
= ((ICompareInput
)element
).getRight();
183 public boolean isRightEditable(Object element
) {
187 return fCompareConfiguration
.isRightEditable();
190 public void saveRightContent(Object element
, byte[] bytes
) {
191 EList
<Match
> matches
= fCompareConfiguration
.getComparison().getMatches();
192 EObject rightEObject
= null;
193 for (Match match
: matches
) {
194 rightEObject
= match
.getRight();
195 if (rightEObject
!= null) {
199 if (rightEObject
!= null) {
200 Resource eResource
= rightEObject
.eResource();
201 if (eResource
!= null) {
202 ResourceSet resourceSet
= eResource
.getResourceSet();
203 if (resourceSet
!= null) {
204 saveAllResources(resourceSet
, ImmutableMap
.of(Resource
.OPTION_SAVE_ONLY_IF_CHANGED
,
205 Resource
.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER
));
207 saveResource(eResource
, ImmutableMap
.of(Resource
.OPTION_SAVE_ONLY_IF_CHANGED
,
208 Resource
.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER
));