Skip to main content
summaryrefslogtreecommitdiffstats
blob: edb68b2d978d4cdd128f29a3f16fdeebab9d8422 (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
/*******************************************************************************
 * Copyright (c) 2009, 2015 Atlassian 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:
 *     Atlassian - initial API and implementation
 *     Tasktop Technologies - improvements
 *     Sebastien Dubois (Ericsson) - Improvements for bug 400266
 *     Guy Perron (Ericsson) - Bug 422673 Insert annotation navigation
 ******************************************************************************/

package org.eclipse.mylyn.internal.reviews.ui.compare;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareViewerPane;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.compare.structuremergeviewer.StructureDiffViewer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.reviews.ui.Messages;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsImages;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsUiPlugin;
import org.eclipse.mylyn.reviews.ui.ReviewBehavior;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

/**
 * @author Steffen Pingel
 * @author Sebastien Dubois
 * @author Miles Parker
 * @author Guy Perron
 * @author Jacques Bouthillier
 */
public abstract class ReviewItemCompareEditorInput extends CompareEditorInput {

	private static final String NAVIGATION_GROUP = "navigation"; //$NON-NLS-1$

	private static final String ID_NEXT_COMMENT = ReviewsUiPlugin.PLUGIN_ID + "navigate.comment.next"; //$NON-NLS-1$

	private static final String ID_PREVIOUS_COMMENT = ReviewsUiPlugin.PLUGIN_ID + "navigate.comment.previous"; //$NON-NLS-1$

	final ReviewBehavior behavior;

	public ReviewItemCompareEditorInput(CompareConfiguration configuration, ReviewBehavior behavior) {
		super(configuration);
		this.behavior = behavior;
	}

	@Override
	public Viewer findStructureViewer(Viewer oldViewer, ICompareInput input, Composite parent) {
		Viewer structureViewer = super.findStructureViewer(oldViewer, input, parent);
		if (structureViewer instanceof StructureDiffViewer) {
			StructureDiffViewer diffViewer = (StructureDiffViewer) structureViewer;
			diffViewer.setLabelProvider(new LabelProvider() {

				@Override
				public String getText(Object element) {
					if (element instanceof ITypedElement) {
						return ((ITypedElement) element).getName();
					}
					return "<no name>"; //$NON-NLS-1$
				}

				@Override
				public Image getImage(Object element) {
					if (element instanceof IDiffElement) {
						IDiffElement input = (IDiffElement) element;
						int kind = input.getKind();
						//We need to swap additions and deletions as work-around. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=410534
						if (kind == Differencer.ADDITION) {
							kind = Differencer.DELETION;
						} else if (kind == Differencer.DELETION) {
							kind = Differencer.ADDITION;
						}
						return getCompareConfiguration().getImage(input.getImage(), kind);
					}
					return null;
				}
			});
		}
		return structureViewer;
	}

	@Override
	public Viewer findContentViewer(Viewer oldViewer, ICompareInput input, Composite parent) {
		Viewer contentViewer = super.findContentViewer(oldViewer, input, parent);
		if (input instanceof FileItemNode && ((FileItemNode) input).getFileItem() != null) {
			ReviewCompareAnnotationSupport support = ReviewCompareAnnotationSupport.getAnnotationSupport(contentViewer);
			support.setReviewItem(((FileItemNode) input).getFileItem(), behavior);
			initializeGotoCommentHandlers(parent, support);
		}
		return contentViewer;
	}

	private void initializeGotoCommentHandlers(Composite parent, final ReviewCompareAnnotationSupport support) {
		ToolBarManager tbm = CompareViewerPane.getToolBarManager(parent);
		if (tbm != null) {
			if (tbm.find(NAVIGATION_GROUP) != null) {
				if (tbm.find(ID_NEXT_COMMENT) == null) {
					Action goToNextAction = new Action(Messages.Reviews_NextComment, ReviewsImages.NEXT_COMMENT) {
						@Override
						public void run() {
							support.gotoAnnotation(Direction.FORWARDS);
						}
					};
					goToNextAction.setId(ID_NEXT_COMMENT);
					goToNextAction.setToolTipText(Messages.Reviews_NextComment_Tooltip);
					tbm.appendToGroup(NAVIGATION_GROUP, goToNextAction);
				}

				if (tbm.find(ID_PREVIOUS_COMMENT) == null) {
					Action goToPreviousAction = new Action(Messages.Reviews_PreviousComment,
							ReviewsImages.PREVIOUS_COMMENT) {
						@Override
						public void run() {
							support.gotoAnnotation(Direction.BACKWARDS);
						}
					};
					goToPreviousAction.setId(ID_PREVIOUS_COMMENT);
					goToPreviousAction.setToolTipText(Messages.Reviews_PreviousComment_Tooltip);
					tbm.appendToGroup(NAVIGATION_GROUP, goToPreviousAction);
				}
			} else {// bug 430151
				StatusHandler.log(new Status(IStatus.ERROR, ReviewsUiPlugin.PLUGIN_ID,
						"Could not create comment navigation buttons", new Exception())); //$NON-NLS-1$
			}
			tbm.update(true);
		}
	}

	public String getItemTaskId() {
		return behavior.getTask().getTaskId();
	}
}

Back to the top