forked from fanruan/finekit
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.
179 lines
6.8 KiB
179 lines
6.8 KiB
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 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(); |
|
} |
|
|
|
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; |
|
} |
|
} |