@ -4,12 +4,12 @@ import com.fr.design.constants.UIConstants;
import java.awt.Component ;
import java.awt.Component ;
import java.awt.Dimension ;
import java.awt.Dimension ;
import java.awt.event.ItemEvent ;
import java.awt.event.ItemEvent ;
import java.util.Iterator ;
import java.util.Map.Entry ;
import javax.swing.DefaultComboBoxModel ;
import javax.swing.DefaultComboBoxModel ;
import javax.swing.JLabel ;
import javax.swing.JLabel ;
import javax.swing.JList ;
import javax.swing.JList ;
import javax.swing.event.AncestorEvent ;
import javax.swing.event.AncestorListener ;
import javax.swing.event.ChangeEvent ;
import javax.swing.event.ChangeEvent ;
import javax.swing.event.ChangeListener ;
import javax.swing.event.ChangeListener ;
@ -33,16 +33,53 @@ public class TableDataComboBox extends UIComboBox implements Prepare4DataSourceC
protected java . util . Map < String , TableDataWrapper > resMap ;
protected java . util . Map < String , TableDataWrapper > resMap ;
private java . util . Map < String , TableDataWrapper > dsMap ;
private java . util . Map < String , TableDataWrapper > dsMap ;
private static final long serialVersionUID = 1L ;
private static final long serialVersionUID = 1L ;
private boolean refresModel = false ;
private boolean refresh Model = false ;
private String treeName ; //树数据集本身的名字
private String treeName = StringUtils . EMPTY ; //树数据集本身的名字
private ChangeListener changeListener ;
private ChangeListener changeListener ;
/ * *
* 兼容插件调用
*
* @param source 插件中传入的数据源
* /
public TableDataComboBox ( TableDataSource source ) {
public TableDataComboBox ( TableDataSource source ) {
this ( source , StringUtils . EMPTY ) ;
this ( ) ;
}
}
/ * *
* 兼容插件调用
*
* @param source 插件中传入的数据源
* @param treeName 树数据集名称
* /
public TableDataComboBox ( TableDataSource source , String treeName ) {
public TableDataComboBox ( TableDataSource source , String treeName ) {
super ( ) ;
this ( treeName ) ;
}
/ * *
* 根据树名称创建TableDataComboBox 。
*
* @param treeName 树数据集名称
* /
public TableDataComboBox ( String treeName ) {
this ( ) ;
// 传入树数据集名称
this . treeName = treeName ;
this . treeName = treeName ;
}
/ * *
* 初始化TableDataComboBox
* /
public TableDataComboBox ( ) {
super ( ) ;
setListCellRenderer ( ) ;
addListener ( ) ;
}
/ * *
* 设置渲染器
* /
private void setListCellRenderer ( ) {
this . setRenderer ( new UIComboBoxRenderer ( ) {
this . setRenderer ( new UIComboBoxRenderer ( ) {
private static final long serialVersionUID = 1L ;
private static final long serialVersionUID = 1L ;
private boolean labelVisible = true ;
private boolean labelVisible = true ;
@ -69,38 +106,46 @@ public class TableDataComboBox extends UIComboBox implements Prepare4DataSourceC
return labelVisible ? super . getPreferredSize ( ) : new Dimension ( super . getPreferredSize ( ) . width , 0 ) ;
return labelVisible ? super . getPreferredSize ( ) : new Dimension ( super . getPreferredSize ( ) . width , 0 ) ;
}
}
} ) ;
} ) ;
refresh ( source ) ;
}
/ * *
* 在comboBox可见时添加下拉事件与数据集响应事件
* /
private void addListener ( ) {
this . addAncestorListener ( new AncestorListener ( ) {
@Override
public void ancestorAdded ( AncestorEvent event ) {
registerDSChangeListener ( ) ;
registerDSChangeListener ( ) ;
refresh ( DesignTableDataManager . getEditingTableDataSource ( ) ) ;
}
@Override
public void ancestorRemoved ( AncestorEvent event ) {
DesignTableDataManager . removeDsChangeListener ( changeListener ) ;
}
@Override
public void ancestorMoved ( AncestorEvent event ) {
}
} ) ;
}
}
/ * *
/ * *
* refresh ComboBox
* refresh ComboBox
* @param source
*
* @param source 数据源
* /
* /
public void refresh ( TableDataSource source ) {
public void refresh ( TableDataSource source ) {
TableDataWrapper dataWrapper = getSelectedItem ( ) ;
refreshModel = true ;
refresModel = true ;
setResMap ( source ) ;
setResMap ( source ) ;
setDsMap ( ) ;
setDsMap ( ) ;
DefaultComboBoxModel model = new DefaultComboBoxModel ( ) ;
// 获取当前选中的数据项
this . setModel ( model ) ;
TableDataWrapper dataWrapper = getSelectedItem ( ) ;
model . addElement ( UIConstants . PENDING ) ;
// 更新下拉模型
// 遍历添加所有数据项到模型
refreshComboBoxModel ( ) ;
Iterator < Entry < String , TableDataWrapper > > entryIt = dsMap . entrySet ( ) . iterator ( ) ;
//处理已选中的数据项
while ( entryIt . hasNext ( ) ) {
updateSelectedItem ( dataWrapper ) ;
TableDataWrapper tableDataWrapper = entryIt . next ( ) . getValue ( ) ;
refreshModel = false ;
if ( ! ComparatorUtils . equals ( tableDataWrapper . getTableDataName ( ) , treeName ) ) {
model . addElement ( tableDataWrapper ) ;
}
}
if ( dataWrapper ! = null ) {
if ( DesignTableDataManager . isDsNameChanged ( dataWrapper . getTableDataName ( ) ) ) {
this . setSelectedTableDataByName ( DesignTableDataManager . getChangedDsNameByOldDsName ( dataWrapper . getTableDataName ( ) ) ) ;
} else {
this . getModel ( ) . setSelectedItem ( dataWrapper ) ;
}
}
refresModel = false ;
}
}
protected void setResMap ( TableDataSource source ) {
protected void setResMap ( TableDataSource source ) {
@ -111,24 +156,54 @@ public class TableDataComboBox extends UIComboBox implements Prepare4DataSourceC
dsMap = DesignTableDataManager . getAllDataSetIncludingProcedure ( resMap ) ;
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 . setSelectedTableData ( DesignTableDataManager . getChangedDsNameByOldDsName ( dataWrapper . getTableDataName ( ) ) ) ;
} else {
this . getModel ( ) . setSelectedItem ( dataWrapper ) ;
}
}
}
/ * *
/ * *
* 向resMap中添加TableData信息
* 向resMap中添加TableData信息
*
* @param name 数据集名字
* @param name 数据集名字
* @param templateTableDataWrappe 数据集
* @param templateTableDataWrapper 数据集
* /
* /
public void putTableDataIntoMap ( String name , TemplateTableDataWrapper templateTableDataWrappe ) {
public void putTableDataIntoMap ( String name , TemplateTableDataWrapper templateTableDataWrapper ) {
if ( dsMap . containsKey ( name ) ) {
if ( dsMap . containsKey ( name ) ) {
return ;
return ;
}
}
this . addItem ( templateTableDataWrappe ) ;
this . addItem ( templateTableDataWrapper ) ;
dsMap . put ( name , templateTableDataWrappe ) ;
dsMap . put ( name , templateTableDataWrapper ) ;
}
}
public void setSelectedTableDataByName ( String name ) {
public void setSelectedTableDataByName ( String name ) {
TableDataWrapper tableDataWrappe = dsMap . get ( name ) = = null ? dsMap . get ( name + "_P_CURSOR" ) : dsMap . get ( name ) ;
setResMap ( DesignTableDataManager . getEditingTableDataSource ( ) ) ;
this . getModel ( ) . setSelectedItem ( tableDataWrappe ) ;
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
@Override
@ -139,10 +214,14 @@ public class TableDataComboBox extends UIComboBox implements Prepare4DataSourceC
return null ;
return null ;
}
}
//august:addElement方法竟然会fireItemStateChanged,蛋疼
/ * *
* august : addElement方法竟然会fireItemStateChanged , 蛋疼
* @param e the event of interest
*
* /
@Override
@Override
protected void fireItemStateChanged ( ItemEvent e ) {
protected void fireItemStateChanged ( ItemEvent e ) {
if ( ! refresModel ) {
if ( ! refresh Model ) {
super . fireItemStateChanged ( e ) ;
super . fireItemStateChanged ( e ) ;
}
}
}
}
@ -161,8 +240,10 @@ public class TableDataComboBox extends UIComboBox implements Prepare4DataSourceC
DesignTableDataManager . addDsChangeListener ( changeListener ) ;
DesignTableDataManager . addDsChangeListener ( changeListener ) ;
}
}
/ * *
* 兼容插件调用
* /
public void registerGlobalDSChangeListener ( ) {
public void registerGlobalDSChangeListener ( ) {
DesignTableDataManager . addGlobalDsChangeListener ( changeListener ) ;
DesignTableDataManager . addGlobalDsChangeListener ( changeListener ) ;
}
}
}
}