How to get the last Textbox value before i hitting Delete key? - gwt

itemBox.addKeyUpHandler( new KeyUpHandler()
{
public void onKeyUp( KeyUpEvent event )
{
String currentValue = itemBox.getValue().trim();
// handle backspace
if( event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE )
{
if( "".equals( currentValue ) )
{
doTheJob( );
}
}
}
} );
Expect behavior:
when the textbox is empty, then i hit delete, will run doTheJob();
Current behavior:
when there is one character, i hit delete, it will trigger doTheJob();
In other word, is there any way i can get textbox content before i hit delete key?
I tried to used a var to hold the last value, but it needs to register another listener and the impl is not so effective.
Thanks for your input.
////////////////////Edit //////////////////////
use KeyDownHandler did solve the above problem, however lead to another problem:
i use itemBox.setValue( "" ); to clear the textbox but it will always have a comma there.
itemBox.addKeyDownHandler( new KeyDownHandler()
{
public void onKeyDown( KeyDownEvent event )
{
// handle backspace
if( event.getNativeKeyCode() == KeyCodes.KEY_BACKSPACE )
{
String currentValue = itemBox.getValue().trim();
if( "".equals( currentValue ) )
{
doTheJob();
}
}
// handle comma
else if( event.getNativeKeyCode() == 188 )
{
doOtherJob();
//clear TextBox for new input
itemBox.setValue( "" );
itemBox.setFocus( setFocus );
}
}
} );

after
itemBox.setFocus( setFocus );
prevent event bubbling with
event.preventDefault();
event.stopPropagation();
So the event will be canceled before comma is added to the textbox content.

Related

Unity UI InputField detect enter or esc key when OnEndEdit

I'm trying to figure out how to detect the end of editing an InputField with either the 'enter' or the 'esc' key. I have a working solution, but was wondering if there's a better way of doing it (using already existing Unity stuff). Actually using Unity 4.7.1
Hereby is the actual solution:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CustomInputField : InputField, ICancelHandler
{
// Action others can subsribe
public System.Action<InputField, bool /*true: enter key, false: cancel key*/> OnCustomEndEdit = null;
override public void OnPointerClick (PointerEventData eventData) {
base.OnPointerClick( eventData );
if( eventData.clickCount > 2 ) {
SelectAll();
}
}
// Need to presss 'enter' twice in order to receive this event. NOT USING IT!
override public void OnSubmit(BaseEventData eventData) {
base.OnSubmit( eventData );
}
// Need to presss 'esc' twice in order to receive this event. NOT USING IT!
public void OnCancel(BaseEventData eventData) {
}
// This event happens before Unity calls the Update() funcion
public void OnEndEdit( string value ) {
if( Input.GetKeyDown( KeyCode.KeypadEnter ) || Input.GetKeyDown( KeyCode.Return ) ) {
if( OnCustomEndEdit != null ) {
OnCustomEndEdit( this, true );
}
Debug.Log( "Enter Key: " + this.text );
}
else {
if( Input.GetKeyDown( KeyCode.Escape ) ) {
if( OnCustomEndEdit != null ) {
OnCustomEndEdit( this, false );
}
Debug.Log( "Esc Key" );
}
}
}
}

FilteredList setPredicate() only reevaluates once

I have a FilteredList building a TableView set up according to the example in http://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/.
The default predicate works fine and when the PropertyChangeListener fires the setPredicate() method is invoked correctly every time but only the FIRST invocation results in the running of the predicate filter code and the subsequent updating of the TableView.
What have I missed here?
#Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
.
.
.
// Wrap the ObservableList in a FilteredList (initially display all data).
FilteredList<Filedata> filteredData = new FilteredList<>( eeModel.data, p -> true );
// 2. Set the filter Predicate whenever the filter changes.
FilterDataClass.addPropertyChangeListener( new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("PCE...");
filteredData.setPredicate(fd -> {
if ( FilterDataClass.getFilenameDupe() == FilterDataClass.FilterStatus.cbIGNORE ) {
return true;
}
else if ( (FilterDataClass.getFilenameDupe() == FilterDataClass.FilterStatus.cbON) && fd.isFilenameDuplicate() ) {
return true;
}
else if ( (FilterDataClass.getFilenameDupe() == FilterDataClass.FilterStatus.cbOFF) && !fd.isFilenameDuplicate() ) {
return true;
}
else {
return false;
}
});
}
});
// Wrap the FilteredList in a SortedList.
SortedList<Filedata> sortedList = new SortedList<>( filteredData );
// create a TableView with the sorted list set as the items it will show
table.setItems( sortedList );
// bind the sortedList comparator to the TableView comparator
sortedList.comparatorProperty().bind(table.comparatorProperty());
}
EDIT:
Might this be an optimisation issue? If I add some junk code to the predicate that requires the evaluation of the event data, such as:
if ( evt.getNewValue() == evt.getOldValue() ) {
System.out.println("This should never happen");
}
then the predicate gets reevaluated.

Selecting a listbox dropdown item in GWT

I have a GWT view from which I grab the value of a dropdown and store it in a DB. The dropdown has the values "one" "two" "three". When I go back to the same view and I have "Two" stored in the DB then I want "Two" to be the selected item. However the only way I can get this to work at the moment is by iterating through each item in the listbox to find the one which matches and then set this as the selected one. Is there a better way to achieve this? I don't want to have to save the selected index.
I recommend you to extend ListBox and implement TakesValue interface. And in this class maintain a list variable which holds all the items in the ListBox. setValue and getValue should looks like the following code snippet -
private List<String> listItems = new ArrayList<String>();
public class MyListBox extends ListBox implements TakesValue<String>
{
public void setValue( String value )
{
if ( listItems.size() > 0 )
{
int valueIndex = 0;
if ( listItems.contains( value ) )
{
valueIndex = listItems.indexOf( value );
this.value = value;
}
setItemSelected( valueIndex, true );
}
}
public String getValue()
{
int selectedIndex = super.getSelectedIndex();
String value = null;
if ( selectedIndex >= 0 )
{
value = super.getValue( selectedIndex );
if ( "null".equals( value ) )
{
value = null;
}
}
return value;
}
public void setOptions(List<String> options)
{
listItems.clear();
listItems.addAll( items );
for ( String item : listItems )
{
addItem( item, item );
}
}
}
Now its just a matter of doing listBox.setValue( value ) method call from the view java file. Prior to this options must be set.

Allow/disallow tab selections for TabPanel in GWT

I have 4 tabs. I want to prevent the user to get (with window.alert example) to another tab as it has not finished filling the current tab. After user finishes filling out all fields, a text will appear (saying he can move to the next tab) inside the current tab. And he will be able to click the tab below.
Should I use BeforeSelectionEvent handler or SelectionHandler?
i do this code to respond my question but is too long
is there a possibility to turn it into a short function ?
this.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() {
public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
if (Application.get().getChampsObligatoire().values().contains("Fiche")) {
if (event.getItem().intValue() > 0){
event.cancel();
Window.alert("You must fill all fields before proceeding to the next step.");
}
}
if (Application.get().getChampsObligatoire().values().contains("projet")) {
if (event.getItem().intValue() > 1){
event.cancel();
Window.alert("You must fill all fields before proceeding to the next step.");
}
}
if (Application.get().getChampsObligatoire().values().contains("cibles")) {
if (event.getItem().intValue() > 2){
event.cancel();
Window.alert("You must fill all fields before proceeding to the next step.");
}
}
if (Application.get().getChampsObligatoire().values().contains("Ressources")) {
if (event.getItem().intValue() > 3){
event.cancel();
Window.alert("You must fill all fields before proceeding to the next step.");
}
}
if (Application.get().getChampsObligatoire().values().contains("ContrĂ´le")) {
if (event.getItem().intValue() > 4){
event.cancel();
Window.alert("You must fill all fields before proceeding to the next step.");
}
}
}
});
For GWT TabPanel:
#Override
public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex)
{
String currentTabName = widget.getTabHTML(widget.getSelectedTab()); // for reference
String tabNameYouTryingToSelect = widget.getTabHTML(tabIndex); // for reference
// check some external self-written method and return true or false to allow/disallow selection
if (isCurrentlyActiveTabBuilt()){
return true;
} else {
Window.alert("You must fill all fields before proceeding to the next step.");
return false;
}
}

Specifying which Cell should receive focus next in a CellTable

I'm using GWT 2.1.0
I have a CellTable populated with Columns that use different Cells to edit different types of values (e.g. date, string, etc). I want the user to be able to click in a cell, type a value, and hit enter to go directly to editing the next cell down, or tab to go directly to editing the next cell over.
I've been looking through the Cell and CellTable interfaces but can't find anything that looks relevant. How can I achieve this effect?
I had a similar requirement and I could not find a out-of-the-box solution. I ended up subclassing TextInputCell and add tabIndex support myself.
Here's some bits and pieces of the subclass (hopefully it will compile, too lazy to check). Unfortunately I cannot post the entire subclass, since it has lot may other things which are not related to the current question. This solution takes care of tabbing to the next cell, but for enter support, you may need to override onBrowserEvent.
public class EditTextInputCell extends TextInputCell
{
int startTabIndex;
interface TabbedTemplate extends SafeHtmlTemplates
{
#Template( "<input type=\"text\" value=\"{0}\" tabindex=\"{1}\" class=\"{2}\" title=\"{3}\"></input>" )
SafeHtml input( String value, String tabindex, String styleClass, String title );
}
private static TabbedTemplate template;
public EditTextInputCell( int startTabIndex )
{
this.startTabIndex = startTabIndex;
}
#Override
public boolean isEditing( Context context, Element parent, String value )
{
return true;
}
#Override
public void render( Context context, String value, SafeHtmlBuilder sb )
{
// Get the view data.
Object key = context.getKey( );
ValidationData viewData = getViewData( key );
if ( viewData != null && value.equals( viewData.getCurrentValue( ) ) )
{
clearViewData( key );
viewData = null;
}
String strToDisp = ( viewData != null && viewData.getCurrentValue( ) != null ) ? viewData.getCurrentValue( ) : value;
String tabIndex = "" + startTabIndex + context.getIndex( ) + context.getColumn( );
boolean invalid = ( viewData == null ) ? false : viewData.isInvalid( );
String styleClass = "cellTableCell-valid";
String errorMessage = "";
if ( invalid )
{
styleClass = "cellTableCell-invalid";
errorMessage = viewData.getMessage( );
}
if ( strToDisp != null )
{
SafeHtml html = SimpleSafeHtmlRenderer.getInstance( ).render( strToDisp );
// Note: template will not treat SafeHtml specially
sb.append( getTemplate( ).input( html.asString( ), tabIndex, styleClass, errorMessage ) );
}
else
{
sb.appendHtmlConstant( "<input type=\"text\" tabindex=\"" + tabIndex + "\" class=\"" + styleClass + "\" title=\"" + errorMessage + "\"></input>" );
}
}
private TabbedTemplate getTemplate( )
{
if ( template == null )
{
template = GWT.create( TabbedTemplate.class );
}
return template;
}}