Finding end of line (EOL) in windows platform
Hello Free Java World,
I have a problem with finding end of line in text pane with Windows platform.
I use a caret listener to get position of cursor
Here is my code to count line number in text pane
publicvoid caretUpdate(CaretEvent e){
int lineNum = getLineNum(e.getDot());
statusLine.setLineNum(lineNum);
statusLine.updateStatus();
}
//
protectedint getLineNum(finalint dot){
int pos=0;
int lineNum = 0;
boolean stop =false;
String s = editZone.getText();
String nlf = System.getProperty("line.separator");
debug("nlf long : "+nlf.length());
int lenNlf = nlf.length();
do{
//pos = s.indexOf("\n",pos);
pos = s.indexOf(nlf,pos);//win
if (pos == -1){// NL not found we are at EOT
debug("EOT");
stop =true;
}
elseif (pos < dot){// cursor is on the next lines
pos += lenNlf;// next NL
debug(" next NL : dot - pos "+dot+"-"+pos);
}
elseif (pos > dot){// we are in the line
debug(" in the line : dot - pos "+dot+"-"+pos);
stop=true;
}
elseif (pos == dot){// cursor at EOL
debug(" EOL : dot - pos "+dot+"-"+pos);
stop =true;
}
lineNum++;
debug(" lineNum "+lineNum);
}
while (!stop);
return lineNum;
}
This code works fine with my linux box but under Win EOL's length is 2 characters and the function "indexOf(nlf,pos)" behave strangely. it reports EOL in a wrong way.
For example in this text :
123456
234567
345678
456789
567890
Caret in first line : indexOf(nlf,pos) reports : 6
Caret in second line : indexOf(nlf,pos) reports : 14
Caret in third line : indexOf(nlf,pos) reports : 22
Caret in fourth line : indexOf(nlf,pos) reports : 30
...
I wonder if caret position mecanism ignore EOL's length ?
In this case caret position must be computed before use.
Thanks for your help

