Java tutorial
/* 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.dialog; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.jface.dialogs.IDialogConstants; 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.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; 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.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import com.clustercontrol.util.HinemosMessage; import com.clustercontrol.util.WidgetTestUtil; import com.clustercontrol.bean.DataRangeConstant; import com.clustercontrol.bean.PropertyDefineConstant; import com.clustercontrol.bean.RequiredFieldColorConstant; import com.clustercontrol.composite.ManagerListComposite; import com.clustercontrol.composite.RoleIdListComposite; import com.clustercontrol.composite.RoleIdListComposite.Mode; import com.clustercontrol.composite.TextWithParameterComposite; import com.clustercontrol.dialog.CommonDialog; import com.clustercontrol.dialog.ValidateResult; import com.clustercontrol.notify.mail.util.MailTemplateEndpointWrapper; import com.clustercontrol.util.Messages; import com.clustercontrol.ws.mailtemplate.InvalidRole_Exception; import com.clustercontrol.ws.mailtemplate.MailTemplateDuplicate_Exception; import com.clustercontrol.ws.mailtemplate.MailTemplateInfo; /** * ID?<BR> * * @version 4.0.0 * @since 2.4.0 */ public class MailTemplateCreateDialog extends CommonDialog { // private static Log m_log = LogFactory.getLog(MailTemplateCreateDialog.class); /** */ public static final int WIDTH = 15; /** */ public static final int WIDTH_LABEL = 4; /** */ public static final int WIDTH_TEXT = 10; /** ??? */ private MailTemplateInfo inputData = null; /** ???? */ protected ValidateResult validateResult = null; /** ?? */ private int mode; /** ?ID */ private String mailTemplateId = null; /** ??? */ private String managerName = null; /** ID */ private Text textMailTemplateId = null; /** */ private Text textDescription = null; /** ID */ private RoleIdListComposite m_ownerRoleId = null; /** ?? ? */ private TextWithParameterComposite textSubject = null; /** ? */ private TextWithParameterComposite textBody = null; /** ?? */ private ManagerListComposite m_managerComposite = null; /** * ???? * * @param parent ? * @param mailTemplateId ??ID * @param dataOperationType ? */ public MailTemplateCreateDialog(Shell parent, String managerName, String mailTemplateId, int mode) { super(parent); this.managerName = managerName; this.mailTemplateId = mailTemplateId; this.mode = mode; } /** * ???? * * @param parent ?? * * @see com.clustercontrol.notify.mail.action.GetMailTemplate#getMailTemplate(String) * @see #setInputData(MailTemplateInfoData) */ @Override protected void customizeDialog(Composite parent) { Shell shell = this.getShell(); // shell.setText(Messages.getString("dialog.mail.template.create.modify")); // ???? Label label = null; // ???? GridData gridData = null; // GridLayout layout = new GridLayout(1, true); layout.marginWidth = 10; layout.marginHeight = 10; layout.numColumns = 15; parent.setLayout(layout); /* * ? */ label = new Label(parent, SWT.LEFT); WidgetTestUtil.setTestId(this, "manager", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("facility.manager") + " : "); if (this.mode == PropertyDefineConstant.MODE_MODIFY || this.mode == PropertyDefineConstant.MODE_SHOW) { this.m_managerComposite = new ManagerListComposite(parent, SWT.NONE, false); } else { this.m_managerComposite = new ManagerListComposite(parent, SWT.NONE, true); } WidgetTestUtil.setTestId(this, "managerComposite", m_managerComposite); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.m_managerComposite.setLayoutData(gridData); this.m_managerComposite.setText(this.managerName); if (this.mode != PropertyDefineConstant.MODE_MODIFY && this.mode != PropertyDefineConstant.MODE_SHOW) { this.m_managerComposite.getComboManagerName().addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String managerName = m_managerComposite.getText(); m_ownerRoleId.createRoleIdList(managerName); } }); } /* * ID */ // label = new Label(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "mailtempateid", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("mail.template.id") + " : "); // this.textMailTemplateId = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "id", textMailTemplateId); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textMailTemplateId.setLayoutData(gridData); if (this.mode == PropertyDefineConstant.MODE_SHOW || this.mode == PropertyDefineConstant.MODE_MODIFY) { this.textMailTemplateId.setEnabled(false); } this.textMailTemplateId.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent arg0) { update(); } }); /* * */ // label = new Label(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "description", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("description") + " : "); // this.textDescription = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "description", textDescription); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textDescription.setLayoutData(gridData); /* * ID */ Label labelRoleId = new Label(parent, SWT.LEFT); WidgetTestUtil.setTestId(this, "roleid", labelRoleId); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; labelRoleId.setLayoutData(gridData); labelRoleId.setText(Messages.getString("owner.role.id") + " : "); if (this.mode == PropertyDefineConstant.MODE_ADD) { this.m_ownerRoleId = new RoleIdListComposite(parent, SWT.NONE, managerName, true, Mode.OWNER_ROLE); } else { this.m_ownerRoleId = new RoleIdListComposite(parent, SWT.NONE, managerName, false, Mode.OWNER_ROLE); } WidgetTestUtil.setTestId(this, "ownerroleid", m_ownerRoleId); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; m_ownerRoleId.setLayoutData(gridData); /* * */ /* * */ Group groupMailTemplate = new Group(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "mailtemplate", groupMailTemplate); layout = new GridLayout(1, true); layout.marginWidth = 10; layout.marginHeight = 10; layout.numColumns = 15; groupMailTemplate.setLayout(layout); gridData = new GridData(); gridData.horizontalSpan = 15; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; groupMailTemplate.setLayoutData(gridData); groupMailTemplate.setText(Messages.getString("mail.template.setting")); /* * ?? */ // label = new Label(groupMailTemplate, SWT.NONE); WidgetTestUtil.setTestId(this, "subject", label); gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("subject") + " : "); // this.textSubject = new TextWithParameterComposite(groupMailTemplate, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "subject", textSubject); gridData = new GridData(); gridData.horizontalSpan = 13; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textSubject.setLayoutData(gridData); this.textSubject.setToolTipText(Messages.getString("notify.parameter.tooltip")); this.textSubject.setColor(new Color(groupMailTemplate.getDisplay(), new RGB(0, 0, 255))); this.textSubject.setInputUpper(DataRangeConstant.VARCHAR_256); this.textSubject.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent arg0) { update(); } }); /* * */ // label = new Label(groupMailTemplate, SWT.NONE); WidgetTestUtil.setTestId(this, "repositorybody", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("response.body") + " : "); // this.textBody = new TextWithParameterComposite(groupMailTemplate, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.LEFT); WidgetTestUtil.setTestId(this, "body", textBody); gridData = new GridData(); gridData.horizontalSpan = WIDTH; gridData.horizontalAlignment = GridData.FILL; gridData.verticalSpan = 50; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; this.textBody.setLayoutData(gridData); this.textBody.setToolTipText(Messages.getString("notify.parameter.tooltip")); this.textBody.setColor(new Color(groupMailTemplate.getDisplay(), new RGB(0, 0, 255))); // ?? Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); WidgetTestUtil.setTestId(this, "line", line); gridData = new GridData(); gridData.horizontalSpan = 15; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; line.setLayoutData(gridData); // ? // ???????????????????? shell.pack(); shell.setSize(new Point(550, shell.getSize().y)); // ?? Display display = shell.getDisplay(); shell.setLocation((display.getBounds().width - shell.getSize().x) / 2, (display.getBounds().height - shell.getSize().y) / 2); // ID??????????? MailTemplateInfo info = null; if (this.mailTemplateId != null) { try { MailTemplateEndpointWrapper wrapper = MailTemplateEndpointWrapper.getWrapper(this.managerName); info = wrapper.getMailTemplateInfo(this.mailTemplateId); } catch (InvalidRole_Exception e) { MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); return; } catch (Exception e) { m_log.warn("customizeDialog() getMailTemplateInfo, " + HinemosMessage.replace(e.getMessage()), e); MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.hinemos.failure.unexpected") + ", " + HinemosMessage.replace(e.getMessage())); return; } } else { info = new MailTemplateInfo(); } this.setInputData(info); } /** * ? * */ public void update() { if ("".equals(this.textMailTemplateId.getText())) { this.textMailTemplateId.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.textMailTemplateId.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } if ("".equals(this.textSubject.getText())) { this.textSubject.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.textSubject.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } } /** * ??????? * * @return */ public MailTemplateInfo getInputData() { return this.inputData; } /** * ?????????? * * @param mailTemplate ???? */ protected void setInputData(MailTemplateInfo mailTemplate) { this.inputData = mailTemplate; // ???? // ID if (mailTemplate.getMailTemplateId() != null) { this.textMailTemplateId.setText(mailTemplate.getMailTemplateId()); } // if (mailTemplate.getDescription() != null) { this.textDescription.setText(mailTemplate.getDescription()); } // ?? if (mailTemplate.getSubject() != null) { this.textSubject.setText(mailTemplate.getSubject()); } // if (mailTemplate.getBody() != null) { this.textBody.setText(mailTemplate.getBody()); } // ID? if (mailTemplate.getOwnerRoleId() != null) { this.m_ownerRoleId.setText(mailTemplate.getOwnerRoleId()); } // if (this.mode == PropertyDefineConstant.MODE_SHOW) { this.textMailTemplateId.setEnabled(false); this.textDescription.setEnabled(false); this.textSubject.setEnabled(false); this.textBody.setEnabled(false); } // ? this.update(); } /** * ?????<BR> * ???????<code>null</code>??? * * @return */ private MailTemplateInfo createInputData() { MailTemplateInfo info = new MailTemplateInfo(); if (this.textMailTemplateId.getText() != null && !"".equals((this.textMailTemplateId.getText()).trim())) { info.setMailTemplateId(this.textMailTemplateId.getText()); } if (this.textDescription.getText() != null && !"".equals((this.textDescription.getText()).trim())) { info.setDescription(this.textDescription.getText()); } if (this.textSubject.getText() != null && !"".equals((this.textSubject.getText()).trim())) { info.setSubject(this.textSubject.getText()); } if (this.textBody.getText() != null && !"".equals((this.textBody.getText()).trim())) { info.setBody(this.textBody.getText()); } //ID if (this.m_ownerRoleId.getText().length() > 0) { info.setOwnerRoleId(this.m_ownerRoleId.getText()); } return info; } /** * ???? * * @return ? */ @Override protected ValidateResult validate() { // ? this.inputData = this.createInputData(); return super.validate(); } /** * ????? * * @return true?false * * @see com.clustercontrol.dialog.CommonDialog#action() */ @Override protected boolean action() { boolean result = false; MailTemplateInfo info = this.getInputData(); if (info != null) { String errMessage = ""; String managerName = m_managerComposite.getText(); String[] args = { info.getMailTemplateId(), managerName }; MailTemplateEndpointWrapper wrapper = MailTemplateEndpointWrapper.getWrapper(managerName); if (this.mode == PropertyDefineConstant.MODE_ADD) { // ??? try { result = wrapper.addMailTemplate(info); MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.notify.mail.1", args)); } catch (MailTemplateDuplicate_Exception e) { // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.notify.mail.10", 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.notify.mail.2", args) + errMessage); } } else if (this.mode == PropertyDefineConstant.MODE_MODIFY) { // ?? try { result = wrapper.modifyMailTemplate(info); MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.notify.mail.3", 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.notify.mail.4", args) + errMessage); } } } return result; } /** * ???? * * @return ? */ @Override protected String getOkButtonText() { return Messages.getString("ok"); } /** * ???? * * @return ? */ @Override protected String getCancelButtonText() { return Messages.getString("cancel"); } /** * ????<BR> * ??<code> true </code>???????<code> false </code>????????? * * @param parent ?? * * @see #createButtonsForButtonBar(Composite) */ @Override protected void createButtonsForButtonBar(Composite parent) { if (this.mode != PropertyDefineConstant.MODE_SHOW) { super.createButtonsForButtonBar(parent); } else { // ? // TODO Remove the following hard-code. IDialogConstants.*_LABEL will causes IncompatibleClassChangeError on RAP this.createButton(parent, IDialogConstants.CANCEL_ID, "Close", false); } } }