aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem in the two listener with in the textarea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in the two listener with in the textarea" Watch "problem in the two listener with in the textarea" New topic
Author

problem in the two listener with in the textarea

alok shukla
Greenhorn

Joined: May 06, 2005
Posts: 4
hi all
i am working on a ui component in which i have textArea which has some coded chareters like yen, degree and many more which r coded like this
<<APS100>> , <<APS101>>, ......like that i have to treet these coded strings like a single character so when i press the cursor inside <.....> it automatically to end of this string , which i done through the caret listenor, now secon thing when i delete the charcters < or > from the either side of the <<APS100>> then it totally delete the whole string <<APS100>> ...........
now the problem arises when i select the characters from the text area then it goes in the function number of times to the caret update.............

another problem arises when i delete the chatactes like "THIS IS TEST <<APS100>> THIS IS TEST " from either side then i use the logic lik that in two string i copy the string before the string <<APS100>> and in second string i copy te string after <<APS100>> now cocatinate these two and set it to the text area now text area has already text when we set other string then it first delete initial characters then it put this new string into that then it throw exceptin like that
java.lang.IllegalStateException: ???


HERE I AM PASTING THE WHOLE CODE OF THAT PART




private ElementEditor elementEditor;
private String str=getSplitView().sourceTxt.getText();
public char Arr[]=str.toCharArray();
private boolean flag=false,flag1=false;
String Temp="";
int loc;
public SplitViewController(SplitView comp) {
super(comp);
elementEditor = comp.getElementEditor();
initialize();
}
public SplitView getSplitView(){
return (SplitView)getView();
}
private void initialize(){
updatePreviewText();
getSplitView().sourceTxt.getDocument().addUndoableEditListener(new UndoableEditListener(){
public void undoableEditHappened(UndoableEditEvent e) {
displayEditInfo(e);
}
});

getSplitView().sourceTxt.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent de) {
documentChanged(de);

}

public void insertUpdate(DocumentEvent de) {
documentChanged(de);

}

public void removeUpdate(DocumentEvent de) {

if(flag1==false)
documentDeleted(de);
else return;
}

});
/*getSplitView().sourceTxt.addCaretListener(new CaretListener(){

public void caretUpdate(CaretEvent ce) {

//if(getSplitView().sourceTxt.getSelectedText().length()>=1)
//return;
if(flag){
return;
}
else if(!flag){
try{
int caretLocation=ce.getDot();
int newLocation= setCaret(String.copyValueOf(Arr),caretLocation);

System.out.println("before caret setting");
getSplitView().sourceTxt.setCaretPosition(newLocation);
System.out.println("AFTER caret setting");


}
catch(Exception e){
e.printStackTrace();
}

}

}
});*/
}
private void documentDeleted(DocumentEvent de){
flag=true;

System.out.println("deleted");
int q=getSplitView().sourceTxt.getCaretPosition();
////////////////////////////////////////

int i=0,j=0,k=0;
int position=q;
int last=q;
String s1=null,s2=null,s=null;
char[] arr1=getSplitView().sourceTxt.getText().toCharArray();
if(arr1.length>=10)
{
j=(q-9<0)? 0:q-9;
k=(q+9>arr1.length)? arr1.length:q+9;
for(i=j;i<k;i++){
if(arr1[i]=='<'&&arr1[i+1]=='A'&&arr1[i+2]=='P'&&arr1[i+3]=='S'&&arr1[i+4]=='1'&&arr1[i+7]=='>')
{//{
//if(q==i+1||q==i+2||q==i+3||q==i+4||q==i+5||q==i+6||q==i+7||q==i+8||q==i+9)
if(arr1[i-1]=='<')
{//if(i!=0)
position=i-1;
last=position+9;
if(position==0){
System.out.println("hellow");
s1="";//getSplitView().sourceTxt.getText().substring(0,position);
s2=getSplitView().sourceTxt.getText().substring(last);
//getSplitView().getSourcetxtField().setText("");
//getSplitView().sourceTxt.setText(s1+s2);
System.out.println("hellow1");
break;
}
else {
s1=getSplitView().sourceTxt.getText().substring(0,position);
s2=getSplitView().sourceTxt.getText().substring(last);
break;
}
//getSplitView().sourceTxt.setText(getSplitView().sourceTxt.getText().substring(last-1,getSplitView().sourceTxt.getText().length()));

}
if(arr1[i+8]=='>')
{
last=i+9;
if(position>0){
s1=getSplitView().sourceTxt.getText().substring(0,position);
s2=getSplitView().sourceTxt.getText().substring(last,getSplitView().sourceTxt.getText().length());
getSplitView().sourceTxt.setText("");
//getSplitView().sourceTxt.setText(s1+s2);
}
else
getSplitView().sourceTxt.setText(getSplitView().sourceTxt.getText().substring(last,getSplitView().sourceTxt.getText().length()));
break;
}
}
}

}

s=s1.concat(s2);
flag1=true;
try{
//EventListener[] z=getSplitView().getSourcetxtField().getListeners(this);
}
catch(Exception e){
e.printStackTrace();
}
getSplitView().getSourcetxtField().append(s);
flag1=false;


/////////////////////////////////////
updatePreviewText();
}
private void documentChanged(DocumentEvent de){
try{

//int i=0;
//Arr=getSplitView().getSourcetxtField().getText().toCharArray();
//char arr1[]=Arr;
//char arr2[]=Arr;
//int loc2=de.getOffset();
//int j=(loc2-9<0)? 0:loc2-9;
//int k=(loc2+9>Arr.length)? Arr.length:loc2+9;
flag=false;
elementEditor.getsaveButton().setEnabled(true);
String s=getSplitView().getSourcetxtField().getText();
Arr=s.toCharArray();
loc=getSplitView().getSourcetxtField().getCaretPosition();
getSplitView().getSourcetxtField().setCaretPosition(setCaret(s,loc));
updatePreviewText();

}
catch(Exception ex){
System.out.println("Updation problem"+ex);
}
}

private void displayEditInfo(UndoableEditEvent e) {
flag=false;
try{
if(e.getEdit().isSignificant()){
elementEditor.getsaveButton().setEnabled(true);
String s=getSplitView().getSourcetxtField().getText();
Arr=s.toCharArray();
loc=getSplitView().getSourcetxtField().getCaretPosition();
getSplitView().getSourcetxtField().setCaretPosition(setCaret(s,loc));

}
}
catch(Exception eq)
{
System.out.print("editing fail"+eq);
}
}

public void updatePreviewText(){

getSplitView().previewTxt.setText("");
char temp[]=Arr;
for(int i=0;i<temp.length {
if(temp[i]=='<'&&temp[i+1]=='<'&&temp[i+2]=='A'&&temp[i+3]=='P'&&temp[i+4]=='S'&&temp[i+5]=='1'&&temp[i+8]=='>'&&temp[i+9]=='>')
{
String s=new String(temp,i+5,3);
for(int l=0;l<getSplitView().getArrayList().size();l++){
if((s.compareTo(getSplitView().getArrayList().get(l)))==0){
Temp=Temp.concat(""+getSplitView().getArrayList1().get(l));
break;
}
}
i=i+10;
if(i>=temp.length)
break;
}
else
{
Temp=Temp.concat(""+temp[i]);
i=i+1;
}
}
Font font=new Font("Courier New",Font.PLAIN,15);
getSplitView().previewTxt.setFont(font);
getSplitView().previewTxt.setText("<html>"+Temp+"</html>");
Temp="";
}
public int setCaret(String text,int loc1){

int i=0,j=0,k=0;
int position=loc1;
char[] arr1=text.toCharArray();
if(arr1.length>=10)
{
j=(loc1-9<0)? 0:loc1-9;
k=(loc1+9>arr1.length)? arr1.length:loc1+9;
for(i=j;i<k;i++){
if(arr1[i]=='<'&&arr1[i+1]=='<'&&arr1[i+2]=='A'&&arr1[i+3]=='P'&&arr1[i+4]=='S'&&arr1[i+5]=='1'&&arr1[i+8]=='>'&&arr1[i+9]=='>')
{
if(loc1==i+1||loc1==i+2||loc1==i+3||loc1==i+4||loc1==i+5||loc1==i+6||loc1==i+7||loc1==i+8||loc1==i+9)
position=i+10;break;
}
else if(loc1==arr1.length||loc1==0)
{position=loc1;break;}
else if(arr1[loc1-1]=='>'&&arr1[loc1]=='<')
{position=loc1;break;}

}
return position;
}
else return (position=loc1);

}
}













 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in the two listener with in the textarea