Browse Source

Pull request #11475: REPORT-86502 数据连接添加驱动冲突检测

Merge in DESIGN/design from ~MAXIMUS/design:feature/x to feature/x

* commit '7435d11986a2fd634d130e45b64480ab27dbb18e':
  REPORT-86502 SwingWorker太大了,拆一下
  REPORT-86502 设计器中增加连接失败时显示驱动加载路径与检测驱动冲突的功能
feature/x
Maximus-郭晓龙 2 years ago
parent
commit
44ce6c4869
  1. 306
      designer-base/src/main/java/com/fr/design/data/datapane/connect/DatabaseConnectionPane.java

306
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.driver.util.JarFileParseUtil;
import com.fr.data.impl.Connection;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.data.impl.JNDIDatabaseConnection;
@ -32,10 +33,12 @@ import com.fr.workspace.WorkContext;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
@ -55,6 +58,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.URI;
import java.util.concurrent.ExecutionException;
@ -93,120 +97,7 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
public void actionPerformed(ActionEvent evt) {
// Try the java connection.
final SwingWorker<Void, Void> connectionThread = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Connection database = DatabaseConnectionPane.this.updateBean();
// 返回连接结果
DriverPage.updateCache();
final Exception[] exception = new Exception[1];
WorkContext.getCurrent().get(DataOperatorProvider.class, new ExceptionHandler() {
@Override
public Object callHandler(RPCInvokerExceptionInfo exceptionInfo) {
// 正常调用发生的异常也会被捕获,因此需要对异常类型进行判断,如果是NoSuchMethodException 就要去调用 testConnection
// 如果不是 NoSuchMethodException 保存下异常上下文
// 两种情况下异常都需要抛出
if (exceptionInfo.getException() instanceof NoSuchMethodException) {
if (!WorkContext.getCurrent().get(DataOperatorProvider.class).testConnection(database)) {
exception[0] = new Exception(Toolkit.i18nText("Fine-Design_Description_Of_Test_Connection"));
}
} else {
exception[0] = exceptionInfo.getException();
}
return null;
}
}).testConnectionWithException(database);
if (exception[0] != null) {
throw exception[0];
}
return null;
}
@Override
protected void done() {
try {
get();
dialog.setSize(new Dimension(380, 125));
okButton.setEnabled(true);
uiLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
message.setText(Toolkit.i18nText("Fine-Design_Basic_Datasource_Connection_Successfully"));
} catch (InterruptedException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
} catch (ExecutionException e) {
dialog.setSize(new Dimension(380, 142));
midPane.setVisible(true);
hiddenPanel.setVisible(false);
okButton.setEnabled(true);
uiLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
message.setText(Toolkit.i18nText("Fine-Design_Basic_Connection_Failed"));
Connection database = DatabaseConnectionPane.this.updateBean();
SolutionProcessor select = ExceptionSolutionSelector.get().select(e, database);
if (select instanceof ClassNotFoundExceptionSolutionProcessor) {
JPanel gridJpanel = new JPanel();
gridJpanel.setLayout(new GridLayout(5, 1, 0, 5));
UILabel driverTips = new UILabel();
driverTips.setText(Toolkit.i18nText("Fine_Designer_Not_Found_Driver"));
gridJpanel.add(driverTips);
UILabel deatail = new UILabel();
String content = Toolkit.i18nText("Fine_Designer_Not_Found") + " " + select.getResultException().getDetailMessage() + " " + Toolkit.i18nText("Fine_Designer_Driver");
deatail.setText(content);
deatail.setToolTipText(content);
gridJpanel.add(deatail);
String solution = select.getResultException().getSolution();
UILabel redirect = new UILabel();
if (solution != null) {
redirect.setText(Toolkit.i18nText("Fine_Designer_Download_Driver"));
redirect.setForeground(Color.BLUE);
redirect.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(new URI(solution));
} catch (Exception clickException) {
FineLoggerFactory.getLogger().warn("can not open browser with {}", solution);
}
}
@Override
public void mouseEntered(MouseEvent e) {
redirect.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseExited(MouseEvent e) {
redirect.setCursor(Cursor.getDefaultCursor());
}
});
} else {
redirect.setText(Toolkit.i18nText("Fine_Designer_Not_Found_Driver_No_Solution"));
}
gridJpanel.add(redirect);
hiddenPanel.add(gridJpanel);
gridJpanel.setBackground(Color.WHITE);
} else {
JPanel borderPanel = new JPanel();
borderPanel.setLayout(new BorderLayout());
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jta.append(select.getResultException().getDetailMessage() + "\n");
jta.append(select.getResultException().getSolution());
jta.setCaretPosition(0);
jta.setEditable(false);
jta.getCaret().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jta.getCaret().setVisible(true);
}
});
borderPanel.add(jsp, BorderLayout.CENTER);
hiddenPanel.add(borderPanel);
}
okButton.setEnabled(true);
}
}
};
final SwingWorker<Void, Void> connectionThread = new TestConnectionWorker();
midPane.setVisible(false);
hiddenPanel.setVisible(false);
initDialogPane();
@ -530,4 +421,191 @@ public abstract class DatabaseConnectionPane<E extends com.fr.data.impl.Connecti
return "JNDI";
}
}
private class TestConnectionWorker extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
Connection database = DatabaseConnectionPane.this.updateBean();
// 返回连接结果
DriverPage.updateCache();
final Exception[] exception = new Exception[1];
WorkContext.getCurrent().get(DataOperatorProvider.class, new ExceptionHandler() {
@Override
public Object callHandler(RPCInvokerExceptionInfo exceptionInfo) {
// 正常调用发生的异常也会被捕获,因此需要对异常类型进行判断,如果是NoSuchMethodException 就要去调用 testConnection
// 如果不是 NoSuchMethodException 保存下异常上下文
// 两种情况下异常都需要抛出
if (exceptionInfo.getException() instanceof NoSuchMethodException) {
if (!WorkContext.getCurrent().get(DataOperatorProvider.class).testConnection(database)) {
exception[0] = new Exception(Toolkit.i18nText("Fine-Design_Description_Of_Test_Connection"));
}
} else {
exception[0] = exceptionInfo.getException();
}
return null;
}
}).testConnectionWithException(database);
if (exception[0] != null) {
throw exception[0];
}
return null;
}
@Override
protected void done() {
try {
get();
dialog.setSize(new Dimension(380, 125));
okButton.setEnabled(true);
uiLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
message.setText(Toolkit.i18nText("Fine-Design_Basic_Datasource_Connection_Successfully"));
} catch (InterruptedException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
} catch (ExecutionException e) {
dealWithException(e);
}
}
private void dealWithException(ExecutionException e) {
dialog.setSize(new Dimension(380, 142));
midPane.setVisible(true);
hiddenPanel.setVisible(false);
okButton.setEnabled(true);
uiLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
message.setText(Toolkit.i18nText("Fine-Design_Basic_Connection_Failed"));
Connection database = DatabaseConnectionPane.this.updateBean();
SolutionProcessor select = ExceptionSolutionSelector.get().select(e, database);
String detail = select.getResultException().getDetailMessage();
String solution = select.getResultException().getSolution();
if (select instanceof ClassNotFoundExceptionSolutionProcessor) {
showClassNotFoundUI(select.getResultException().getDetailMessage(), select.getResultException().getSolution());
} else {
showExceptionMessageUI(detail, solution);
}
okButton.setEnabled(true);
}
private void showClassNotFoundUI(String detailMessage, String solution) {
JPanel gridJpanel = new JPanel();
gridJpanel.setLayout(new GridLayout(5, 1, 0, 5));
UILabel driverTips = new UILabel();
driverTips.setText(Toolkit.i18nText("Fine_Designer_Not_Found_Driver"));
gridJpanel.add(driverTips);
UILabel deatail = new UILabel();
String content = Toolkit.i18nText("Fine_Designer_Not_Found") + " " + detailMessage+ " " + Toolkit.i18nText("Fine_Designer_Driver");
deatail.setText(content);
deatail.setToolTipText(content);
gridJpanel.add(deatail);
UILabel redirect = new UILabel();
if (solution != null) {
redirect.setText(Toolkit.i18nText("Fine_Designer_Download_Driver"));
redirect.setForeground(Color.BLUE);
redirect.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(new URI(solution));
} catch (Exception clickException) {
FineLoggerFactory.getLogger().warn("can not open browser with {}", solution);
}
}
@Override
public void mouseEntered(MouseEvent e) {
redirect.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseExited(MouseEvent e) {
redirect.setCursor(Cursor.getDefaultCursor());
}
});
} else {
redirect.setText(Toolkit.i18nText("Fine_Designer_Not_Found_Driver_No_Solution"));
}
gridJpanel.add(redirect);
hiddenPanel.add(gridJpanel);
gridJpanel.setBackground(Color.WHITE);
}
private void showExceptionMessageUI(String detail, String solution) {
JPanel borderPanel = new JPanel();
borderPanel.setLayout(new BorderLayout());
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Connection con = DatabaseConnectionPane.this.updateBean();
if(con instanceof JDBCDatabaseConnection) {
String driverPath = JarFileParseUtil.getDriverClassPath((JDBCDatabaseConnection) con);
jta.append(Toolkit.i18nText("Fine_Designer_Current_Driver_Path") + ":" + driverPath + "\n");
JPanel testDriverPanel = generateTestDriverPanel((JDBCDatabaseConnection) con, driverPath);
borderPanel.add(testDriverPanel, BorderLayout.NORTH);
}
jta.append(detail + "\n");
jta.append(solution);
jta.setCaretPosition(0);
jta.setEditable(false);
jta.getCaret().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
jta.getCaret().setVisible(true);
}
});
borderPanel.add(jsp, BorderLayout.CENTER);
hiddenPanel.add(borderPanel);
}
private JPanel generateTestDriverPanel(JDBCDatabaseConnection con, String driverPath) {
JPanel xBorderPanel = new JPanel();
xBorderPanel.setLayout(new BorderLayout());
UILabel driverTestTip = new UILabel();
JLabel testResult = new JLabel();
driverTestTip.setForeground(Color.BLUE);
driverTestTip.setVisible(true);
testResult.setVisible(false);
testResult.setHorizontalAlignment(SwingConstants.CENTER);
driverTestTip.setText(Toolkit.i18nText("Fine_Designer_Driver_Path_Test"));
xBorderPanel.add(driverTestTip, BorderLayout.WEST);
xBorderPanel.add(testResult, BorderLayout.CENTER);
driverTestTip.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
driverTestTip.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseExited(MouseEvent e) {
driverTestTip.setCursor(Cursor.getDefaultCursor());
}
@Override
public void mouseClicked(MouseEvent e) {
try {
String path;
if(driverPath.endsWith(JarFileParseUtil.JAR_MARKER)) {
path = new File(driverPath).getParent();
} else {
path = driverPath;
}
if(JarFileParseUtil.hasDuplicateDriver(con.getDriver(), path)) {
testResult.setForeground(Color.RED);
testResult.setText(Toolkit.i18nText("Fine_Designer_Driver_Conflict"));
} else {
testResult.setForeground(Color.BLACK);
testResult.setText(Toolkit.i18nText("Fine_Designer_Driver_No_Conflict"));
}
testResult.setVisible(true);
} catch (Exception clickException) {
FineLoggerFactory.getLogger().warn(clickException, "can not test driver conflict");
}
}
});
return xBorderPanel;
}
}
}

Loading…
Cancel
Save