Browse Source

改进数据连接的API

master
richie 5 years ago
parent
commit
403a0cf144
  1. 23
      src/main/java/com/fanruan/api/conf/ConfigurationKit.java
  2. 11
      src/main/java/com/fanruan/api/data/ConnectionKit.java
  3. 22
      src/main/java/com/fanruan/api/design/DesignKit.java
  4. 13
      src/main/java/com/fanruan/api/design/macro/DatasourceConstants.java
  5. 176
      src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java
  6. 0
      src/main/resources/com/fanruan/api/locale.properties
  7. 0
      src/main/resources/com/fanruan/api/locale_en_US.properties
  8. 0
      src/main/resources/com/fanruan/api/locale_ja_JP.properties
  9. 0
      src/main/resources/com/fanruan/api/locale_ko_KR.properties
  10. 0
      src/main/resources/com/fanruan/api/locale_zh_CN.properties
  11. 0
      src/main/resources/com/fanruan/api/locale_zh_TW.properties

23
src/main/java/com/fanruan/api/conf/ConfigurationKit.java

@ -6,6 +6,7 @@ import com.fr.config.Configuration;
import com.fr.config.holder.ConfigChangeListener;
import com.fr.transaction.Configurations;
import com.fr.transaction.ValidateProxy;
import com.fr.transaction.WorkerCallBack;
import com.fr.transaction.WorkerFacade;
/**
@ -58,4 +59,26 @@ public class ConfigurationKit {
}
});
}
/**
* 在一个事务中修改配置
*
* @param configType 配置类
* @param runner 执行器
* @param callBack 事务回调
*/
public static void modify(Class<? extends Configuration> configType, Runner runner, WorkerCallBack callBack) {
WorkerFacade facade = new WorkerFacade(configType) {
@Override
public void run() {
if (runner != null) {
runner.run();
}
}
};
if (callBack != null) {
facade.addCallBack(callBack);
}
Configurations.modify(facade);
}
}

11
src/main/java/com/fanruan/api/data/ConnectionKit.java

@ -7,6 +7,8 @@ import com.fr.file.ConnectionConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* 数据连接相关工具类
*/
@ -46,4 +48,13 @@ public class ConnectionKit {
public static Connection createNameConnection(String name) {
return new NameDatabaseConnection(name);
}
/**
* 获取所有的数据连接
*
* @return 所有的数据连接组成的集合
*/
public static Map<String, Connection> getConnections() {
return ConnectionConfig.getInstance().getConnections();
}
}

22
src/main/java/com/fanruan/api/design/DesignKit.java

@ -4,6 +4,7 @@ import com.fr.base.TableData;
import com.fr.design.data.datapane.preview.PreviewTablePane;
import com.fr.design.file.HistoryTemplateListPane;
import com.fr.design.i18n.Toolkit;
import com.fr.design.mainframe.DesignerBean;
import com.fr.design.mainframe.DesignerContext;
import com.fr.design.mainframe.DesignerFrame;
import com.fr.design.mainframe.JTemplate;
@ -11,14 +12,16 @@ import com.fr.design.mainframe.JTemplate;
public class DesignKit {
/**
* 直接预览数据集没有实际值及显示值
*
* @param tableData 数据集
*/
public static void previewTableData(TableData tableData){
public static void previewTableData(TableData tableData) {
PreviewTablePane.previewTableData(tableData, -1, -1);
}
/**
* 文本国际化
*
* @param key 国际化键
* @return 国际化后的值
*/
@ -28,7 +31,8 @@ public class DesignKit {
/**
* 带参数的文本国际化
* @param key 国际化键
*
* @param key 国际化键
* @param args 参数
* @return 国际化后的值
*/
@ -38,6 +42,7 @@ public class DesignKit {
/**
* 单例模式返回DesignerFrame对象
*
* @return DesignerFrame对象
*/
public static DesignerFrame getDesignerFrame() {
@ -46,9 +51,20 @@ public class DesignKit {
/**
* 得到当前在修改的模板
*
* @return 返回当前正在编辑的模板
*/
public static JTemplate<?, ?> getCurrentEditingTemplate(){
public static JTemplate<?, ?> getCurrentEditingTemplate() {
return HistoryTemplateListPane.getInstance().getCurrentEditingTemplate();
}
/**
* 根据名字获取已经定义的bean对象
*
* @param name bean的名字
* @return bean对象
*/
public static DesignerBean getDesignerBean(String name) {
return DesignerContext.getDesignerBean(name);
}
}

13
src/main/java/com/fanruan/api/design/macro/DatasourceConstants.java

@ -0,0 +1,13 @@
package com.fanruan.api.design.macro;
import com.fr.design.gui.NameInspector;
/**
* @author richie
* @version 10.0
* Created by richie on 2019/9/24
*/
public class DatasourceConstants {
public static final String ILLEGAL_NAME_HOLDER = NameInspector.ILLEGAL_NAME_HOLDER;
}

176
src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java

@ -1,9 +1,179 @@
package com.fanruan.api.design.work;
import com.fanruan.api.conf.ConfigurationKit;
import com.fanruan.api.data.ConnectionKit;
import com.fanruan.api.data.open.BaseConnection;
import com.fanruan.api.design.DesignKit;
import com.fanruan.api.design.macro.DatasourceConstants;
import com.fanruan.api.generic.Runner;
import com.fanruan.api.util.AssistKit;
import com.fanruan.api.util.StringKit;
import com.fr.data.impl.Connection;
import com.fr.design.DesignerEnvManager;
import com.fr.design.data.datapane.connect.ConnectionListPane;
import com.fr.design.data.datapane.connect.ConnectionShowPane;
import com.fr.design.data.datapane.connect.ItemEditableComboBoxPanel;
import com.fr.design.dialog.BasicDialog;
import com.fr.design.dialog.DialogActionAdapter;
import com.fr.file.ConnectionConfig;
import com.fr.stable.NameReference;
import com.fr.transaction.WorkerCallBack;
import com.fr.workspace.WorkContext;
import com.fr.workspace.server.connection.DBConnectAuth;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 选择数据连接的下拉框
*/
public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel {
private static final long serialVersionUID = 1L;
private Class<? extends Connection> cls;
private List<String> nameList = new ArrayList<>();
public class ConnectionComboBoxPanel extends com.fr.design.data.datapane.connect.ConnectionComboBoxPanel{
public ConnectionComboBoxPanel(Class<? extends Connection> cls) {
super(cls);
super();
this.cls = cls;
this.itemComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String selected = ConnectionComboBoxPanel.this.getSelectedItem();
if (StringKit.isNotBlank(selected)) {
DesignerEnvManager.getEnvManager().setRecentSelectedConnection(selected);
}
}
});
refreshItems();
}
protected Iterator<String> items() {
nameList = new ArrayList<>();
Iterator<String> nameIt = ConnectionKit.getConnections().keySet().iterator();
Collection<String> noAuthConnections = WorkContext.getCurrent().get(DBConnectAuth.class).getNoAuthConnections();
if (noAuthConnections == null) {
return nameList.iterator();
}
while (nameIt.hasNext()) {
String conName = nameIt.next();
if (noAuthConnections.contains(conName)) {
continue;
}
Connection connection = ConnectionKit.getConnection(conName);
filterConnection(connection, conName, nameList);
}
return nameList.iterator();
}
protected void filterConnection(@Nullable Connection connection, String conName, List<String> nameList) {
if (connection != null) {
connection.addConnection(nameList, conName, new Class[]{BaseConnection.class});
}
}
public int getConnectionSize() {
return nameList.size();
}
private boolean isEmptyConnection() {
return nameList.isEmpty();
}
public String getConnection(int i) {
return nameList.get(i);
}
protected void editItems() {
final ConnectionListPane connectionListPane = new ConnectionListPane();
final ConnectionConfig connectionConfig = ConnectionConfig.getInstance();
ConnectionConfig cloned = connectionConfig.mirror();
connectionListPane.populate(cloned);
final BasicDialog connectionListDialog = connectionListPane.showLargeWindow(
SwingUtilities.getWindowAncestor(ConnectionComboBoxPanel.this), null);
connectionListDialog.addDialogActionListener(new DialogActionAdapter() {
public void doOk() {
if (!connectionListPane.isNamePermitted()) {
connectionListDialog.setDoOKSucceed(false);
return;
}
ConfigurationKit.modify(ConnectionConfig.class, new Runner() {
@Override
public void run() {
connectionListPane.update(connectionConfig);
}
}, new WorkerCallBack() {
@Override
public boolean beforeCommit() {
//如果更新失败,则不关闭对话框,也不写xml文件,并且将对话框定位在请重命名的那个对象页面
return doWithDatasourceManager(connectionConfig, connectionListPane, connectionListDialog);
}
@Override
public void afterCommit() {
DesignKit.getDesignerBean("databasename").refreshBeanElement();
}
@Override
public void afterRollback() {
}
});
}
});
connectionListDialog.setVisible(true);
refreshItems();
}
/**
* @param connection 数据库链接
*/
public void populate(Connection connection) {
editButton.setEnabled(WorkContext.getCurrent().isRoot());
if (connection instanceof NameReference) {
this.setSelectedItem(((NameReference) connection).getName());
} else {
String s = DesignerEnvManager.getEnvManager().getRecentSelectedConnection();
if (StringKit.isNotBlank(s)) {
for (int i = 0; i < this.getConnectionSize(); i++) {
String t = this.getConnection(i);
if (AssistKit.equals(s, t)) {
this.setSelectedItem(s);
break;
}
}
}
if (StringKit.isBlank(this.getSelectedItem()) && !isEmptyConnection()) {
this.setSelectedItem(this.getConnection(0));
}
}
}
private boolean doWithDatasourceManager(ConnectionConfig datasourceManager, ConnectionShowPane connectionShowPane, BasicDialog databaseListDialog) {
boolean isFailed = false;
//存在请重命名则不能更新
int index = isConnectionMapContainsRename(datasourceManager);
if (index != -1) {
isFailed = true;
connectionShowPane.setSelectedIndex(index);
}
databaseListDialog.setDoOKSucceed(!isFailed);
return !isFailed;
}
private int isConnectionMapContainsRename(ConnectionConfig datasourceManager) {
Map<String, Connection> tableDataMap = datasourceManager.getConnections();
if (tableDataMap.containsKey(DatasourceConstants.ILLEGAL_NAME_HOLDER)) {
return datasourceManager.getConnectionIndex(DatasourceConstants.ILLEGAL_NAME_HOLDER);
}
return -1;
}
}
}

0
src/main/resources/com/fanruan/api/locale.properties

0
src/main/resources/com/fanruan/api/locale_en_US.properties

0
src/main/resources/com/fanruan/api/locale_ja_JP.properties

0
src/main/resources/com/fanruan/api/locale_ko_KR.properties

0
src/main/resources/com/fanruan/api/locale_zh_CN.properties

0
src/main/resources/com/fanruan/api/locale_zh_TW.properties

Loading…
Cancel
Save