package com.vci.client.uif.engine.client.tableArea; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Iterator; import com.vci.client.ClientSession; import com.vci.client.portal.utility.UITools; import com.vci.client.ui.swing.components.VCIJDialog; import com.vci.client.ui.swing.components.VCIJOptionPane; import com.vci.client.uif.actions.client.AbstractBatchBusionessOperationAction; import com.vci.client.uif.actions.client.BusinessOperationAction; import com.vci.client.uif.engine.client.IRegionPanel; import com.vci.corba.common.VCIError; import com.vci.corba.portal.data.PLAction; import com.vci.corba.portal.data.PLCommandParameter; import com.vci.corba.portal.data.PLTabButton; import com.vci.mw.ClientContextVariable; public class ButtonActionListener implements ActionListener { private String type = ""; private String context = ""; private PLTabButton button = null; private PLAction action = null; private IRegionPanel regionPanel = null; private BusinessOperationAction boa = null; private VCIJDialog ownerDialog = null; private Component buttonComponent = null; public ButtonActionListener(String type, String context, PLTabButton button, PLAction action, IRegionPanel regionPanel, Component buttonComponent) { super(); this.type = type; this.context = context; this.button = button; this.action = action; this.regionPanel = regionPanel; this.buttonComponent = buttonComponent; } private BusinessOperationAction createBusinessOperationActionInstance(String actionClass){ BusinessOperationAction boa = null; if(actionClass != null && !"".equals(actionClass)){ try { Object obj = Class.forName(actionClass).getConstructor().newInstance(); if(obj instanceof BusinessOperationAction && obj instanceof AbstractBatchBusionessOperationAction){ boa = (BusinessOperationAction)obj; } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return boa; } @Override public void actionPerformed(ActionEvent e) { boolean isExpired = ClientSession.checkExpired(); if(isExpired){ ClientSession.relogin(); return; } invoke(e); } private void invoke(ActionEvent e){ //TODO 从定义中取出KEY String key = getAction().plCode; String actionClass = getAction().plCSClass; // 根据Action的 Class,动态创建实例 boa = createBusinessOperationActionInstance(actionClass); if(boa != null){ boa.setButtonComponent(getButtonComponent()); boa.setOwnerDialog(getOwnerDialog()); // 组织参数Map HashMap paramsMap = buildActionParamsMap(); boa.setButtonParams(paramsMap); //将button参数加入到全局变量中 ClientContextVariable.cleanOtherGlobalVariable(); Iterator itor = paramsMap.keySet().iterator(); while (itor.hasNext()) { String cKey = itor.next(); String cValue = paramsMap.get(cKey); if (cValue == null) { cValue = ""; } ClientContextVariable.putGlobalVariable(cKey, cValue); } boa.setButton(getButton()); boa.setAction(getAction()); boa.setDefination(getRegionPanel().getDefination()); boa.setRegionPanel(regionPanel); // 设置父容器对象 boa.setParentComponent(getRegionPanel().getComponentPanel()); // 设置Button所在区域的DataModel boa.setDataModel(regionPanel.getDataModel()); // 执行 Action ((AbstractBatchBusionessOperationAction)boa).doAction(); } else { VCIJOptionPane.showError(ClientContextVariable.getFrame(), " 未能根据 " + actionClass + " 创建出对应的 Action 实例"); } } public HashMap buildActionParamsMap(){ HashMap paramsMap = new HashMap(); paramsMap.put("buttonOid", getButton().plOId); paramsMap.put("actionOid", getAction().plOId); // 查询出Button上定义的参数,传递给Action // 一般必须要定义 type、context // 即:表明该Buttonr要操作哪个业务类型、使用该业务类型的哪个上下文 // 在Action 的具体的实现中,将提取上面两个参数,进行相关初始化操作 try { PLCommandParameter[] btnParams = UITools.getService().getPLCommandParametersByCommandOId(getButton().plOId); for(PLCommandParameter cp : btnParams){ String key = cp.plKey; String value = cp.plValue; paramsMap.put(key, value); } } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } return paramsMap; } public PLTabButton getButton() { return button; } public void setButton(PLTabButton button) { this.button = button; } public PLAction getAction() { return action; } public void setAction(PLAction action) { this.action = action; } 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 IRegionPanel getRegionPanel() { return regionPanel; } public void setRegionPanel(IRegionPanel regionPanel) { this.regionPanel = regionPanel; } public BusinessOperationAction getBoa() { return boa; } public void setBoa(BusinessOperationAction boa) { this.boa = boa; } public VCIJDialog getOwnerDialog() { return ownerDialog; } public void setOwnerDialog(VCIJDialog ownerDialog) { this.ownerDialog = ownerDialog; if(boa != null){ boa.setOwnerDialog(ownerDialog); } } /** * action 所在的 * @return */ public Component getButtonComponent() { return buttonComponent; } public void setButtonComponent(Component buttonComponent) { this.buttonComponent = buttonComponent; } }