Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0a7c676d6234628cf4fc2fee5dea21aa8e8e303b (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
116
117
118
119
120
/*******************************************************************************
 * Copyright (c) 2013, Ericsson AB and others.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 *     Sebastien Dubois (Ericsson) - Adapted to use with Mylyn Reviews
 *******************************************************************************/
package org.eclipse.mylyn.internal.reviews.ui.compare;

import java.io.InputStream;

import org.apache.commons.io.FilenameUtils;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.SharedDocumentAdapter;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.graphics.Image;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * An {@link ITypedElement} wrapper for {@link IFileRevision} for use with Mylyn Reviews
 * 
 * @author Sebastien Dubois
 */
public class FileRevisionTypedElement implements IAdaptable, IStreamContentAccessor, ITypedElement {

	private final IFileRevision fileRevision;

	private final IProgressMonitor runningMonitor;

	private ISharedDocumentAdapter sharedDocumentAdapter;

	public FileRevisionTypedElement(IFileRevision fileRevision, IProgressMonitor monitor) {
		this.fileRevision = fileRevision;
		this.runningMonitor = monitor;
	}

	private IFileRevision getFileRevision() {
		return fileRevision;
	}

	public Object getAdapter(@SuppressWarnings("rawtypes")
	Class adapter) {
		if (ISharedDocumentAdapter.class.equals(adapter)) {
			synchronized (this) {
				if (sharedDocumentAdapter == null) {
					sharedDocumentAdapter = new SharedDocumentAdapter() {
						@Override
						public IEditorInput getDocumentKey(Object element) {
							return FileRevisionTypedElement.this.getDocumentKey(element);
						}

						public void flushDocument(IDocumentProvider provider, IEditorInput documentKey,
								IDocument document, boolean overwrite) {
							// The document is read-only
						}
					};
				}
				return sharedDocumentAdapter;
			}
		}
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	public InputStream getContents() throws CoreException {
		return fileRevision.getStorage(runningMonitor).getContents();
	}

	public IEditorInput getDocumentKey(Object element) {
		if (element instanceof FileRevisionTypedElement) {
			if (element.equals(this)) {
				return new FileRevisionEditorInput(fileRevision, runningMonitor);
			}
		}
		return null;
	}

	public String getName() {
		return fileRevision.getName();
	}

	public Image getImage() {
		return CompareUI.getImage(getType());
	}

	public String getType() {
		String extension = FilenameUtils.getExtension(getName());
		return extension != null && extension.length() > 0 ? extension : ITypedElement.TEXT_TYPE;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof FileRevisionTypedElement) {
			final FileRevisionTypedElement otherElement = (FileRevisionTypedElement) obj;
			return otherElement.getFileRevision().equals(fileRevision);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return fileRevision.hashCode();
	}

}

Back to the top