com.clustercontrol.repository.dialog.ScopeCreateDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.repository.dialog.ScopeCreateDialog.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.repository.dialog;

import java.util.ArrayList;
import java.util.Locale;

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.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.clustercontrol.util.WidgetTestUtil;
import com.clustercontrol.accesscontrol.util.ClientSession;
import com.clustercontrol.bean.Property;
import com.clustercontrol.bean.PropertyDefineConstant;
import com.clustercontrol.bean.PropertyFieldColorConstant;
import com.clustercontrol.bean.RequiredFieldColorConstant;
import com.clustercontrol.composite.RoleIdListComposite;
import com.clustercontrol.composite.RoleIdListComposite.Mode;
import com.clustercontrol.dialog.CommonDialog;
import com.clustercontrol.dialog.ValidateResult;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.repository.bean.ScopeConstant;
import com.clustercontrol.repository.util.RepositoryEndpointWrapper;
import com.clustercontrol.repository.util.ScopePropertyUtil;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.PropertyUtil;
import com.clustercontrol.viewer.PropertySheet;
import com.clustercontrol.ws.repository.FacilityDuplicate_Exception;
import com.clustercontrol.ws.repository.InvalidRole_Exception;
import com.clustercontrol.ws.repository.ScopeInfo;

/**
 * ??<BR>
 *
 * @version 4.0.0
 * @since 1.0.0
 */
public class ScopeCreateDialog extends CommonDialog {

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

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

    /** ??ID */
    private String facilityId = "";

    /** ID */
    private String parentFacilityId = "";

    /**  */
    private boolean isModifyDialog = false;

    /**  */
    private PropertySheet propertySheet = null;

    /**  **/
    private Tree table = null;

    /** ID */
    private RoleIdListComposite m_ownerRoleId = null;

    /** ??? */
    private String managerName = null;

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

    /**
     * ????????
     *
     * @param parent
     *            ?
     * @param managerName
     *            ???
     * @param facilityId
     *            ???ID
     * @param isModifyDialog
     *            ???????true
     */
    public ScopeCreateDialog(Shell parent, String managerName, String facilityId, boolean isModifyDialog) {
        super(parent);

        this.managerName = managerName;
        this.facilityId = facilityId;
        this.isModifyDialog = isModifyDialog;
    }

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

    /**
     * ?????
     *
     * @return ?
     */
    @Override
    protected Point getInitialSize() {
        return new Point(450, 400);
    }

    /**
     * ????
     *
     * @param parent
     *            ?
     */
    @Override
    protected void customizeDialog(Composite parent) {
        Shell shell = this.getShell();

        // 
        shell.setText(Messages.getString("dialog.repository.scope.create.modify"));

        // 
        GridLayout layout = new GridLayout(1, true);
        layout.marginWidth = 10;
        layout.marginHeight = 10;
        parent.setLayout(layout);
        GridData gridData = new GridData();

        /*
         * ID
         */
        Label labelRoleId = new Label(parent, SWT.LEFT);
        WidgetTestUtil.setTestId(this, "roleid", labelRoleId);
        gridData = new GridData();
        gridData.horizontalSpan = 1;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        labelRoleId.setLayoutData(gridData);
        labelRoleId.setText(Messages.getString("owner.role.id") + " : ");
        if (!this.isModifyDialog()) {
            this.m_ownerRoleId = new RoleIdListComposite(parent, SWT.NONE, this.managerName, true, Mode.OWNER_ROLE);
        } else {
            this.m_ownerRoleId = new RoleIdListComposite(parent, SWT.NONE, this.managerName, false,
                    Mode.OWNER_ROLE);
        }
        WidgetTestUtil.setTestId(this, null, m_ownerRoleId);
        gridData = new GridData();
        gridData.horizontalSpan = 1;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        m_ownerRoleId.setLayoutData(gridData);

        /*
         * 
         */
        // 
        Label label = new Label(parent, SWT.LEFT);
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalSpan = 1;
        label.setLayoutData(gridData);
        label.setText(Messages.getString("attribute") + " : ");

        // 
        table = new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
        WidgetTestUtil.setTestId(this, null, table);
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        gridData.horizontalSpan = 1;
        table.setLayoutData(gridData);
        table.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                update();
            }
        });

        this.propertySheet = new PropertySheet(table);

        // ???
        Property property = null;
        RepositoryEndpointWrapper wrapper = RepositoryEndpointWrapper.getWrapper(this.managerName);
        if (this.isModifyDialog) {
            try {
                ScopeInfo scopeInfo = wrapper.getScope(this.facilityId);
                if (scopeInfo == null)
                    throw new HinemosUnknown("ScopeInfo is null, facilityId : " + this.facilityId);

                // ID?
                if (scopeInfo.getOwnerRoleId() != null) {
                    m_ownerRoleId.setText(scopeInfo.getOwnerRoleId());
                }
                property = getScopeProperty(scopeInfo, PropertyDefineConstant.MODE_MODIFY);
            } catch (InvalidRole_Exception e) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
            } catch (Exception e) {
                m_log.warn("getScopeProperty(), " + HinemosMessage.replace(e.getMessage()), e);
                MessageDialog.openError(null, Messages.getString("failed"),
                        Messages.getString("message.hinemos.failure.unexpected") + ", "
                                + HinemosMessage.replace(e.getMessage()));
            }
        } else {
            try {
                ScopeInfo scopeInfo = wrapper.getScope(null);
                // ID?
                if (scopeInfo == null)
                    throw new HinemosUnknown("ScopeInfo is null");

                if (scopeInfo.getOwnerRoleId() != null) {
                    m_ownerRoleId.setText(scopeInfo.getOwnerRoleId());
                }
                property = getScopeProperty(scopeInfo, PropertyDefineConstant.MODE_ADD);
            } catch (InvalidRole_Exception e) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
            } catch (Exception e) {
                m_log.warn("getScopeProperty(), " + HinemosMessage.replace(e.getMessage()), e);
                MessageDialog.openError(null, Messages.getString("failed"),
                        Messages.getString("message.hinemos.failure.unexpected") + ", "
                                + HinemosMessage.replace(e.getMessage()));
            }
        }
        if (property != null)
            this.propertySheet.setInput(property);

        // ??
        Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalSpan = 1;
        line.setLayoutData(gridData);

        // ??
        Display display = shell.getDisplay();
        shell.setLocation((display.getBounds().width - shell.getSize().x) / 2,
                (display.getBounds().height - shell.getSize().y) / 2);

        // ?
        this.update();
    }

    /**
     * Set required (pink background color) when empty
     * @param item
     */
    private void setRequired(TreeItem item) {
        Property element = (Property) item.getData();
        if ("".equals(element.getValueText())) {
            item.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED);
        } else {
            item.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED);
        }
    }

    /**
     * ??????
     * @param item
     */
    private void setForegroundColor(TreeItem item) {
        if (item == null) {
            return;
        }
        Property element = (Property) item.getData();
        if (element != null && "".equals(element.getValueText())) {
            item.setForeground(1, PropertyFieldColorConstant.COLOR_EMPTY);
        } else {
            item.setForeground(1, PropertyFieldColorConstant.COLOR_FILLED);
        }

        for (TreeItem child : item.getItems()) {
            setForegroundColor(child);
        }
    }

    /**
     * ?
     *
     */
    public void update() {
        // 
        // ID?0
        setRequired(table.getItem(0));
        // ID?1
        setRequired(table.getItem(1));

        for (TreeItem item : table.getItems()) {
            setForegroundColor(item);
        }

    }

    /**
     * ????
     *
     * @return ?
     */
    @Override
    protected ValidateResult validate() {
        return null;
    }

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

        Property property = this.getInputData();
        if (property != null) {
            String errMessage = "";
            Property copy = PropertyUtil.copy(property);
            PropertyUtil.deletePropertyDefine(copy);
            RepositoryEndpointWrapper wrapper = RepositoryEndpointWrapper.getWrapper(this.managerName);
            Object[] arg = { this.managerName };
            if (!this.isModifyDialog()) {
                // ???
                try {
                    ScopeInfo scopeinfo = ScopePropertyUtil.property2scope(copy);
                    if (m_ownerRoleId.getText().length() > 0) {
                        scopeinfo.setOwnerRoleId(m_ownerRoleId.getText());
                    }
                    wrapper.addScope(parentFacilityId, scopeinfo);

                    // ??
                    ClientSession.doCheck();

                    MessageDialog.openInformation(null, Messages.getString("successful"),
                            Messages.getString("message.repository.14", arg));

                    result = true;

                } catch (FacilityDuplicate_Exception e) {
                    //ID?
                    ArrayList<?> values = PropertyUtil.getPropertyValue(copy, ScopeConstant.FACILITY_ID);
                    String args[] = { (String) values.get(0) };

                    // ID????????
                    MessageDialog.openInformation(null, Messages.getString("message"),
                            Messages.getString("message.repository.26", args));

                } catch (Exception e) {
                    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.repository.15") + errMessage);
                }
            } else {
                // ??
                try {
                    ScopeInfo scopeInfo = ScopePropertyUtil.property2scope(copy);
                    if (m_ownerRoleId.getText().length() > 0) {
                        scopeInfo.setOwnerRoleId(m_ownerRoleId.getText());
                    }
                    wrapper.modifyScope(scopeInfo);

                    // ??
                    ClientSession.doCheck();

                    MessageDialog.openInformation(null, Messages.getString("successful"),
                            Messages.getString("message.repository.18", arg));

                    result = true;

                } catch (Exception e) {
                    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.repository.19") + errMessage);
                }
            }
        }

        return result;
    }

    /**
     * ??????
     *
     * @return ???true
     */
    public boolean isModifyDialog() {
        return this.isModifyDialog;
    }

    /**
     * ????????
     *
     * @return 
     */
    public Property getInputData() {
        return (Property) this.propertySheet.getInput();
    }

    /**
     * ID???
     *
     * @return ID
     */
    public String getParentFacilityId() {
        return parentFacilityId;
    }

    /**
     * ID???
     *
     * @param parentFacilityId ID
     */
    public void setParentFacilityId(String parentFacilityId) {
        this.parentFacilityId = parentFacilityId;
    }

    /**
     * ????
     *
     * @return ?
     */
    @Override
    protected String getOkButtonText() {
        return Messages.getString("register");
    }

    /**
     * ????
     *
     * @return ?
     */
    @Override
    protected String getCancelButtonText() {
        return Messages.getString("cancel");
    }

    /**
     * ????<BR>
     *
     * @param facilityId
     * @param property
     * @return 
     */
    private static Property getScopeProperty(ScopeInfo scopeInfo, int mode) {

        Property property = ScopePropertyUtil.scope2property(scopeInfo, mode, Locale.getDefault());
        return property;
    }
}