com.clustercontrol.notify.mail.composite.MailTemplateIdListComposite.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.notify.mail.composite.MailTemplateIdListComposite.java

Source

/*
    
Copyright (C) 2008 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.notify.mail.composite;

import java.util.ArrayList;
import java.util.List;

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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.clustercontrol.bean.PropertyDefineConstant;
import com.clustercontrol.notify.mail.dialog.MailTemplateCreateDialog;
import com.clustercontrol.notify.mail.util.MailTemplateEndpointWrapper;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.ws.mailtemplate.InvalidRole_Exception;
import com.clustercontrol.ws.mailtemplate.MailTemplateInfo;
import com.clustercontrol.util.WidgetTestUtil;

/**
 * ID?<BR>
 * <p>
 * <dl>
 *  <dt>?</dt>
 *  <dd>ID? </dd>
 *  <dd>ID? </dd>
 *  <dd>[]??? </dd>
 * </dl>
 *
 * @version 3.0.0
 * @since 2.4.0
 */
public class MailTemplateIdListComposite extends Composite {

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

    /** ID  */
    private String m_text = null;

    /** ID  */
    private Label m_labelMailTemplateId = null;

    /** ID  */
    private Combo m_comboMailTemplateId = null;

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

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

    /**
     * ???
     * <p>
     * ??????????
     *
     * @param parent ??
     * @param style 
     * @param labelFlg ID???<code> true </code>
     *
     * @see org.eclipse.swt.SWT
     * @see org.eclipse.swt.widgets.Composite#Composite(Composite parent, int style)
     * @see #initialize(Composite, boolean)
     */
    public MailTemplateIdListComposite(Composite parent, int style, String managerName, boolean labelFlg) {
        super(parent, style);
        m_text = Messages.getString("mail.template.id");
        this.managerName = managerName;

        this.initialize(parent, labelFlg);
    }

    /**
     * ???
     * <p>
     * ??????????
     * ID????
     *
     * @param parent ??
     * @param style 
     * @param text ID 
     *
     * @see org.eclipse.swt.SWT
     * @see org.eclipse.swt.widgets.Composite#Composite(Composite parent, int style)
     * @see #initialize(Composite, boolean)
     */
    public MailTemplateIdListComposite(Composite parent, int style, String text) {
        super(parent, style);
        m_text = text;

        this.initialize(parent, true);
    }

    /**
     * ?????
     *
     * @see #update()
     */
    private void initialize(Composite parent, boolean labelFlg) {

        // ????
        GridData gridData = null;

        GridLayout layout = new GridLayout(1, true);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        if (labelFlg) {
            layout.numColumns = 10;
        } else {
            layout.numColumns = 5;
        }
        this.setLayout(layout);

        /*
         * ID
         */

        if (labelFlg) {
            // 
            this.m_labelMailTemplateId = new Label(this, SWT.NONE);
            WidgetTestUtil.setTestId(this, "mailtemplateid", m_labelMailTemplateId);
            gridData = new GridData();
            gridData.horizontalSpan = 3;
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;
            this.m_labelMailTemplateId.setLayoutData(gridData);
            this.m_labelMailTemplateId.setText(m_text + " : ");
        }

        // 
        this.m_comboMailTemplateId = new Combo(this, SWT.DROP_DOWN | SWT.READ_ONLY);
        WidgetTestUtil.setTestId(this, null, m_comboMailTemplateId);
        gridData = new GridData();
        gridData.horizontalSpan = 5;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_comboMailTemplateId.setLayoutData(gridData);
        //        this.m_comboMailTemplateId.addSelectionListener(new SelectionAdapter() {
        //           public void widgetSelected(SelectionEvent e) {
        //
        //           }
        //        });

        // ? 
        this.m_buttonRefer = new Button(this, SWT.NONE);
        WidgetTestUtil.setTestId(this, null, m_buttonRefer);
        gridData = new GridData();
        gridData.horizontalSpan = 2;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        this.m_buttonRefer.setLayoutData(gridData);
        this.m_buttonRefer.setText(Messages.getString("refer"));
        this.m_buttonRefer.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {

                String mailTemplateId = m_comboMailTemplateId.getText();
                if (mailTemplateId != null && !"".equals(mailTemplateId.trim())) {
                    // ?
                    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

                    // /???
                    MailTemplateCreateDialog dialog = new MailTemplateCreateDialog(shell,
                            MailTemplateIdListComposite.this.managerName, mailTemplateId,
                            PropertyDefineConstant.MODE_SHOW);
                    dialog.open();
                }
            }
        });

        this.update();
    }

    /**
     * ????<BR>
     * ID???ID?????
     *
     * @see com.clustercontrol.notify.mail.action.GetMailTemplate#getMailTemplateIdList()
     */
    @Override
    public void update() {
    }

    /**
     * ????<BR>
     * ID???ID?????
     *
     * @param ownerRoleId
     */
    public void update(String ownerRoleId) {
        // ?
        this.m_comboMailTemplateId.removeAll();

        // 
        this.m_comboMailTemplateId.add("");

        // ?
        List<String> list = null;

        try {
            MailTemplateEndpointWrapper wrapper = MailTemplateEndpointWrapper.getWrapper(this.managerName);
            List<MailTemplateInfo> listTmp = wrapper.getMailTemplateListByOwnerRole(ownerRoleId);
            list = new ArrayList<String>();
            for (MailTemplateInfo info : listTmp) {
                list.add(info.getMailTemplateId());
            }
        } catch (InvalidRole_Exception e) {
            MessageDialog.openInformation(null, Messages.getString("message"),
                    Messages.getString("message.accesscontrol.16"));
        } catch (Exception e) {
            m_log.warn("update() getMailTemplateList, " + e.getMessage(), e);
            MessageDialog.openError(null, Messages.getString("failed"),
                    Messages.getString("message.hinemos.failure.unexpected") + ", "
                            + HinemosMessage.replace(e.getMessage()));
        }

        if (list != null) {
            // ID
            for (int index = 0; index < list.size(); index++) {
                this.m_comboMailTemplateId.add(list.get(index));
            }
        }
    }

    public void setManagerName(String managerName) {
        this.managerName = managerName;
    }

    /* (? Javadoc)
     * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
     */
    @Override
    public void setEnabled(boolean enabled) {
        this.m_comboMailTemplateId.setEnabled(enabled);
        this.m_buttonRefer.setEnabled(enabled);
    }

    /**
     * ID???
     *
     * @return ID
     *
     * @see org.eclipse.swt.widgets.Combo#getText()
     */
    public String getText() {
        return this.m_comboMailTemplateId.getText();
    }

    /**
     * ID???
     *
     * @param string ID
     *
     * @see org.eclipse.swt.widgets.Combo#setText(java.lang.String)
     */
    public void setText(String string) {
        this.m_comboMailTemplateId.setText(string);
    }
}