|
|
|
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.design.ui.component.UIButton;
|
|
|
|
import com.fanruan.api.design.work.compat.ConnectionLockHelper;
|
|
|
|
import com.fanruan.api.design.work.component.ItemEditableComboBoxPanel;
|
|
|
|
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.dialog.BasicDialog;
|
|
|
|
import com.fr.design.dialog.DialogActionAdapter;
|
|
|
|
import com.fr.file.ConnectionConfig;
|
|
|
|
import com.fr.general.GeneralUtils;
|
|
|
|
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.*;
|
|
|
|
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 ConnectionComboBoxPanel(Class<? extends Connection> 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected UIButton initEditButton(UIButton editButton, Dimension buttonSize) {
|
|
|
|
if (isChangedVersion()) {
|
|
|
|
return ConnectionLockHelper.generateLockButton(buttonSize, e -> editItems());
|
|
|
|
} else {
|
|
|
|
return super.initEditButton(editButton, buttonSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isChangedVersion() {
|
|
|
|
return GeneralUtils.readBuildNO().compareTo("2021.03.15") > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
if (isChangedVersion()) {
|
|
|
|
boolean lock = ConnectionLockHelper.doLock(this);
|
|
|
|
if (lock) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
try {
|
|
|
|
connectionListPane.update(connectionConfig);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, new WorkerCallBack() {
|
|
|
|
@Override
|
|
|
|
public boolean beforeCommit() {
|
|
|
|
//如果更新失败,则不关闭对话框,也不写xml文件,并且将对话框定位在请重命名的那个对象页面
|
|
|
|
return doWithDatasourceManager(connectionConfig, connectionListPane, connectionListDialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterCommit() {
|
|
|
|
DesignKit.getDesignerBean("databasename").refreshBeanElement();
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterRollback() {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doCancel() {
|
|
|
|
super.doCancel();
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connectionListDialog.setVisible(true);
|
|
|
|
refreshItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unlock() {
|
|
|
|
if (isChangedVersion()) {
|
|
|
|
ConnectionLockHelper.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|