From acab3d3c0c8eda303a8aa331ba5b17d38164542a Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Wed, 3 Feb 2016 14:19:25 +0200 Subject: Add simple ListBox component to use with the DataProvider This patch changes the DummyDataProvider test to use the ListBox storing runnables instead of Buttons with actions. Change-Id: Ie3831ccbd397eb2c145cf2c036fa5e512dcfa230 --- server/src/com/vaadin/ui/proto/ListBox.java | 145 ++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 server/src/com/vaadin/ui/proto/ListBox.java (limited to 'server/src') diff --git a/server/src/com/vaadin/ui/proto/ListBox.java b/server/src/com/vaadin/ui/proto/ListBox.java new file mode 100644 index 0000000000..bc952c1617 --- /dev/null +++ b/server/src/com/vaadin/ui/proto/ListBox.java @@ -0,0 +1,145 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.proto; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +import com.vaadin.server.communication.data.typed.CollectionDataSource; +import com.vaadin.server.communication.data.typed.DataProvider; +import com.vaadin.server.communication.data.typed.DataSource; +import com.vaadin.server.communication.data.typed.TypedDataGenerator; +import com.vaadin.shared.ui.proto.listbox.ListBoxConstants; +import com.vaadin.shared.ui.proto.listbox.ListBoxSelectRpc; +import com.vaadin.ui.AbstractComponent; + +import elemental.json.JsonObject; + +/** + * ListBox is a simple select component typed to a data type. + * + * @since + */ +public class ListBox extends AbstractComponent { + + private static class DefaultNameProvider implements NameProvider { + @Override + public String getName(T value) { + return value.toString(); + } + } + + public interface NameProvider extends Serializable { + String getName(T value); + } + + public interface ValueChange extends Serializable { + void valueChange(T value); + } + + private T selected; + private DataProvider dataProvider; + private Set> listeners = new LinkedHashSet>(); + + public ListBox(Collection data) { + this(new CollectionDataSource(data), new DefaultNameProvider()); + } + + public ListBox(DataSource data) { + this(data, new DefaultNameProvider()); + } + + public ListBox(Collection data, NameProvider nameProvider) { + this(new CollectionDataSource(data), nameProvider); + } + + public ListBox(DataSource data, NameProvider nameProvider) { + setDataSource(data, nameProvider); + registerRpc(new ListBoxSelectRpc() { + + @Override + public void select(String key) { + if (key != null && !key.isEmpty()) { + // Key mapper gives the object represented by the given + // key communicated to the client-side + selected = dataProvider.getKeyMapper().get(key); + fireSelectionChange(selected); + } else { + selected = null; + } + } + }); + } + + public ListBox() { + this(new CollectionDataSource(new ArrayList()), + new DefaultNameProvider()); + + } + + public void setDataSource(Collection data) { + setDataSource(data, new DefaultNameProvider()); + } + + public void setDataSource(DataSource data) { + setDataSource(data, new DefaultNameProvider()); + } + + public void setDataSource(Collection data, NameProvider nameProvider) { + setDataSource(new CollectionDataSource(data), nameProvider); + } + + public void setDataSource(DataSource data, + final NameProvider nameProvider) { + dataProvider = DataProvider.create(data, this); + dataProvider.addDataGenerator(new TypedDataGenerator() { + + @Override + public void generateData(T bean, JsonObject rowData) { + rowData.put(ListBoxConstants.NAME_KEY, + nameProvider.getName(bean)); + } + + @Override + public void destroyData(T bean) { + // No data needs to be destroyed. + } + }); + } + + public void addValueChangeListener(ValueChange listener) { + listeners.add(listener); + } + + public void removeValueChangeListener(ValueChange listener) { + listeners.remove(listener); + } + + public T getSelected() { + return selected; + } + + protected void fireSelectionChange(T selected) { + List> set = new ArrayList>(listeners); + for (ValueChange listener : set) { + listener.valueChange(selected); + } + } +} -- cgit v1.2.3