From 4d62508f9bc1f92803a1d8b327c201e9d62e1b58 Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Mon, 19 Aug 2013 15:57:30 +0530 Subject: [PATCH] improving old code in ErrorBar/Error Marker --- .../mode/experimental/ErrorBar.java | 178 ++++++------------ .../experimental/ErrorCheckerService.java | 8 +- .../mode/experimental/ErrorMarker.java | 31 ++- .../mode/experimental/TextAreaPainter.java | 4 +- 4 files changed, 92 insertions(+), 129 deletions(-) diff --git a/pdex/src/processing/mode/experimental/ErrorBar.java b/pdex/src/processing/mode/experimental/ErrorBar.java index 24d552ae5..d6b64f9bf 100755 --- a/pdex/src/processing/mode/experimental/ErrorBar.java +++ b/pdex/src/processing/mode/experimental/ErrorBar.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import javax.swing.JPanel; import javax.swing.SwingWorker; +import javax.swing.text.BadLocationException; import processing.app.Base; import processing.app.SketchCode; @@ -109,12 +110,12 @@ public class ErrorBar extends JPanel { g.fillRect(0, 0, getWidth(), getHeight()); for (ErrorMarker emarker : errorPoints) { - if (emarker.type == ErrorMarker.Error) { + if (emarker.getType() == ErrorMarker.Error) { g.setColor(errorColor); } else { g.setColor(warningColor); } - g.fillRect(2, emarker.y, (getWidth() - 3), errorMarkerHeight); + g.fillRect(2, emarker.getY(), (getWidth() - 3), errorMarkerHeight); } } @@ -152,56 +153,46 @@ public class ErrorBar extends JPanel { final int fheight = this.getHeight(); SwingWorker worker = new SwingWorker() { - protected Object doInBackground() throws Exception { - return null; - } + 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); - protected void done() { - int totalLines = 0; - int currentTab = 0; - for (SketchCode sc : editor.getSketch().getCode()) { - if (sc.isExtension("pde")) { - try { - if (editor.getSketch().getCurrentCode().equals(sc)) { - // Adding + 1 to len because \n gets appended - // for each - // sketchcode extracted during processPDECode() - totalLines = Base.countLines(sc.getDocument() - .getText(0, - sc.getDocument().getLength())) + 1; - break; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - currentTab++; - } - // System.out.println("Total lines: " + totalLines); + errorPointsOld.clear(); + for (ErrorMarker marker : errorPoints) { + errorPointsOld.add(marker); + } + errorPoints.clear(); - 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 - for (Problem problem : problems) { - if (problem.tabIndex == currentTab) { - // Ratio of error line to total lines + // Each problem.getSourceLine() will have an extra line added + // because of + // class declaration in the beginning as well as default imports + for (Problem problem : problems) { + if (problem.tabIndex == currentTab) { + // Ratio of error line to total lines float y = (problem.lineNumber - errorCheckerService.defaultImportsOffset) / ((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); - } - } + // 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(); } }; @@ -229,7 +220,7 @@ public class ErrorBar extends JPanel { else { for (int i = 0; i < errorPoints.size(); i++) { - if (errorPoints.get(i).y != errorPointsOld.get(i).y) { + if (errorPoints.get(i).getY() != errorPointsOld.get(i).getY()) { editor.getTextArea().repaint(); // System.out.println("3 Repaint " + // System.currentTimeMillis()); @@ -262,36 +253,14 @@ public class ErrorBar extends JPanel { for (ErrorMarker eMarker : errorPoints) { // -2 and +2 are extra allowance, clicks in the // vicinity of the markers register that way - if (e.getY() >= eMarker.y - 2 - && e.getY() <= eMarker.y + 2 + if (e.getY() >= eMarker.getY() - 2 + && e.getY() <= eMarker.getY() + 2 + errorMarkerHeight) { - int currentTabErrorIndex = errorPoints - .indexOf(eMarker); - // System.out.println("Index: " + - // currentTabErrorIndex); - int currentTab = editor.getSketch() - .getCodeIndex( - editor.getSketch() - .getCurrentCode()); - - int totalErrorIndex = currentTabErrorIndex; - - for (int i = 0; i < errorCheckerService.problemsList - .size(); i++) { - Problem p = errorCheckerService.problemsList - .get(i); - if (p.tabIndex < currentTab) { - totalErrorIndex++; - } - if (p.tabIndex == currentTab) { - break; - } - } errorCheckerService - .scrollToErrorLine(totalErrorIndex); + .scrollToErrorLine(eMarker.getProblem()); + return; } } - } }; @@ -311,7 +280,7 @@ public class ErrorBar extends JPanel { @SuppressWarnings("rawtypes") @Override - public void mouseMoved(final MouseEvent e) { + public void mouseMoved(final MouseEvent evt) { // System.out.println(e); SwingWorker worker = new SwingWorker() { @@ -319,51 +288,22 @@ public class ErrorBar extends JPanel { return null; } - protected void done() { - - for (ErrorMarker eMarker : errorPoints) { - if (e.getY() >= eMarker.y - 2 - && e.getY() <= eMarker.y + 2 - + errorMarkerHeight) { - // System.out.println("Index: " + - // errorPoints.indexOf(y)); - int currentTab = editor.getSketch() - .getCodeIndex( - editor.getSketch() - .getCurrentCode()); - int currentTabErrorCount = 0; - - for (int i = 0; i < errorPoints.size(); i++) { - Problem p = errorPoints.get(i).problem; - if (p.tabIndex == currentTab) { - if (currentTabErrorCount == errorPoints - .indexOf(eMarker)) { - // System.out.println("Roger that."); - String msg = (p.isError() ? "Error: " - : "Warning: ") - + p.message; - setToolTipText(msg); - setCursor(Cursor - .getPredefinedCursor(Cursor.HAND_CURSOR)); - return; - } else { - currentTabErrorCount++; - // System.out.println("Still looking.."); - } - } - - } - } - // Reset cursor and tooltip - else { - setToolTipText(""); - setCursor(Cursor - .getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - } - } - - } + protected void done() { + for (ErrorMarker eMarker : errorPoints) { + if (evt.getY() >= eMarker.getY() - 2 + && evt.getY() <= eMarker.getY() + 2 + errorMarkerHeight) { + Problem p = eMarker.getProblem(); + String msg = (p.isError() ? "Error: " : "Warning: ") + + p.message; + setToolTipText(msg); + setCursor(Cursor + .getPredefinedCursor(Cursor.HAND_CURSOR)); + break; + } + } + } }; + try { worker.execute(); } catch (Exception exp) { diff --git a/pdex/src/processing/mode/experimental/ErrorCheckerService.java b/pdex/src/processing/mode/experimental/ErrorCheckerService.java index f25a08854..da498bdf5 100644 --- a/pdex/src/processing/mode/experimental/ErrorCheckerService.java +++ b/pdex/src/processing/mode/experimental/ErrorCheckerService.java @@ -787,15 +787,15 @@ public class ErrorCheckerService implements Runnable{ // editor.statusNotice("Position: " + // editor.getTextArea().getCaretLine()); for (ErrorMarker emarker : editor.errorBar.errorPoints) { - if (emarker.problem.lineNumber == editor.getTextArea() + if (emarker.getProblem().lineNumber == editor.getTextArea() .getCaretLine() + 1) { - if (emarker.type == ErrorMarker.Warning) { - editor.statusNotice(emarker.problem.message); + if (emarker.getType() == ErrorMarker.Warning) { + editor.statusNotice(emarker.getProblem().message); //+ " : " + errorMsgSimplifier.getIDName(emarker.problem.getIProblem().getID())); //TODO: this is temporary } else { - editor.statusError(emarker.problem.message); + editor.statusError(emarker.getProblem().message); //+ " : " + errorMsgSimplifier.getIDName(emarker.problem.getIProblem().getID())); } return; diff --git a/pdex/src/processing/mode/experimental/ErrorMarker.java b/pdex/src/processing/mode/experimental/ErrorMarker.java index 0a10cff4b..a3eab8ceb 100755 --- a/pdex/src/processing/mode/experimental/ErrorMarker.java +++ b/pdex/src/processing/mode/experimental/ErrorMarker.java @@ -6,14 +6,14 @@ package processing.mode.experimental; * */ public class ErrorMarker { - /** + /** * y co-ordinate of the marker */ - public int y; + private int y; /** * Type of marker: Error or Warning? */ - public int type = -1; + private int type = -1; /** * Error Type constant */ @@ -26,11 +26,34 @@ package processing.mode.experimental; * Problem that the error marker represents * @see Problem */ - public Problem problem; + private Problem problem; public ErrorMarker(Problem problem, int y, int type) { this.problem = problem; this.y = y; this.type = type; } + + /** + * y co-ordinate of the marker + */ + public int getY() { + return y; + } + + /** + * Type of marker: ErrorMarker.Error or ErrorMarker.Warning? + */ + public int getType() { + return type; + } + + /** + * Problem that the error marker represents + * @see Problem + */ + public Problem getProblem() { + return problem; + } + } \ No newline at end of file diff --git a/pdex/src/processing/mode/experimental/TextAreaPainter.java b/pdex/src/processing/mode/experimental/TextAreaPainter.java index 89170edbe..b5d174038 100644 --- a/pdex/src/processing/mode/experimental/TextAreaPainter.java +++ b/pdex/src/processing/mode/experimental/TextAreaPainter.java @@ -311,9 +311,9 @@ public class TextAreaPainter extends processing.app.syntax.TextAreaPainter { // Check if current line contains an error. If it does, find if it's an // error or warning for (ErrorMarker emarker : errorCheckerService.getEditor().errorBar.errorPoints) { - if (emarker.problem.lineNumber == line + 1) { + if (emarker.getProblem().lineNumber == line + 1) { notFound = false; - if (emarker.type == ErrorMarker.Warning) { + if (emarker.getType() == ErrorMarker.Warning) { isWarning = true; } break;