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

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.ui.ServiceButtonsBottom.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 jp.primecloud.auto.common.status.ComponentInstanceStatus;
import jp.primecloud.auto.common.status.ComponentStatus;
import jp.primecloud.auto.service.ComponentService;
import jp.primecloud.auto.service.dto.ComponentDto;
import jp.primecloud.auto.service.dto.ComponentInstanceDto;
import jp.primecloud.auto.ui.DialogConfirm.Buttons;
import jp.primecloud.auto.ui.DialogConfirm.Result;
import jp.primecloud.auto.ui.util.BeanContext;
import jp.primecloud.auto.ui.util.Icons;
import jp.primecloud.auto.ui.util.OperationLogger;
import jp.primecloud.auto.ui.util.ViewMessages;
import jp.primecloud.auto.ui.util.ViewProperties;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.CloseEvent;

@SuppressWarnings("serial")
public class ServiceButtonsBottom extends CssLayout {

    private MainView sender;

    private Button addButton;

    private Button editButton;

    private Button deleteButton;

    public ServiceButtonsBottom(MainView sender) {
        this.sender = sender;
    }

    @Override
    public void attach() {
        setWidth("100%");
        setMargin(true);
        addStyleName("service-buttons");

        // Add
        addButton = new Button(ViewProperties.getCaption("button.addService"));
        addButton.setDescription(ViewProperties.getCaption("description.addService"));
        addButton.setIcon(Icons.ADD.resource());
        addButton.addStyleName("left");
        addButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                addButtonClick(event);
            }
        });
        addComponent(addButton);

        // 
        Label spacer = new Label(" ", Label.CONTENT_XHTML);
        spacer.setWidth("30px");
        spacer.addStyleName("left");
        addComponent(spacer);

        // Edit
        editButton = new Button(ViewProperties.getCaption("button.editService"));
        editButton.setDescription(ViewProperties.getCaption("description.editService"));
        editButton.setWidth("90px");
        editButton.setIcon(Icons.EDITMINI.resource());
        editButton.addStyleName("left");
        editButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                editButtonClick(event);
            }
        });
        addComponent(editButton);

        // Delete
        deleteButton = new Button(ViewProperties.getCaption("button.deleteService"));
        deleteButton.setDescription(ViewProperties.getCaption("description.deleteService"));
        deleteButton.setWidth("90px");
        deleteButton.setIcon(Icons.DELETEMINI.resource());
        deleteButton.addStyleName("left");
        deleteButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                deleteButtonClick(event);
            }
        });
        addComponent(deleteButton);

        initialize();
    }

    public void initialize() {
        addButton.setEnabled(true);
        editButton.setEnabled(false);
        deleteButton.setEnabled(false);
    }

    public void refresh(ComponentDto component) {
        // ?????
        ComponentStatus status = ComponentStatus.fromStatus(component.getStatus());

        // Edit
        if (status == ComponentStatus.STOPPED || status == ComponentStatus.RUNNING) {
            editButton.setEnabled(true);
        } else if (status == ComponentStatus.WARNING) {
            // ????????
            boolean processing = false;
            for (ComponentInstanceDto componentInstance : component.getComponentInstances()) {
                ComponentInstanceStatus status2 = ComponentInstanceStatus
                        .fromStatus(componentInstance.getComponentInstance().getStatus());
                if (status2 != ComponentInstanceStatus.RUNNING && status2 != ComponentInstanceStatus.WARNING
                        && status2 != ComponentInstanceStatus.STOPPED) {
                    processing = true;
                    break;
                }
            }
            editButton.setEnabled(!processing);
        } else {
            editButton.setEnabled(false);
        }

        // Delete
        if (status == ComponentStatus.STOPPED) {
            deleteButton.setEnabled(true);
        } else {
            deleteButton.setEnabled(false);
        }
    }

    private void refreshTable() {
        // ???????
        Long selectedComponentNo = null;
        if (sender.servicePanel.serviceTable.getValue() != null) {
            ComponentDto component = (ComponentDto) sender.servicePanel.serviceTable.getValue();
            selectedComponentNo = component.getComponent().getComponentNo();
        }
        int index = sender.servicePanel.serviceTable.getCurrentPageFirstItemIndex();

        // 
        sender.refreshTable();

        // ????????
        if (selectedComponentNo != null) {
            for (Object itemId : sender.servicePanel.serviceTable.getItemIds()) {
                ComponentDto component = (ComponentDto) itemId;
                if (selectedComponentNo.equals(component.getComponent().getComponentNo())) {
                    sender.servicePanel.serviceTable.select(itemId);
                    sender.servicePanel.serviceTable.setCurrentPageFirstItemIndex(index);
                    break;
                }
            }
        }
    }

    private void addButtonClick(ClickEvent event) {
        WinServiceAdd winServiceAdd = new WinServiceAdd();
        winServiceAdd.addListener(new Window.CloseListener() {
            @Override
            public void windowClose(CloseEvent e) {
                refreshTable();
            }
        });

        getWindow().addWindow(winServiceAdd);
    }

    private void editButtonClick(Button.ClickEvent event) {
        ComponentDto component = (ComponentDto) sender.servicePanel.serviceTable.getValue();

        WinServiceEdit winServiceEdit = new WinServiceEdit(component.getComponent().getComponentNo());
        winServiceEdit.addListener(new Window.CloseListener() {
            @Override
            public void windowClose(CloseEvent e) {
                refreshTable();
            }
        });

        getWindow().addWindow(winServiceEdit);
    }

    private void deleteButtonClick(Button.ClickEvent event) {
        final ComponentDto component = (ComponentDto) sender.servicePanel.serviceTable.getValue();

        // ?
        String message = ViewMessages.getMessage("IUI-000018", component.getComponent().getComponentName());
        DialogConfirm dialog = new DialogConfirm(ViewProperties.getCaption("dialog.confirm"), message,
                Buttons.OKCancel);
        dialog.setCallback(new DialogConfirm.Callback() {
            @Override
            public void onDialogResult(Result result) {
                if (result != Result.OK) {
                    return;
                }

                delete(component.getComponent().getComponentNo());
            }
        });

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

    private void delete(Long componentNo) {
        // 
        OperationLogger.writeComponent("SERVICE", "Delete Service", componentNo, null);

        // 
        ComponentService componentService = BeanContext.getBean(ComponentService.class);
        componentService.deleteComponent(componentNo);

        // ??
        sender.servicePanel.serviceTable.select(null);

        // 
        refreshTable();
    }

}