Browse Source

Pull request #15435: REPORT-137628 TableDataComboBox事件无限叠加优化

Merge in DESIGN/design from ~RICHARD.FANG/design:fbp/feature to fbp/feature

* commit 'c0ce47fbb3b5cbd47680869900908062067d19a4':
  REPORT-137628 TableDataComboBox事件无限叠加优化
fbp/feature
Richard.Fang-方超 1 month ago
parent
commit
4d8cd89820
  1. 33
      designer-base/src/main/java/com/fr/design/data/DesignTableDataManager.java
  2. 15
      designer-base/src/main/java/com/fr/design/data/datapane/GlobalTableDataComboBox.java
  3. 2
      designer-base/src/main/java/com/fr/design/data/datapane/GlobalTreeTableDataDictPane.java
  4. 301
      designer-base/src/main/java/com/fr/design/data/datapane/TableDataComboBox.java
  5. 3
      designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataDictPane.java
  6. 3
      designer-base/src/main/java/com/fr/design/editor/editor/ColumnSelectedEditor.java
  7. 2
      designer-base/src/main/java/com/fr/design/mainframe/widget/editors/DataBindingEditor.java
  8. 2
      designer-base/src/main/java/com/fr/design/mainframe/widget/editors/DataTableEditor.java
  9. 3
      designer-base/src/main/java/com/fr/design/present/dict/TableDataDictPane.java
  10. 2
      designer-chart/src/main/java/com/fr/design/chart/AutoChartTypePane.java
  11. 2
      designer-chart/src/main/java/com/fr/design/chartx/component/MapAreaMatchPane.java
  12. 2
      designer-chart/src/main/java/com/fr/design/mainframe/chart/gui/data/DatabaseTableDataPane.java
  13. 2
      designer-realize/src/main/java/com/fr/design/dscolumn/SelectedDataColumnPane.java

33
designer-base/src/main/java/com/fr/design/data/DesignTableDataManager.java

@ -121,6 +121,9 @@ public abstract class DesignTableDataManager {
//增强for循环用的iterator实现的, 如果中间哪个listener修改或删除了(如ChartEditPane.dsChangeListener),
// 由于dsListeners是arraylist, 此时会ConcurrentModifyException
ChangeEvent e = null;
if (dsListeners.get(i) == null) {
continue;
}
dsListeners.get(i).stateChanged(e);
}
}
@ -197,17 +200,27 @@ public abstract class DesignTableDataManager {
* @param l ChangeListener监听器
*/
public static void addDsChangeListener(ChangeListener l) {
getDsListenersForCurrentTemplate().add(l);
}
/**
* 移除模板数据集改变 监听事件.
*
* @param l ChangeListener监听器
*/
public static void removeDsChangeListener(ChangeListener l) {
getDsListenersForCurrentTemplate().remove(l);
}
/**
* 获取当前模板的监听器列表.
*
* @return 模板对应的监听器列表如果列表不存在则新建.
*/
private static List<ChangeListener> getDsListenersForCurrentTemplate() {
JTemplate<?, ?> template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate();
String key = StringUtils.EMPTY;
if (JTemplate.isValid(template)) {
key = template.getPath();
}
List<ChangeListener> dsListeners = dsListenersMap.get(key);
if (dsListeners == null) {
dsListeners = new ArrayList<ChangeListener>();
dsListenersMap.put(key, dsListeners);
}
dsListeners.add(l);
String key = JTemplate.isValid(template) ? template.getPath() : StringUtils.EMPTY;
return dsListenersMap.computeIfAbsent(key, k -> new ArrayList<>());
}
/**

15
designer-base/src/main/java/com/fr/design/data/datapane/GlobalTableDataComboBox.java

@ -12,9 +12,24 @@ import com.fr.data.TableDataSource;
*/
public class GlobalTableDataComboBox extends TableDataComboBox {
/**
* 兼容插件调用
* @param source 数据源
* @param treeName 树数据集名称
*/
public GlobalTableDataComboBox(TableDataSource source, String treeName) {
super(source, treeName);
}
/**
* 初始化GlobalTableDataComboBox
* @param treeName 树数据集名称
*/
public GlobalTableDataComboBox(String treeName) {
super(treeName);
}
@Override
protected void setResMap(TableDataSource source){
resMap = DesignTableDataManager.getGlobalDataSet();

2
designer-base/src/main/java/com/fr/design/data/datapane/GlobalTreeTableDataDictPane.java

@ -16,6 +16,6 @@ public class GlobalTreeTableDataDictPane extends TreeTableDataDictPane {
}
protected void setTableDataNameComboBox(String string) {
tableDataNameComboBox = new GlobalTableDataComboBox(DesignTableDataManager.getEditingTableDataSource(),string);
tableDataNameComboBox = new GlobalTableDataComboBox(string);
}
}

301
designer-base/src/main/java/com/fr/design/data/datapane/TableDataComboBox.java

@ -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 refreshModel = 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);
}
}

3
designer-base/src/main/java/com/fr/design/data/datapane/TreeTableDataDictPane.java

@ -180,8 +180,7 @@ public class TreeTableDataDictPane extends BasicPane implements Previewable {
}
protected void setTableDataNameComboBox(String treeName) {
tableDataNameComboBox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource(), treeName);
tableDataNameComboBox = new TableDataComboBox(treeName);
}
private void tdChange(boolean isChangeDS) {

3
designer-base/src/main/java/com/fr/design/editor/editor/ColumnSelectedEditor.java

@ -35,7 +35,7 @@ public class ColumnSelectedEditor extends Editor<SimpleDSColumn> implements Prep
public ColumnSelectedEditor() {
this.setName(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_DS_Column"));
this.setLayout(new BorderLayout());
tableDataComboBox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource());
tableDataComboBox = new TableDataComboBox();
columnNames = new String[0];
tableDataComboBox.addItemListener(new ItemListener() {
@ -117,6 +117,5 @@ public class ColumnSelectedEditor extends Editor<SimpleDSColumn> implements Prep
@Override
public void registerDSChangeListener() {
tableDataComboBox.registerGlobalDSChangeListener();
}
}

2
designer-base/src/main/java/com/fr/design/mainframe/widget/editors/DataBindingEditor.java

@ -65,7 +65,7 @@ public class DataBindingEditor extends Editor<DataBinding> {
int vgap = UIManager.getInt("DataBindingEditor.vgap");
int hgap = UIManager.getInt("DataBindingEditor.hgap");
this.setLayout(new BorderLayout(FineUIScale.scale(hgap), FineUIScale.scale(vgap)));
tableDataComboBox = new TableDataComboBox(getTableDataSource());
tableDataComboBox = new TableDataComboBox();
tableDataComboBox.setPreferredSize(new Dimension(55, 20));
tableDataComboBox.addItemListener(tableDataComboBoxListener);

2
designer-base/src/main/java/com/fr/design/mainframe/widget/editors/DataTableEditor.java

@ -29,7 +29,7 @@ public class DataTableEditor extends Editor<DataTableConfig> {
private void initCompontents() {
this.setLayout(FRGUIPaneFactory.createBorderLayout());
tableDataComboBox = new TableDataComboBox(getTableDataSource());
tableDataComboBox = new TableDataComboBox();
tableDataComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {

3
designer-base/src/main/java/com/fr/design/present/dict/TableDataDictPane.java

@ -78,7 +78,7 @@ public class TableDataDictPane extends FurtherBasicBeanPane<TableDataDictionary>
}
private void initBasicComponets() {
tableDataNameComboBox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource());
tableDataNameComboBox = new TableDataComboBox();
tableDataNameComboBox.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
tdChange(e);
@ -362,6 +362,5 @@ public class TableDataDictPane extends FurtherBasicBeanPane<TableDataDictionary>
@Override
public void registerDSChangeListener() {
tableDataNameComboBox.registerGlobalDSChangeListener();
}
}

2
designer-chart/src/main/java/com/fr/design/chart/AutoChartTypePane.java

@ -141,7 +141,7 @@ public class AutoChartTypePane extends ChartWizardPane implements CallbackEvent
}
private void initDataFiledBox() {
tableNameComboBox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource());
tableNameComboBox = new TableDataComboBox();
tableNameComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {

2
designer-chart/src/main/java/com/fr/design/chartx/component/MapAreaMatchPane.java

@ -126,7 +126,7 @@ public class MapAreaMatchPane extends BasicBeanPane<MapMatchResult> {
}
private void initButtonGroup() {
tableNameCombox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource());
tableNameCombox = new TableDataComboBox();
tableNameCombox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {

2
designer-chart/src/main/java/com/fr/design/mainframe/chart/gui/data/DatabaseTableDataPane.java

@ -75,7 +75,7 @@ public class DatabaseTableDataPane extends BasicPane{
}
private void initTableCombox() {
tableNameCombox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource()){
tableNameCombox = new TableDataComboBox(){
//图表的数据集选择下拉框,不需要注册监听,chartEditPane已经注册了。
public void registerDSChangeListener() {

2
designer-realize/src/main/java/com/fr/design/dscolumn/SelectedDataColumnPane.java

@ -324,7 +324,7 @@ public class SelectedDataColumnPane extends BasicPane {
protected void initTableNameComboBox() {
tableNameComboBox = new TableDataComboBox(DesignTableDataManager.getEditingTableDataSource());
tableNameComboBox = new TableDataComboBox();
}
@Override

Loading…
Cancel
Save