Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
http://mail.openjdk.java.net/pipermail/openjfx-dev/2012-December/004685.html Mark Claassen proposed an API that seemed really simple and straightforward and allowed developers to add filtering capabilities very easily to TextInputControls. Here is the idea: > Add the following sub-interface to the Content interface. No methods are > added to the Content interface. > interface Filter { > String filter(int index, String text, String currentContent); > } > > Add the following to TextFieldContent and TextAreaContent: > private Content.Filter filter; > public void setContentFilter(Content.Filter filter) { > this.filter = filter; > } > > Change the "insert" method of TextFieldContent and TextAreaContent to start > with: > @Override public void insert(int index, String text, boolean > notifyListeners) { > text = TextInputControl.filterInput(text, [boolean], [boolean]); > if (filter != null) > text = filter.filter(index, text, get()); > > Add a method in TextField: > public void setContentFilter(Content.Filter filter) { > ((TextFieldContent)getContent()).setContentFilter(filter); > } > > Add a method in TextArea: > public void setContentFilter(Content.Filter filter) { > ((TextAreaContent)getContent()).setContentFilter(filter); > } > > This would do a few things: > 1) Use the current "TextInputControl.filterInput" mechanisms of TextField > and TextArea (like to strip out special chars and newlines). > 2) By specifying the Filter interface outside of TextField and TextArea, it > allows the same filter to be used for TextField and TextArea > 3) Makes it simple to do things like translate all input to upper case or > limit the overall length > 4) By not adding a method to Content directly, something like the > InputMaskContent would not be forced to deal with the Filter interface at > all. > > This implementation would allow someone creating a filter to "filter" the > text to have illegal characters. I thought it was more validate this > first, so that the implementer would only have to deal with proper input. > However, TextInputControl.filterInput could be done both before and after > the Filter.filter() call. > text = TextInputControl.filterInput(text, [boolean], [boolean]); > if (filter != null) { > text = filter.filter(index, text, get()); > text = TextInputControl.filterInput(text, [boolean], > [boolean]); > } > > Another possible addition to this may be to allow the filter to return > null, signifying an error input and causing the system to "beep".
|