Recalculate positions in error marker column on resize

Fixes #3619
This commit is contained in:
Jakub Valtar
2015-09-08 17:14:38 -04:00
parent e227a77dcd
commit ffc3f3853c
2 changed files with 51 additions and 14 deletions

View File

@@ -89,6 +89,12 @@ public class MarkerColumn extends JPanel {
}
@Override
public void repaint() {
recalculateMarkerPositions();
super.repaint();
}
public void paintComponent(Graphics g) {
g.setColor(backgroundColor);
g.fillRect(0, 0, getWidth(), getHeight());
@@ -117,14 +123,7 @@ public class MarkerColumn extends JPanel {
new SwingWorker() {
protected Object doInBackground() throws Exception {
Sketch sketch = editor.getSketch();
SketchCode code = sketch.getCurrentCode();
int totalLines = 0;
int currentTab = sketch.getCurrentCodeIndex();
try {
totalLines = Util.countLines(code.getDocumentText());
} catch (BadLocationException e) {
e.printStackTrace();
}
errorPoints = new ArrayList<>();
// Each problem.getSourceLine() will have an extra line added because
@@ -132,14 +131,12 @@ public class MarkerColumn extends JPanel {
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 *= getHeight() - 15; // -15 is just a vertical offset
errorPoints.add(new LineMarker(problem, (int) y, problem.isError()));
errorPoints.add(new LineMarker(problem, problem.isError()));
}
}
}
recalculateMarkerPositions();
return null;
}
@@ -195,6 +192,31 @@ public class MarkerColumn extends JPanel {
}
private void recalculateMarkerPositions() {
List<LineMarker> errorPoints = getErrorPoints();
if (errorPoints != null && errorPoints.size() > 0) {
Sketch sketch = editor.getSketch();
SketchCode code = sketch.getCurrentCode();
int totalLines;
try {
totalLines = Util.countLines(code.getDocumentText());
} catch (BadLocationException e) {
e.printStackTrace();
totalLines = 1; // do not divide by zero
}
for (LineMarker m : errorPoints) {
// Ratio of error line to total lines
float y = (m.getLineNumber() + 1) / ((float) totalLines);
// Ratio multiplied by height of the error bar
y *= getHeight() - 15; // -15 is just a vertical offset
m.setY((int) y);
}
}
}
private LineMarker findClosestMarker(final int y) {
LineMarker closest = null;
int closestDist = Integer.MAX_VALUE;

View File

@@ -48,13 +48,20 @@ public class LineMarker {
private Problem problem;
public LineMarker(Problem problem, int y, boolean error) {
public LineMarker(Problem problem, boolean error) {
this.problem = problem;
this.y = y;
this.type = error ? ERROR : WARNING;
}
/**
* set y co-ordinate of the marker
*/
public void setY(int y) {
this.y = y;
}
/**
* y co-ordinate of the marker
*/
@@ -63,6 +70,14 @@ public class LineMarker {
}
/**
* line number of the marker
*/
public int getLineNumber() {
return problem.getLineNumber();
}
/**
* Type of marker: ErrorMarker.Error or ErrorMarker.Warning?
*/