does java have an inteligent JTextPane?
Hello,
Im trying to do something like Inteligent textpane :) but I have several problems with it. Im doing something with DocumentFilters but it is not enough.
How can I :
1. make my textpane listen to 40 sign lines without spliting the words?
(if you have a sentence and the sum of lenghts of words in this sentece gives more then 40, then the ORDINARY textpane would split the last word somethere in the middle - this is not acceptable.It should make a new line to the beggining of the LAST word and If the 40th sign is a space it should just replace it with a newline)
The problem #1 is almost solved with this:
This is the DocumentSplitFilter which I aplied to the textpane: the replace method is call everytime I write something:
public DocumentSplitFilter(){
this.signsOnLine = 40;
this.signsOnLineOneLess = signsOnLine-1;
this.signsOnLineOneMore = signsOnLine+1;
}
publicvoid replace(FilterBypass fb,int offs,int length,String str, AttributeSet a)
throws BadLocationException{
doTheMagic(fb,offs,length,str,a);
if (DEBUG){
System.out.println("Replace String method");
}
super.replace(fb, offs, length, str, a);
}
privatevoid doTheMagic(FilterBypass fb,int offs,int length,String str, AttributeSet a){
int rows,start,end;
//printInfo();
Element e = fb.getDocument().getDefaultRootElement();
rows = e.getElementCount();
start = e.getStartOffset();
end = e.getEndOffset();
int len = getText(fb, start, end).length();
Testing(fb,a,start,end,false);
if(exceded(end)){
if(end == signsOnLine+last) Testing(fb,a,start,end,true);
if(end == signsOnLineOneMore+last){
if(getText(fb, signsOnLineOneLess+last, 1).equalsIgnoreCase(" ")){
doReplace(fb,a,signsOnLineOneLess+last);
last = signsOnLineOneLess+last;
}else{
doReplace(fb,a,lastIndex);
last = lastIndex;
}
//if(bb) {
getMyQueue().push(last);
getMyQueue().print();
//bb = !bb;
//}
}
}
if(len<getMyQueue().top()){
last = getMyQueue().positionCorrection(len);
getMyQueue().print();
}
if(len><signsOnLineOneMore)
if(!isEnter || rows == 1)
last = 0;
}
privatevoid Testing(FilterBypass fb, AttributeSet a,int start,int end,boolean space){
try{
if(space) lastIndex = fb.getDocument().getText(start, end).lastIndexOf(" ");
else{
if(fb.getDocument().getText(start, end-1).endsWith("\n")){
lastIndex = fb.getDocument().getText(start, end-1).lastIndexOf("\n");
isEnter =true;
last = lastIndex;
getMyQueue().push(last);
getMyQueue().print();
}
else{
}
}
}catch (BadLocationException ee){
// TODO Auto-generated catch block
ee.printStackTrace();
}
}
privateboolean exceded(int off){
if(off == last+signsOnLine || off == last+signsOnLineOneMore)returntrue;
elsereturnfalse;
}
privatevoid doReplace(FilterBypass fb, AttributeSet a,int off){
try{
fb.replace(off, 1,"\n", a);
}catch (BadLocationException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getText(FilterBypass fb,int off,int len){
try{
return fb.getDocument().getText(off,len);
}catch (BadLocationException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return"";
}
and with this:
publicclass OwnQextends ArrayList><Integer>{
publicvoid push(Integer o){
add(0, o);
}
public Integer pop(){
//int last = this.size()-1;
//if(last == -1) return 0;
try{
Integer i = get(0);
remove(0);
return i;
}catch (Exception e){
//e.printStackTrace();
return 0;
}
}
publicvoid print(){
System.out.println(this);
}
public Integer top(){
//int last = this.size()-1;
//if(last == -1) return 0;
try{
Integer i = get(0);
return i;
}catch (Exception e){
//e.printStackTrace();
return 0;
}
}
publicint positionCorrection(int caretPos){
int i=0;
for(i=0;;i++){
pop();
try{
if(caretPos>get(i)){
pop();
break;
}
}catch (Exception e){
return top();
}
}
return top();
}
publicint NtimesTop(int i){
try{
return get(i-1);
}catch (Exception e){
// TODO Auto-generated catch block
return 0;
}
}
}
The problem with this solution is that if you start writing in the middle of the written text the whole checking and replacing brokes down.
The question is if I can do it other way? or
Does Jtextpane have any limiting(spliting,blocking) atributes which I can use?
Of course the whole problem should be applied to a textpane of any size.
Thx.

