jp.primecloud.auto.ui.MyCloudAdd.java Source code

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.ui.MyCloudAdd.java

Source

/*
 * Copyright 2014 by SCSK Corporation.
 * 
 * This file is part of PrimeCloud Controller(TM).
 * 
 * PrimeCloud Controller(TM) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * PrimeCloud Controller(TM) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>.
 */
package jp.primecloud.auto.ui;

import java.util.List;

import jp.primecloud.auto.exception.AutoApplicationException;
import jp.primecloud.auto.service.FarmService;
import jp.primecloud.auto.service.TemplateService;
import jp.primecloud.auto.service.dto.TemplateDto;
import jp.primecloud.auto.ui.DialogConfirm.Buttons;
import jp.primecloud.auto.ui.DialogConfirm.Callback;
import jp.primecloud.auto.ui.DialogConfirm.Result;
import jp.primecloud.auto.ui.util.BeanContext;
import jp.primecloud.auto.ui.util.ContextUtils;
import jp.primecloud.auto.ui.util.Icons;
import jp.primecloud.auto.ui.util.VaadinUtils;
import jp.primecloud.auto.ui.util.ViewContext;
import jp.primecloud.auto.ui.util.ViewMessages;
import jp.primecloud.auto.ui.util.ViewProperties;
import com.vaadin.Application;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

/**
 * <p>
 * myCloud???????
 * </p>
 *
 */
@SuppressWarnings("serial")
public class MyCloudAdd extends Window {
    final String COLUMN_HEIGHT = "30px";

    Application apl;

    TextField cloudNameField;

    TextField commentField;

    Table templateTable;

    List<TemplateDto> templates;

    MyCloudAdd(Application ap) {
        apl = ap;

        //
        setIcon(Icons.ADD.resource());
        setCaption(ViewProperties.getCaption("window.myCloudAdd"));
        setModal(true);
        setWidth("550px");

        VerticalLayout layout = (VerticalLayout) getContent();
        layout.setMargin(false, true, false, true);
        layout.setSpacing(true);

        // 
        layout.addComponent(new BasicTab());

        // ??
        HorizontalLayout okbar = new HorizontalLayout();
        okbar.setSpacing(true);
        okbar.setMargin(false, false, true, false);
        //okbar.setWidth("100%");
        layout.addComponent(okbar);
        layout.setComponentAlignment(okbar, Alignment.BOTTOM_RIGHT);

        // Add
        Button addButton = new Button(ViewProperties.getCaption("button.add"));
        addButton.setDescription(ViewProperties.getCaption("description.mycloud.add"));
        addButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                MyCloudAdd.this.addButtonClick(event);
            }
        });
        okbar.addComponent(addButton);

        // Cancel
        Button cancelButton = new Button(ViewProperties.getCaption("button.cancel"));
        cancelButton.setDescription(ViewProperties.getCaption("description.cansel"));
        cancelButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                MyCloudAdd.this.close();
            }
        });
        okbar.addComponent(cancelButton);

        // ??
        initValidation();

        // ???
        initData();

        // 
        showTemplates();
    }

    private class BasicTab extends Form {
        Label ldlTemplate = new Label();
        Label ldlDesciption = new Label();

        BasicTab() {
            // ??
            cloudNameField = new TextField(ViewProperties.getCaption("field.cloudName"));
            getLayout().addComponent(cloudNameField);

            // 
            commentField = new TextField(ViewProperties.getCaption("field.comment"));
            commentField.setWidth("100%");
            getLayout().addComponent(commentField);

            // ?
            templateTable = new SelectTemplateTable();
            getLayout().addComponent(templateTable);

            // 
            Panel descTemplate = new Panel();
            CssLayout lay = new CssLayout();
            lay.addStyleName("template-desc");
            descTemplate.setHeight("80px");
            descTemplate.setContent(lay);
            lay.setSizeFull();
            lay.addComponent(ldlTemplate);
            lay.addComponent(ldlDesciption);
            getLayout().addComponent(descTemplate);

            templateTable.addListener(new Table.ValueChangeListener() {
                @Override
                public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
                    TemplateDto template = (TemplateDto) templateTable.getValue();
                    ldlTemplate.setValue(template.getTemplate().getTemplateNameDisp() + "");
                    ldlDesciption.setValue(template.getTemplate().getTemplateDescriptionDisp());
                }

            });

            cloudNameField.focus();

        }
    }

    private class SelectTemplateTable extends Table {
        SelectTemplateTable() {
            //
            setWidth("100%");

            addStyleName("win-mycloud-add-temp");
            setCaption(ViewProperties.getCaption("table.selectTemplate"));
            setPageLength(4);
            setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);
            setSortDisabled(true);
            setColumnReorderingAllowed(false);
            setColumnCollapsingAllowed(false);
            setSelectable(true);
            setMultiSelect(false);
            setNullSelectionAllowed(false);
            setImmediate(true);

            //
            addContainerProperty("No", Integer.class, null);
            addContainerProperty("Name", Label.class, new Label());
            setColumnExpandRatio("Name", 100);

            //????StyleName
            setCellStyleGenerator(new Table.CellStyleGenerator() {
                public String getStyle(Object itemId, Object propertyId) {

                    if (propertyId == null) {
                        return "";
                    } else {
                        return propertyId.toString().toLowerCase();
                    }
                }
            });
        }
    }

    private void initValidation() {
        String message = ViewMessages.getMessage("IUI-000002");
        cloudNameField.setRequired(true);
        cloudNameField.setRequiredError(message);
        cloudNameField.addValidator(new StringLengthValidator(message, 1, 15, false));
        cloudNameField.addValidator(new RegexpValidator("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", true, message));

        message = ViewMessages.getMessage("IUI-000003");
        commentField.addValidator(new StringLengthValidator(message, -1, 100, true));
    }

    private void initData() {
        // ?
        Long userNo = ViewContext.getUserNo();
        TemplateService templateService = BeanContext.getBean(TemplateService.class);
        templates = templateService.getTemplates(userNo);
    }

    private void showTemplates() {
        templateTable.removeAllItems();

        // ????
        for (int i = 0; i < templates.size(); i++) {
            TemplateDto template = templates.get(i);

            // ??
            String name = template.getTemplate().getTemplateNameDisp();
            Icons nameIcon = Icons.CUSTOM;
            Label slbl = new Label(
                    "<img src=\"" + VaadinUtils.getIconPath(apl, nameIcon) + "\"><div>" + name + "</div>",
                    Label.CONTENT_XHTML);
            slbl.setHeight(COLUMN_HEIGHT);

            templateTable.addItem(new Object[] { (i + 1), slbl }, template);
        }

        // ???
        templateTable.select(templateTable.firstItemId());
    }

    private void addButtonClick(ClickEvent event) {

        // ?
        String cloudName = (String) cloudNameField.getValue();
        //        String comment = (String) commentField.getValue();
        TemplateDto template = (TemplateDto) templateTable.getValue();

        // ?
        try {
            cloudNameField.validate();
            commentField.validate();
        } catch (InvalidValueException e) {
            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), e.getMessage());
            getApplication().getMainWindow().addWindow(dialog);
            return;
        }
        if (template == null) {
            // ????????
            DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"),
                    ViewMessages.getMessage("IUI-000004"));
            getApplication().getMainWindow().addWindow(dialog);
            return;
        }

        //???
        String diagMessage = ViewMessages.getMessage("IUI-000040", cloudName);
        DialogConfirm dialogConfirm = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), diagMessage,
                Buttons.OKCancel);
        dialogConfirm.setCallback(new Callback() {
            @Override
            public void onDialogResult(Result result) {
                if (result != Result.OK) {
                    return;
                }
                String cloudName = (String) cloudNameField.getValue();
                String comment = (String) commentField.getValue();
                TemplateDto template = (TemplateDto) templateTable.getValue();

                //??????????UserNo??
                if (ViewContext.getPowerUser()) {
                    ViewContext.setUserNo(ViewContext.getPowerDefaultMaster());
                }
                // ?
                Long userNo = ViewContext.getUserNo();

                // 
                FarmService farmService = BeanContext.getBean(FarmService.class);
                Long farmNo;
                try {
                    farmNo = farmService.createFarm(userNo, cloudName, comment);
                } catch (AutoApplicationException e) {
                    String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                    DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                    getApplication().getMainWindow().addWindow(dialog);
                    return;
                }

                //
                AutoApplication aapl = (AutoApplication) apl;
                aapl.doOpLog("CLOUD", "Make Cloud", farmNo, null);

                // ?
                TemplateService templateService = BeanContext.getBean(TemplateService.class);
                try {
                    templateService.applyTemplate(farmNo, template.getTemplate().getTemplateNo());
                } catch (AutoApplicationException e) {
                    String message = ViewMessages.getMessage(e.getCode(), e.getAdditions());
                    DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.error"), message);
                    getApplication().getMainWindow().addWindow(dialog);
                    return;
                }

                // ?????
                ContextUtils.setAttribute("newfarmNo", farmNo);
                ContextUtils.setAttribute("newfarmName", cloudName);

                // ??
                close();

            }
        });
        getApplication().getMainWindow().addWindow(dialogConfirm);

    }

}