You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
338 lines
11 KiB
338 lines
11 KiB
package com.fr.design.data.datapane; |
|
|
|
import com.fr.base.TableData; |
|
import com.fr.base.TableDataBean; |
|
import com.fr.config.RemoteConfigEvent; |
|
import com.fr.data.MultiResultTableData; |
|
import com.fr.data.TableDataSource; |
|
import com.fr.design.data.BasicTableDataUtils; |
|
import com.fr.design.data.DesignTableDataManager; |
|
import com.fr.design.dialog.FineJOptionPane; |
|
import com.fr.design.gui.controlpane.JListControlPane; |
|
import com.fr.design.gui.controlpane.NameableCreator; |
|
import com.fr.design.gui.ilist.ListModelElement; |
|
import com.fr.design.i18n.Toolkit; |
|
import com.fr.event.EventDispatcher; |
|
import com.fr.file.ProcedureConfig; |
|
import com.fr.file.TableDataConfig; |
|
import com.fr.file.TableDataOperator; |
|
import com.fr.file.TableDataOperatorImpl; |
|
import com.fr.general.ComparatorUtils; |
|
import com.fr.general.NameObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.rpc.ExceptionHandler; |
|
import com.fr.rpc.RPCInvokerExceptionInfo; |
|
import com.fr.stable.ArrayUtils; |
|
import com.fr.stable.Nameable; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.core.PropertyChangeAdapter; |
|
import com.fr.third.org.apache.commons.collections4.MapUtils; |
|
import com.fr.workspace.WorkContext; |
|
|
|
import javax.swing.DefaultListModel; |
|
import javax.swing.JPanel; |
|
import javax.swing.SwingUtilities; |
|
import java.util.ArrayList; |
|
import java.util.Collection; |
|
import java.util.HashMap; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* TableDataList Pane. |
|
*/ |
|
public class TableDataPaneListPane extends JListControlPane implements TableDataPaneController { |
|
private boolean isNamePermitted = true; |
|
private Map<String, String> dsNameChangedMap = new HashMap<>(); |
|
|
|
public TableDataPaneListPane() { |
|
super(); |
|
dsNameChangedMap.clear(); |
|
this.addEditingListener(new PropertyChangeAdapter() { |
|
@Override |
|
public void propertyChange() { |
|
isNamePermitted = true; |
|
if (!checkName()) { |
|
isNamePermitted = false; |
|
return; |
|
} |
|
|
|
String tempName = getEditingName(); |
|
Object editingType = getEditingType(); |
|
|
|
if (!BasicTableDataUtils.checkName(tempName)) { |
|
isNamePermitted = false; |
|
nameableList.stopEditing(); |
|
setIllegalIndex(editingIndex); |
|
return; |
|
} |
|
|
|
if (editingType instanceof MultiResultTableData<?> && isIncludeUnderline(tempName)) { |
|
isNamePermitted = false; |
|
nameableList.stopEditing(); |
|
FineJOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(TableDataPaneListPane.this), Toolkit.i18nText("Fine-Design_Basic_Multi_Result_Table_Data_Name_Tips")); |
|
setIllegalIndex(editingIndex); |
|
return; |
|
} |
|
|
|
if (nameableList.getSelectedValue() instanceof ListModelElement) { |
|
rename(selectedName, tempName); |
|
} |
|
} |
|
}); |
|
} |
|
|
|
@Override |
|
public String getEmptyNameTip() { |
|
return Toolkit.i18nText("Fine-Design_Basic_Table_Data_Empty_Name_Tips"); |
|
} |
|
|
|
@Override |
|
public String getDuplicatedNameTip() { |
|
return Toolkit.i18nText("Fine-Design_Basic_Table_Data_Duplicate_Name_Tips", getEditingName()); |
|
} |
|
|
|
@Override |
|
public Collection getExtraItemsToCheckNameRepeat() { |
|
return DesignTableDataManager.getGlobalDataSet().keySet(); |
|
} |
|
|
|
@Override |
|
public void rename(String oldName, String newName) { |
|
//如果a改成了b,b又被改成了c,就认为是a改成了c |
|
for (Map.Entry<String, String> entry : dsNameChangedMap.entrySet()) { |
|
if (StringUtils.equals(oldName, entry.getValue())) { |
|
oldName = entry.getKey(); |
|
break; |
|
} |
|
} |
|
if (StringUtils.equals(oldName, newName)) { |
|
//a -> b;b -> a,说明没改 |
|
dsNameChangedMap.remove(oldName); |
|
} else { |
|
dsNameChangedMap.put(oldName, newName); |
|
} |
|
} |
|
|
|
/** |
|
* 名字是否允许 |
|
* |
|
* @return 是/否 |
|
*/ |
|
@Override |
|
public boolean isNamePermitted() { |
|
return isNamePermitted; |
|
} |
|
|
|
|
|
/** |
|
* 检查按钮可用状态 Check button enabled. |
|
*/ |
|
@Override |
|
public void checkButtonEnabled() { |
|
super.checkButtonEnabled(); |
|
isNamePermitted = !isContainsRename(); |
|
} |
|
|
|
private boolean isIncludeUnderline(String name) { |
|
return name.contains(MultiResultTableData.GROUP_MARKER); |
|
} |
|
|
|
/** |
|
* 创建服务器数据集所需要的NameableCreator数组 |
|
* |
|
* @return 数组 |
|
*/ |
|
@Override |
|
public NameableCreator[] createNameableCreators() { |
|
return TableDataCreatorProducer.getInstance().createServerTableDataCreator(); |
|
} |
|
|
|
|
|
// 只能保证当前模板不重名了 |
|
|
|
/** |
|
* 新建不重名的面板 |
|
* |
|
* @param prefix 前缀字符 |
|
* @return 生成的名字 |
|
*/ |
|
@Override |
|
public String createUnrepeatedName(String prefix) { |
|
TableDataSource source = DesignTableDataManager.getEditingTableDataSource(); |
|
if (source == null) { |
|
return super.createUnrepeatedName(prefix); |
|
} |
|
String[] allDsNames = DesignTableDataManager.getAllDSNames(source); |
|
|
|
// richer:生成的名字从1开始. kunsnat: 添加属性从0开始. |
|
int count = 1; |
|
while (isDsNameRepeated(prefix + count, allDsNames)) { |
|
count++; |
|
} |
|
return prefix + count; |
|
} |
|
|
|
private boolean isDsNameRepeated(String name, String[] allDsNames) { |
|
DefaultListModel model = this.getModel(); |
|
Nameable[] all = new Nameable[model.getSize()]; |
|
for (int i = 0; i < model.size(); i++) { |
|
all[i] = ((ListModelElement) model.get(i)).wrapper; |
|
} |
|
for (int i = 0, len = model.size(); i < len; i++) { |
|
Nameable nameable = all[i]; |
|
if (ComparatorUtils.equals(nameable.getName(), name)) { |
|
return true; |
|
} |
|
} |
|
return ArrayUtils.contains(allDsNames, name); |
|
} |
|
|
|
@Override |
|
protected String title4PopupWindow() { |
|
return "TableData"; |
|
} |
|
|
|
/** |
|
* Populate. |
|
*/ |
|
@Override |
|
public void populate(TableDataSource tds) { |
|
List<NameObject> nameObjectList = new ArrayList<NameObject>(); |
|
|
|
Iterator tableDataNameIterator = tds.getTableDataNameIterator(); |
|
while (tableDataNameIterator.hasNext()) { |
|
String tableDataName = (String) tableDataNameIterator.next(); |
|
TableData tableData = tds.getTableData(tableDataName); |
|
|
|
if (tableData != null) { |
|
nameObjectList.add(new NameObject(tableDataName, tableData)); |
|
} |
|
} |
|
|
|
populate(nameObjectList.toArray(new NameObject[0])); |
|
} |
|
|
|
/** |
|
* Populate. |
|
*/ |
|
@Override |
|
public void populate(TableDataConfig tableDataConfig) { |
|
Iterator<String> nameIt = tableDataConfig.getTableDatas().keySet().iterator(); |
|
Iterator<String> procedurenameIt = ProcedureConfig.getInstance().getProcedures().keySet().iterator(); |
|
List<NameObject> nameObjectList = new ArrayList<NameObject>(); |
|
while (nameIt.hasNext()) { |
|
String name = nameIt.next(); |
|
nameObjectList.add(new NameObject(name, tableDataConfig.getTableData(name))); |
|
} |
|
while (procedurenameIt.hasNext()) { |
|
String name = procedurenameIt.next(); |
|
nameObjectList.add(new NameObject(name, ProcedureConfig.getInstance().getProcedure(name))); |
|
} |
|
|
|
populate(nameObjectList.toArray(new NameObject[0])); |
|
} |
|
|
|
@Override |
|
public void update(TableDataConfig tableDataConfig) { |
|
List<TableDataBean> tableDataBeans = getUpdateTableDataBeans(); |
|
try { |
|
WorkContext.getCurrent().get(TableDataOperator.class, new ExceptionHandler() { |
|
@Override |
|
public Object callHandler(RPCInvokerExceptionInfo exceptionInfo) { |
|
// 走老的方式 |
|
return saveByOldWay(tableDataBeans); |
|
} |
|
}).saveTableData(new ArrayList<>(tableDataConfig.getTableDatas().keySet()), tableDataBeans); |
|
if (!WorkContext.getCurrent().isLocal()) { |
|
EventDispatcher.fire(RemoteConfigEvent.EDIT, TableDataConfig.getInstance().getNameSpace()); |
|
} |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
} |
|
|
|
/** |
|
* 获取更新时的数据集快照 |
|
* |
|
* @return 快照 |
|
*/ |
|
private List<TableDataBean> getUpdateTableDataBeans() { |
|
Nameable[] tableDataArray = this.update(); |
|
List<TableDataBean> tableDataBeans = new ArrayList<>(); |
|
Map<String, String> map = MapUtils.invertMap(getDsNameChangedMap()); |
|
for (Nameable nameable : tableDataArray) { |
|
NameObject nameObject = (NameObject) nameable; |
|
String oldName = map.get(nameObject.getName()); |
|
if (oldName == null) { |
|
oldName = StringUtils.EMPTY; |
|
} |
|
tableDataBeans.add(new TableDataBean(nameObject.getName(), oldName, (TableData) nameObject.getObject())); |
|
} |
|
return tableDataBeans; |
|
} |
|
private boolean saveByOldWay(List<TableDataBean> tableDataBean) { |
|
try { |
|
return TableDataOperatorImpl.getInstance().saveTableData(tableDataBean); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
return false; |
|
} |
|
} |
|
|
|
@Override |
|
public void update(TableDataSource tds) { |
|
tds.clearAllTableData(); |
|
|
|
Nameable[] tableDataArray = this.update(); |
|
for (int i = 0; i < tableDataArray.length; i++) { |
|
NameObject nameObject = (NameObject) tableDataArray[i]; |
|
tds.putTableData(nameObject.getName(), (TableData) nameObject.getObject()); |
|
} |
|
} |
|
|
|
/** |
|
* 判断数据集是否重名 |
|
*/ |
|
@Override |
|
public void checkValid() throws Exception { |
|
List<String> exsitTableDataNameList = new ArrayList<String>(); |
|
// _denny: 判断是否有重复的数据集名 |
|
checkRepeatedDSName(exsitTableDataNameList); |
|
|
|
Nameable[] tableDataArray = this.update(); |
|
for (int i = 0; i < tableDataArray.length; i++) { |
|
NameObject nameObject = (NameObject) tableDataArray[i]; |
|
|
|
if (exsitTableDataNameList.contains(nameObject.getName())) { |
|
throw new Exception(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Table_Data_Duplicate_Name_Tips", nameObject.getName())); |
|
} |
|
|
|
exsitTableDataNameList.add(nameObject.getName()); |
|
} |
|
} |
|
|
|
protected void checkRepeatedDSName(List<String> exsitTableDataNameList) { |
|
// Do nothing |
|
} |
|
|
|
/** |
|
* 在JJControlPane的左侧Tree里面选中某一Item |
|
* |
|
* @param name 被选择的Item名称 |
|
*/ |
|
public void selectName(String name) { |
|
this.setSelectedName(name); |
|
} |
|
|
|
@Override |
|
public Map<String, String> getDsNameChangedMap() { |
|
return this.dsNameChangedMap; |
|
} |
|
|
|
@Override |
|
public JPanel getPanel() { |
|
return this; |
|
} |
|
}
|
|
|