Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 065f75b3d5f1d30e7c6837d1bf927e5998be07ea (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2006, 2010 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.team.internal.ui.actions;

import org.eclipse.compare.*;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.history.provider.FileRevision;
import org.eclipse.team.internal.core.history.LocalFileRevision;
import org.eclipse.team.internal.ui.*;
import org.eclipse.team.internal.ui.history.*;
import org.eclipse.team.ui.history.HistoryPage;
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.ui.*;
import org.eclipse.ui.actions.BaseSelectionListenerAction;

public class CompareRevisionAction extends BaseSelectionListenerAction {
	HistoryPage page;
	IStructuredSelection selection;
	IFileRevision currentFileRevision;

	public CompareRevisionAction(String text, HistoryPage page) {
		super(text);
		this.page = page;
	}

	public CompareRevisionAction(HistoryPage page) {
		this(TeamUIMessages.LocalHistoryPage_CompareAction, page);
	}

	@Override
	public void run() {
		IStructuredSelection structSel = selection;

		if (structSel == null)
			return;

		Object[] objArray = structSel.toArray();

		IFileRevision file1 = null;
		IFileRevision file2 = null;

		switch (structSel.size()){
			case 1:
				file1 = getCurrentFileRevision();
				Object tempRevision = objArray[0];
				if (tempRevision instanceof IFileRevision) {
					file2 = (IFileRevision) tempRevision;
				} else {
					return;
				}
			break;

			case 2:
				Object tempRevision2 = objArray[0];
				Object tempRevision3 = objArray[1];

				if (tempRevision2 instanceof IFileRevision &&
					tempRevision3 instanceof IFileRevision){
					file1 = (IFileRevision) objArray[0];
					file2 = (IFileRevision) objArray[1];
				} else {
					return;
				}
			break;
		}

		if (file1 == null || file2 == null ||
		   !file1.exists() || !file2.exists()){
			MessageDialog.openError(page.getSite().getShell(), TeamUIMessages.OpenRevisionAction_DeletedRevTitle, TeamUIMessages.CompareRevisionAction_DeleteCompareMessage);
			return;
		}

		IResource resource = getResource(file2);
		if (resource != null) {
			IFileRevision temp = file1;
			file1 = file2;
			file2 = temp;
		}
		ITypedElement left;
		resource = getResource(file1);
		if (resource != null) {
			left =  getElementFor(resource);
		} else {
			left = new FileRevisionTypedElement(file1, getLocalEncoding());
		}
		ITypedElement right = new FileRevisionTypedElement(file2, getLocalEncoding());

	    openInCompare(left, right);
	}

	private String getLocalEncoding() {
		IResource resource = getResource(getCurrentFileRevision());
		if (resource instanceof IFile) {
			IFile file = (IFile) resource;
			try {
				return file.getCharset();
			} catch (CoreException e) {
				TeamUIPlugin.log(e);
			}
		}
		return null;
	}

	protected ITypedElement getElementFor(IResource resource) {
		return SaveableCompareEditorInput.createFileElement((IFile)resource);
	}

	private void openInCompare(ITypedElement left, ITypedElement right) {
		CompareEditorInput input = createCompareEditorInput(left, right, page.getSite().getPage());
		IWorkbenchPage workBenchPage = page.getSite().getPage();
		IEditorPart editor = Utils.findReusableCompareEditor(input,
				workBenchPage,
				new Class[] { CompareFileRevisionEditorInput.class });
		if (editor != null) {
			IEditorInput otherInput = editor.getEditorInput();
			if (otherInput.equals(input)) {
				// simply provide focus to editor
				if (OpenStrategy.activateOnOpen())
					workBenchPage.activate(editor);
				else
					workBenchPage.bringToTop(editor);
			} else {
				// if editor is currently not open on that input either re-use
				// existing
				CompareUI.reuseCompareEditor(input, (IReusableEditor) editor);
				if (OpenStrategy.activateOnOpen())
					workBenchPage.activate(editor);
				else
					workBenchPage.bringToTop(editor);
			}
		} else {
			CompareUI.openCompareEditor(input, OpenStrategy.activateOnOpen());
		}
	}

	protected CompareFileRevisionEditorInput createCompareEditorInput(
			ITypedElement left, ITypedElement right, IWorkbenchPage page) {
		return new CompareFileRevisionEditorInput(left,
				right, page);
	}

	private IResource getResource(IFileRevision revision) {
		if (revision instanceof LocalFileRevision) {
			LocalFileRevision local = (LocalFileRevision) revision;
			return local.getFile();
		}
		return null;
	}

	private IFileRevision getCurrentFileRevision() {
		return currentFileRevision;
	}

	public void setCurrentFileRevision(IFileRevision fileRevision){
		this.currentFileRevision = fileRevision;
	}

	/**
	 * DO NOT REMOVE, used in a product.
	 *
	 * @deprecated As of 3.5, replaced by
	 *             {@link Utils#findReusableCompareEditor(CompareEditorInput, IWorkbenchPage, Class[])}
	 */
	@SuppressWarnings("javadoc")
	@Deprecated
	public static IEditorPart findReusableCompareEditor(IWorkbenchPage workbenchPage) {
		return Utils.findReusableCompareEditor(null, workbenchPage,
				new Class[] { CompareFileRevisionEditorInput.class });
	}

	@Override
	protected boolean updateSelection(IStructuredSelection selection) {
		this.selection = selection;
		if (selection.size() == 1){
			Object el = selection.getFirstElement();
			if (el instanceof LocalFileRevision)
				this.setText(TeamUIMessages.CompareRevisionAction_Local);
			else if (el instanceof FileRevision){
				FileRevision tempFileRevision = (FileRevision) el;
				this.setText(NLS.bind(TeamUIMessages.CompareRevisionAction_Revision, new String[]{tempFileRevision.getContentIdentifier()}));
			}
			else
				this.setText(TeamUIMessages.CompareRevisionAction_CompareWithCurrent);
			return shouldShow();
		}
		else if (selection.size() == 2){
			this.setText(TeamUIMessages.CompareRevisionAction_CompareWithOther);
			return shouldShow();
		}

		return false;
	}

	private boolean shouldShow() {
		IStructuredSelection structSel = selection;
		Object[] objArray = structSel.toArray();

		if (objArray.length == 0)
			return false;

		for (int i = 0; i < objArray.length; i++) {

			//Don't bother showing if this a category
			if (objArray[i] instanceof AbstractHistoryCategory)
				return false;

			IFileRevision revision = (IFileRevision) objArray[i];
			//check to see if any of the selected revisions are deleted revisions
			if (revision != null && !revision.exists())
				return false;
		}

		return true;
	}
}

Back to the top