import java.util.function.BiConsumer;
import java.util.stream.Collectors;
+import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
private static final int VISIBLE_COUNT = 10;
private static final int DEFAULT_COLUMN_COUNT = 10;
+ private static int scheduledScrollToItem = -1;
private final DoubleClickListBox optionsListBox;
private static void updateListBox(ListBox listBox,
List<JsonObject> options) {
+ List<String> selected = new ArrayList<String>();
+ // Retain right visible selection, see #11287
+ for (int i = 0; i < listBox.getItemCount(); ++i) {
+ if (listBox.isItemSelected(i)) {
+ selected.add(listBox.getItemText(i));
+ }
+ }
for (int i = 0; i < options.size(); i++) {
final JsonObject item = options.get(i);
// reuse existing option if possible
+ String caption = MultiSelectWidget.getCaption(item);
if (i < listBox.getItemCount()) {
- listBox.setItemText(i, MultiSelectWidget.getCaption(item));
+ listBox.setItemText(i, caption);
listBox.setValue(i, MultiSelectWidget.getKey(item));
} else {
- listBox.addItem(MultiSelectWidget.getCaption(item),
- MultiSelectWidget.getKey(item));
+ listBox.addItem(caption, MultiSelectWidget.getKey(item));
+ }
+ boolean isSelected = selected.contains(caption);
+ listBox.setItemSelected(i, isSelected);
+ if (isSelected) {
+ // Ensure that last selected item is visible
+ scrollToView(listBox,i);
}
}
// remove extra
}
}
+ private static void scrollToView(ListBox listBox, int i) {
+ if (scheduledScrollToItem == -1) {
+ scheduledScrollToItem = i;
+ Scheduler.get().scheduleDeferred(() -> {
+ Element el = (Element) listBox.getElement().getChild(scheduledScrollToItem);
+ el.scrollIntoView();
+ scheduledScrollToItem = -1;
+ });
+ } else {
+ scheduledScrollToItem = i;
+ }
+ }
+
private static boolean[] getSelectionBitmap(ListBox listBox) {
final boolean[] selectedIndexes = new boolean[listBox.getItemCount()];
for (int i = 0; i < listBox.getItemCount(); i++) {