Fix to support EOLs different from the current System's.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6586 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Ryan Gustafson committed 2008-10-10 02:14:14 +00:00
1 parent 178a0a10e5
commit 354519f879
1 file changed
+10 -13
@@ -1,25 +1,22 @@
package net.sourceforge.pmd.util.designer;
import java.util.StringTokenizer;
import javax.swing.JTextPane;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.util.LineGetter;
public class CodeEditorTextPane extends JTextPane implements LineGetter {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private String[] getLines() {
// Support files with line separators from various platforms
return getText().split("\r\n|\r|\n");
}
public String getLine(int number) {
int count = 1;
for (StringTokenizer st = new StringTokenizer(getText(), "\n"); st.hasMoreTokens();) {
String tok = st.nextToken();
if (count == number) {
return tok;
}
count++;
}
String[] lines= getLines();
if (number < lines.length) {
return lines[number];
}
throw new RuntimeException("Line number " + number + " not found");
}
@@ -46,7 +43,7 @@ public class CodeEditorTextPane extends JTextPane implements LineGetter {
}
public void select(Node node) {
String[] lines = getText().split(LINE_SEPARATOR);
String[] lines = getLines();
if (node.getBeginLine() >= 0) {
setSelectionStart(getPosition(lines, node.getBeginLine(), node.getBeginColumn()));
setSelectionEnd(getPosition(lines, node.getEndLine(), node.getEndColumn()) + 1);