Uploaded image for project: 'Pivot'
  1. Pivot
  2. PIVOT-841

TextArea - ParagraphListener - textInserted method problem - count is equal 0

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.0.1, 2.0.2
    • 2.5
    • wtk
    • None
    • Windows 7, Java 1.7.0_01

    Description

      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?

      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
      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 <- count is 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 <- again, count is 0!!!

      One more thing, maybe it would be better to have paragraph insertion without any content (not like it is now), 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?

      Attachments

        Activity

          People

            rwhitcomb Roger Lee Whitcomb
            imbuelab Imbue Lab
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: