Skip to main content
summaryrefslogtreecommitdiffstats
blob: e0e33b62eb59c31e0d31924b6bfe40503206c1bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
 * Copyright (c) 2006, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal.patch;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.eclipse.compare.IContentChangeListener;
import org.eclipse.compare.IContentChangeNotifier;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.ContentChangeNotifier;
import org.eclipse.compare.internal.core.patch.FileDiff;
import org.eclipse.compare.internal.core.patch.HunkResult;
import org.eclipse.compare.patch.PatchConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

public class UnmatchedHunkTypedElement extends HunkTypedElement implements IContentChangeNotifier, IEditableContent {

	private ContentChangeNotifier changeNotifier;
	
	public UnmatchedHunkTypedElement(HunkResult result) {
		// An unmatched hunk element is always used for the before state and is full context
		super(result, false, true);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IContentChangeNotifier#addContentChangeListener(org.eclipse.compare.IContentChangeListener)
	 */
	public synchronized void addContentChangeListener(IContentChangeListener listener) {
		if (changeNotifier == null)
			changeNotifier = new ContentChangeNotifier(this);
		changeNotifier.addContentChangeListener(listener);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IContentChangeNotifier#removeContentChangeListener(org.eclipse.compare.IContentChangeListener)
	 */
	public synchronized void removeContentChangeListener(IContentChangeListener listener) {
		if (changeNotifier != null)
			changeNotifier.removeContentChangeListener(listener);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IEditableContent#isEditable()
	 */
	public boolean isEditable() {
		IFile file = ((WorkspaceFileDiffResult)getHunkResult().getDiffResult()).getTargetFile();
		return file != null && file.isAccessible();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IEditableContent#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement)
	 */
	public ITypedElement replace(ITypedElement dest, ITypedElement src) {
		// Not supported
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.IEditableContent#setContent(byte[])
	 */
	public void setContent(byte[] newContent) {
		getPatcher().setManuallyMerged(getHunkResult().getHunk(), true);
		getPatcher().cacheContents(getDiff(), newContent);
		if (changeNotifier != null)
			changeNotifier.fireContentChanged();
	}

	private FileDiff getDiff() {
		return getHunkResult().getDiffResult().getDiff();
	}

	private Patcher getPatcher() {
		return Patcher.getPatcher(getConfiguration());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.compare.internal.patch.HunkTypedElement#getContents()
	 */
	public InputStream getContents() throws CoreException {
		// If there are cached contents, use them
		if (getPatcher().hasCachedContents(getDiff()))
			return new ByteArrayInputStream(getPatcher().getCachedContents(getDiff()));
		// Otherwise return the after state of the diff result
		List lines = getHunkResult().getDiffResult().getAfterLines();
		String content = LineReader.createString(getHunkResult().getDiffResult().isPreserveLineDelimeters(), lines);
		byte[] bytes = null;
		if (getCharset() != null)
			try {
				bytes = content.getBytes(getCharset());
			} catch (UnsupportedEncodingException e) {
				CompareUIPlugin.log(e);
			}
		if (bytes == null)
			bytes = content.getBytes();
		return new ByteArrayInputStream(bytes);
	}

	private PatchConfiguration getConfiguration() {
		return getHunkResult().getDiffResult().getConfiguration();
	}
}

Back to the top