com.clustercontrol.performance.monitor.dialog.PerformanceCreateDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.performance.monitor.dialog.PerformanceCreateDialog.java

Source

/*
    
 Copyright (C) 2006 NTT DATA Corporation
    
 This program 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, version 2.
    
 This program 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.
    
 */

package com.clustercontrol.performance.monitor.dialog;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.clustercontrol.bean.HinemosModuleConstant;
import com.clustercontrol.monitor.run.bean.MonitorTypeConstant;
import com.clustercontrol.monitor.run.dialog.CommonMonitorNumericDialog;
import com.clustercontrol.monitor.util.MonitorSettingEndpointWrapper;
import com.clustercontrol.performance.monitor.composite.CollectorItemComboComposite;
import com.clustercontrol.performance.util.CollectorItemCodeFactory;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.WidgetTestUtil;
import com.clustercontrol.ws.monitor.CollectorItemInfo;
import com.clustercontrol.ws.monitor.InvalidRole_Exception;
import com.clustercontrol.ws.monitor.MonitorDuplicate_Exception;
import com.clustercontrol.ws.monitor.MonitorInfo;
import com.clustercontrol.ws.monitor.PerfCheckInfo;

/**
 * ?
 *
 * @version 4.0.0
 * @since 2.0.0
 */
public class PerformanceCreateDialog extends CommonMonitorNumericDialog {

    // 
    private static Log m_log = LogFactory.getLog(PerformanceCreateDialog.class);

    // ----- instance  ----- //

    /** ? */
    private CollectorItemComboComposite m_comboCollectorItem = null;

    /** ???????? */
    private Button m_breakdownFlg = null;

    // -----  ----- //

    /**
     * ?????
     *
     * @param parent
     *            ?
     */
    public PerformanceCreateDialog(Shell parent) {
        super(parent, null);
    }

    /**
     * ????
     *
     * @param parent
     *            ?
     * @param managerName
     *            ???
     * @param notifyId
     *            ?ID
     * @param updateFlg
     *            ????true:?false:?
     */
    public PerformanceCreateDialog(Shell parent, String managerName, String monitorId, boolean updateFlg) {
        super(parent, managerName);
        this.managerName = managerName;
        this.monitorId = monitorId;
        this.updateFlg = updateFlg;
    }

    // ----- instance  ----- //

    /**
     * ????
     *
     * @param parent
     *            ?
     */
    @Override
    protected void customizeDialog(Composite parent) {
        // ?????
        item1 = Messages.getString("select.value");
        item2 = Messages.getString("select.value");

        super.customizeDialog(parent);
        itemName.setEditable(false);
        measure.setEditable(false);

        // 
        shell.setText(Messages.getString("dialog.performance.monitor.create.modify"));

        // ????
        GridData gridData = null;

        /*
         * ????
         */
        Group groupCheckRule = new Group(groupRule, SWT.NONE);
        WidgetTestUtil.setTestId(this, "checkrule", groupCheckRule);
        GridLayout layout = new GridLayout(1, true);
        layout.marginWidth = 5;
        layout.marginHeight = 5;
        layout.numColumns = BASIC_UNIT;
        groupCheckRule.setLayout(layout);
        gridData = new GridData();
        gridData.horizontalSpan = BASIC_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        groupCheckRule.setLayoutData(gridData);
        groupCheckRule.setText(Messages.getString("check.rule"));

        // 
        this.m_comboCollectorItem = new CollectorItemComboComposite(groupCheckRule, SWT.DROP_DOWN | SWT.READ_ONLY);
        WidgetTestUtil.setTestId(this, "collectoritem", m_comboCollectorItem);
        gridData = new GridData();
        gridData.horizontalSpan = BASIC_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_comboCollectorItem.setLayoutData(gridData);

        // ?????????
        m_comboCollectorItem.setCollectorItemCombo(this.m_monitorBasic.getManagerListComposite().getText(), null);

        m_monitorBasic.getButtonScope().addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                m_comboCollectorItem.setCollectorItemCombo(PerformanceCreateDialog.this.getManagerName(),
                        m_monitorBasic.getFacilityId());
            }
        });

        //????
        if (!updateFlg) {
            this.getMonitorBasicScope().getManagerListComposite().getComboManagerName()
                    .addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            m_comboCollectorItem.setCollectorItemCombo(
                                    PerformanceCreateDialog.this.getManagerName(), m_monitorBasic.getFacilityId());
                        }
                    });
        }

        // 
        this.m_breakdownFlg = new Button(groupCheckRule, SWT.CHECK);
        WidgetTestUtil.setTestId(this, "breakdownflg", m_breakdownFlg);
        this.m_breakdownFlg.setText(Messages.getString("dialog.performance.monitor.collect.detail"));
        gridData = new GridData();
        gridData.horizontalSpan = BASIC_UNIT;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_breakdownFlg.setLayoutData(gridData);

        // ???????
        m_comboCollectorItem.getCombo().addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent arg0) {
                itemName.setText(m_comboCollectorItem.getCombo().getText());
                if (m_comboCollectorItem.getCollectorItem() != null) {
                    measure.setText(HinemosMessage.replace(
                            CollectorItemCodeFactory.getMeasure(PerformanceCreateDialog.this.getManagerName(),
                                    m_comboCollectorItem.getCollectorItem().getItemCode())));
                    update();
                }
            }
        });

        // 
        this.adjustDialog();

        // ?
        MonitorInfo info = null;
        if (this.monitorId == null) {
            // ???
            info = new MonitorInfo();
            this.setInfoInitialValue(info);
        } else {
            // ????
            try {
                MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper
                        .getWrapper(this.getManagerName());
                info = wrapper.getMonitor(this.monitorId);
            } catch (InvalidRole_Exception e) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
                throw new InternalError(e.getMessage());
            } catch (Exception e) {
                // ?
                m_log.warn("customizeDialog() getMonitor, " + HinemosMessage.replace(e.getMessage()), e);
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.hinemos.failure.unexpected") + ", "
                                + HinemosMessage.replace(e.getMessage()));
                throw new InternalError(e.getMessage());
            }
        }
        this.setInputData(info);
    }

    /**
     * ?
     *
     */
    @Override
    protected void update() {
        super.update();

        try {
            // ?????????????
            item1 = Messages.getString("select.value") + "(" + HinemosMessage.replace(CollectorItemCodeFactory
                    .getMeasure(this.getManagerName(), m_comboCollectorItem.getCollectorItem().getItemCode()))
                    + ")";
            item2 = item1;

            this.m_numericValueInfo.setTextItem1(item1);
            this.m_numericValueInfo.setTextItem2(item2);

        } catch (NullPointerException e) {
            // ????????
        }

    }

    /**
     * ?????
     *
     * @param monitor
     *            ????
     */
    @Override
    protected void setInputData(MonitorInfo monitor) {
        super.setInputData(monitor);

        this.inputData = monitor;

        // ?
        PerfCheckInfo perfInfo = monitor.getPerfCheckInfo();
        if (perfInfo == null) {
            perfInfo = new PerfCheckInfo();
        }
        // ????
        this.m_breakdownFlg.setSelection(isBreakdown(perfInfo));

        // ?
        m_comboCollectorItem.select(this.getManagerName(), monitor);

        m_numericValueInfo.setInputData(monitor);

        this.update();
    }

    /**
     * ??????
     *
     * @return ????
     */
    @Override
    protected MonitorInfo createInputData() {
        super.createInputData();
        if (validateResult != null) {
            return null;
        }

        // 
        monitorInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_PERFORMANCE);
        monitorInfo.setMonitorType(MonitorTypeConstant.TYPE_NUMERIC);

        // ?
        PerfCheckInfo perfInfo = new PerfCheckInfo();
        perfInfo.setMonitorTypeId(HinemosModuleConstant.MONITOR_PERFORMANCE);
        perfInfo.setMonitorId(monitorInfo.getMonitorId());

        // ????
        perfInfo.setBreakdownFlg(m_breakdownFlg.getSelection());

        monitorInfo.setPerfCheckInfo(perfInfo);

        // ??
        validateResult = m_numericValueInfo.createInputData(monitorInfo);
        if (validateResult != null) {
            return null;
        }

        // ??
        validateResult = m_comboCollectorItem.createInputData(monitorInfo);
        if (validateResult != null) {
            return null;
        }

        // ??
        validateResult = m_notifyInfo.createInputData(monitorInfo);
        if (validateResult != null) {
            if (validateResult.getID() == null) { // ID
                if (!displayQuestion(validateResult)) {
                    validateResult = null;
                    return null;
                }
            } else { // ?
                return null;
            }
        }
        return monitorInfo;
    }

    /**
     * ?????
     *
     * @return true?false
     *
     * @see com.clustercontrol.dialog.CommonDialog#action()
     */
    @Override
    protected boolean action() {
        boolean result = false;

        MonitorInfo info = this.inputData;
        CollectorItemInfo itemInfo = (CollectorItemInfo) this.m_comboCollectorItem.getCombo()
                .getData(this.itemName.getText());
        String managerName = this.getManagerName();
        String[] args = { info.getMonitorId(), managerName };
        if (itemInfo == null || itemInfo.getItemCode().length() == 0) {
            MessageDialog.openError(null, Messages.getString("failed"),
                    Messages.getString("message.monitor.34", args));
            return false;
        }
        String itemCode = CollectorItemCodeFactory.getFullItemName(managerName, itemInfo);
        String itemName = itemCode.split("]")[0].concat("]");
        info.setItemName(itemName);
        String itemMeasure = CollectorItemCodeFactory.getMeasure(itemName, itemInfo.getItemCode());
        info.setMeasure(itemMeasure);

        MonitorSettingEndpointWrapper wrapper = MonitorSettingEndpointWrapper.getWrapper(managerName);
        if (!this.updateFlg) {
            // ???
            try {
                result = wrapper.addMonitor(info);

                if (result) {
                    MessageDialog.openInformation(null, Messages.getString("successful"),
                            Messages.getString("message.monitor.33", args));
                } else {
                    MessageDialog.openError(null, Messages.getString("failed"),
                            Messages.getString("message.monitor.34", args));
                }
            } catch (MonitorDuplicate_Exception e) {
                // ID????????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.monitor.53", args));

            } catch (Exception e) {
                String errMessage = "";
                if (e instanceof InvalidRole_Exception) {
                    // ??????
                    MessageDialog.openInformation(null, Messages.getString("message"),
                            Messages.getString("message.accesscontrol.16"));
                } else {
                    errMessage = ", " + HinemosMessage.replace(e.getMessage());
                }

                MessageDialog.openError(null, Messages.getString("failed"),
                        Messages.getString("message.monitor.34", args) + errMessage);
            }
        } else {
            // ??
            String errMessage = "";
            try {
                result = wrapper.modifyMonitor(info);
            } catch (InvalidRole_Exception e) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
            } catch (Exception e) {
                errMessage = ", " + HinemosMessage.replace(e.getMessage());
            }

            if (result) {
                MessageDialog.openInformation(null, Messages.getString("successful"),
                        Messages.getString("message.monitor.35", args));
            } else {
                MessageDialog.openError(null, Messages.getString("failed"),
                        Messages.getString("message.monitor.36", args) + errMessage);
            }
        }

        return result;
    }

    private static boolean isBreakdown(PerfCheckInfo perfCheckInfo) {
        return perfCheckInfo.isBreakdownFlg() == null ? false : perfCheckInfo.isBreakdownFlg();
    }
}