From ffc3f3853c522f98a10c8ea0f18b91f8b933215f Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Tue, 8 Sep 2015 17:14:38 -0400 Subject: [PATCH] Recalculate positions in error marker column on resize Fixes #3619 --- .../processing/mode/java/MarkerColumn.java | 46 ++++++++++++++----- .../processing/mode/java/pdex/LineMarker.java | 19 +++++++- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/java/src/processing/mode/java/MarkerColumn.java b/java/src/processing/mode/java/MarkerColumn.java index c138f887e..b3f343b9f 100644 --- a/java/src/processing/mode/java/MarkerColumn.java +++ b/java/src/processing/mode/java/MarkerColumn.java @@ -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 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; diff --git a/java/src/processing/mode/java/pdex/LineMarker.java b/java/src/processing/mode/java/pdex/LineMarker.java index 053021a84..25dfb8c87 100644 --- a/java/src/processing/mode/java/pdex/LineMarker.java +++ b/java/src/processing/mode/java/pdex/LineMarker.java @@ -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? */