Skip to main content
summaryrefslogtreecommitdiffstats
blob: cce91e39d33b97f7fa124baf560fe32fc73ff45f (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
/*******************************************************************************
 * Copyright (c) 2009, 2014 Atlassian 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
 *
 *     Atlassian - initial API and implementation
 ******************************************************************************/

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

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.source.LineRange;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.workbench.WorkbenchUtil;
import org.eclipse.mylyn.internal.reviews.ui.ReviewUiUtil;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsUiPlugin;
import org.eclipse.mylyn.internal.reviews.ui.annotations.IReviewCompareSourceViewer;
import org.eclipse.mylyn.internal.reviews.ui.dialogs.AddCommentDialog;
import org.eclipse.mylyn.reviews.core.model.ILineLocation;
import org.eclipse.mylyn.reviews.core.model.ILineRange;
import org.eclipse.mylyn.reviews.core.model.ILocation;
import org.eclipse.mylyn.reviews.core.model.IReviewItem;
import org.eclipse.mylyn.reviews.internal.core.model.ReviewsFactory;
import org.eclipse.mylyn.reviews.ui.ReviewBehavior;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;

/**
 * Action for adding a comment to a line in the active review
 * 
 * @author Shawn Minto
 * @author Miles Parker
 */
public class AddLineCommentToFileAction extends AbstractReviewAction {

	private final IReviewCompareSourceViewer compareSourceViewer;

	private IEditorInput editorInput;

	private LineRange selectedRange;

	public AddLineCommentToFileAction(IReviewCompareSourceViewer compareSourceViewer) {
		super(Messages.AddLineCommentToFileAction_Add_Comment);
		this.compareSourceViewer = compareSourceViewer;
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		super.selectionChanged(action, selection);
		editorInput = getEditorInputFromSelection(selection);
	}

	@Override
	protected boolean updateSelection(IStructuredSelection selection) {
		if (compareSourceViewer != null) {
			selectedRange = compareSourceViewer.getSelection();
			if (selectedRange != null) {
				return !ReviewUiUtil.isAnonymous(getItem());
			}
		}
		return false;
	}

	private LineRange getJavaEditorSelection(IEditorInput editorInput) {
		LineRange lines = null;

		IEditorPart editorPart = getActiveEditor();

		if (editorPart != null && editorInput != null) {
			lines = ReviewUiUtil.getSelectedLineNumberRangeFromEditorInput(editorPart, editorInput);
			if (lines == null) {
				StatusHandler.log(new Status(IStatus.INFO, ReviewsUiPlugin.PLUGIN_ID,
						"Editor is not an ITextEditor or there's no text selection available.")); //$NON-NLS-1$
			}
		}

		return lines;
	}

	@Override
	public String getToolTipText() {
		return Messages.AddLineCommentToFileAction_Add_Comment_tooltip;
	}

	protected LineRange getSelectedRange() {
		//if its the action from the compare editor, get currently selected lines
		if (compareSourceViewer != null) {
			LineRange selectedRange = compareSourceViewer.getSelection();
			int maxNumberOfLines = compareSourceViewer.getAnnotationModel().getDocument().getNumberOfLines();
			if (selectedRange.getStartLine() == maxNumberOfLines) {
				selectedRange = new LineRange(maxNumberOfLines - 1, 1);
			}
			return selectedRange;
		} else {
			return getJavaEditorSelection(getEditorInput());
		}
	}

	protected IEditorInput getEditorInput() {
		return editorInput;
	}

	private ILocation getLocation() {
		LineRange selectedRange = getSelectedRange();
		ILineLocation location = ReviewsFactory.eINSTANCE.createLineLocation();
		ILineRange range = ReviewsFactory.eINSTANCE.createLineRange();
		range.setStart(selectedRange.getStartLine());
		range.setEnd(selectedRange.getStartLine() + selectedRange.getNumberOfLines());
		location.getRanges().add(range);
		return location;
	}

	public void run(IAction action) {
		IReviewItem item = getItem();
		ReviewBehavior reviewBehavior = compareSourceViewer.getAnnotationModel().getBehavior();

		AddCommentDialog dialog = new AddCommentDialog(WorkbenchUtil.getShell(), reviewBehavior, item, getLocation());
		dialog.open();
	}

	public IReviewItem getItem() {
		if (compareSourceViewer != null && compareSourceViewer.getAnnotationModel() != null) {
			return compareSourceViewer.getAnnotationModel().getItem();
		}
		return null;
	}

}

Back to the top