com.save.area.CreateNewAreaWindow.java Source code

Java tutorial

Introduction

Here is the source code for com.save.area.CreateNewAreaWindow.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.save.area;

import com.save.abstractclasses.AbstractWindow;
import com.save.service.AreaService;
import com.save.area.serviceprovider.AreaServiceImpl;
import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

/**
 *
 * @author jetdario
 */
public class CreateNewAreaWindow extends AbstractWindow {

    AreaService as = new AreaServiceImpl();

    public CreateNewAreaWindow() {
        setCaption("CREATE NEW AREA");
        setWidth("300px");
        setResizable(false);

        setContent(getVlayout());
        getContent().setHeightUndefined();
    }

    VerticalLayout getVlayout() {
        VerticalLayout vlayout = new VerticalLayout();
        vlayout.setSizeFull();
        vlayout.setMargin(true);
        vlayout.setSpacing(true);

        final TextField areaField = new TextField("Create Area: ");
        areaField.setWidth("100%");
        areaField.setRequired(true);
        areaField.setRequiredError("*Required Field");
        vlayout.addComponent(areaField);

        Button createBtn = new Button("CREATE NEW AREA");
        createBtn.setWidth("100%");
        createBtn.addClickListener(new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (as.isAreaExist(areaField.getValue().trim().toLowerCase())) {
                    Notification.show("Area already Exist!", Notification.Type.WARNING_MESSAGE);
                    return;
                }

                if (areaField.getValue() == null || areaField.getValue().trim().isEmpty()) {
                    Notification.show("Area Field cannot be empty!", Notification.Type.ERROR_MESSAGE);
                    return;
                }

                boolean result = as.createNewArea(areaField.getValue().trim().toLowerCase());
                if (result) {
                    Notification.show("Done!");
                    close();
                }
            }
        });
        createBtn.setImmediate(true);

        vlayout.addComponent(createBtn);

        return vlayout;
    }

}