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.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.InvalidRole_Exception; import com.clustercontrol.ws.access.UnEditableUser_Exception; import com.clustercontrol.ws.access.UserDuplicate_Exception; import com.clustercontrol.ws.access.UserInfo; /** * [??]?? * * @version 4.0.0 * @since 2.0.0 */ public class UserDialog extends CommonDialog { /** */ private int mode = 0; /** */ private boolean isModifyDialog = false; /** ID */ private Text textUserId = null; /** ?? */ private Text textUserName = null; /** */ private Text textDescription = null; /** */ private Text textMailAddress = null; /** */ public static final int WIDTH = 15; /** */ public static final int WIDTH_LABEL = 4; /** */ public static final int WIDTH_TEXT = 10; /** ??? */ private UserInfo inputData; private boolean permission = false; // ???????? /** ???? */ private ManagerListComposite m_managerComposite = null; private String managerName = null; /** * * * @param parent * @param managerName ??? * @param uid ID * @param isModifyDialog ???????true */ public UserDialog(Shell parent, String managerName, UserInfo userInfo, boolean isModifyDialog) { super(parent); this.managerName = managerName; this.inputData = userInfo; 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.user.create.modify")); //$NON-NLS-1$ // 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, "userid", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.horizontalSpan = WIDTH_LABEL; label.setLayoutData(gridData); label.setText(Messages.getString("user.id") + " : "); // this.textUserId = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "id", textUserId); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textUserId.setLayoutData(gridData); this.textUserId.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, "username", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("user.name") + " : "); // this.textUserName = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "name", textUserName); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textUserName.setLayoutData(gridData); this.textUserName.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(); } }); /* * */ // label = new Label(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "mailaddress", label); gridData = new GridData(); gridData.horizontalSpan = WIDTH_LABEL; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("mail.address") + " : "); // this.textMailAddress = new Text(parent, SWT.BORDER | SWT.LEFT); WidgetTestUtil.setTestId(this, "mailaddress", textMailAddress); gridData = new GridData(); gridData.horizontalSpan = WIDTH_TEXT; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; this.textMailAddress.setLayoutData(gridData); this.textMailAddress.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); this.setInputData(); } /** * ???? * * @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; UserInfo userInfo = this.inputData; if (userInfo == null) { return result; } AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(m_managerComposite.getText()); if (!this.isModifyDialog) { // ??? try { wrapper.addUserInfo(userInfo); result = true; Object[] arg = { this.m_managerComposite.getText() }; // MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.accesscontrol.7", arg)); } catch (UserDuplicate_Exception e) { //ID? String args[] = { userInfo.getUserId() }; // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.20", 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.8") + errMessage); } } else { // ?? try { wrapper.modifyUserInfo(userInfo); result = true; Object[] arg = { this.m_managerComposite.getText() }; // MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.accesscontrol.9", 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 UnEditableUser_Exception) { // ???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.38")); } else { errMessage = ", " + HinemosMessage.replace(e.getMessage()); } MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.accesscontrol.10") + errMessage); } } return result; } /** * ? * */ public void update() { // // ID if ("".equals(this.textUserId.getText())) { this.textUserId.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.textUserId.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } // ?? if ("".equals(this.textUserName.getText())) { this.textUserName.setBackground(RequiredFieldColorConstant.COLOR_REQUIRED); } else { this.textUserName.setBackground(RequiredFieldColorConstant.COLOR_UNREQUIRED); } } /** * ??????ID????? * * @param userInfo ???? */ protected void setInputData() { // ???? // ID if (inputData.getUserId() != null) { this.textUserId.setText(inputData.getUserId()); } // ?? if (inputData.getUserName() != null) { this.textUserName.setText(inputData.getUserName()); } // if (inputData.getDescription() != null) { this.textDescription.setText(inputData.getDescription()); } // if (inputData.getMailAddress() != null) { this.textMailAddress.setText(inputData.getMailAddress()); } // if (this.mode == PropertyDefineConstant.MODE_SHOW || this.mode == PropertyDefineConstant.MODE_MODIFY) { this.textUserId.setEnabled(false); } else { this.textUserId.setEnabled(true); } /* *** ??? *** start *** List<String> roleList = new ArrayList<String>(); roleList.add("AccessControlRead"); roleList.add("AccessControlWrite"); roleList.add("CalendarRead"); roleList.add("CalendarWrite"); roleList.add("HinemosUser"); roleList.add("InfraExecute"); roleList.add("InfraRead"); roleList.add("InfraWrite"); roleList.add("JobManagementExecute"); roleList.add("JobManagementRead"); roleList.add("JobManagementWrite"); roleList.add("MaintenanceRead"); roleList.add("MaintenanceWrite"); roleList.add("MonitorResultRead"); roleList.add("MonitorResultWrite"); roleList.add("MonitorSettingRead"); roleList.add("MonitorSettingWrite"); roleList.add("NotifyRead"); roleList.add("NotifyWrite"); roleList.add("PerformanceExecute"); roleList.add("PerformanceRead"); roleList.add("RepositoryExecute"); roleList.add("RepositoryRead"); roleList.add("RepositoryWrite"); roleList.add("VmManagementExecute"); roleList.add("VmManagementRead"); roleList.add("VmManagementWrite"); *** ??? *** end *** */ // ? this.update(); } /** * ???ID???????????<BR> * ????????null????????? * * @return */ private UserInfo createInputData() { final UserInfo info = new UserInfo(); info.setUserId(this.textUserId.getText()); info.setUserName(this.textUserName.getText()); info.setDescription(this.textDescription.getText()); info.setMailAddress(this.textMailAddress.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$ } }