KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > editor > EditorATERenderingView


1 package org.antlr.works.editor;
2
3 import org.antlr.works.ate.ATEPanel;
4 import org.antlr.works.ate.swing.ATERenderingView;
5 import org.antlr.works.debugger.Debugger;
6
7 import javax.swing.text.*;
8 import java.awt.*;
9 /*
10
11 [The "BSD licence"]
12 Copyright (c) 2005-2006 Jean Bovet
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. The name of the author may not be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */

39
40 public class EditorATERenderingView extends ATERenderingView {
41
42     protected Debugger debugger;
43
44     public EditorATERenderingView(Element elem, ATEPanel textEditor, Debugger debugger) {
45         super(elem, textEditor);
46         this.debugger = debugger;
47         displayOp = new EditorDisplayOperation();
48     }
49
50     public class EditorDisplayOperation extends DisplayOperation {
51
52         public int renderTextPortion(Graphics g, int x, int y, int start, int end, int max, Document doc, AttributeSet attribute)
53                 throws BadLocationException
54         {
55             if(debugger == null) {
56                 return super.renderTextPortion(g, x, y, start, end, max, doc, attribute);
57             }
58
59             final int debuggerCursorIndex = debugger.getDebuggerCursorIndex();
60             if(debuggerCursorIndex == -1) {
61                 return super.renderTextPortion(g, x, y, start, end, max, doc, attribute);
62             }
63
64             int length = end - start;
65             if(start + length > max)
66                 length = max - start;
67
68             if(debuggerCursorIndex >= start && debuggerCursorIndex < start+length) {
69                 final Segment text = getLineBuffer();
70                 doc.getText(debuggerCursorIndex, 1, text);
71                 final char c = text.first();
72
73                 if(debuggerCursorIndex == start) {
74                     drawDebuggerCursor(g, x, y, c);
75                     return super.renderTextPortion(g, x, y, start, end, max, doc, attribute);
76                 } else {
77                     x = super.renderTextPortion(g, x, y, start, debuggerCursorIndex-1, max, doc, attribute);
78                     drawDebuggerCursor(g, x, y, c);
79                     return super.renderTextPortion(g, x, y, debuggerCursorIndex-1, end, max, doc, attribute);
80                 }
81             } else {
82                 return super.renderTextPortion(g, x, y, start, end, max, doc, attribute);
83             }
84         }
85
86         private void drawDebuggerCursor(Graphics g, int x, int y, char c) {
87             saveColor(g);
88             g.setColor(Color.red);
89             final int fontHeight = metrics.getHeight();
90             g.fillRect(x, y-fontHeight+metrics.getDescent(), metrics.charWidth(c), fontHeight);
91             restore(g);
92         }
93
94     }
95 }
96
Popular Tags