forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorBar.java
More file actions
316 lines (271 loc) · 9.3 KB
/
ErrorBar.java
File metadata and controls
316 lines (271 loc) · 9.3 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.mode.java;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.text.BadLocationException;
import processing.app.Base;
import processing.app.SketchCode;
import processing.mode.java.pdex.ErrorCheckerService;
import processing.mode.java.pdex.ErrorMarker;
import processing.mode.java.pdex.Problem;
import processing.app.Language;
/**
* The bar on the left of the text area which displays all errors as rectangles. <br>
* <br>
* All errors and warnings of a sketch are drawn on the bar, clicking on one,
* scrolls to the tab and location. Error messages displayed on hover. Markers
* are not in sync with the error line. Similar to eclipse's right error bar
* which displays the overall errors in a document
*
* @author Manindra Moharana <[email protected]>
*
*/
public class ErrorBar extends JPanel {
/**
* Preferred height of the component
*/
protected int preferredHeight;
/**
* Preferred height of the component
*/
protected int preferredWidth = 12;
/**
* Height of marker
*/
public static final int errorMarkerHeight = 4;
/**
* Color of Error Marker
*/
public Color errorColor; // = new Color(0xED2630);
/**
* Color of Warning Marker
*/
public Color warningColor; // = new Color(0xFFC30E);
/**
* Background color of the component
*/
public Color backgroundColor; // = new Color(0x2C343D);
/**
* JavaEditor instance
*/
protected JavaEditor editor;
/**
* ErrorCheckerService instance
*/
protected ErrorCheckerService errorCheckerService;
/**
* Stores error markers displayed PER TAB along the error bar.
*/
protected List<ErrorMarker> errorPoints = new ArrayList<ErrorMarker>();
/**
* Stores previous list of error markers.
*/
protected ArrayList<ErrorMarker> errorPointsOld = new ArrayList<ErrorMarker>();
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
for (ErrorMarker emarker : errorPoints) {
if (emarker.getType() == ErrorMarker.Error) {
g.setColor(errorColor);
} else {
g.setColor(warningColor);
}
g.fillRect(2, emarker.getY(), (getWidth() - 3), errorMarkerHeight);
}
}
public Dimension getPreferredSize() {
return new Dimension(preferredWidth, preferredHeight);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public ErrorBar(JavaEditor editor, int height, JavaMode mode) {
this.editor = editor;
this.preferredHeight = height;
this.errorCheckerService = editor.errorCheckerService;
errorColor = mode.getColor("errorbar.errorcolor"); //, errorColor);
warningColor = mode.getColor("errorbar.warningcolor"); //, warningColor);
//backgroundColor = mode.getColor("errorbar.backgroundcolor"); //, backgroundColor);
backgroundColor = mode.getColor("gutter.bgcolor");
addListeners();
}
/**
* Update error markers in the error bar.
*
* @param problems
* - List of problems.
*/
synchronized public void updateErrorPoints(final List<Problem> problems) {
// NOTE TO SELF: ErrorMarkers are calculated for the present tab only
// Error Marker index in the arraylist is LOCALIZED for current tab.
// Also, need to do the update in the UI thread via SwingWorker to prevent
// concurrency issues.
final int fheight = this.getHeight();
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
SketchCode sc = editor.getSketch().getCurrentCode();
int totalLines = 0, currentTab = editor.getSketch()
.getCurrentCodeIndex();
try {
totalLines = Base.countLines(sc.getDocument()
.getText(0, sc.getDocument().getLength())) + 1;
} catch (BadLocationException e) {
e.printStackTrace();
}
// System.out.println("Total lines: " + totalLines);
synchronized (errorPoints) {
errorPointsOld.clear();
for (ErrorMarker marker : errorPoints) {
errorPointsOld.add(marker);
}
errorPoints.clear();
// Each problem.getSourceLine() will have an extra line added
// because of
// class declaration in the beginning as well as default imports
synchronized (problems) {
for (Problem problem : problems) {
if (problem.getTabIndex() == currentTab) {
// Ratio of error line to total lines
float y = (problem.getLineNumber() + 1)
/ ((float) totalLines);
// Ratio multiplied by height of the error bar
y *= fheight - 15; // -15 is just a vertical offset
errorPoints
.add(new ErrorMarker(problem, (int) y,
problem.isError() ? ErrorMarker.Error
: ErrorMarker.Warning));
// System.out.println("Y: " + y);
}
}
}
}
return null;
}
protected void done() {
repaint();
}
};
try {
worker.execute(); // I eat concurrency bugs for breakfast.
} catch (Exception exp) {
System.out.println("Errorbar update markers is slacking."
+ exp.getMessage());
// e.printStackTrace();
}
}
/**
* Check if new errors have popped up in the sketch since the last check
*
* @return true - if errors have changed
*/
public boolean errorPointsChanged() {
if (errorPointsOld.size() != errorPoints.size()) {
editor.getTextArea().repaint();
// System.out.println("2 Repaint " + System.currentTimeMillis());
return true;
}
else {
for (int i = 0; i < errorPoints.size(); i++) {
if (errorPoints.get(i).getY() != errorPointsOld.get(i).getY()) {
editor.getTextArea().repaint();
// System.out.println("3 Repaint " +
// System.currentTimeMillis());
return true;
}
}
}
return false;
}
/**
* Add various mouse listeners.
*/
protected void addListeners() {
addMouseListener(new MouseAdapter() {
// Find out which error/warning the user has clicked
// and then scroll to that
@Override
public void mouseClicked(final MouseEvent e) {
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
for (ErrorMarker eMarker : errorPoints) {
// -2 and +2 are extra allowance, clicks in the
// vicinity of the markers register that way
if (e.getY() >= eMarker.getY() - 2
&& e.getY() <= eMarker.getY() + 2 + errorMarkerHeight) {
errorCheckerService.scrollToErrorLine(eMarker.getProblem());
return null;
}
}
return null;
}
};
try {
worker.execute();
} catch (Exception exp) {
System.out.println("Errorbar mouseClicked is slacking."
+ exp.getMessage());
// e.printStackTrace();
}
}
});
// Tooltip on hover
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(final MouseEvent evt) {
SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
for (ErrorMarker eMarker : errorPoints) {
if (evt.getY() >= eMarker.getY() - 2 &&
evt.getY() <= eMarker.getY() + 2 + errorMarkerHeight) {
Problem p = eMarker.getProblem();
String msg = ((p.isError()
? Language.text("editor.status.error")
: Language.text("editor.status.warning")) + ": "
+ p.getMessage());
setToolTipText(msg);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
break;
}
}
return null;
}
};
try {
worker.execute();
} catch (Exception exp) {
System.out
.println("Errorbar mousemoved Worker is slacking."
+ exp.getMessage());
// e.printStackTrace();
}
}
});
}
}