Java tutorial
/* 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.accesscontrol.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.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.Text; import com.clustercontrol.accesscontrol.util.AccessEndpointWrapper; import com.clustercontrol.bean.PropertyDefineConstant; import com.clustercontrol.bean.RequiredFieldColorConstant; import com.clustercontrol.composite.ManagerListComposite; import com.clustercontrol.dialog.CommonDialog; import com.clustercontrol.dialog.ValidateResult; import com.clustercontrol.util.HinemosMessage; import com.clustercontrol.util.Messages; import com.clustercontrol.util.WidgetTestUtil; import com.clustercontrol.ws.access.FacilityDuplicate_Exception; import com.clustercontrol.ws.access.InvalidRole_Exception; import com.clustercontrol.ws.access.RoleDuplicate_Exception; import com.clustercontrol.ws.access.RoleInfo; import com.clustercontrol.ws.access.UnEditableRole_Exception; /** * [??]?? * * @version 4.0.0 * @since 2.0.0 */ public class RoleDialog extends CommonDialog { // private static Log m_log = LogFactory.getLog(RoleDialog.class); /** ID */ private String roleId = ""; /** */ private int mode = 0; /** */ private boolean isModifyDialog = false; /** ID */ private Text textRoleId = null; /** ?? */ private Text textRoleName = null; /** */ private Text textDescription = null; /** */ public static final int WIDTH = 15; /** */ public static final int WIDTH_LABEL = 4; /** */ public static final int WIDTH_TEXT = 10; /** ??? */ private RoleInfo inputData = null; private boolean permission = false; // ???????? /** ???? */ private ManagerListComposite m_managerComposite = null; private String managerName = null; /** * * * @param parent * @param managerName ??? * @param uid ID * @param isModifyDialog ???????true */ public RoleDialog(Shell parent, String managerName, String roleId, boolean isModifyDialog) { super(parent); this.managerName = managerName; this.roleId = roleId; this.isModifyDialog = isModifyDialog; } /** * ????? * * @return ? * * @see org.eclipse.jface.window.Window#getInitialSize() */ @Override protected Point getInitialSize() { return new Point(600, 600); } /** * ???? * * @param parent ? * * @see com.clustercontrol.dialog.CommonDialog#customizeDialog(org.eclipse.swt.widgets.Composite) */ @Override protected void customizeDialog(Composite parent) { Shell shell = this.getShell(); // shell.setText(Messages.getString("dialog.accesscontrol.role.create.modify")); // GridLayout layout = new GridLayout(1, true); layout.marginWidth = 10; layout.marginHeight = 10; layout.numColumns = 15; parent.setLayout(layout); /* * ? */ Label labelManager = new Label(parent, SWT.LEFT); WidgetTestUtil.setTestId(this, "manager", labelManager); GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.horizontalSpan = WIDTH_LABEL; labelManager.setLayoutData(gridData); labelManager.setText(Messages.getString("facility.manager") + " : "); if (this.isModifyDialog) { this.m_managerComposite = new ManagerListComposite(parent, SWT.NONE, false); } else { this.m_managerComposite = new ManagerListComposite(parent, SWT.NONE, true); } WidgetTestUtil.setTestId(this, "managerComposite", this.m_managerComposite); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.horizontalSpan = WIDTH_TEXT; gridData.grabExcessHorizontalSpace = true; this.m_managerComposite.setLayoutData(gridData); if (this.managerName != null) { this.m_managerComposite.setText(this.managerName); } /* * ID */ // Label label = new Label(parent, SWT.NONE); WidgetTestUtil.setTestId(this, null, label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.horizontalSpan = WIDTH_LABEL; label.setLayoutData(gridData); label.setText(Messages.getString("role.id") + " : "); // this.textRoleId = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "id", textRoleId); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textRoleId.setLayoutData(gridData); this.textRoleId.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent arg0) { update(); } }); // ???? if (this.isModifyDialog) { if (permission) { this.mode = PropertyDefineConstant.MODE_MODIFY; } else { this.mode = PropertyDefineConstant.MODE_SHOW; } } else { this.mode = PropertyDefineConstant.MODE_ADD; } /* * ?? */ // label = new Label(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "role", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("role.name") + " : "); // this.textRoleName = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "name", textRoleName); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textRoleName.setLayoutData(gridData); this.textRoleName.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); this.textDescription.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent arg0) { update(); } }); // ? // ???????????????????? 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??????????? RoleInfo info = null; if (this.roleId != null) { try { AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(m_managerComposite.getText()); info = wrapper.getRoleInfo(this.roleId); this.setInputData(info); } catch (InvalidRole_Exception e) { MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); } catch (Exception e) { m_log.warn("customizeDialog(), " + HinemosMessage.replace(e.getMessage()), e); MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.hinemos.failure.unexpected") + ", " + HinemosMessage.replace(e.getMessage())); } } else { info = new RoleInfo(); this.setInputData(info); } } /** * ???? * * @return ? * * @see com.clustercontrol.dialog.CommonDialog#validate() */ @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; RoleInfo roleInfo = this.inputData; if (roleInfo == null) { return result; } AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(this.m_managerComposite.getText()); if (!this.isModifyDialog) { // ??? try { wrapper.addRoleInfo(roleInfo); result = true; Object[] arg = { this.m_managerComposite.getText() }; // MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.accesscontrol.26", arg)); } catch (RoleDuplicate_Exception e) { //ID? String args[] = { roleInfo.getRoleId() }; // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.33", args)); } catch (FacilityDuplicate_Exception e) { //ID? String args[] = { roleInfo.getRoleId() }; // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.repository.26", 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.accesscontrol.27") + errMessage); } } else { // ?? try { wrapper.modifyRoleInfo(roleInfo); result = true; Object[] arg = { this.m_managerComposite.getText() }; // MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.accesscontrol.28", arg)); } catch (Exception e) { String errMessage = ""; if (e instanceof InvalidRole_Exception) { // ??? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); } else if (e instanceof UnEditableRole_Exception) { //??????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.40")); } else { errMessage = ", " + HinemosMessage.replace(e.getMessage()); } MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.accesscontrol.29") + errMessage); } } return result; } /** * ? * */ public void update() { // // ID if ("".equals(this.textRoleId.getText())) { this.textRoleId.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.textRoleId.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } // ?? if ("".equals(this.textRoleName.getText())) { this.textRoleName.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.textRoleName.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } } /** * ??????ID????? * * @param roleInfo ???? */ protected void setInputData(RoleInfo roleInfo) { this.inputData = roleInfo; // ???? // ID if (roleInfo.getRoleId() != null) { this.textRoleId.setText(roleInfo.getRoleId()); } // ?? if (roleInfo.getRoleName() != null) { this.textRoleName.setText(roleInfo.getRoleName()); } // if (roleInfo.getDescription() != null) { this.textDescription.setText(roleInfo.getDescription()); } // if (this.mode == PropertyDefineConstant.MODE_SHOW || this.mode == PropertyDefineConstant.MODE_MODIFY) { this.textRoleId.setEnabled(false); } else { this.textRoleId.setEnabled(true); } // ? this.update(); } /** * ???RoleIDRole???????????<BR> * ????????null????????? * * @return */ private RoleInfo createInputData() { final RoleInfo info = new RoleInfo(); info.setRoleId(this.textRoleId.getText()); info.setRoleName(this.textRoleName.getText()); info.setDescription(this.textDescription.getText()); return info; } /** * ???? * * @return ? */ @Override protected String getOkButtonText() { if (this.isModifyDialog) { return Messages.getString("modify"); //$NON-NLS-1$ } else { return Messages.getString("register"); //$NON-NLS-1$ } } /** * ???? * * @return ? */ @Override protected String getCancelButtonText() { return Messages.getString("cancel"); //$NON-NLS-1$ } }