package com.teamlans.lepta.view.bill.edit.component.items;

import com.teamlans.lepta.entities.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;

import java.util.ArrayList;
import java.util.List;

public class ItemTable extends Table {

  private BeanItemContainer<Item> itemContainer;

  public ItemTable() {
    setImmediate(true);
    itemContainer = new BeanItemContainer<>(Item.class);
    itemContainer.addAll(new ArrayList<>());
    setContainerDataSource(itemContainer);
    addGeneratedColumn("test", new DeleteButtonGenerator());
    setVisibleColumns("name", "price", "test");
  }

  public List<Item> getItems() {
    return itemContainer.getItemIds();
  }

  public void addItems(List<Item> items) {
    itemContainer.addAll(items);
  }

  public void addItem(Item item) {
    itemContainer.addItem(item);
  }

  private class DeleteButtonGenerator implements ColumnGenerator {
    @Override
    public Object generateCell(Table table, Object itemId, Object columnId) {
      Button button = new Button(itemId == table.getData() ? "Save" : "Edit");
      button.addClickListener((Button.ClickListener) event -> {
        if (table.getData() == null) {
          table.setData(itemId);
          table.refreshRowCache();
        } else if (itemId == table.getData()) {
          table.setData(null);
          table.refreshRowCache();
        }
      });
      return button;
    }
  }

  private class EditButtonGenerator implements ColumnGenerator {
    @Override
    public Object generateCell(Table table, Object itemId, Object columnId) {
      Button button = new Button(itemId == table.getData() ? "Save" : "Edit");
      return button;
    }
  }
}

@@ -94,16 +94,50 @@
 s.Item;%0A
+import com.vaadin.data.Container;%0A
 import c
@@ -225,21 +225,228 @@
 adin.ui.
-Table
+Component;%0Aimport com.vaadin.ui.DefaultFieldFactory;%0Aimport com.vaadin.ui.Field;%0Aimport com.vaadin.ui.ListSelect;%0Aimport com.vaadin.ui.Table;%0Aimport com.vaadin.ui.TableFieldFactory;%0Aimport com.vaadin.ui.TextField
 ;%0A%0Aimpor
@@ -523,21 +523,26 @@
 extends 
-Table
+ListSelect
  %7B%0A%0A  pr
@@ -614,32 +614,8 @@
 ) %7B%0A
-    setImmediate(true);%0A
     
@@ -759,117 +759,8 @@
 r);%0A
-    addGeneratedColumn(%22test%22, new DeleteButtonGenerator());%0A    setVisibleColumns(%22name%22, %22price%22, %22test%22);%0A
   %7D%0A
@@ -1032,32 +1032,38 @@
 ator implements 
+Table.
 ColumnGenerator 
@@ -1148,32 +1148,55 @@
 ect columnId) %7B%0A
+      setData(itemId);%0A
       Button but
@@ -1216,239 +1216,641 @@
 ton(
-itemId == table.getData() ? %22Save%22 : %22Edit%22);%0A      button.addClickListener((Button.ClickListener) event -%3E %7B%0A        if (table.getData() == null) %7B%0A          table.setData(itemId);%0A          table.refreshRowCache();%0A        %7D else
+%22X%22);%0A      button.addClickListener(clickEvent -%3E table.removeItem(getData()));%0A      return button;%0A    %7D%0A  %7D%0A%0A  private class EditButtonGenerator implements Table.ColumnGenerator %7B%0A    @Override%0A    public Object generateCell(Table table, Object itemId, Object columnId) %7B%0A      Button button = new Button(itemId == getData() ? %22Save%22 : %22Edit%22);%0A      button.addClickListener(event -%3E editTable(event));%0A      return button;%0A    %7D%0A  %7D%0A%0A  private class CustomFieldFactory implements TableFieldFactory %7B%0A    @Override%0A    public Field%3C?%3E createField(Container container, Object itemId, Object propertyId, Component uiContext) %7B%0A     
  if 
@@ -1852,38 +1852,32 @@
   if (itemId == 
-table.
 getData()) %7B%0A   
@@ -1885,76 +1885,104 @@
     
-  table.setData(null);%0A          table.refreshRowCache(
+return DefaultFieldFactory.get().createField(container, itemId, propertyId, uiContext
 );%0A
-  
       %7D%0A
     
@@ -1981,18 +1981,8 @@
   %7D%0A
-      %7D);%0A
     
@@ -1982,38 +1982,36 @@
  %7D%0A      return 
-button
+null
 ;%0A    %7D%0A  %7D%0A%0A  p
@@ -2021,67 +2021,110 @@
 ate 
-class EditButtonGenerator implements ColumnGenerator
+void editTable(final Button.ClickEvent ce) %7B%0A    setTableFieldFactory(new TableFieldFactory()
  %7B%0A    
+  
 @Ove
@@ -2137,161 +2137,328 @@
     
+  
 public 
-Object generateCell(Table table, Object itemId, Object columnId) %7B%0A      Button button = new Button(itemId == table.getData() ? %22Save%22 : %22Edit%22);%0A
+Field createField(Container container, Object itemId, Object propertyId, Component uiContext) %7B%0A          if (propertyId.toString().equals(%22expDir%22)) %7B%0A            return new TextField();%0A          %7D%0A          if (propertyId.toString().equals(%22participant%22)) %7B%0A            return new TextField();%0A          %7D%0A  
     
@@ -2466,28 +2466,61 @@
  return 
-button
+null
 ;%0A    
+  
 %7D%0A  
+ 
-%7D
+ %7D);%0A    setEditable(true);%0A  %7D%0A%0A
 %0A%7D%0A

Improve Item table