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