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 java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; 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.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; import com.clustercontrol.accesscontrol.util.AccessEndpointWrapper; 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.RoleInfo; import com.clustercontrol.ws.access.UnEditableRole_Exception; import com.clustercontrol.ws.access.UserDuplicate_Exception; import com.clustercontrol.ws.access.UserInfo; /** * [??]?? * * @version 4.1.0 * @since 4.1.0 */ public class RoleSettingDialog extends CommonDialog { // private static Log m_log = LogFactory.getLog(RoleSettingDialog.class); /** ID */ private String roleId = ""; /** ??? */ private String managerName = ""; /** */ private List listNotRoleUser = null; /** */ private List listRoleUser = null; /** */ private Button buttonRoleUser = null; /** */ private Button buttonNotRoleUser = 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; /** ??????ID????? */ private HashMap<String, String> mapNotRoleUser = null; /** ??????ID????? */ private HashMap<String, String> mapRoleUser = null; /** * * * @param parent * @paramn managerName ??? * @param uid ID */ public RoleSettingDialog(Shell parent, String managerName, String roleId) { super(parent); this.managerName = managerName; this.roleId = roleId; } /** * ????? * * @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.setting")); // ?? RoleInfo info = null; try { AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(this.managerName); info = wrapper.getRoleInfo(this.roleId); } catch (InvalidRole_Exception e) { MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); throw new InternalError(e.getMessage()); } 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())); throw new InternalError(e.getMessage()); } // GridLayout layout = new GridLayout(1, true); layout.marginWidth = 10; layout.marginHeight = 10; layout.numColumns = WIDTH; parent.setLayout(layout); /* * ?? */ // Label label = new Label(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "role", label); GridData gridData = new GridData(); gridData.horizontalSpan = WIDTH; gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("role.name") + " : " + info.getRoleName()); /* * ? */ Composite compositeNotRole = new Composite(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "all", compositeNotRole); layout = new GridLayout(1, true); layout.numColumns = 1; compositeNotRole.setLayout(layout); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.horizontalSpan = 6; gridData.verticalSpan = 2; compositeNotRole.setLayoutData(gridData); // label = new Label(compositeNotRole, SWT.NONE); WidgetTestUtil.setTestId(this, "alluserlist", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("RoleSettingDialog.not_role_user")); // this.listNotRoleUser = new List(compositeNotRole, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); WidgetTestUtil.setTestId(this, "all", compositeNotRole); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.heightHint = this.listNotRoleUser.getItemHeight() * 12; this.listNotRoleUser.setLayoutData(gridData); // ? this.mapNotRoleUser = new HashMap<String, String>(); /* * ?? */ Composite compositeButton = new Composite(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "composite", compositeButton); layout = new GridLayout(1, true); layout.numColumns = 1; compositeButton.setLayout(layout); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.horizontalSpan = 3; compositeButton.setLayoutData(gridData); // label = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "blank1", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); // Label dummy = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "dummy", dummy); this.buttonRoleUser = this.createButton(compositeButton, Messages.getString("RoleSettingDialog.role_user_button")); WidgetTestUtil.setTestId(this, "assign", buttonRoleUser); this.buttonRoleUser.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String[] items = listNotRoleUser.getSelection(); for (String item : items) { listNotRoleUser.remove(item); listRoleUser.add(item); // ?????ID?HashMap??????? mapRoleUser.put(item, mapNotRoleUser.get(item)); mapNotRoleUser.remove(item); } } }); // dummy = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "unassign", dummy); this.buttonNotRoleUser = this.createButton(compositeButton, Messages.getString("RoleSettingDialog.not_role_user_button")); WidgetTestUtil.setTestId(this, "unassign", buttonNotRoleUser); this.buttonNotRoleUser.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String[] items = listRoleUser.getSelection(); for (String item : items) { listRoleUser.remove(item); listNotRoleUser.add(item); // ?????ID?HashMap??????? mapNotRoleUser.put(item, mapRoleUser.get(item)); mapRoleUser.remove(item); } } }); // label = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "blank", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); /* * ? */ Composite compositeRole = new Composite(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "role", compositeRole); layout = new GridLayout(1, true); layout.numColumns = 1; compositeRole.setLayout(layout); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.horizontalSpan = 6; gridData.verticalSpan = 2; compositeRole.setLayoutData(gridData); // label = new Label(compositeRole, SWT.NONE); WidgetTestUtil.setTestId(this, "userlist", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("RoleSettingDialog.role_user")); // this.listRoleUser = new List(compositeRole, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); WidgetTestUtil.setTestId(this, "roleuserlist", listRoleUser); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.heightHint = this.listRoleUser.getItemHeight() * 12; this.listRoleUser.setLayoutData(gridData); // ? this.mapRoleUser = new HashMap<String, String>(); // ? // ???????????????????? 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(this.managerName, 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; } try { AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(managerName); wrapper.assignUserRole(roleInfo.getRoleId(), roleInfo.getUserList()); result = true; Object[] arg = { managerName }; // MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.accesscontrol.34", arg)); } catch (UserDuplicate_Exception e) { //ID? String args[] = { roleInfo.getRoleId() }; // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.35", args)); } 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) { // ????????ALL_USERS MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.43")); } else { errMessage = ", " + HinemosMessage.replace(e.getMessage()); } MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.accesscontrol.35") + errMessage); } return result; } /** * ??? * * @param roleInfo ???? */ protected void setInputData(String managerName, RoleInfo roleInfo) { this.inputData = roleInfo; // ???? java.util.List<UserInfo> allUserList = null; // ? try { AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(managerName); allUserList = wrapper.getUserInfoList(); // Collections.sort(allUserList, new Comparator<UserInfo>() { @Override public int compare(UserInfo o1, UserInfo o2) { return o1.getUserId().compareTo(o2.getUserId()); } }); } catch (InvalidRole_Exception e) { // ??? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); } catch (Exception e) { // ? m_log.warn("getOwnUserList(), " + HinemosMessage.replace(e.getMessage()), e); MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.hinemos.failure.unexpected") + ", " + HinemosMessage.replace(e.getMessage())); } if (allUserList == null) throw new InternalError("allUserList is null"); // ? String listName = null; for (UserInfo userInfo : allUserList) { //????? listName = userInfo.getUserName() + "(" + userInfo.getUserId() + ")"; if (roleInfo.getUserList().contains(userInfo.getUserId())) { this.listRoleUser.add(listName); this.mapRoleUser.put(listName, userInfo.getUserId()); } else { this.listNotRoleUser.add(listName); this.mapNotRoleUser.put(listName, userInfo.getUserId()); } } } /** * ?????<BR> * ???????<code>null</code>??? * * @return */ private RoleInfo createInputData() { RoleInfo info = this.inputData; // ??? java.util.List<String> roleUserList = info.getUserList(); roleUserList.clear(); if (this.listRoleUser.getItemCount() > 0) { roleUserList.addAll(getUserIdList(this.mapRoleUser)); } return info; } /** * ???? * * @return ? */ @Override protected String getOkButtonText() { return Messages.getString("setup"); } /** * ???? * * @return ? */ @Override protected String getCancelButtonText() { return Messages.getString("cancel"); //$NON-NLS-1$ } /** * ????? * * @param parent * ?? * @param label * ? * @return ??? */ private Button createButton(Composite parent, String label) { Button button = new Button(parent, SWT.NONE); WidgetTestUtil.setTestId(this, null, button); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; button.setLayoutData(gridData); button.setText(label); return button; } /** * ???????ID?? * * @param userMap HashMap * @return ID? */ private java.util.List<String> getUserIdList(java.util.HashMap<String, String> userMap) { java.util.List<String> resultList = new ArrayList<String>(); for (Map.Entry<String, String> entry : userMap.entrySet()) { resultList.add(entry.getValue()); } return resultList; } }