From 403a0cf1449da892551697f90959ed60c2776ca7 Mon Sep 17 00:00:00 2001 From: richie Date: Tue, 24 Sep 2019 10:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=95=B0=E6=8D=AE=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fanruan/api/conf/ConfigurationKit.java | 23 +++ .../com/fanruan/api/data/ConnectionKit.java | 11 ++ .../com/fanruan/api/design/DesignKit.java | 22 ++- .../api/design/macro/DatasourceConstants.java | 13 ++ .../design/work/ConnectionComboBoxPanel.java | 176 +++++++++++++++++- .../com/fanruan/api/locale.properties | 0 .../com/fanruan/api/locale_en_US.properties | 0 .../com/fanruan/api/locale_ja_JP.properties | 0 .../com/fanruan/api/locale_ko_KR.properties | 0 .../com/fanruan/api/locale_zh_CN.properties | 0 .../com/fanruan/api/locale_zh_TW.properties | 0 11 files changed, 239 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/fanruan/api/design/macro/DatasourceConstants.java create mode 100644 src/main/resources/com/fanruan/api/locale.properties create mode 100644 src/main/resources/com/fanruan/api/locale_en_US.properties create mode 100644 src/main/resources/com/fanruan/api/locale_ja_JP.properties create mode 100644 src/main/resources/com/fanruan/api/locale_ko_KR.properties create mode 100644 src/main/resources/com/fanruan/api/locale_zh_CN.properties create mode 100644 src/main/resources/com/fanruan/api/locale_zh_TW.properties diff --git a/src/main/java/com/fanruan/api/conf/ConfigurationKit.java b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java index 86cebc0..6f7ccb1 100644 --- a/src/main/java/com/fanruan/api/conf/ConfigurationKit.java +++ b/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 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); + } } diff --git a/src/main/java/com/fanruan/api/data/ConnectionKit.java b/src/main/java/com/fanruan/api/data/ConnectionKit.java index 3931a38..1715ae8 100644 --- a/src/main/java/com/fanruan/api/data/ConnectionKit.java +++ b/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 getConnections() { + return ConnectionConfig.getInstance().getConnections(); + } } diff --git a/src/main/java/com/fanruan/api/design/DesignKit.java b/src/main/java/com/fanruan/api/design/DesignKit.java index 194234a..b4b48d3 100644 --- a/src/main/java/com/fanruan/api/design/DesignKit.java +++ b/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); + } } diff --git a/src/main/java/com/fanruan/api/design/macro/DatasourceConstants.java b/src/main/java/com/fanruan/api/design/macro/DatasourceConstants.java new file mode 100644 index 0000000..197531e --- /dev/null +++ b/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; +} diff --git a/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java b/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java index 9e3228b..36284c6 100644 --- a/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java +++ b/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 cls; + private List nameList = new ArrayList<>(); -public class ConnectionComboBoxPanel extends com.fr.design.data.datapane.connect.ConnectionComboBoxPanel{ public ConnectionComboBoxPanel(Class 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 items() { + nameList = new ArrayList<>(); + Iterator nameIt = ConnectionKit.getConnections().keySet().iterator(); + Collection 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 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 tableDataMap = datasourceManager.getConnections(); + if (tableDataMap.containsKey(DatasourceConstants.ILLEGAL_NAME_HOLDER)) { + return datasourceManager.getConnectionIndex(DatasourceConstants.ILLEGAL_NAME_HOLDER); + } + return -1; } -} +} \ No newline at end of file diff --git a/src/main/resources/com/fanruan/api/locale.properties b/src/main/resources/com/fanruan/api/locale.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/com/fanruan/api/locale_en_US.properties b/src/main/resources/com/fanruan/api/locale_en_US.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/com/fanruan/api/locale_ja_JP.properties b/src/main/resources/com/fanruan/api/locale_ja_JP.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/com/fanruan/api/locale_ko_KR.properties b/src/main/resources/com/fanruan/api/locale_ko_KR.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/com/fanruan/api/locale_zh_CN.properties b/src/main/resources/com/fanruan/api/locale_zh_CN.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/com/fanruan/api/locale_zh_TW.properties b/src/main/resources/com/fanruan/api/locale_zh_TW.properties new file mode 100644 index 0000000..e69de29