to.hacklab.cardmanager.MyVaadinApplication.java Source code

Java tutorial

Introduction

Here is the source code for to.hacklab.cardmanager.MyVaadinApplication.java

Source

/*
 * Copyright 2009 IT Mill 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 to.hacklab.cardmanager;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.data.Property;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import to.hacklab.cardmanager.model.Card;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
@Title("Hacklab Card Manager")
@Theme("valo")
public class MyVaadinApplication extends UI {
    //    private Window window;
    private final Logger log = LoggerFactory.getLogger(MyVaadinApplication.class);

    @Override
    public void init(VaadinRequest request) {
        VerticalLayout layout = new VerticalLayout();
        setContent(layout);
        List<Card> cardList = new ArrayList<>();
        try {
            cardList = CardApi.getCards();
        } catch (IOException e) {
            Notification.show("Unable to load cards: " + e.getMessage());
        }

        final BeanItemContainer<Card> beanItemContainer = new BeanItemContainer<>(Card.class, cardList);

        final Table table = new Table("Cards");

        table.setContainerDataSource(beanItemContainer);
        table.setSortContainerPropertyId("name");
        table.setSelectable(true);
        table.setImmediate(true);

        layout.addComponent(table);

        CardForm cardForm = new CardForm();
        layout.addComponent(cardForm);

        FieldGroup fieldGroup = new FieldGroup(new BeanItem(new Card("Cow", "Moo")));
        fieldGroup.bindMemberFields(cardForm);
        fieldGroup.addCommitHandler(new FieldGroup.CommitHandler() {
            @Override
            public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {

            }

            @Override
            public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
                // /send the form values!
            }
        });

        table.addValueChangeListener(
                event -> fieldGroup.setItemDataSource(beanItemContainer.getItem(table.getValue())));

        final Button saveButton = new Button("Save");
        final Button discardButton = new Button("Discard");
        layout.addComponent(saveButton);
        layout.addComponent(discardButton);
        discardButton.addClickListener(event -> fieldGroup.discard());
        saveButton.addClickListener(event -> {
            try {
                fieldGroup.commit();
            } catch (FieldGroup.CommitException e) {
                new Notification("Couldn't save: " + e.getMessage()).show(Page.getCurrent());
            }
        });

    }

}