25
25
import java .util .function .BiConsumer ;
26
26
import java .util .stream .Collectors ;
27
27
28
+ import com .google .gwt .core .client .Scheduler ;
28
29
import com .google .gwt .dom .client .Style .Overflow ;
29
30
import com .google .gwt .dom .client .Style .Position ;
30
31
import com .google .gwt .event .dom .client .ClickEvent ;
37
38
import com .google .gwt .event .dom .client .MouseDownEvent ;
38
39
import com .google .gwt .event .dom .client .MouseDownHandler ;
39
40
import com .google .gwt .event .shared .HandlerRegistration ;
41
+ import com .google .gwt .user .client .Element ;
40
42
import com .google .gwt .user .client .ui .Composite ;
41
43
import com .google .gwt .user .client .ui .FlowPanel ;
42
44
import com .google .gwt .user .client .ui .HTML ;
@@ -80,6 +82,7 @@ public class VTwinColSelect extends Composite implements MultiSelectWidget,
80
82
private static final int VISIBLE_COUNT = 10 ;
81
83
82
84
private static final int DEFAULT_COLUMN_COUNT = 10 ;
85
+ private static int scheduledScrollToItem = -1 ;
83
86
84
87
private final DoubleClickListBox optionsListBox ;
85
88
@@ -327,15 +330,28 @@ public void setItems(List<JsonObject> items) {
327
330
328
331
private static void updateListBox (ListBox listBox ,
329
332
List <JsonObject > options ) {
333
+ List <String > selected = new ArrayList <String >();
334
+ // Retain right visible selection, see #11287
335
+ for (int i = 0 ; i < listBox .getItemCount (); ++i ) {
336
+ if (listBox .isItemSelected (i )) {
337
+ selected .add (listBox .getItemText (i ));
338
+ }
339
+ }
330
340
for (int i = 0 ; i < options .size (); i ++) {
331
341
final JsonObject item = options .get (i );
332
342
// reuse existing option if possible
343
+ String caption = MultiSelectWidget .getCaption (item );
333
344
if (i < listBox .getItemCount ()) {
334
- listBox .setItemText (i , MultiSelectWidget . getCaption ( item ) );
345
+ listBox .setItemText (i , caption );
335
346
listBox .setValue (i , MultiSelectWidget .getKey (item ));
336
347
} else {
337
- listBox .addItem (MultiSelectWidget .getCaption (item ),
338
- MultiSelectWidget .getKey (item ));
348
+ listBox .addItem (caption , MultiSelectWidget .getKey (item ));
349
+ }
350
+ boolean isSelected = selected .contains (caption );
351
+ listBox .setItemSelected (i , isSelected );
352
+ if (isSelected ) {
353
+ // Ensure that last selected item is visible
354
+ scrollToView (listBox ,i );
339
355
}
340
356
}
341
357
// remove extra
@@ -344,6 +360,19 @@ private static void updateListBox(ListBox listBox,
344
360
}
345
361
}
346
362
363
+ private static void scrollToView (ListBox listBox , int i ) {
364
+ if (scheduledScrollToItem == -1 ) {
365
+ scheduledScrollToItem = i ;
366
+ Scheduler .get ().scheduleDeferred (() -> {
367
+ Element el = (Element ) listBox .getElement ().getChild (scheduledScrollToItem );
368
+ el .scrollIntoView ();
369
+ scheduledScrollToItem = -1 ;
370
+ });
371
+ } else {
372
+ scheduledScrollToItem = i ;
373
+ }
374
+ }
375
+
347
376
private static boolean [] getSelectionBitmap (ListBox listBox ) {
348
377
final boolean [] selectedIndexes = new boolean [listBox .getItemCount ()];
349
378
for (int i = 0 ; i < listBox .getItemCount (); i ++) {
0 commit comments