GWT Datagrid selection - gwt

I would like to reject the selection of a row in a datagrid based on the state of a form. If the form has fields with changed data, I would like the row selection to rejected.
Is there an event I could trap (before selection) and cancel or do I have to implement the logic myself?

You can either use a SelectionChangeHandler on your SelectionModel (it gives you an object which was selected, and you can unselect it), or you can use the CellPreviewHandler on your DataGrid (it gives you a row which was clicked - event.getContext().getIndex() - and you can unselect it in your SelectionModel).
The choice depends on what you want to do after the event: you have to do something obvious in your UI so that users are not confused why clicking on one row selects it, while clicking on another row does not. For example, you can change the background color of unselectable rows as soon as you render your DataGrid, and then show an error message when the wrong row is selected.

Related

How can I get an edit request for full row when in readOnlyEdit and fullRow mode?

I want to have my AgGrid editable and want to run validation logic before a row is saved. So, basically when a user clicks the cell, enters into edit mode, edits a few cells, clicks away - the row is SAVED (unless validation fails).
I started with setting
editType="fullRow"
readOnlyEdit
on AgGrid. This allows me to get multiple onCellEditRequest events when I click away from the row (=save the row). But ideally I want to receive single event with all edited cells (or a full row data).
Is it possible? Can I request this feature on AgGrid?
You can use the onRowValueChanged callback. This will be called once you save any changes within the row.
Documentation.

GWT Deselect keyboard selection

When keyboard selecting a row in a GWT DataGrid, how do you deselect it?
When you select a row like this:
table.setKeyboardSelectedRow(rowInd, true);
You can unselect the row by clicking outside the grid. How do I make this same 'unselection' programmatically?
Selecting a row like this:
table.setKeyboardSelectedRow(rowInd, false);
Since the focus is not on this selection, it does not blur out like usual when you click outside the grid. I need to keep the focus on something else, but I wish to still deselect this row at a certain point. How do you deselect a keyboard selection programmatically?
Note: using the Selection Model's setSelection() with a false param does not deselect keyboard selection, only the selection made by the selection model. Using setKeyboardSelectedRow(-1) also does not work.
I find it baffling that GWT does not offer a way to unselect a keyboard selected row!

Why is setItem on JComboBox is called twice?

I have a JComboBox with a custom Editor.
Why I make a selection from the dropdown list, I find that setItem() method of the Editor is called twice with the same selected item from the drop list.
Why is that?
As explained here:
It fires twice because one item becomes DESELECTED and another becomes SELECTED. Event fires for both. You can check which one occured by calling e.getStateChange().

GWT ListBox getSelectedIndex() not working as intended when hovered over list items

I have a GWT ListBox. My validation code inquires whether the ListBox's selected index is -1 (my ListBox's default state). If I do not touch the ListBox, the getSelectedIndex() returns -1, as expected. If I hover over the ListBox items but do not click on one (i.e. click outside), getSelectedIndex() returns a non-(-1) index. I want it to return -1 unless I click on a ListBox item and the ListBox updates itself to reflect that selection.
What should I do?
You won't be able to use the native <select> element for this in a nice way since the browsers determine what the selected element is, not GWT. In other words, this is just how <select> works.
You'll have to either create your own select widget (which might be preferred for styling reasons anyway) or get creative and kludge up something next to the existing widgets. You could add a click handler to the ListBox and "manually" set the selected index to -1, but you'll have to figure out if the user cancels his operation and restore the selection afterwards. You'll have to suppress selection events from your custom code because you probably don't want the rest of your app to react to your new -1 hack. Ugh! Probably easiest to handle this in a different way.

GWT cells selection

I wish to make a drag and select application in GWT where I wish to have cell table or grid of say 20*100 columns*rows. I want to add a event such that I can drag something like a rectangle with my mouse and all the cells in that region get selected or I can fire an event for each cell and assign each of them a same ID. The main idea behind the thing is to perform a selection by dragging and then grouping all selected cells as one, something like Excel sheet selection. Can any one help me out in this?
I have once implemented GWT widget allowing to "select" some rectangular region of a table. Basically the idea was to subclass a Grid or FlexTable and do all the logic in various mouse event handlers (mouse down, mouse up, mouse out, mouse over).
The only minor hack I had to introduce was a method for getting the cell for any mouse event. There is a method HTMLTable#getCellForEvent that works for a click event, but when I looked into implementation of this method, I saw that it could actually work for any event, so I just implemented my own method for getting cell for any mouse event based on mentioned implementation.
Maybe it would be also possible to achieve this using HTMLTable#getEventTargetCell