View Javadoc

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