|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ErrorCaretListener.java | 64.3% | 88.9% | 83.3% | 78.6% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.ui; | |
| 38 | ||
| 39 | import edu.rice.cs.drjava.model.DJError; | |
| 40 | import edu.rice.cs.drjava.model.OpenDefinitionsDocument; | |
| 41 | import edu.rice.cs.drjava.model.compiler.CompilerErrorModel; | |
| 42 | import edu.rice.cs.util.swing.Utilities; | |
| 43 | ||
| 44 | import java.awt.EventQueue; | |
| 45 | ||
| 46 | import javax.swing.event.CaretEvent; | |
| 47 | import javax.swing.event.CaretListener; | |
| 48 | import javax.swing.text.Position; | |
| 49 | ||
| 50 | /** Listens to the caret in the associated DefinitionsPane and highlights the text containing CompilerErrors. | |
| 51 | * @version $Id: ErrorCaretListener.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 52 | */ | |
| 53 | public class ErrorCaretListener implements CaretListener { | |
| 54 | private final OpenDefinitionsDocument _openDoc; | |
| 55 | private final DefinitionsPane _definitionsPane; | |
| 56 | protected final MainFrame _frame; | |
| 57 | ||
| 58 | /** Constructs a new caret listener to highlight errors. */ | |
| 59 | 93 | public ErrorCaretListener(OpenDefinitionsDocument doc, DefinitionsPane defPane, MainFrame frame) { |
| 60 | 93 | _openDoc = doc; |
| 61 | 93 | _definitionsPane = defPane; |
| 62 | 93 | _frame = frame; |
| 63 | } | |
| 64 | ||
| 65 | /** Gets the OpenDefinitionsDocument corresponding to this listener. */ | |
| 66 | 0 | public OpenDefinitionsDocument getOpenDefDoc() { return _openDoc; } |
| 67 | ||
| 68 | /** After each update to the caret, determine if changes in highlighting need to be made. Highlights the line | |
| 69 | * if the compiler output tab is showing. Only runs in the event thread. | |
| 70 | */ | |
| 71 | 107 | public void caretUpdate(final CaretEvent evt) { |
| 72 | 107 | assert EventQueue.isDispatchThread(); |
| 73 | 104 | if (_frame.getSelectedCompilerErrorPanel() == null) return; |
| 74 | 3 | updateHighlight(evt.getDot()); |
| 75 | } | |
| 76 | ||
| 77 | /** Update the highlight appropriately. */ | |
| 78 | 69 | public void updateHighlight(final int curPos) { |
| 79 | 69 | assert EventQueue.isDispatchThread(); |
| 80 | 69 | CompilerErrorPanel panel = _frame.getSelectedCompilerErrorPanel(); |
| 81 | 61 | if (panel == null) return; // no error panel is currently selected; redundant! |
| 82 | ||
| 83 | 8 | CompilerErrorModel model = panel.getErrorModel(); |
| 84 | ||
| 85 | 2 | if (! model.hasErrorsWithPositions(_openDoc)) return; |
| 86 | ||
| 87 | // Utilities.showDebug("ErrorCaretListener.updateHighlight invoked"); | |
| 88 | ||
| 89 | 6 | DJError error = model.getErrorAtOffset(_openDoc, curPos); |
| 90 | ||
| 91 | 6 | ErrorPanel.ErrorListPane errorListPane = panel.getErrorListPane(); |
| 92 | // if no error is on this line, select the (none) item | |
| 93 | 2 | if (error == null) errorListPane.selectNothing(); |
| 94 | else { | |
| 95 | 4 | if (errorListPane.shouldShowHighlightsInSource()) { |
| 96 | // No need to move the caret since it's already here! | |
| 97 | 4 | _highlightErrorInSource(model.getPosition(error)); |
| 98 | } | |
| 99 | // Select item wants the DJError | |
| 100 | 4 | errorListPane.selectItem(error); |
| 101 | } | |
| 102 | } | |
| 103 | ||
| 104 | /** Hides the error highlight in the document. */ | |
| 105 | 4 | public void removeHighlight() { |
| 106 | 4 | assert EventQueue.isDispatchThread(); |
| 107 | 4 | _definitionsPane.removeErrorHighlight(); |
| 108 | } | |
| 109 | ||
| 110 | /** Highlights the given error in the source. Only runs in event thread. | |
| 111 | * @param pos the position of the error | |
| 112 | */ | |
| 113 | 4 | private void _highlightErrorInSource(Position pos) { |
| 114 | 4 | assert EventQueue.isDispatchThread(); |
| 115 | 0 | if (pos == null) return; |
| 116 | 4 | int errPos = pos.getOffset(); |
| 117 | ||
| 118 | 4 | String text = _openDoc.getText(); |
| 119 | ||
| 120 | // Look for the previous newline BEFORE this character. Thus start looking | |
| 121 | // on the character one before this character. If this is not the case, | |
| 122 | // if the error is at a newline character, both prev and next newlines | |
| 123 | // will be set to that place, resulting in nothing being highlighted. | |
| 124 | 4 | int prevNewline = text.lastIndexOf('\n', errPos - 1); |
| 125 | 0 | if (prevNewline == -1) prevNewline = 0; |
| 126 | ||
| 127 | 4 | int nextNewline = text.indexOf('\n', errPos); |
| 128 | 0 | if (nextNewline == -1) nextNewline = text.length(); |
| 129 | ||
| 130 | 4 | removeHighlight(); |
| 131 | ||
| 132 | //Add 1 if not the first line of the file, so that the highlight range | |
| 133 | // will match the range chosen for the highlight manager. | |
| 134 | 4 | if (prevNewline > 0) prevNewline++; |
| 135 | ||
| 136 | 4 | if (prevNewline <= nextNewline) { |
| 137 | 4 | _definitionsPane.addErrorHighlight(prevNewline, nextNewline); |
| 138 | } | |
| 139 | } | |
| 140 | } | |
| 141 |
|
||||||||||