package com.vci.client.portal.UI.v3.comptdesign.compt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.text.JTextComponent;
import com.vci.client.omd.linktype.LinkTypeStart;
import com.vci.client.oq.QTClient;
import com.vci.client.portal.UI.v3.comptdesign.UIComptDesignDialog;
import com.vci.client.portal.UI.v3.comptdesign.compt.al.BaseActionListener;
import com.vci.client.portal.utility.PLDefination;
import com.vci.client.portal.utility.UITools;
import com.vci.client.ui.swing.components.VCIJComboBox;
import com.vci.client.ui.swing.components.VCIJLabel;
import com.vci.client.ui.swing.components.VCIJOptionPane;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJTextArea;
import com.vci.client.ui.swing.components.VCIJTextField;
import com.vci.common.portal.enums.PortalVIType;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.ltm.LinkType;
import com.vci.corba.portal.data.PLPageDefination;
/**
* UI 组件基础面板
*
*
Title:
* Description:
* Copyright: Copyright (c) 2016
* Company: VCI
* @author xiongchao
* @time 2017-2-22
* @version 1.0
*/
public abstract class BaseComptPanel extends VCIJPanel implements ComptPanelInterface{
/**
*
*/
private static final long serialVersionUID = 968715722185037438L;
private PLPageDefination pageDefination = null;
private PLDefination defination = null;
protected boolean isCompleted = false;
private UIComptDesignDialog ownedUIComptDesignDialog = null;
protected Map> txtRefBtnALMap = new LinkedHashMap>();
public BaseComptPanel(UIComptDesignDialog ownedUIComptDesignDialog){
this.ownedUIComptDesignDialog = ownedUIComptDesignDialog;
}
public abstract boolean checkInputIsOk();
public abstract void buildPanel();
private List> actions = new LinkedList>();
public void initActionListener(){
initBtmTypeActionListener();
initLinkTypeActionListener();
initActionListenerObservers();
}
/**
* 由子类实现的初始化BtmType模式下各按钮的ActionListener
*/
protected abstract void initBtmTypeActionListener();
/**
* 由子类实现的初始化LinkType模式下各按钮的ActionListener
*/
protected abstract void initLinkTypeActionListener();
/**
* 为各按钮的ActionListeners之间配置观察\被观察关系
*/
protected void initActionListenerObservers(){
for (BaseActionListener> ba : actions) {
for (BaseActionListener> baa : actions) {
if(!ba.equals(baa)){
ba.addObserver(baa);
}
}
}
}
public List> getActions() {
return actions;
}
public void setActions(List> actions) {
this.actions = actions;
}
@Override
public void setDataToUI(PLPageDefination pageDefination){
if(isCompleted) return;
setPageDefination(pageDefination);
if(pageDefination != null){
try {
defination = UITools.getPLDefination(pageDefination.plDefination);
if(defination != null){
setDefination(defination);
setDataToUISpec(defination);
}
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
isCompleted = true;
}
@Override
public boolean isCompleted(){
return isCompleted;
}
public abstract void setDataToUISpec(PLDefination defination);
public void setPageDefination(PLPageDefination pageDefination){
this.pageDefination = pageDefination;
}
public PLPageDefination getPageDefination() {
return pageDefination;
}
public PLDefination getDefination() {
return defination;
}
public void setDefination(PLDefination defination) {
this.defination = defination;
}
/**
*
* @param srcCombox
* @param destQTNamesCombox
*/
protected void refrechQueryTemplateNamesCombox(VCIJComboBox srcCombox, VCIJComboBox destQTNamesCombox){
destQTNamesCombox.removeAllItems();
String selectedItem = (String) srcCombox.getSelectedItem();
if (selectedItem == null) {
return;
}
try{
String[] qtNames = getQTNamesFromMap(selectedItem);
destQTNamesCombox.setModel(new DefaultComboBoxModel(qtNames));
} catch (VCIError e) {
e.printStackTrace();
}
destQTNamesCombox.updateUI();
}
/**
*
* @param srcCombox
* @param destRefCombox
* @param viType
*/
protected void refreshChangableTablePortalVIs(VCIJComboBox srcCombox, VCIJComboBox destRefCombox,
PortalVIType viType) {
destRefCombox.removeAllItems();
String selectedItem = (String) srcCombox.getSelectedItem();
if (selectedItem == null) {
return;
}
try {
String[] viNames = getTypePortalVINamesFromMap(viType, selectedItem);
destRefCombox.setModel(new DefaultComboBoxModel(viNames));
} catch (VCIError e) {
e.printStackTrace();
}
destRefCombox.updateUI();
}
/**
* 刷新业务类型选择后 可以选择的链接类型(linktype)下拉列表数据
* @param btmCombox
* @param destLinkTypeCombox
*/
protected void refrechChangableLinkType(VCIJComboBox btmCombox, VCIJComboBox destLinkTypeCombox){
destLinkTypeCombox.removeAllItems();
String selectedItem = (String) btmCombox.getSelectedItem();
if (selectedItem == null) {
return;
}
try{
List list = new ArrayList();
list.add("");
LinkType[] lts = LinkTypeStart.getService().getLinkTypes();
for (LinkType lt : lts) {
boolean exist = existInArray(selectedItem, lt.btmItemsFrom);
if(!exist){
exist = existInArray(selectedItem, lt.btmItemsTo);
}
if(exist){
if(!list.contains(lt.name)){
list.add(lt.name);
}
}
}
destLinkTypeCombox.setModel(new DefaultComboBoxModel(list.toArray(new String[0])));
} catch (VCIError e) {
e.printStackTrace();
}
destLinkTypeCombox.updateUI();
}
private boolean existInArray(String value, String[] values){
boolean res = false;
for (String string : values) {
if(string.equals(value)){
res = true;
break;
}
}
return res;
}
protected Map qtNamesMap = new HashMap();
protected String[] getQTNamesFromMap(String type) throws VCIError{
String[] qtNames = new String[0];
if(qtNamesMap.containsKey(type)){
qtNames = qtNamesMap.get(type);
} else {
qtNames = getTypeQTNames(type);
qtNamesMap.put(type, qtNames);
}
return qtNames;
}
/**
* 业务类型下全部的表单名缓存Map
* key:业务类型名
* value:Map
* key: 表单类型(1:表单、0:表格)
* value: 表单名称的一维数组
*/
protected Map> typePortalVINames = new HashMap>();
/**
* 从Dialog组件页面缓存中取出业务类型下指定表单类型的表单名
* @param viType 表单类型
* @param type 业务类型
* @return
* @throws VCIError
*/
private String[] getTypePortalVINamesFromMap(PortalVIType viType, String type) throws VCIError{
if(typePortalVINames.containsKey(type)){
if(typePortalVINames.get(type).containsKey(viType.getIntVal())){
return typePortalVINames.get(type).get(viType.getIntVal());
} else {
String[] viNames = getTypePortalVINames(viType, type);
Map map = typePortalVINames.get(type);
map.put(viType.getIntVal(), viNames);
typePortalVINames.put(type, map);
return viNames;
}
} else {
String[] viNames = getTypePortalVINames(viType, type);
Map map = new HashMap();
map.put(viType.getIntVal(), viNames);
typePortalVINames.put(type, map);
return viNames;
}
}
/**
* 查出指定业务类型下全部的表单名称
* @param viType 表单类型
* @param type 业务类型名
* @return
* @throws VCIError
*/
protected String[] getTypePortalVINames(PortalVIType viType, String type) throws VCIError{
String sql = String.format("" +
"select vi.viname from plportalvi vi " +
"where vi.vitype='%s' and vi.typename='%s' " +
"order by vi.viname ",
String.valueOf(viType.getIntVal()),
type);
List viNameList = new LinkedList();
viNameList.add("");
String[][] kvss = QTClient.getService().queryBySqlWithoutKey(sql);
for(String[] kvs : kvss){
viNameList.add(kvs[0]);
}
String[] viNames = viNameList.toArray(new String[]{});
Map map = new HashMap();
map.put(viType.getIntVal(), viNames);
return viNameList.toArray(new String[]{});
}
/**
* 返回指定类型下的查询模板名称
* @param type
* @return
* @throws VCIError
*/
private String[] getTypeQTNames(String type) throws VCIError{
String sql = String.format(
"select qt.qtname from PL_QTEMPLATE qt where qt.btmname='%s'",
type);
List qtNameList = new LinkedList();
qtNameList.add("");
String[][] kvss = QTClient.getService().queryBySqlWithoutKey(sql);
for(String[] kvs : kvss){
qtNameList.add(kvs[0]);
}
return qtNameList.toArray(new String[]{});
}
private String[] btmNames = null;
protected void loadBtmTypeDataToCombox(VCIJComboBox cbox) throws VCIError{
loadBtmTypeDataToCombox(cbox, -1);
}
protected void loadBtmTypeDataToCombox(VCIJComboBox cbox, int selectedIndex) throws VCIError{
if(btmNames == null){
String sql = "select bt.name from plbtmtype bt";
List list = new LinkedList();
list.add("");
String[][] kvss = QTClient.getService().queryBySqlWithoutKey(sql);
for(String[] kvs : kvss){
list.add(kvs[0]);
}
btmNames = list.toArray(new String[]{});
}
cbox.setModel(new DefaultComboBoxModel(btmNames));
if(btmNames.length > 0 && selectedIndex >= 0 && selectedIndex <= btmNames.length - 1){
cbox.setSelectedIndex(selectedIndex);
}
}
public UIComptDesignDialog getOwnedUIComptDesignDialog() {
return ownedUIComptDesignDialog;
}
public void setOwnedUIComptDesignDialog(
UIComptDesignDialog ownedUIComptDesignDialog) {
this.ownedUIComptDesignDialog = ownedUIComptDesignDialog;
}
public Map> getTxtRefBtnALMap() {
return txtRefBtnALMap;
}
public void setTxtRefBtnALMap(Map> txtRefBtnALMap) {
this.txtRefBtnALMap = txtRefBtnALMap;
}
/**
* 隐藏\关闭本组件中可能弹出的PopupDialog
*/
public void hidePopupDialog(){
if(getTxtRefBtnALMap() != null){
Iterator its = getTxtRefBtnALMap().keySet().iterator();
while(its.hasNext()){
Integer key = its.next();
BaseActionListener> bal = getTxtRefBtnALMap().get(key);
bal.hidePopupDialog();
}
}
}
/**
* 检查BtmType输入是否有效
* @param lbl
* @param txtBtmName
* @return
*/
protected boolean checkBtmTypeTxtIsOk(VCIJLabel lbl, JTextComponent txtBtmName){
boolean res = false;
if(lbl == null) return true;
if(txtBtmName == null) return true;
if(!checkRequiredIsOk(lbl, txtBtmName)){
res = false;
} else if(!checkBtmNameIsExist(lbl, txtBtmName)){
res = false;
} else {
res = true;
}
return res;
}
/**
* 检查LinkType输入是否有效
* @param lbl
* @param txtLinkTypeName
* @return
*/
protected boolean checkLinkTypeTxtIsOk(VCIJLabel lbl, JTextComponent txtLinkTypeName){
boolean res = false;
if(lbl == null) return true;
if(txtLinkTypeName == null) return true;
if(!checkRequiredIsOk(lbl, txtLinkTypeName)){
res = false;
} else if(!checkLinkTypeIsExist(lbl, txtLinkTypeName)){
res = false;
} else {
res = true;
}
return res;
}
/**
* 检查表单输入是否有效
* @param lbl
* @param txtVIName
* @param btmLinkType
* @return
*/
protected boolean checkPortalVITxtIsOk(VCIJLabel lbl, JTextComponent txtVIName, JTextComponent btmLinkType){
boolean res = false;
if(lbl == null) return true;
if(txtVIName == null) return true;
if(!checkRequiredIsOk(lbl, txtVIName)){
res = false;
} else if(!checkPortalVIIsExist(lbl, txtVIName, btmLinkType)){
res = false;
} else {
res = true;
}
return res;
}
/**
* 检查表单输入是否有效
* @param lbl
* @param txtVIName
* @param btmLinkType
* @return
*/
protected boolean checkUILayoutTxtIsOk(VCIJLabel lbl, JTextComponent txtUIName, JTextComponent btmLinkType){
boolean res = false;
if(lbl == null) return true;
if(txtUIName == null) return true;
if(!checkRequiredIsOk(lbl, txtUIName)){
res = false;
} else if(!checkUILayoutIsExist(lbl, txtUIName, btmLinkType)){
res = false;
} else {
res = true;
}
return res;
}
/**
* 检查查询模板输入是否有效
* @param lbl
* @param txtQTName
* @param btmLinkType
* @return
*/
protected boolean checkQTNameTxtIsOk(VCIJLabel lbl, JTextComponent txtQTName, JTextComponent btmLinkType){
boolean res = false;
if(lbl == null) return true;
if(txtQTName == null) return true;
if(!checkRequiredIsOk(lbl, txtQTName)){
res = false;
} else if(!"".equals(txtQTName.getText()) && !checkQTIsExist(lbl, txtQTName, btmLinkType)){
res = false;
} else {
res = true;
}
return res;
}
protected boolean checkRequiredIsOk(VCIJLabel lbl, JTextComponent txt){
boolean res = false;
String text = txt.getText().trim();
boolean isRequired = false;
if(txt instanceof VCIJTextField){
isRequired = ((VCIJTextField)txt).isRequired();
} else if(txt instanceof VCIJTextArea){
isRequired = ((VCIJTextArea)txt).isRequired();
}
if("".equals(text) && isRequired){
txt.requestFocus();
VCIJOptionPane.showMessage(getOwnedUIComptDesignDialog(), lbl.getText() + " 不能为空!");
res = false;
} else {
res = true;
}
return res;
}
private boolean checkBtmNameIsExist(VCIJLabel lbl, JTextComponent txt){
boolean res = false;
String sql = "select count(1) count_ from plbtmtype bt where bt.name='" + txt.getText().trim() + "'";
res = checkCountNotEqualZero(sql);
if(!res){
txt.requestFocus();
VCIJOptionPane.showMessage(getOwnedUIComptDesignDialog(),
String.format("%s %s 无效!", lbl.getText(), txt.getText()));
res = false;
} else {
res = true;
}
return res;
}
private boolean checkLinkTypeIsExist(VCIJLabel lbl, JTextComponent txt){
boolean res = false;
String sql = "select count(1) count_ from pllinktype lt " +
"where lt.name ='" + txt.getText().trim() + "'";
res = checkCountNotEqualZero(sql);
if(!res){
txt.requestFocus();
VCIJOptionPane.showMessage(getOwnedUIComptDesignDialog(),
String.format("%s %s 无效!", lbl.getText(), txt.getText()));
res = false;
} else {
res = true;
}
return res;
}
private boolean checkPortalVIIsExist(VCIJLabel lbl, JTextComponent txtVIName, JTextComponent txtType){
boolean res = false;
String sql = "select count(1) count_ from plportalvi vi " +
"where vi.typename='" + txtType.getText().trim() + "' " +
"and vi.viname='" + txtVIName.getText().trim() + "'";
res = checkCountNotEqualZero(sql);
if(!res){
txtVIName.requestFocus();
VCIJOptionPane.showMessage(getOwnedUIComptDesignDialog(),
String.format("%s %s 无效!", lbl.getText(), txtVIName.getText()));
res = false;
} else {
res = true;
}
return res;
}
private boolean checkUILayoutIsExist(VCIJLabel lbl, JTextComponent txtUIName, JTextComponent txtType){
boolean res = false;
String sql = "select count(1) count_ from PLUILAYOUT ui " +
"where ui.PLRELATEDTYPE='" + txtType.getText().trim() + "' " +
"and ui.plcode='" + txtUIName.getText().trim() + "'";
res = checkCountNotEqualZero(sql);
if(!res){
txtUIName.requestFocus();
VCIJOptionPane.showMessage(getOwnedUIComptDesignDialog(),
String.format("%s %s 无效!", lbl.getText(), txtUIName.getText()));
res = false;
} else {
res = true;
}
return res;
}
private boolean checkQTIsExist(VCIJLabel lbl, JTextComponent txtQTName, JTextComponent txtType){
boolean res = false;
String sql = "select count(1) count_ from PL_QTEMPLATE qt " +
"where qt.btmname ='" + txtType.getText().trim() + "' " +
"and qt.qtname='" + txtQTName.getText().trim() + "'";
res = checkCountNotEqualZero(sql);
if(!res){
txtQTName.requestFocus();
VCIJOptionPane.showMessage(getOwnedUIComptDesignDialog(),
String.format("%s %s 无效!", lbl.getText(), txtQTName.getText()));
res = false;
} else {
res = true;
}
return res;
}
private boolean checkCountNotEqualZero(String sql){
boolean res = false;
try{
String[][] kvss = QTClient.getService().queryBySqlWithoutKey(sql);
res = Integer.valueOf(kvss[0][0]) > 0;
}catch(Exception ex){
ex.printStackTrace();
}
return res;
}
}