KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > ErrorCaretListener


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32 END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
37 import edu.rice.cs.drjava.model.compiler.CompilerError;
38 import edu.rice.cs.drjava.model.compiler.CompilerErrorModel;
39 import edu.rice.cs.util.swing.Utilities;
40
41 import java.awt.EventQueue JavaDoc;
42
43 import javax.swing.event.CaretEvent JavaDoc;
44 import javax.swing.event.CaretListener JavaDoc;
45 import javax.swing.text.Position JavaDoc;
46
47 /** Listens to the caret in the associated DefinitionsPane and highlights the text containing CompilerErrors.
48  * @version $Id: ErrorCaretListener.java 4031 2006-11-15 22:09:06Z rcartwright $
49  */

50 public class ErrorCaretListener implements CaretListener JavaDoc {
51   private final OpenDefinitionsDocument _openDoc;
52   private final DefinitionsPane _definitionsPane;
53   protected final MainFrame _frame;
54
55   /** Constructs a new caret listener to highlight errors. */
56   public ErrorCaretListener(OpenDefinitionsDocument doc, DefinitionsPane defPane, MainFrame frame) {
57     _openDoc = doc;
58     _definitionsPane = defPane;
59     _frame = frame;
60   }
61
62   /** Gets the OpenDefinitionsDocument corresponding to this listener. */
63   public OpenDefinitionsDocument getOpenDefDoc() { return _openDoc; }
64
65   /** After each update to the caret, determine if changes in highlighting need to be made. Highlights the line
66    * if the compiler output tab is showing. Only runs in the event thread.
67    */

68   public void caretUpdate(final CaretEvent JavaDoc evt) {
69     // Now we can assume at least one error.
70
updateHighlight(evt.getDot()); // invokeLater unnecessary here; this method runs in the event thread!
71
}
72
73   /** Update the highlight appropriately. */
74   public void updateHighlight(final int curPos) {
75     Utilities.invokeLater(new Runnable JavaDoc() { public void run() {
76       ErrorPanel panel = _frame.getSelectedErrorPanel();
77       if (panel == null) {
78         // no error panel is currently selected
79
return;
80       }
81       CompilerErrorModel model = panel.getErrorModel();
82       
83       if (!model.hasErrorsWithPositions(_openDoc)) return;
84       
85 // Utilities.showDebug("ErrorCaretListener.updateHighlight invoked");
86

87       CompilerError error = model.getErrorAtOffset(_openDoc, curPos);
88       
89       ErrorPanel.ErrorListPane errorListPane = panel.getErrorListPane();
90       // if no error is on this line, select the (none) item
91
if (error == null) errorListPane.selectNothing();
92       else {
93         if (errorListPane.shouldShowHighlightsInSource()) {
94           // No need to move the caret since it's already here!
95
_highlightErrorInSource(model.getPosition(error));
96         }
97         
98         // Select item wants the CompilerError
99
errorListPane.selectItem(error);
100       }
101     }
102     });
103   }
104   
105   /** Hides the error highlight in the document. */
106   public void removeHighlight() {
107     Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _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   private void _highlightErrorInSource(Position JavaDoc pos) {
114     assert EventQueue.isDispatchThread();
115     if (pos == null) return;
116     int errPos = pos.getOffset();
117     
118     String JavaDoc 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
int prevNewline = text.lastIndexOf('\n', errPos - 1);
125     if (prevNewline == -1) prevNewline = 0;
126     
127     int nextNewline = text.indexOf('\n', errPos);
128     if (nextNewline == -1) nextNewline = text.length();
129     
130     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
if (prevNewline>0) prevNewline++;
135     
136     if (prevNewline <= nextNewline) {
137       _definitionsPane.addErrorHighlight(prevNewline, nextNewline);
138     }
139   }
140 }
141
142
Popular Tags