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.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Locale; 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.accesscontrol.util.SystemPrivilegePropertyUtil; 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.InvalidSetting_Exception; import com.clustercontrol.ws.access.RoleInfo; import com.clustercontrol.ws.access.SystemPrivilegeInfo; import com.clustercontrol.ws.access.UnEditableRole_Exception; import com.clustercontrol.ws.access.UserDuplicate_Exception; /** * [?]?? * * @version 4.0.0 * @since 2.0.0 */ public class SystemPrivilegeDialog extends CommonDialog { // private static Log m_log = LogFactory.getLog(SystemPrivilegeDialog.class); /**??? */ private String managerName = ""; /** ID */ private String roleId = ""; /** ? */ private List listNotRoleSystemPrivilege = null; /** ? */ private List listRoleSystemPrivilege = null; /** ? */ private Button buttonRoleSystemPrivilege = null; /** ? */ private Button buttonNotRoleSystemPrivilege = null; /** ? */ private Button buttonSortRole = null; /** */ private Button buttonSortFunction = 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; /** * * * @param parent * @param managerName ??? * @param uid ID */ public SystemPrivilegeDialog(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); } /** * ?????? */ public List sortFunctionPrivilege(List sortItem, Boolean sortOrder) { String[] items = sortItem.getItems(); Map<String, String> keys_Map = new HashMap<String, String>(); ArrayList<String> item_keys = new ArrayList<String>(); //item??KEY????????? for (String value : items) { //????KEY?KEY?? SystemPrivilegeInfo systemPrivilegeInfo = SystemPrivilegePropertyUtil.getFunctionPrivilege(managerName, value); String func = ""; String priv = ""; if (systemPrivilegeInfo != null) { func = systemPrivilegeInfo.getSystemFunction(); priv = systemPrivilegeInfo.getSystemPrivilege(); } String key = ""; if (sortOrder) { key = func + priv; } else { key = priv + func; } item_keys.add(key); keys_Map.put(key, value); } Collections.sort(item_keys); sortItem.removeAll(); //sort?KEY??map?item?? for (String sortKey : item_keys) { sortItem.add(keys_Map.get(sortKey)); } return sortItem; } /** * ???? * * @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.system.privilege.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(), " + 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, null, 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, "systemprivilegelist", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("SystemPrivilegeDialog.not_role_system_privilege")); // ? this.listNotRoleSystemPrivilege = new List(compositeNotRole, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); WidgetTestUtil.setTestId(this, "all", listNotRoleSystemPrivilege); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.heightHint = this.listNotRoleSystemPrivilege.getItemHeight() * 12; this.listNotRoleSystemPrivilege.setLayoutData(gridData); /* * ?? */ Composite compositeButton = new Composite(parent, SWT.NONE); WidgetTestUtil.setTestId(this, "button", 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, "blank", 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.buttonRoleSystemPrivilege = this.createButton(compositeButton, Messages.getString("SystemPrivilegeDialog.role_system_privilege_button")); WidgetTestUtil.setTestId(this, "assign", buttonRoleSystemPrivilege); this.buttonRoleSystemPrivilege.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String[] items = listNotRoleSystemPrivilege.getSelection(); for (String item : items) { listNotRoleSystemPrivilege.remove(item); listRoleSystemPrivilege.add(item); } } }); // ? dummy = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "notrolesystemprivilege", dummy); this.buttonNotRoleSystemPrivilege = this.createButton(compositeButton, Messages.getString("SystemPrivilegeDialog.not_role_system_privilege_button")); WidgetTestUtil.setTestId(this, "unassign", buttonNotRoleSystemPrivilege); this.buttonNotRoleSystemPrivilege.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String[] items = listRoleSystemPrivilege.getSelection(); for (String item : items) { listRoleSystemPrivilege.remove(item); listNotRoleSystemPrivilege.add(item); } } }); // dummy = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "buttonsortfunction", dummy); this.buttonSortFunction = this.createButton(compositeButton, Messages.getString("SystemPrivilegeDialog.sort_function_button")); WidgetTestUtil.setTestId(this, "sortfunction", buttonSortFunction); this.buttonSortFunction.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { listRoleSystemPrivilege = sortFunctionPrivilege(listRoleSystemPrivilege, true); listNotRoleSystemPrivilege = sortFunctionPrivilege(listNotRoleSystemPrivilege, true); } }); // ? dummy = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, "buttonsortrole", dummy); this.buttonSortRole = this.createButton(compositeButton, Messages.getString("SystemPrivilegeDialog.sort_privilege_button")); WidgetTestUtil.setTestId(this, "sortrole", buttonSortRole); this.buttonSortRole.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { listRoleSystemPrivilege = sortFunctionPrivilege(listRoleSystemPrivilege, false); listNotRoleSystemPrivilege = sortFunctionPrivilege(listNotRoleSystemPrivilege, false); } }); // label = new Label(compositeButton, SWT.NONE); WidgetTestUtil.setTestId(this, null, label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); /* * ?? */ Composite compositeRole = new Composite(parent, SWT.NONE); WidgetTestUtil.setTestId(this, null, 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, "systemprivilege", label); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; label.setLayoutData(gridData); label.setText(Messages.getString("SystemPrivilegeDialog.role_system_privilege")); // ? this.listRoleSystemPrivilege = new List(compositeRole, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); WidgetTestUtil.setTestId(this, null, listRoleSystemPrivilege); gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.heightHint = this.listRoleSystemPrivilege.getItemHeight() * 12; this.listRoleSystemPrivilege.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); 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(this.managerName); wrapper.replaceSystemPrivilegeRole(this.roleId, roleInfo.getSystemPrivilegeList()); result = true; Object[] arg = { this.managerName }; // MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.accesscontrol.47", arg)); } catch (UserDuplicate_Exception e) { //ID? String args[] = { roleInfo.getRoleId() }; // ID???????? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.48", args)); } catch (InvalidSetting_Exception e) { // ? - ???????????? String args[] = { Messages.getString("system_privilege.function.repository", Locale.getDefault()) + " - " + Messages.getString("system_privilege.privilege.read", Locale.getDefault()) }; MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.51", 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) { // ??????ADMINISTRATORS MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.44")); } else { errMessage = ", " + HinemosMessage.replace(e.getMessage()); } MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.accesscontrol.48") + errMessage); } return result; } /** * ??? * * @param roleInfo ???? */ protected void setInputData(String managerName, RoleInfo roleInfo) { this.inputData = roleInfo; // ???? java.util.List<String> allSystemPrivilegeList = null; java.util.List<SystemPrivilegeInfo> roleSystemPrivilegeKeyList = null; // ?? allSystemPrivilegeList = SystemPrivilegePropertyUtil.getSystemPrivilegeNameList(managerName); //TODO ?????????????????????SWT????????? java.util.Collections.sort(allSystemPrivilegeList); // ?? try { AccessEndpointWrapper wrapper = AccessEndpointWrapper.getWrapper(managerName); roleSystemPrivilegeKeyList = wrapper.getSystemPrivilegeInfoListByRoleId(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("getOwnUserList(), " + 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()); } java.util.List<String> roleSystemPrivilegeValueList = getSystemPrivilegeValueList( roleSystemPrivilegeKeyList); // ? for (String SystemPrivilege : allSystemPrivilegeList) { if (roleSystemPrivilegeValueList.contains(SystemPrivilege)) { this.listRoleSystemPrivilege.add(SystemPrivilege); } else { this.listNotRoleSystemPrivilege.add(SystemPrivilege); } } } /** * ?????<BR> * ???????<code>null</code>??? * * @return */ private RoleInfo createInputData() { RoleInfo info = this.inputData; java.util.List<SystemPrivilegeInfo> roleSystemPrivilegeList = info.getSystemPrivilegeList(); roleSystemPrivilegeList.clear(); if (this.listRoleSystemPrivilege.getItemCount() > 0) { roleSystemPrivilegeList .addAll(getSystemPrivilegeKeyList(Arrays.asList(this.listRoleSystemPrivilege.getItems()))); } 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; } /** * ????? * * @param beforeList ?? * @return ? */ private java.util.List<SystemPrivilegeInfo> getSystemPrivilegeKeyList(java.util.List<String> beforeList) { java.util.List<SystemPrivilegeInfo> afterList = new ArrayList<SystemPrivilegeInfo>(); for (String beforeStr : beforeList) { SystemPrivilegeInfo info = SystemPrivilegePropertyUtil.getFunctionPrivilege(managerName, beforeStr); // ??? if (info != null) { afterList.add(info); } } return afterList; } /** * ????? * * @param beforeList ? * @return ?? */ private java.util.List<String> getSystemPrivilegeValueList(java.util.List<SystemPrivilegeInfo> beforeList) { java.util.List<String> afterList = new ArrayList<String>(); for (SystemPrivilegeInfo systemPrivilegeInfo : beforeList) { afterList.add(SystemPrivilegePropertyUtil.getSystemPrivilegeName(managerName, systemPrivilegeInfo)); } return afterList; } }