@ -4,12 +4,12 @@ import com.fr.design.constants.UIConstants;
import java.awt.Component ;
import java.awt.Dimension ;
import java.awt.event.ItemEvent ;
import java.util.Iterator ;
import java.util.Map.Entry ;
import javax.swing.DefaultComboBoxModel ;
import javax.swing.JLabel ;
import javax.swing.JList ;
import javax.swing.event.AncestorEvent ;
import javax.swing.event.AncestorListener ;
import javax.swing.event.ChangeEvent ;
import javax.swing.event.ChangeListener ;
@ -33,136 +33,217 @@ public class TableDataComboBox extends UIComboBox implements Prepare4DataSourceC
protected java . util . Map < String , TableDataWrapper > resMap ;
private java . util . Map < String , TableDataWrapper > dsMap ;
private static final long serialVersionUID = 1L ;
private boolean refresModel = false ;
private String treeName ; //树数据集本身的名字
private ChangeListener changeListener ;
private boolean refresh Model = false ;
private String treeName = StringUtils . EMPTY ; //树数据集本身的名字
private ChangeListener changeListener ;
public TableDataComboBox ( TableDataSource source ) {
this ( source , StringUtils . EMPTY ) ;
/ * *
* 兼容插件调用
*
* @param source 插件中传入的数据源
* /
public TableDataComboBox ( TableDataSource source ) {
this ( ) ;
}
public TableDataComboBox ( TableDataSource source , String treeName ) {
super ( ) ;
/ * *
* 兼容插件调用
*
* @param source 插件中传入的数据源
* @param treeName 树数据集名称
* /
public TableDataComboBox ( TableDataSource source , String treeName ) {
this ( treeName ) ;
}
/ * *
* 根据树名称创建TableDataComboBox 。
*
* @param treeName 树数据集名称
* /
public TableDataComboBox ( String treeName ) {
this ( ) ;
// 传入树数据集名称
this . treeName = treeName ;
this . setRenderer ( new UIComboBoxRenderer ( ) {
private static final long serialVersionUID = 1L ;
private boolean labelVisible = true ;
@Override
public Component getListCellRendererComponent ( JList list , Object value , int index , boolean isSelected , boolean cellHasFocus ) {
JLabel renderer = ( JLabel ) super . getListCellRendererComponent ( list , value , index , isSelected , cellHasFocus ) ;
if ( value instanceof TableDataWrapper ) {
labelVisible = true ;
TableDataWrapper tableDataWrapper = ( TableDataWrapper ) value ;
renderer . setIcon ( tableDataWrapper . getIcon ( ) ) ;
renderer . setText ( tableDataWrapper . getTableDataName ( ) ) ;
renderer . setToolTipText ( tableDataWrapper . getTableDataName ( ) ) ;
} else {
labelVisible = false ;
renderer . setIcon ( null ) ;
renderer . setText ( StringUtils . EMPTY ) ;
}
return renderer ;
}
@Override
public Dimension getPreferredSize ( ) {
return labelVisible ? super . getPreferredSize ( ) : new Dimension ( super . getPreferredSize ( ) . width , 0 ) ;
}
} ) ;
refresh ( source ) ;
registerDSChangeListener ( ) ;
}
}
/ * *
* 初始化TableDataComboBox
* /
public TableDataComboBox ( ) {
super ( ) ;
setListCellRenderer ( ) ;
addListener ( ) ;
}
/ * *
* 设置渲染器
* /
private void setListCellRenderer ( ) {
this . setRenderer ( new UIComboBoxRenderer ( ) {
private static final long serialVersionUID = 1L ;
private boolean labelVisible = true ;
@Override
public Component getListCellRendererComponent ( JList list , Object value , int index , boolean isSelected , boolean cellHasFocus ) {
JLabel renderer = ( JLabel ) super . getListCellRendererComponent ( list , value , index , isSelected , cellHasFocus ) ;
if ( value instanceof TableDataWrapper ) {
labelVisible = true ;
TableDataWrapper tableDataWrapper = ( TableDataWrapper ) value ;
renderer . setIcon ( tableDataWrapper . getIcon ( ) ) ;
renderer . setText ( tableDataWrapper . getTableDataName ( ) ) ;
renderer . setToolTipText ( tableDataWrapper . getTableDataName ( ) ) ;
} else {
labelVisible = false ;
renderer . setIcon ( null ) ;
renderer . setText ( StringUtils . EMPTY ) ;
}
return renderer ;
}
@Override
public Dimension getPreferredSize ( ) {
return labelVisible ? super . getPreferredSize ( ) : new Dimension ( super . getPreferredSize ( ) . width , 0 ) ;
}
} ) ;
}
/ * *
* 在comboBox可见时添加下拉事件与数据集响应事件
* /
private void addListener ( ) {
this . addAncestorListener ( new AncestorListener ( ) {
@Override
public void ancestorAdded ( AncestorEvent event ) {
registerDSChangeListener ( ) ;
refresh ( DesignTableDataManager . getEditingTableDataSource ( ) ) ;
}
@Override
public void ancestorRemoved ( AncestorEvent event ) {
DesignTableDataManager . removeDsChangeListener ( changeListener ) ;
}
@Override
public void ancestorMoved ( AncestorEvent event ) {
}
} ) ;
}
/ * *
* refresh ComboBox
* @param source
*
* @param source 数据源
* /
public void refresh ( TableDataSource source ) {
TableDataWrapper dataWrapper = getSelectedItem ( ) ;
refresModel = true ;
setResMap ( source ) ;
public void refresh ( TableDataSource source ) {
refreshModel = true ;
setResMap ( source ) ;
setDsMap ( ) ;
DefaultComboBoxModel model = new DefaultComboBoxModel ( ) ;
this . setModel ( model ) ;
model . addElement ( UIConstants . PENDING ) ;
// 遍历添加所有数据项到模型
Iterator < Entry < String , TableDataWrapper > > entryIt = dsMap . entrySet ( ) . iterator ( ) ;
while ( entryIt . hasNext ( ) ) {
TableDataWrapper tableDataWrapper = entryIt . next ( ) . getValue ( ) ;
if ( ! ComparatorUtils . equals ( tableDataWrapper . getTableDataName ( ) , treeName ) ) {
model . addElement ( tableDataWrapper ) ;
}
}
// 获取当前选中的数据项
TableDataWrapper dataWrapper = getSelectedItem ( ) ;
// 更新下拉模型
refreshComboBoxModel ( ) ;
//处理已选中的数据项
updateSelectedItem ( dataWrapper ) ;
refreshModel = false ;
}
protected void setResMap ( TableDataSource source ) {
this . resMap = DesignTableDataManager . getAllEditingDataSet ( source ) ;
}
private void setDsMap ( ) {
dsMap = DesignTableDataManager . getAllDataSetIncludingProcedure ( resMap ) ;
}
private void refreshComboBoxModel ( ) {
//创建ComboBox模型并设置
DefaultComboBoxModel model = new DefaultComboBoxModel ( ) ;
this . setModel ( model ) ;
model . addElement ( UIConstants . PENDING ) ;
// 遍历添加所有数据项到模型,树数据集comboBox下拉模型中排除掉本身
dsMap . values ( ) . stream ( )
. filter ( tableDataWrapper - > tableDataWrapper ! = null & & ! ComparatorUtils . equals ( tableDataWrapper . getTableDataName ( ) , treeName ) )
. forEach ( model : : addElement ) ;
}
private void updateSelectedItem ( TableDataWrapper dataWrapper ) {
if ( dataWrapper ! = null ) {
if ( DesignTableDataManager . isDsNameChanged ( dataWrapper . getTableDataName ( ) ) ) {
this . setSelectedTableDataByName ( DesignTableDataManager . getChangedDsNameByOldDsName ( dataWrapper . getTableDataName ( ) ) ) ;
this . setSelectedTableData ( DesignTableDataManager . getChangedDsNameByOldDsName ( dataWrapper . getTableDataName ( ) ) ) ;
} else {
this . getModel ( ) . setSelectedItem ( dataWrapper ) ;
}
}
refresModel = false ;
}
}
protected void setResMap ( TableDataSource source ) {
this . resMap = DesignTableDataManager . getAllEditingDataSet ( source ) ;
/ * *
* 向resMap中添加TableData信息
*
* @param name 数据集名字
* @param templateTableDataWrapper 数据集
* /
public void putTableDataIntoMap ( String name , TemplateTableDataWrapper templateTableDataWrapper ) {
if ( dsMap . containsKey ( name ) ) {
return ;
}
this . addItem ( templateTableDataWrapper ) ;
dsMap . put ( name , templateTableDataWrapper ) ;
}
private void setDsMap ( ) {
dsMap = DesignTableDataManager . getAllDataSetIncludingProcedure ( resMap ) ;
public void setSelectedTableDataByName ( String name ) {
setResMap ( DesignTableDataManager . getEditingTableDataSource ( ) ) ;
setDsMap ( ) ;
// 数据集名称修改后控件传入的还是旧名称
if ( DesignTableDataManager . isDsNameChanged ( name ) ) {
name = DesignTableDataManager . getChangedDsNameByOldDsName ( name ) ;
}
setSelectedTableData ( name ) ;
}
private void setSelectedTableData ( String name ) {
TableDataWrapper tableDataWrapper = dsMap . get ( name ) = = null ? dsMap . get ( name + "_P_CURSOR" ) : dsMap . get ( name ) ;
this . getModel ( ) . setSelectedItem ( tableDataWrapper ) ;
}
@Override
public TableDataWrapper getSelectedItem ( ) {
if ( dataModel . getSelectedItem ( ) instanceof TableDataWrapper ) {
return ( TableDataWrapper ) dataModel . getSelectedItem ( ) ;
}
return null ;
}
/ * *
* 向resMap中添加TableData信息
* @param name 数据集名字
* @param templateTableDataWrappe 数据集
* august : addElement方法竟然会fireItemStateChanged , 蛋疼
* @param e the event of interest
*
* /
public void putTableDataIntoMap ( String name , TemplateTableDataWrapper templateTableDataWrappe ) {
if ( dsMap . containsKey ( name ) ) {
return ;
}
this . addItem ( templateTableDataWrappe ) ;
dsMap . put ( name , templateTableDataWrappe ) ;
}
public void setSelectedTableDataByName ( String name ) {
TableDataWrapper tableDataWrappe = dsMap . get ( name ) = = null ? dsMap . get ( name + "_P_CURSOR" ) : dsMap . get ( name ) ;
this . getModel ( ) . setSelectedItem ( tableDataWrappe ) ;
}
@Override
public TableDataWrapper getSelectedItem ( ) {
if ( dataModel . getSelectedItem ( ) instanceof TableDataWrapper ) {
return ( TableDataWrapper ) dataModel . getSelectedItem ( ) ;
}
return null ;
}
//august:addElement方法竟然会fireItemStateChanged,蛋疼
@Override
protected void fireItemStateChanged ( ItemEvent e ) {
if ( ! refresModel ) {
super . fireItemStateChanged ( e ) ;
}
}
/ * *
* 注册listener , 相应数据集改变
* /
@Override
public void registerDSChangeListener ( ) {
changeListener = new ChangeListener ( ) {
@Override
public void stateChanged ( ChangeEvent e ) {
TableDataComboBox . this . refresh ( DesignTableDataManager . getEditingTableDataSource ( ) ) ;
}
} ;
DesignTableDataManager . addDsChangeListener ( changeListener ) ;
}
public void registerGlobalDSChangeListener ( ) {
DesignTableDataManager . addGlobalDsChangeListener ( changeListener ) ;
}
@Override
protected void fireItemStateChanged ( ItemEvent e ) {
if ( ! refreshModel ) {
super . fireItemStateChanged ( e ) ;
}
}
/ * *
* 注册listener , 相应数据集改变
* /
@Override
public void registerDSChangeListener ( ) {
changeListener = new ChangeListener ( ) {
@Override
public void stateChanged ( ChangeEvent e ) {
TableDataComboBox . this . refresh ( DesignTableDataManager . getEditingTableDataSource ( ) ) ;
}
} ;
DesignTableDataManager . addDsChangeListener ( changeListener ) ;
}
/ * *
* 兼容插件调用
* /
public void registerGlobalDSChangeListener ( ) {
DesignTableDataManager . addGlobalDsChangeListener ( changeListener ) ;
}
}