Search in sources :

Example 1 with ValidationException

use of com.vaadin.data.ValidationException in project v-leaflet by mstahv.

the class JtsPointFieldTest method getTestComponent.

// private Polygon polygon;
@Override
public Component getTestComponent() {
    content.setMargin(true);
    display.setContentMode(ContentMode.PREFORMATTED);
    display.setCaption("Pojo state:");
    display.setValue(pojo.toString());
    HorizontalLayout horizontalLayout = new HorizontalLayout();
    horizontalLayout.setSizeFull();
    VerticalLayout editorform = new VerticalLayout();
    editorform.setSizeFull();
    editorform.setSpacing(true);
    editorform.setCaption("Edit JTS pojo:");
    TabSheet jtsFields = new TabSheet(point);
    jtsFields.setCaption("JTS fiels:");
    jtsFields.setSizeFull();
    editorform.addComponents(new HorizontalLayout(name, date), jtsFields);
    editorform.setExpandRatio(jtsFields, 1);
    final Binder<JtsPojo> beanBinder = new Binder<>(JtsPojo.class);
    beanBinder.readBean(pojo);
    beanBinder.bindInstanceFields(this);
    HorizontalLayout buttonLayout = new HorizontalLayout();
    buttonLayout.addComponent(new Button("Save", (ClickListener) event -> {
        try {
            beanBinder.writeBean(pojo);
            display.setValue(pojo.toString());
        } catch (ValidationException e) {
            System.err.println("Validation errors:" + Arrays.toString(e.getBeanValidationErrors().toArray()));
        }
    }));
    CheckBox roCheckBox = new CheckBox("Read only", false);
    roCheckBox.addValueChangeListener(event -> beanBinder.setReadOnly(event.getValue()));
    buttonLayout.addComponent(roCheckBox);
    buttonLayout.addComponent(new Button("Assign new empty bean", (ClickListener) event -> {
        pojo = new JtsPojo();
        beanBinder.readBean(pojo);
        display.setValue(pojo.toString());
    }));
    buttonLayout.addComponent(new Button("Assign bean with preset data", (ClickListener) event -> {
        pojo = new JtsPojo();
        pojo.setPoint(JTSUtil.toPoint(new org.vaadin.addon.leaflet.shared.Point(61, 22)));
        beanBinder.readBean(pojo);
        display.setValue(pojo.toString());
    }));
    editorform.addComponent(buttonLayout);
    horizontalLayout.addComponents(editorform, display);
    horizontalLayout.setExpandRatio(editorform, 1);
    horizontalLayout.setExpandRatio(display, 1);
    return horizontalLayout;
}
Also used : ValidationException(com.vaadin.data.ValidationException) Point(com.vividsolutions.jts.geom.Point) HorizontalLayout(com.vaadin.ui.HorizontalLayout) Binder(com.vaadin.data.Binder) Button(com.vaadin.ui.Button) TabSheet(com.vaadin.ui.TabSheet) CheckBox(com.vaadin.ui.CheckBox) VerticalLayout(com.vaadin.ui.VerticalLayout) ClickListener(com.vaadin.ui.Button.ClickListener)

Example 2 with ValidationException

use of com.vaadin.data.ValidationException in project charts by vaadin.

the class ChartWithExternalDataProviderWithChangingData method getChart.

@Override
protected Component getChart() {
    HorizontalLayout lo = new HorizontalLayout();
    VerticalLayout vlo = new VerticalLayout();
    DataProviderSeries<Data> ds = createChartDS();
    Component grid = createGrid();
    TextField field = new TextField("New data value");
    Binder<Data> binder = new Binder<>();
    binder.forField(field).withValidator((String v) -> v != null && !v.isEmpty(), "The field cannot be empty").withConverter(new StringToDoubleConverter("Not a double")).bind(Data::getValue, Data::setValue);
    binder.readBean(new Data(0.0));
    Button button = new Button("Add data", e -> {
        Data v = new Data(0.0);
        try {
            binder.writeBean(v);
            data.add(v);
            v = new Data(1.0);
            binder.readBean(v);
        } catch (ValidationException ve) {
        }
    });
    Component chart = createChart(ds);
    vlo.addComponents(field, button, grid);
    vlo.setSpacing(true);
    lo.addComponents(vlo, chart);
    grid.setSizeFull();
    chart.setSizeFull();
    lo.setSizeFull();
    lo.setExpandRatio(vlo, 1);
    lo.setExpandRatio(chart, 3);
    return lo;
}
Also used : Binder(com.vaadin.data.Binder) ValidationException(com.vaadin.data.ValidationException) Button(com.vaadin.ui.Button) StringToDoubleConverter(com.vaadin.data.converter.StringToDoubleConverter) VerticalLayout(com.vaadin.ui.VerticalLayout) TextField(com.vaadin.ui.TextField) Component(com.vaadin.ui.Component) HorizontalLayout(com.vaadin.ui.HorizontalLayout)

Aggregations

Binder (com.vaadin.data.Binder)2 ValidationException (com.vaadin.data.ValidationException)2 Button (com.vaadin.ui.Button)2 HorizontalLayout (com.vaadin.ui.HorizontalLayout)2 VerticalLayout (com.vaadin.ui.VerticalLayout)2 StringToDoubleConverter (com.vaadin.data.converter.StringToDoubleConverter)1 ClickListener (com.vaadin.ui.Button.ClickListener)1 CheckBox (com.vaadin.ui.CheckBox)1 Component (com.vaadin.ui.Component)1 TabSheet (com.vaadin.ui.TabSheet)1 TextField (com.vaadin.ui.TextField)1 Point (com.vividsolutions.jts.geom.Point)1