You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Rafał Gierusz <ra...@imbuelab.com> on 2012/02/17 15:44:26 UTC

2.0.1 TextArea - ParagraphListener - textInserted method question

Hi,

I use TextArea and want to listen to the inserted text (simply by using 
keyboard). I do it using ParagraphListener. Every time new character is 
typed a listener's method textInserted(Paragraph paragraph, int index, 
int count) is invoked. Everything works fine till I press ENTER. In that 
case textInserted method is invoked with "count" parameter set to 0. 
Does it make any sense? Why it's implemented that way?

Regards,
Rafal

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Noel Grandin <no...@gmail.com>.
It's because of the somewhat weird internal model of TextArea. The
ENTER key terminates a paragraph but is not logically part of the
paragraph.


2012/2/20 Rafał Gierusz <ra...@imbuelab.com>:
> Hi Sandro,
>
> please just remember that my question is different than the problem
> described in 838 bug, it is somehow connected but it's not the same. This
> time I was asking about "count" equal 0 which creates a problems for me. Bug
> 838 is about removed text and listeners.
>
> Best regards,
> Rafal
>
> W dniu 2012-02-20 14:35, Sandro Martini pisze:
>
>> Hi Rafal,
>> thank you for the example (minimal like this is perfect :-) ), I'll
>> create a Pivot838.java test for this issue, under tests and the usual
>> package, but we have to look better at it, Noel is our expert in Text
>> Components and not only ...
>>
>> Let's update.
>>
>> Bye

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Sandro Martini <sa...@gmail.com>.
Hi all, and thanks for having created an issue for this:

https://issues.apache.org/jira/browse/PIVOT-841

I just renamed the test case class to Pivot841.java


Let's update.

Bye

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Sandro Martini <sa...@gmail.com>.
Hi,

> please just remember that my question is different than the problem
> described in 838 bug, it is somehow connected but it's not the same. This
> time I was asking about "count" equal 0 which creates a problems for me. Bug
> 838 is about removed text and listeners.
Uh I made some confusion, you are right ... tomorrow I'll rename the
test class in something a little different to avoid more confusion.
Better, if you have some time, could you create an issue for this
targeted to 2.0.2, so I'll use it as new name for this test class ?

At this point we have to see if Greg has some idea on what is the best
way to achieve this (and if possible without touching the api now,
otherwise we have to wait for 2.1.x) ... for this and for PIVOT-838.

Let's update.

Bye

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Rafał Gierusz <ra...@imbuelab.com>.
Hi Sandro,

please just remember that my question is different than the problem 
described in 838 bug, it is somehow connected but it's not the same. 
This time I was asking about "count" equal 0 which creates a problems 
for me. Bug 838 is about removed text and listeners.

Best regards,
Rafal

W dniu 2012-02-20 14:35, Sandro Martini pisze:
> Hi Rafal,
> thank you for the example (minimal like this is perfect :-) ), I'll
> create a Pivot838.java test for this issue, under tests and the usual
> package, but we have to look better at it, Noel is our expert in Text
> Components and not only ...
>
> Let's update.
>
> Bye

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Sandro Martini <sa...@gmail.com>.
Hi Rafal,
thank you for the example (minimal like this is perfect :-) ), I'll
create a Pivot838.java test for this issue, under tests and the usual
package, but we have to look better at it, Noel is our expert in Text
Components and not only ...

Let's update.

Bye

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Rafał Gierusz <ra...@imbuelab.com>.
Hi Sandro,

I've prepared the simplest example I could imagine, it looks ugly, but 
it's simple and works :-)

Please take a look on the code below and description after it:

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.TextArea;
import org.apache.pivot.wtk.TextArea.Paragraph;
import org.apache.pivot.wtk.TextArea.ParagraphListener;
import org.apache.pivot.wtk.TextAreaContentListener;
import org.apache.pivot.wtk.Window;

public class TextAreaListenersExample extends Application.Adapter {
   @Override
   public void startup(Display display, Map<String, String> properties) 
throws Exception {
     TextArea textArea = new TextArea();
     textArea.setText("abcxyz");
     //---
     final ParagraphListener paragraphListener = new 
ParagraphListener.Adapter() {
       @Override
       public void textInserted(Paragraph paragraph, int index, int count) {
         System.out.println("Text inserted\n\tparagraph content: '" + 
paragraph.getCharacters() + "" + "'\n\tindex: " + index + "\n\tcount: " 
+ count);
       }

       @Override
       public void textRemoved(Paragraph paragraph, int index, int count) {
         System.out.println("Text removed\n\tparagraph content: '" + 
paragraph.getCharacters() + "'\n\tindex: " + index + "\n\tcount: " + 
count);      }
     };

     
textArea.getParagraphs().get(0).getParagraphListeners().add(paragraphListener);

     textArea.getTextAreaContentListeners().add(new 
TextAreaContentListener.Adapter() {
       @Override
       public void paragraphInserted(TextArea textArea, int index) {
         Paragraph paragraph = textArea.getParagraphs().get(index);

         System.out.println("Paragraph inserted\n\tparagraph content: '" 
+ paragraph.getCharacters() + "'\n\tindex: " + index);

         paragraph.getParagraphListeners().add(paragraphListener);
       }
     });

     Window window = new Window(textArea);

     window.open(display);
   }

   public static void main(String[] args) throws Exception {
     DesktopApplicationContext.main(TextAreaListenersExample.class, new 
String[0]);
   }
}

When you place carret after the "c" character in the TextArea and press 
ENTER, you get following output:

Text removed */<- additionally, it's impossible to get information about 
removed text because it's already removed from paragraph, I created bug 
for this issue: https://issues.apache.org/jira/browse/PIVOT-838/*
     paragraph content: 'abc' /*<- text 'xyz' was removed from the first 
paragraph 'abcxyz'*/
     index: 3
     count: 3
Text inserted
     paragraph content: 'abc' /*<- first paragraph insertion, probably 
it was ENTER*/
     index: 3
*count: 0 <- !!!*
Paragraph inserted
     paragraph content: 'xyz' /*<- new paragraph is inserted together 
with the content*
/    index: 1
Text inserted
     paragraph content: 'xyz'/*<- I don't know why insertion happens 
here***/
     index: 0
*count: 0 <- !!!*

One more thing, maybe it would be better to have paragraph insertion 
without any content (not like in this example), and after that text 
insertion with the all content of the new created paragraph, it would be 
more intuitive and easier to handle, I really struggle with that now. 
Can you consider that?

Regards,
Rafal

W dniu 2012-02-20 13:04, Sandro Martini pisze:
> Hi,
> in 2.0.1 we had an issue (now solved in trunk), see here:
> https://issues.apache.org/jira/browse/PIVOT-835
> maybe even TextArea could have some little thing to fix, but I'm sorry
> I know very little about it ... we have to see what other Pivot
> developers say.
> Bur Enter key shouldn't trigger a new Paragraph creation ?
>
> In the meantime, could you post here (maybe inside a zip) a minimal
> sample to show your problem, so we can easily look at it ?
>
> Thank you,
> Sandro

Re: 2.0.1 TextArea - ParagraphListener - textInserted method question

Posted by Sandro Martini <sa...@gmail.com>.
Hi,
in 2.0.1 we had an issue (now solved in trunk), see here:
https://issues.apache.org/jira/browse/PIVOT-835
maybe even TextArea could have some little thing to fix, but I'm sorry
I know very little about it ... we have to see what other Pivot
developers say.
Bur Enter key shouldn't trigger a new Paragraph creation ?

In the meantime, could you post here (maybe inside a zip) a minimal
sample to show your problem, so we can easily look at it ?

Thank you,
Sandro