Skip to main content
summaryrefslogtreecommitdiffstats
blob: 97992c520000084340672062f6eca84d77cc93cf (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
/*******************************************************************************
 * Copyright (c) 2009 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
 ******************************************************************************/

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

import java.lang.reflect.Method;

import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.compare.internal.MergeSourceViewer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsUiPlugin;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.ITopic;
import org.eclipse.swt.widgets.Display;

/**
 * Model for annotations in the diff view.
 * 
 * @author Thomas Ehrnhoefer
 */
@SuppressWarnings("restriction")
public class ReviewCompareAnnotationModel {

	static SourceViewer getSourceViewer(MergeSourceViewer sourceViewer) {
		if (SourceViewer.class.isInstance(sourceViewer)) {
			return SourceViewer.class.cast(sourceViewer);
		} else {
			Object returnValue;
			try {
				Method getSourceViewerRefl = MergeSourceViewer.class.getDeclaredMethod("getSourceViewer");
				getSourceViewerRefl.setAccessible(true);
				returnValue = getSourceViewerRefl.invoke(sourceViewer);
				if (returnValue instanceof SourceViewer) {
					return (SourceViewer) returnValue;
				}
			} catch (Exception e) {
				// ignore
			}
		}
		return null;
	}

	final ReviewAnnotationModel leftAnnotationModel;

	private final ReviewAnnotationModel rightAnnotationModel;

	private ReviewCompareInputListener leftViewerListener;

	private ReviewCompareInputListener rightViewerListener;

	private final ITopic commentToFocus;

	private TextMergeViewer fMergeViewer;

	private MergeSourceViewer fRightSourceViewer;

	private MergeSourceViewer fLeftSourceViewer;

	public ReviewCompareAnnotationModel(IFileItem reviewItem, ITopic commentToFocus) {
		this.leftAnnotationModel = new ReviewAnnotationModel(null, null, null, reviewItem.getTarget());
		this.rightAnnotationModel = new ReviewAnnotationModel(null, null, null, reviewItem.getBase());
		this.commentToFocus = commentToFocus;
	}

	public void attachToViewer(final TextMergeViewer viewer, final MergeSourceViewer fLeft,
			final MergeSourceViewer fRight) {
		fMergeViewer = viewer;
		fLeftSourceViewer = fLeft;
		fRightSourceViewer = fRight;

		/*
		 * only create listeners if they are not already existing
		 */
		if (!isListenerFor(leftViewerListener, fLeft, leftAnnotationModel)) {
			leftViewerListener = addTextInputListener(fLeft, leftAnnotationModel);
		} else {
			/*
			 * Using asyncExec here because if the underlying slaveDocument (part of the file that gets displayed when clicking
			 * on a java structure in the compare editor) is changed, but the master document is not, we do not get any event
			 * afterwards that would give us a place to hook our code to override the annotationHover. Since all is done in the
			 * UI thread, using this asyncExec hack works because the unconfigure and configure of the document is finished and
			 * our hover-hack stays.
			 */
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					try {
						// if listeners exist, just make sure the hover hack is in there
						leftViewerListener.forceCustomAnnotationHover();
					} catch (Exception e) {
						StatusHandler.log(new Status(IStatus.ERROR, ReviewsUiPlugin.PLUGIN_ID,
								"Error attaching annotation hover", e));
					}
				}
			});
		}
		if (!isListenerFor(rightViewerListener, fRight, rightAnnotationModel)) {
			rightViewerListener = addTextInputListener(fRight, rightAnnotationModel);
		} else {
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					try {
						// if listeners exist, just make sure the hover hack is in there
						rightViewerListener.forceCustomAnnotationHover();
					} catch (Exception e) {
						StatusHandler.log(new Status(IStatus.ERROR, ReviewsUiPlugin.PLUGIN_ID,
								"Error attaching annotation hover", e));
					}
				}
			});
		}
	}

	private boolean isListenerFor(ReviewCompareInputListener listener, MergeSourceViewer viewer,
			ReviewAnnotationModel annotationModel) {
		if (listener == null) {
			return false;
		}
		return listener.isListenerFor(viewer, annotationModel);
	}

	private ReviewCompareInputListener addTextInputListener(final MergeSourceViewer sourceViewer,
			final ReviewAnnotationModel annotationModel) {
		ReviewCompareInputListener listener = new ReviewCompareInputListener(sourceViewer, annotationModel);
		SourceViewer viewer = getSourceViewer(sourceViewer);
		if (viewer != null) {
			viewer.addTextInputListener(listener);
		}
		return listener;
	}

	public void focusOnComment() {
		focusOnComment(commentToFocus);
	}

	public void focusOnComment(ITopic commentToFocus) {
		// FIXME
//		if (commentToFocus != null) {
//			CrucibleFile leftFile = leftAnnotationModel.getCrucibleFile();
//			VersionedVirtualFile virtualLeft = leftFile.getSelectedFile();
//
//			CrucibleFile rightFile = rightAnnotationModel.getCrucibleFile();
//			VersionedVirtualFile virtualRight = rightFile.getSelectedFile();
//
//			MergeSourceViewer focusViewer = null;
//			Map<String, IntRanges> lineRanges = commentToFocus.getLineRanges();
//			if (lineRanges != null) {
//				IntRanges range;
//				if ((range = lineRanges.get(virtualLeft.getRevision())) != null) {
//					// get the correct listener (new file is left)
//					leftViewerListener.focusOnLines(range);
//					focusViewer = fLeftSourceViewer;
//				} else if ((range = lineRanges.get(virtualRight.getRevision())) != null) {
//					rightViewerListener.focusOnLines(range);
//					focusViewer = fRightSourceViewer;
//				}
//				setActiveViewer(focusViewer);
//			}
//		}
	}

	private void setActiveViewer(MergeSourceViewer focusViewer) {
		try {
			Method setActiveViewer = TextMergeViewer.class.getDeclaredMethod("setActiveViewer",
					MergeSourceViewer.class, boolean.class);
			setActiveViewer.setAccessible(true);
			setActiveViewer.invoke(fMergeViewer, focusViewer, true);
		} catch (Exception e) {
			StatusHandler.log(new Status(IStatus.WARNING, ReviewsUiPlugin.PLUGIN_ID, "Failed to activate viewer", e));
		}
	}

	public void registerContextMenu() {
		rightViewerListener.registerContextMenu();
		leftViewerListener.registerContextMenu();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((leftAnnotationModel == null) ? 0 : leftAnnotationModel.hashCode());
		result = prime * result + ((rightAnnotationModel == null) ? 0 : rightAnnotationModel.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		ReviewCompareAnnotationModel other = (ReviewCompareAnnotationModel) obj;
		if (leftAnnotationModel == null) {
			if (other.leftAnnotationModel != null) {
				return false;
			}
		} else if (!leftAnnotationModel.equals(other.leftAnnotationModel)) {
			return false;
		}
		if (rightAnnotationModel == null) {
			if (other.rightAnnotationModel != null) {
				return false;
			}
		} else if (!rightAnnotationModel.equals(other.rightAnnotationModel)) {
			return false;
		}
		return true;
	}

}

Back to the top