Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9043114f50a8df0c0e19f624e2ee45e1c260c53f (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
/*******************************************************************************
 * Copyright (c) 2012 Ericsson
 * 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:
 *     Miles Parker, Tasktop Technologies - initial API and implementation
 *     Sebastien Dubois (Ericsson) - Improvements for bug 400266
 *******************************************************************************/

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

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareUI;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.gerrit.ui.GerritReviewBehavior;
import org.eclipse.mylyn.internal.reviews.ui.compare.FileItemCompareEditorInput;
import org.eclipse.mylyn.internal.reviews.ui.views.ReviewExplorer;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.IFileVersion;
import org.eclipse.mylyn.reviews.core.model.IReview;
import org.eclipse.mylyn.reviews.core.model.IComment;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorInput;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.navigator.ICommonActionConstants;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;

/**
 * @author Miles Parker
 */
public class OpenCompareEditorProvider extends CommonActionProvider {

	private Action openAction;

	@Override
	public void init(ICommonActionExtensionSite site) {
		if (site.getViewSite() instanceof ICommonViewerWorkbenchSite) {
			final ICommonViewerWorkbenchSite viewSite = (ICommonViewerWorkbenchSite) site.getViewSite();
			final ISelectionProvider selectionProvider = viewSite.getSelectionProvider();
			CommonViewer viewer = (CommonViewer) selectionProvider;
			final ReviewExplorer explorer = (ReviewExplorer) viewer.getCommonNavigator();
			openAction = new Action() {

				@Override
				public void run() {
					IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
					if (selection.size() == 1) {
						IFileItem fileItem = getFileFor(selection.getFirstElement());
						if (fileItem != null) {
							IWorkbenchPart currentPart = explorer.getCurrentPart();
							if (currentPart instanceof TaskEditor) {

								//Here we try to resolve the Git repository in the workspace for this Patch Set.  
								//If so, we will use the appropriate file revision to provide navigability in the Compare Editor.
								//TODO:  Here we need to find a way to get a IUiContext and IReviewItemSet from the fileItem
								//TODO:  Once this is done, we can get use the  OpenFileUiFactory#execute command here
								//IUiContext context...;
								//IReviewItemSet set...context;
								//OpenFileUiFactory openFileFactory = new OpenFileUiFactory(context, set, fileItem);
								//openFileFactory.execute();

								TaskEditor part = (TaskEditor) currentPart;
								TaskEditorInput input = (TaskEditorInput) part.getEditorInput();
								GerritReviewBehavior behavior = new GerritReviewBehavior(input.getTask());
								CompareConfiguration configuration = new CompareConfiguration();
								CompareUI.openCompareEditor(new FileItemCompareEditorInput(configuration, fileItem,
										behavior));
							}
						}
					}
				}

				@Override
				public boolean isEnabled() {
					return true;
				}
			};
		}
	}

	private static IFileItem getFileFor(Object element) {
		//TODO Move to adapter?
		if (element instanceof IComment) {
			return getFileFor(((IComment) element).getItem());
		}
		if (element instanceof IFileVersion) {
			return ((IFileVersion) element).getFile();
		}
		if (element instanceof IFileItem) {
			return (IFileItem) element;
		}
		return null;
	}

	private static IReview getReviewFor(Object element) {
		if (element instanceof IComment) {
			return ((IComment) element).getReview();
		}
		if (element instanceof IFileVersion) {
			return ((IFileVersion) element).getReview();
		}
		if (element instanceof IFileItem) {
			return ((IFileItem) element).getReview();
		}
		return null;
	}

	@Override
	public void fillActionBars(IActionBars actionBars) {
		if (openAction.isEnabled()) {
			actionBars.setGlobalActionHandler(ICommonActionConstants.OPEN, openAction);
		}
	}
}

Back to the top