Skip to main content
summaryrefslogtreecommitdiffstats
blob: de3cfb8997cab09a18ae1e4e02cd0d7e20bac1a5 (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
/*******************************************************************************
 * Copyright (c) 2015 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.gerrit.ui;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareUI;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.reviews.ui.compare.FileItemCompareEditorInput;
import org.eclipse.mylyn.internal.reviews.ui.compare.ReviewItemSetCompareEditorInput;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.IReviewItemSet;
import org.eclipse.mylyn.reviews.ui.ReviewBehavior;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;

public class GerritCompareUi {

	public static CompareEditorInput getReviewItemSetComparisonEditor(ReviewItemSetCompareEditorInput editorInput,
			IReviewItemSet items, String taskId) {
		return getComparisonEditor(editorInput, getReviewItemSetComparePredicate(items, taskId));
	}

	public static void openCompareEditor(CompareEditorInput input) {
		CompareUI.openCompareEditor(input);
	}

	public static void openFileComparisonEditor(CompareConfiguration configuration, IFileItem item,
			ReviewBehavior behavior) {
		CompareEditorInput editorInput = new FileItemCompareEditorInput(configuration, item, behavior);
		CompareEditorInput newInput = getComparisonEditor(editorInput, getFileComparePredicate(item));
		openCompareEditor(newInput);
	}

	private static CompareEditorInput getComparisonEditor(CompareEditorInput editorInput,
			Predicate<CompareEditorInput> function) {
		IEditorReference[] editorReferences = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow()
				.getActivePage()
				.getEditorReferences();
		return getComparisonEditorInput(editorReferences, editorInput, function);
	}

	static CompareEditorInput getComparisonEditorInput(IEditorReference[] editorReferences,
			CompareEditorInput editorInput, Predicate<CompareEditorInput> predicate) {
		return FluentIterable.from(Lists.newArrayList(editorReferences)).filter(new Predicate<IEditorReference>() {
			public boolean apply(IEditorReference ref) {
				return ref.getId().equals("org.eclipse.compare.CompareEditor"); //$NON-NLS-1$
			}
		}).transform(new Function<IEditorReference, CompareEditorInput>() {
			public CompareEditorInput apply(IEditorReference reference) {
				try {
					return (CompareEditorInput) reference.getEditorInput();
				} catch (PartInitException e) {
					handleError(e);
				}
				return null;
			}
		}).firstMatch(predicate).or(editorInput);
	}

	static Predicate<CompareEditorInput> getFileComparePredicate(final IFileItem item) {
		return new Predicate<CompareEditorInput>() {

			@Override
			public boolean apply(CompareEditorInput existingEditorInput) {
				if (existingEditorInput instanceof FileItemCompareEditorInput) {
					return (((FileItemCompareEditorInput) existingEditorInput).getFileItemId().equals(item.getId()));
				}
				return false;
			}
		};
	}

	static Predicate<CompareEditorInput> getReviewItemSetComparePredicate(final IReviewItemSet itemSet,
			final String taskId) {
		return new Predicate<CompareEditorInput>() {

			@Override
			public boolean apply(CompareEditorInput existingEditorInput) {
				if (existingEditorInput instanceof ReviewItemSetCompareEditorInput) {
					return (((ReviewItemSetCompareEditorInput) existingEditorInput).getName().equals(itemSet.getName()) && taskId.equals(((ReviewItemSetCompareEditorInput) existingEditorInput).getItemTaskId()));
				}
				return false;
			}
		};
	}

	protected static void handleError(PartInitException error) {
		StatusHandler.log(new Status(IStatus.ERROR, GerritUiPlugin.PLUGIN_ID,
				"There was an error restoring the editor input.", error)); //$NON-NLS-1$
	}

}

Back to the top