CodeEditorTextPane.java
01 package net.sourceforge.pmd.util.designer;
02 
03 import javax.swing.JTextPane;
04 
05 import net.sourceforge.pmd.lang.ast.Node;
06 import net.sourceforge.pmd.util.LineGetter;
07 
08 public class CodeEditorTextPane extends JTextPane implements LineGetter {
09     
10     private String[] getLines() {
11   // Support files with line separators from various platforms
12         return getText().split("\r\n|\r|\n");
13     }
14 
15     public String getLine(int number) {
16   String[] lines= getLines();
17   if (number < lines.length) {
18       return lines[number];
19   }
20         throw new RuntimeException("Line number " + number + " not found");
21     }
22 
23     private int getPosition(String[] lines, int line, int column) {
24         int pos = 0;
25         for (int count = 0; count < lines.length;) {
26             String tok = lines[count++];
27             if (count == line) {
28                 int linePos = 0;
29                 int i;
30                 for (i = 0; linePos < column; i++) {
31                     linePos++;
32                     if (tok.charAt(i== '\t') {
33                         linePos--;
34                         linePos += (linePos & 07);
35                     }
36                 }
37 
38                 return pos + i - 1;
39             }
40             pos += tok.length() 1;
41         }
42         throw new RuntimeException("Line " + line + " not found");
43     }
44 
45     public void select(Node node) {
46         String[] lines = getLines();
47         if (node.getBeginLine() >= 0) {
48       setSelectionStart(getPosition(lines, node.getBeginLine(), node.getBeginColumn()));
49       setSelectionEnd(getPosition(lines, node.getEndLine(), node.getEndColumn()) 1);
50   }
51         requestFocus();
52     }
53 }