TextField Document model

Richard Bair richard.bair at oracle.com
Tue Dec 4 08:32:29 PST 2012


It has been a long time since I last worked in TextInputControl, so I am familiarizing myself with it again. Earlier it was asked whether getContent() could be made public, along with a setContent etc. One other problem I think we'd see if we did this, is that it is possible to get the caret out of sync with the text. That is, if you call getContent().delete(0, 20, true), for example, and the caret was at index 15 (say), then the caret would remain out of range. This is because the content does not call back into the text input control to readjust the caret -- rather, each of the methods on TextInputControl that manipulates the text also repositions the caret as required.

Just something to be aware of, when it comes to making Content public. The present design is around Content being an implementation detail of a specific control (although it is protected so that any custom text-input-based control can customize the content). What this means in the context of the larger question of "how do we handle a formatted text field", is that if we decide to make Content public, then we will need to either document this or rewrite the code so that any change to content automatically repositions the caret correctly. I'm not sure whether this is feasible, as text has a bazillion corner cases and we'd have to first analyze each of these and see if it works.

I see there are a number of other things that would be impacted as well. For example, the getText(start, end) method of TextInputControl does range validation, and if Content were meant to be used by developers (rather than control authors) then we would have to move those checks into Content.

Another potential issue here is that the Content interface defines a delete and an insert method, and is observable. So when we replace text, there are actually two change notifications which are going to happen -- a delete followed by an insert (and I believe this bleeds out through the interface, such that if you have a listener or binding on the "text" property of a TextInputControl, you will get two notifications). This is unfortunate, it would have been better to get a single notification (much like the ObservableList manages). However, because Content is an interface (!!) we cannot now add a replace method to it. Well, at least yet. In Java 8 maybe we can, which would be nice.

Richard


More information about the openjfx-dev mailing list