Browse Source

KERNEL-6188 DatabaseConnectionPane中的SwingWorker问题

【问题原因】在Swingworker的doInBackground中更新UI会导致出现卡死等问题
【改动方案】将doInBackground中与UI交互的部分移到done方法中,在EDT线程处理UI,比较安全;另外修改了dialog.show()过时方法和一些导包相关问题
feature/big-screen
Yvan 4 years ago
parent
commit
40663f225c
  1. 68
      designer-base/src/main/java/com/fr/design/data/datapane/connect/DatabaseConnectionPane.java

68
designer-base/src/main/java/com/fr/design/data/datapane/connect/DatabaseConnectionPane.java

@ -3,6 +3,7 @@
*/
package com.fr.design.data.datapane.connect;
import com.fr.data.impl.Connection;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.data.impl.JNDIDatabaseConnection;
import com.fr.data.operator.DataOperator;
@ -10,6 +11,7 @@ import com.fr.design.beans.BasicBeanPane;
import com.fr.design.gui.ibutton.UIButton;
import com.fr.design.gui.icombobox.UIComboBox;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.scrollruler.ModLineBorder;
import com.fr.design.utils.gui.GUICoreUtils;
@ -19,12 +21,24 @@ import com.fr.stable.ArrayUtils;
import com.fr.stable.EncodeConstants;
import com.fr.stable.StringUtils;
import javax.swing.*;
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/**
* Database Connection pane.
@ -48,9 +62,9 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
protected void initComponents() {
message = new UILabel();
uiLabel = new UILabel();
okButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_OK"));
cancelButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Cancel"));
String[] defaultEncode = new String[] {com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Encode_Auto")};
okButton = new UIButton(Toolkit.i18nText("Fine-Design_Report_OK"));
cancelButton = new UIButton(Toolkit.i18nText("Fine-Design_Basic_Cancel"));
String[] defaultEncode = new String[] {Toolkit.i18nText("Fine-Design_Encode_Auto")};
charSetComboBox = new UIComboBox(ArrayUtils.addAll(defaultEncode, EncodeConstants.ENCODING_ARRAY));
this.setLayout(FRGUIPaneFactory.createBorderLayout());
JPanel northPane = FRGUIPaneFactory.createY_AXISBoxInnerContainer_L_Pane();
@ -59,7 +73,7 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
// 按钮.
JPanel testPane = FRGUIPaneFactory.createNormalFlowInnerContainer_M_Pane();
northPane.add(testPane, BorderLayout.NORTH);
UIButton testButton = new UIButton(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Datasource_Test_Connection"));
UIButton testButton = new UIButton(Toolkit.i18nText("Fine-Design_Basic_Datasource_Test_Connection"));
testPane.add(testButton);
testButton.addActionListener(testConnectionActionListener);
testPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 3, 4));
@ -71,9 +85,9 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
northPane.add(chartSetPane);
chartSetPane.setBorder(BorderFactory.createTitledBorder(
new ModLineBorder(ModLineBorder.TOP),
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Advanced")
Toolkit.i18nText("Fine-Design_Basic_Advanced")
));
chartSetPane.add(GUICoreUtils.createNamedPane(charSetComboBox, com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Datasource_Charset") + ":"));
chartSetPane.add(GUICoreUtils.createNamedPane(charSetComboBox, Toolkit.i18nText("Fine-Design_Basic_Datasource_Charset") + ":"));
}
protected abstract JPanel mainPanel();
@ -84,7 +98,7 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
public void populateBean(com.fr.data.impl.Connection ob) {
this.originalCharSet = ob.getOriginalCharsetName();
if (StringUtils.isBlank(originalCharSet)) {
this.charSetComboBox.setSelectedItem(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Encode_Auto"));
this.charSetComboBox.setSelectedItem(Toolkit.i18nText("Fine-Design_Encode_Auto"));
} else {
this.charSetComboBox.setSelectedItem(ob.getOriginalCharsetName());
}
@ -118,23 +132,39 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
// Try the java connection.
final SwingWorker connectionThread = new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
try {
com.fr.data.impl.Connection database = DatabaseConnectionPane.this.updateBean();
Connection database = DatabaseConnectionPane.this.updateBean();
boolean connect = DataOperator.getInstance().testConnection(database);
Map<String, Object> map = new HashMap<>();
map.put("database", database);
map.put("connect", connect);
return map;
} catch (Exception exp) {
FineLoggerFactory.getLogger().error(exp.getMessage(), exp);
}
return null;
}
@Override
protected void done() {
try {
Map<String, Object> map = (Map<String, Object>) get();
Connection database = (Connection) map.get("database");
boolean connect = (boolean) map.get("connect");
okButton.setEnabled(true);
message.setText(database.connectMessage(connect));
if (connect) {
uiLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
message.setText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Datasource_Connection_Successfully"));
message.setText(Toolkit.i18nText("Fine-Design_Basic_Datasource_Connection_Successfully"));
}else{
uiLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
message.setText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Connection_Failed"));
message.setText(Toolkit.i18nText("Fine-Design_Basic_Connection_Failed"));
}
} catch (Exception exp) {
FineLoggerFactory.getLogger().error(exp.getMessage(), exp);
} catch (InterruptedException | ExecutionException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
return null;
}
};
@ -158,18 +188,18 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
}
});
dialog.show();
dialog.setVisible(true);
dialog.dispose();
}
};
private void initDialogPane() {
message.setText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Datasource_Test_Connection") + "...");
message.setText(Toolkit.i18nText("Fine-Design_Basic_Datasource_Test_Connection") + "...");
message.setBorder(BorderFactory.createEmptyBorder(8, 5, 0, 0));
okButton.setEnabled(false);
dialog = new JDialog((Dialog) SwingUtilities.getWindowAncestor(DatabaseConnectionPane.this), com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Datasource_Test_Connection"), true);
dialog = new JDialog((Dialog) SwingUtilities.getWindowAncestor(DatabaseConnectionPane.this), Toolkit.i18nText("Fine-Design_Basic_Datasource_Test_Connection"), true);
dialog.setSize(new Dimension(268, 118));
okButton.setEnabled(false);
JPanel jp = new JPanel();
@ -250,4 +280,4 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
}
}
}
}

Loading…
Cancel
Save