package com.example.DemoProject;
import com.vaadin.server.ThemeResource;
public class Company {
private int serialNo;
private String name;
private RatingStars rtstars;
private ThemeResource rsrc;
public Company() {
}
public Company(int serialNo, String name, RatingStars rtstars) {
this.serialNo = serialNo;
this.name = name;
this.rtstars = rtstars;
}
public ThemeResource getRsrc() {
return rsrc;
}
public void setRsrc(ThemeResource rsrc) {
this.rsrc = rsrc;
}
public Company(int serialNo, String name, RatingStars rtstars, ThemeResource rsrc) {
this.serialNo = serialNo;
this.name = name;
this.rtstars = rtstars;
this.rsrc = rsrc;
}
public RatingStars getRtstars() {
return rtstars;
}
public void setRtstars(RatingStars rtstars) {
this.rtstars = rtstars;
}
public Company(int serialNo, String name) {
this.serialNo = serialNo;
this.name = name;
}
public int getSerialNo() {
return serialNo;
}
public void setSerialNo(int serialNo) {
this.serialNo = serialNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
AND THE UI
package com.example.DemoProject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.vaadin.annotations.PreserveOnRefresh;
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.navigator.View;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.dnd.DropEffect;
import com.vaadin.shared.ui.dnd.EffectAllowed;
import com.vaadin.shared.ui.grid.DropMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.components.grid.GridDragSource;
import com.vaadin.ui.components.grid.GridDropTarget;
import com.vaadin.ui.renderers.ImageRenderer;
@PreserveOnRefresh
public class CompanyRating extends HorizontalLayout implements View {
/**
*
*/
private static final long serialVersionUID = 1L;
private ListDataProvider<Company> dataProvider;
private List<Company> company;
protected Grid<Company> grid = new Grid<>();
private Optional<Company> dragged;
public HorizontalLayout hlytbase = new HorizontalLayout();
private Grid<Company> childgrid = new Grid<>();
private ListDataProvider<Company> dp;
TextField name = new TextField();
TextField srno = new TextField();
Button btn = new com.vaadin.ui.Button("ADD");
private List<Company> items;
public CompanyRating() {
srno.setPlaceholder("enter Serial no");
name.setPlaceholder("enter name");
company = new ArrayList<>();
company.add(new Company(1, "Dominoes", new RatingStars()));
company.add(new Company(2, "Google", new RatingStars()));
company.add(new Company(3, "Yahoo", new RatingStars()));
company.add(new Company(4, "Ford", new RatingStars()));
company.add(new Company(5, "Nissan", new RatingStars()));
company.add(new Company(6, "BMW", new RatingStars()));
dataProvider = new ListDataProvider<>(company);
Map<Company, TextField> textFields = new HashMap<>();
GridDragSource<Company> dragSource = new GridDragSource<>(grid);
dragSource.setEffectAllowed(EffectAllowed.MOVE);
dragSource.setDragDataGenerator("text", person -> {
return person.getName() + " " + person.getSerialNo();
});
dragSource.addGridDragStartListener(event -> {
List<Company> list = new ArrayList<>(event.getDraggedItems());
if (!list.isEmpty()) {
dragged = Optional.of(list.get(0));
}
});
dragSource.addGridDragEndListener(event -> {
if (event.getDropEffect() == DropEffect.MOVE) {
grid.getDataProvider().refreshAll();
}
});
GridDropTarget<Company> dropTarget = new GridDropTarget<>(grid, DropMode.ON_TOP);
dropTarget.setDropEffect(DropEffect.MOVE);
dropTarget.addGridDropListener(event -> {
Optional<Company> dropTargetRow = event.getDropTargetRow();
if (dropTargetRow.isPresent() && dragged.isPresent()) {
Company drag = dragged.get();
Company drop = dropTargetRow.get();
Collections.swap(company, company.indexOf(drag), company.indexOf(drop));
dataProvider.refreshAll();
}
});
grid.addColumn(Company::getSerialNo).setCaption("Serial Number").setWidth(50);
grid.addColumn(Company::getName).setCaption("Name").setWidth(100);
grid.addComponentColumn(Company::getRtstars).setCaption("Rate It");
Column<Company, ThemeResource> imageColumn = grid.addColumn(
p -> new ThemeResource(p.getName() + ".jpg"),
new ImageRenderer<Company>()).setCaption("Image");
grid.setDataProvider(dataProvider);
btn.addClickListener(e -> {
if (!srno.getValue().isEmpty()) {
company.add(new Company(Integer.parseInt(srno.getValue()), name.getValue(),
new RatingStars()));
dataProvider.refreshAll();
}
});
addComponents(srno, name, btn, grid);
items = new ArrayList<>();
dp = new ListDataProvider<>(items);
childgrid.addColumn(Company::getSerialNo).setCaption("Number");
childgrid.addColumn(Company::getName).setCaption("Company");
childgrid.setDataProvider(dp);
addComponent(childgrid);
childgrid.setVisible(false);
grid.addItemClickListener(e -> {
items.clear();
Company company = e.getItem();
if (company != null) {
childgrid.setVisible(true);
items.add(company);
} else {
childgrid.setVisible(false);
}
dp.refreshAll();
});
}
}
Comments
Post a Comment