parent
922c1fd143
commit
f6cf18ce31
@ -0,0 +1,75 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
import java.util.function.Consumer; |
||||
|
||||
import com.vaadin.data.Binder; |
||||
import com.vaadin.data.provider.DataProvider; |
||||
import com.vaadin.data.provider.ListDataProvider; |
||||
import com.vaadin.icons.VaadinIcons; |
||||
import com.vaadin.ui.Button; |
||||
import com.vaadin.ui.Component; |
||||
import com.vaadin.ui.Grid; |
||||
import com.vaadin.ui.components.grid.Editor; |
||||
|
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||
|
||||
public abstract class AbstractDataGrid<T> extends Grid<T> { |
||||
|
||||
private static final long serialVersionUID = -3404971410481135696L; |
||||
private final List<T> contactSource; |
||||
private final ListDataProvider<T> contactDataProvider; |
||||
private boolean hasChanges; |
||||
private Column<T, Component> deleteButtonColumn; |
||||
private Consumer<T> deleteConsumer; |
||||
|
||||
public AbstractDataGrid() { |
||||
|
||||
this.contactSource = new ArrayList<>(); |
||||
this.contactDataProvider = DataProvider.ofCollection(contactSource); |
||||
setDataProvider(contactDataProvider); |
||||
|
||||
Editor<T> editor = getEditor(); |
||||
editor.setEnabled(true); |
||||
Binder<T> binder = editor.getBinder(); |
||||
editor.addSaveListener(ev -> hasChanges = true); |
||||
|
||||
createColumnAndBinding(binder); |
||||
|
||||
deleteButtonColumn = addComponentColumn(c -> { |
||||
Button deleteButton = new Button(VaadinIcons.TRASH); |
||||
deleteButton.addClickListener(ev -> deleteConsumer.accept(c)); |
||||
return deleteButton; |
||||
}); |
||||
deleteButtonColumn.setHidden(true); |
||||
} |
||||
|
||||
protected abstract void createColumnAndBinding(Binder<T> binder); |
||||
|
||||
public void setDeleteConsumer(Consumer<T> deleteConsumer) { |
||||
this.deleteConsumer = deleteConsumer; |
||||
if (deleteConsumer != null) { |
||||
deleteButtonColumn.setHidden(false); |
||||
} else { |
||||
deleteButtonColumn.setHidden(true); |
||||
} |
||||
} |
||||
|
||||
public final void setPerson(Person person) { |
||||
hasChanges = false; |
||||
contactSource.clear(); |
||||
if (person != null) { |
||||
contactSource.addAll(readValues(person)); |
||||
} |
||||
contactDataProvider.refreshAll(); |
||||
} |
||||
|
||||
protected abstract Collection<? extends T> readValues(Person person); |
||||
|
||||
public final boolean hasChanges() { |
||||
return hasChanges; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,35 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import com.vaadin.data.Binder; |
||||
import com.vaadin.data.Binder.Binding; |
||||
import com.vaadin.ui.TextField; |
||||
|
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Contact; |
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||
|
||||
public class ContactGrid extends AbstractDataGrid<Contact> { |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
private static final long serialVersionUID = -2573761302198992085L; |
||||
|
||||
@Override |
||||
public void createColumnAndBinding(Binder<Contact> binder) { |
||||
Binding<Contact, String> typeBinding = binder.bind(new ContactTypeComponent(), Contact::getType, |
||||
Contact::setType); |
||||
Binding<Contact, String> valueBinding = binder.bind(new TextField(), Contact::getValue, Contact::setValue); |
||||
|
||||
addColumn(Contact::getType).setCaption("Kontaktart").setEditorBinding(typeBinding); |
||||
addColumn(Contact::getValue).setCaption("Wert").setEditorBinding(valueBinding); |
||||
} |
||||
|
||||
@Override |
||||
protected Collection<? extends Contact> readValues(Person person) { |
||||
return person.getContacts().stream().filter(e -> e.getDeleted() == null).collect(Collectors.toList()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,33 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.components; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
import com.vaadin.data.Binder; |
||||
|
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.PersonDao; |
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Relation; |
||||
|
||||
public class RelationComponent extends AbstractDataGrid<Relation> { |
||||
|
||||
private static final long serialVersionUID = 7813969695936351799L; |
||||
private PersonDao dao; |
||||
|
||||
public RelationComponent(PersonDao dao) { |
||||
this.dao = dao; |
||||
getEditor().setEnabled(false); |
||||
} |
||||
|
||||
@Override |
||||
protected void createColumnAndBinding(Binder<Relation> binder) { |
||||
addColumn(r -> r.getRelation().getLocalized()).setCaption("Beiehung"); |
||||
addColumn(r -> r.getPerson().getPrename() + " " + r.getPerson().getSurname()); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected Collection<? extends Relation> readValues(Person person) { |
||||
return dao.findRelationsFor(person); |
||||
} |
||||
|
||||
} |
||||
|
After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 1.9 KiB |
Loading…
Reference in new issue