Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98f4315e24975e192bffd52fc20388cef41ce9ef (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
/**
 *  Copyright (c) 2017 Angelo ZERR.
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Provide CodeMining support with CodeMiningManager - Bug 527720
 */
package org.eclipse.jface.internal.text.codemining;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.core.runtime.IProgressMonitor;

import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.codemining.ICodeMining;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.inlined.LineContentAnnotation;

/**
 * Code Mining annotation in line content.
 *
 * @since 3.13
 */
public class CodeMiningLineContentAnnotation extends LineContentAnnotation implements ICodeMiningAnnotation {

	private static final String SEPARATOR= " | "; //$NON-NLS-1$

	/**
	 * List of resolved minings which contains current resolved minings and last resolved minings
	 * and null if mining is not resolved.
	 */
	private ICodeMining[] fResolvedMinings;

	/**
	 * List of current resolved/unresolved minings
	 */
	private final List<ICodeMining> fMinings;

	/**
	 * List of bounds minings
	 */
	private final List<Rectangle> fBounds;

	/**
	 * The current progress monitor
	 */
	private IProgressMonitor fMonitor;

	/**
	 * Code mining annotation constructor.
	 *
	 * @param position the position
	 * @param viewer the viewer
	 */
	public CodeMiningLineContentAnnotation(Position position, ISourceViewer viewer) {
		super(position, viewer);
		fResolvedMinings= null;
		fMinings= new ArrayList<>();
		fBounds= new ArrayList<>();
	}

	@Override
	public void update(List<ICodeMining> minings, IProgressMonitor monitor) {
		disposeMinings();
		fMonitor= monitor;
		fMinings.addAll(minings);
		if (fResolvedMinings == null || (fResolvedMinings.length != fMinings.size())) {
			// size of resolved minings are different from size of minings to update, initialize it with size of minings to update
			fResolvedMinings= new ICodeMining[fMinings.size()];
		}
	}

	@Override
	public void markDeleted(boolean deleted) {
		super.markDeleted(deleted);
		if (deleted) {
			disposeMinings();
			fResolvedMinings= null;
		}
	}

	private void disposeMinings() {
		fMinings.stream().forEach(ICodeMining::dispose);
		fMinings.clear();
	}

	@Override
	protected int drawAndComputeWidth(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) {
		List<ICodeMining> minings= new ArrayList<>(fMinings);
		int nbDraw= 0;
		int originalX= x;
		int separatorWidth= -1;
		boolean redrawn= false;
		fBounds.clear();
		for (int i= 0; i < minings.size(); i++) {
			ICodeMining mining= minings.get(i);
			if (!mining.isResolved()) {
				// the mining is not resolved.
				if (!redrawn) {
					// redraw the annotation when mining is resolved.
					redraw();
					redrawn= true;
				}
				// try to get the last resolved mining.
				if (fResolvedMinings != null) {
					mining= fResolvedMinings[i];
				}
				if (mining == null) {
					// the last mining was not resolved, don't draw it.
					continue;
				}
			} else {
				// mining is resolved, update the resolved mining list
				fResolvedMinings[i]= mining;
			}
			// draw the mining
			if (nbDraw > 0) {
				initGC(textWidget, color, gc);
				gc.drawText(SEPARATOR, x, y);
				if (separatorWidth == -1) {
					separatorWidth= gc.stringExtent(SEPARATOR).x;
				}
				x+= separatorWidth;
			}
			initGC(textWidget, color, gc);
			Point loc= mining.draw(gc, textWidget, color, x, y);
			fBounds.add(new Rectangle(x, y, loc.x, loc.y));
			x+= loc.x;
			nbDraw++;
		}
		return x - originalX;
	}

	/**
	 * Initialize GC with given color and styled text background color and font.
	 *
	 * @param textWidget the text widget
	 * @param color the color
	 * @param gc the gc to initialize
	 */
	private void initGC(StyledText textWidget, Color color, GC gc) {
		gc.setForeground(color);
		gc.setBackground(textWidget.getBackground());
		gc.setFont(textWidget.getFont());
	}

	@Override
	public void redraw() {
		// redraw codemining annotation is done only if all current minings are resolved.
		List<ICodeMining> minings= new ArrayList<>(fMinings);
		for (ICodeMining mining : minings) {
			if (!mining.isResolved()) {
				// one of mining is not resolved, resolve it and then redraw the annotation.
				mining.resolve(getViewer(), fMonitor).thenRunAsync(() -> {
					this.redraw();
				});
				return;
			}
		}
		// all minings are resolved, redraw the annotation
		super.redraw();
	}

	@Override
	public Consumer<MouseEvent> getAction(MouseEvent e) {
		for (int i= 0; i < fBounds.size(); i++) {
			Rectangle bound= fBounds.get(i);
			if (bound.contains(e.x, e.y)) {
				ICodeMining mining= fMinings.get(i);
				return mining.getAction();
			}
		}
		return null;
	}

	@Override
	public boolean isInVisibleLines() {
		return super.isInVisibleLines();
	}
}

Back to the top