package com.vci.client.uif.engine.client; import java.awt.Dimension; import java.awt.FlowLayout; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Map; import com.vci.client.LogonApplication; import com.vci.client.framework.delegate.RoleRightClientDelegate; import com.vci.client.framework.delegate.SystemCfgClientDelegate; import com.vci.client.framework.rightdistribution.object.RoleRightObject; import com.vci.client.framework.util.RightControlUtil; import com.vci.client.portal.utility.DataModelFactory; import com.vci.client.portal.utility.PLDefination; import com.vci.client.portal.utility.UITools; import com.vci.client.ui.exception.VCIException; import com.vci.client.ui.swing.VCISwingUtil; import com.vci.client.ui.swing.components.VCIJButton; import com.vci.client.ui.swing.components.VCIJPanel; import com.vci.client.uif.actions.client.BOAFactory; import com.vci.client.uif.engine.client.tableArea.ButtonActionListener; import com.vci.corba.common.VCIError; import com.vci.corba.portal.data.PLAction; import com.vci.corba.portal.data.PLTabButton; import com.vci.mw.ClientContextVariable; public class BasePanel extends VCIJPanel{ /** * */ private static final long serialVersionUID = -160889706670945852L; private DataModelFactory factory = null; private String type = ""; private String context = ""; private PLDefination defination; public BasePanel(){ } public BasePanel(DataModelFactory factory, String type, String context, PLDefination defination) { super(); this.factory = factory; this.type = type; this.context = context; this.defination = defination; } public DataModelFactory getFactory() { return factory; } public void setFactory(DataModelFactory factory) { this.factory = factory; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public PLDefination getDefination() { return defination; } public void setDefination(PLDefination defination) { this.defination = defination; } public VCIJPanel addButtonsToSelf(String tabId, IRegionPanel regionPanel){ VCIJButton[] btns = getButtonsByTabPageId(tabId, regionPanel); addButtonsToPanel(this, btns); return this; } public VCIJPanel addButtonsPanel(String tabId, IRegionPanel regionPanel, VCIJPanel pal){ VCIJButton[] btns = getButtonsByTabPageId(tabId, regionPanel); pal = addButtonsToPanel(pal, btns); return pal; } private VCIJPanel addButtonsToPanel(VCIJPanel pal, VCIJButton[] btns){ pal.setLayout(new FlowLayout(FlowLayout.LEADING)); for (VCIJButton btn : btns) { pal.add(btn); } return pal; } private Comparator COMPARABLE_COMAPRATOR = new Comparator() { //随第一个参数小于、等于或大于第二个参数而分别返回负整数、零或正整数。 public int compare(PLTabButton o1, PLTabButton o2) { int res = -1; if(o1.plSeq < o2.plSeq) res = -1; else if(o1.plSeq == o2.plSeq) res = 0; else if(o1.plSeq > o2.plSeq) res = 1; return res; } }; private Comparator COMPARABLE_COMAPRATOR_RRO = new Comparator() { @SuppressWarnings("unchecked") //随第一个参数小于、等于或大于第二个参数而分别返回负整数、零或正整数。 public int compare(RoleRightObject o1, RoleRightObject o2) { int res = -1; if(o1.getRightValue() < o2.getRightValue()) res = -1; else if(o1.getRightValue() == o2.getRightValue()) res = 0; else if(o1.getRightValue() > o2.getRightValue()) res = 1; return res; } }; /** * 根据TagId、IRegionPanl 返回Tab所在Definition定义的Buttons * @param tabId tabPageId * @param regionPanel IRegionPanel * @see IRegionPanel * @return * @throws */ public VCIJButton[] getButtonsByTabPageId(String tabId, IRegionPanel regionPanel) { VCIJButton[] btns = new VCIJButton[0]; List btnList = new LinkedList(); try { RoleRightClientDelegate delegate = new RoleRightClientDelegate(); PLTabButton[] buttons = new PLTabButton[0]; String pageDefId = regionPanel.getPageDefinition().plOId; LinkedList rightValueList = new LinkedList(); String uiRightSwith = new SystemCfgClientDelegate().getConfigValue("ui.right.swith"); // UI权限开关是否关闭 boolean uiRightSwithIsOff = "off".equalsIgnoreCase(uiRightSwith); if(RightControlUtil.isAdminOrDeveloperOrRoot(ClientContextVariable.getInvocationInfo().userName)){ // 显示标识UI权限开关为关闭 buttons = UITools.getService().getPLTabButtonsByTableOId(pageDefId); uiRightSwithIsOff = true; } else { // 当前用户拥有的角色对于 pageDefId 对应的模块 所拥有的角色权限数据 RoleRightObject[] rights = delegate.getRoleRightByModule( pageDefId, ClientContextVariable.getInvocationInfo().userName, LogonApplication.currentUserRoleRights); // 角色对于该模块没有权限,直接返回 if(rights.length <= 0) { return btns; } for(RoleRightObject rro : rights){ long rightValue = rro.getRightValue(); rightValueList.add(rightValue); } // 根据角色权限(值)进行小-》大排序,取权限值最大的有效 Arrays.sort(rights, COMPARABLE_COMAPRATOR_RRO); long right = rights[rights.length - 1].getRightValue(); if (right == 0) { return btns; } // 查看模块上定义的按钮 buttons = UITools.getService().getPLTabButtonsByTableOId(pageDefId); } // 按钮排序 Arrays.sort(buttons, COMPARABLE_COMAPRATOR); if (buttons.length < 1) { return btns; } for (int i = 0; i < buttons.length; i++) { if(buttons[i].show.equals("0")){ if(buttons[i].authorization.equals("0")){ if (uiRightSwithIsOff || hasRight(rightValueList, buttons[i].plSeq)) { PLAction action = UITools.getService().getPLActionById(buttons[i].plActionOId); VCIJButton btn = getVCIJButton(buttons[i], regionPanel, action); btnList.add(btn); } } else { PLAction action = UITools.getService().getPLActionById(buttons[i].plActionOId); VCIJButton btn = getVCIJButton(buttons[i], regionPanel, action); btnList.add(btn); } } } } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (VCIException e) { // TODO Auto-generated catch block e.printStackTrace(); } return btnList.toArray(new VCIJButton[]{}); } public VCIJButton getVCIJButton(PLTabButton button, IRegionPanel regionPanel, PLAction action) { VCIJButton btn = new VCIJButton(button.plLabel); btn.setActionCommand(action.plCode); if(button.plDesc != null && !"".equals(button.plDesc)){ btn.setToolTipText(button.plDesc); } ButtonActionListener bal = new ButtonActionListener( getType(), getContext(), button, action, regionPanel, btn); String icon = BOAFactory.getActionIcon(action.plCode); // add by xchao 2015.07.16 end // 增加按钮图标解析,支持定义及参数定义 String iconPath = button.iconPath; Map paramMap = bal.buildActionParamsMap(); // 按钮上直接定义的图标 if(iconPath == null || "".equals(iconPath) || "undefined".equals(iconPath)){ // 如果直接定义的为空,则尝试从按钮参照上取 if(paramMap.containsKey("iconPath")){ icon = paramMap.get("iconPath"); // 按钮参照上的也空为,则取默认规则处理的图标 if(icon == null || "".equals(icon)){ icon = BOAFactory.getActionIcon(action.plCode); } } } else { icon = iconPath; } // add by xchao 2015.07.16 end if(icon != null && !"".equals(icon)){ try{ btn.setIcon(VCISwingUtil.createImageIcon(icon)); }catch(Exception e){ e.printStackTrace(); } } btn.addActionListener(bal); //add by guo 设置button的大小 Dimension btnSize = btn.getPreferredSize();//btn的显示参数 if(paramMap.containsKey("bheight")){ int bheight = parseInt(paramMap.get("bheight"), btnSize.height); btnSize.height = bheight; } if(paramMap.containsKey("bwidth")){ int bwidth = parseInt(paramMap.get("bwidth"), btnSize.width); btnSize.width = bwidth; } btn.setPreferredSize(btnSize); //add by guo end return btn; } private int parseInt(String value, int num) { int tvalue = num; try { tvalue = Integer.parseInt(value); } catch (NumberFormatException e) { e.printStackTrace(); } return tvalue; } private boolean hasRight(List rightValueList,int operNum){ boolean res = false; for(Long rightValue : rightValueList){ res = hasRight(rightValue, operNum); if(res){ break; } } return res; } private boolean hasRight(long rightValue,int operNum){ boolean res = false; long preValue = (rightValue >> operNum) & 1; //int preValue = (int)Math.pow(2, operNum); //if(preValue == (rightValue & preValue)){ if (preValue == 1) { res = true; } return res; } }