Skip to content

Commit

Permalink
Merge pull request #1 from actiontech/fix-issue2309
Browse files Browse the repository at this point in the history
Fix issue2309
  • Loading branch information
hasa1K authored Mar 21, 2024
2 parents c2065d9 + f720246 commit 076258b
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 78 deletions.
177 changes: 121 additions & 56 deletions src/sqle/action/HomePreferencePage.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package sqle.action;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.swt.widgets.Text;

import sqle.util.HttpClientSQLE;
import sqle.config.SQLESettings;
Expand All @@ -30,6 +39,8 @@ public class HomePreferencePage extends FieldEditorPreferencePage implements IWo
private StringFieldEditor addrInput;
private StringFieldEditor userInput;
private StringFieldEditor passwordInput;
private StringFieldEditor tokenInput;

private CustomComboFieldEditor projectCombo;
private CustomComboFieldEditor dbTypeCombo;
private CustomComboFieldEditor dataSourceCombo;
Expand All @@ -40,6 +51,8 @@ public class HomePreferencePage extends FieldEditorPreferencePage implements IWo
private HttpClientSQLE client;
private DialogInfo dialog;

private String loginType;

public HomePreferencePage() {
super(GRID);
}
Expand All @@ -51,84 +64,91 @@ public void init(IWorkbench workbench) {
this.client = new HttpClientSQLE();

Display display = PlatformUI.getWorkbench().getDisplay();
Shell activeShell = display.getActiveShell();
Shell activeShell = display.getActiveShell();
this.dialog = new DialogInfo(activeShell);
this.loginType = settings.getLoginType();
}

@Override
protected void createFieldEditors() {
Composite parent = getFieldEditorParent();

// 添加sqle地址输入框
addrInput = new StringFieldEditor(SQLESettings.SQLE_ADDR_PREFERENCE_KEY, "SQLE Addr:", parent);

addField(addrInput);
// 添加http/https选项框
String[][] httpHttpsOptions = {{"HTTP", "http"}, {"HTTPS", "https"}};
httpHttpsRadioGroup = new RadioGroupFieldEditor(
SQLESettings.HTTPS_PREFERENCE_KEY,
"HTTP",
2,
httpHttpsOptions,
parent,
true
);
addField(httpHttpsRadioGroup);

String[][] httpHttpsOptions = { { "HTTP", "http" }, { "HTTPS", "https" } };
httpHttpsRadioGroup = new RadioGroupFieldEditor(SQLESettings.HTTPS_PREFERENCE_KEY, "HTTP", 2, httpHttpsOptions,
parent, true);
addField(httpHttpsRadioGroup);

addCustomRadio(parent, SQLESettings.PasswordLogin);
addCustomRadio(parent, SQLESettings.TokenLogin);

// 添加用户输入框
userInput = new StringFieldEditor(SQLESettings.USER_PREFERENCE_KEY, "用户:", parent);
addField(userInput);

// 添加密码输入框
passwordInput = new StringFieldEditor(SQLESettings.PASSWORD_PREFERENCE_KEY, "密码:", parent);
passwordInput.getTextControl(parent).setEchoChar('*');

addField(passwordInput);

tokenInput = new StringFieldEditor(SQLESettings.ACCESS_TOKEN_KEY, "token:", parent);
addField(tokenInput);

changeRadioSelect(settings.getLoginType(), parent);

// 添加按钮
addCustomButtonField(getFieldEditorParent());

// 初始化选项
String[][] projectOptions = settings.getProjectList();
projectCombo = new CustomComboFieldEditor(SQLESettings.PROJECT_PREFERENCE_KEY, "Project", projectOptions, parent);
projectCombo = new CustomComboFieldEditor(SQLESettings.PROJECT_PREFERENCE_KEY, "Project", projectOptions,
parent);
addField(projectCombo);
projectCombo.fCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setProjectName(projectCombo.fCombo.getText());
addDBSource();
}
});
@Override
public void widgetSelected(SelectionEvent e) {
settings.setProjectName(projectCombo.fCombo.getText());
addDBSource();
}
});

String[][] dbTypeOptions = settings.getDBTypeList();
dbTypeCombo = new CustomComboFieldEditor(SQLESettings.DBTYPE_PREFERENCE_KEY, "DBType", dbTypeOptions, parent);
addField(dbTypeCombo);
dbTypeCombo.fCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setDBType(dbTypeCombo.fCombo.getText());
addDBSource();
}
});
@Override
public void widgetSelected(SelectionEvent e) {
settings.setDBType(dbTypeCombo.fCombo.getText());
addDBSource();
}
});

String[][] dbSourceOptions = settings.getDBSourceList();
dataSourceCombo = new CustomComboFieldEditor(SQLESettings.DATASOURCE_PREFERENCE_KEY, "Data Source", dbSourceOptions, parent);
dataSourceCombo = new CustomComboFieldEditor(SQLESettings.DATASOURCE_PREFERENCE_KEY, "Data Source",
dbSourceOptions, parent);
addField(dataSourceCombo);
dataSourceCombo.fCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setDataSourceName(dataSourceCombo.fCombo.getText());
addSchema();
}
});
@Override
public void widgetSelected(SelectionEvent e) {
settings.setDataSourceName(dataSourceCombo.fCombo.getText());
addSchema();
}
});

String[][] schemaOptions = settings.getSchemaList();
schemaCombo = new CustomComboFieldEditor(SQLESettings.SCHEMA_PREFERENCE_KEY, "Schema", schemaOptions, parent);
addField(schemaCombo);
schemaCombo.fCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setSchemaName(schemaCombo.fCombo.getText());
}
});
@Override
public void widgetSelected(SelectionEvent e) {
settings.setSchemaName(schemaCombo.fCombo.getText());
}
});
}

private void addCustomButtonField(Composite parent) {
Expand All @@ -155,29 +175,35 @@ private void handleButtonClick() {
settings.setSQLEAddr(addrInput.getStringValue());
settings.setUserName(userInput.getStringValue());
settings.setPassword(passwordInput.getStringValue());

settings.setAccessToken(tokenInput.getStringValue());

Composite radioBoxControl = httpHttpsRadioGroup.getRadioBoxControl(getFieldEditorParent());

String selectedValue = "http";
if (radioBoxControl != null) {
Control[] children = radioBoxControl.getChildren();
for (Control child : children) {
if (child instanceof Button) {
Button button = (Button) child;
if (button.getSelection()) {
// 获取选中的按钮的数据
selectedValue = (String) button.getData();
break;
}
}
}
Control[] children = radioBoxControl.getChildren();

for (Control child : children) {
if (child instanceof Button) {
Button button = (Button) child;

if (button.getSelection()) {
// 获取选中的按钮的数据
selectedValue = (String) button.getData();
break;
}
}
}
}

settings.setEnableHttps(selectedValue);
try {
client.Login();
if (settings.getLoginType().equals(SQLESettings.PasswordLogin) || settings.getLoginType().isEmpty()) {
client.Login();
} else {
client.GetProjectList();
}

dialog.displaySuccessDialog("Test Connect", "Test Connection Success");
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -272,4 +298,43 @@ private static String[][] convertArrayListToArray(ArrayList<String> arrayList) {
}
return resultArray;
}

private void addCustomRadio(Composite parent, String layout) {
Button radioButton = new Button(parent, SWT.RADIO);
radioButton.setText(layout);

if (settings.getLoginType().equals(layout)) {
radioButton.setSelection(true);
}

// 添加选择监听器以处理按钮点击事件
radioButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
loginType = layout;
settings.setLoginType(layout);
changeRadioSelect(layout, parent);
}
});
}

private void changeRadioSelect(String layout, Composite parent) {
if (layout.equals(SQLESettings.PasswordLogin) || layout.isEmpty()) {
userInput.setEnabled(true, parent);
passwordInput.setEnabled(true, parent);
tokenInput.setEnabled(false, parent);
} else {
userInput.setEnabled(false, parent);
passwordInput.setEnabled(false, parent);
tokenInput.setEnabled(true, parent);
}
}

@Override
public boolean performOk() {
super.performOk();
IPreferenceStore preferenceStore = Activator.getDefault().getPreferenceStore();
preferenceStore.setValue(SQLESettings.LOGIN_TYPE_PREFERENCE_KEY, loginType);
return true;
}
}
28 changes: 28 additions & 0 deletions src/sqle/config/SQLESettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public class SQLESettings {
public static final String DATASOURCE_PREFERENCE_KEY = "dataSourcePreference";
public static final String SCHEMA_PREFERENCE_KEY = "schemaPreference";
public static final String HTTPS_PREFERENCE_KEY = "httpPreference";
public static final String LOGIN_TYPE_PREFERENCE_KEY = "loginTypePreference";
public static final String ACCESS_TOKEN_KEY = "accessTokenKey";

public static final String PasswordLogin = "账号密码登录";
public static final String TokenLogin = "token登录";



private String SQLEAddr;
private boolean EnableHttps;
Expand All @@ -29,6 +36,8 @@ public class SQLESettings {
private String DataSourceName;
private String SchemaName;
private String Token;
private String LoginType;
private String AccessToken;

public String[][] ProjectList = {};
public String[][] DBTypeList = {};
Expand All @@ -47,6 +56,8 @@ public void UpdateSettings() {
String dataSource = store.getString(DATASOURCE_PREFERENCE_KEY);
String schema = store.getString(SCHEMA_PREFERENCE_KEY);
String httpType = store.getString(HTTPS_PREFERENCE_KEY);
String loginType = store.getString(LOGIN_TYPE_PREFERENCE_KEY);
String accessToken = store.getString(ACCESS_TOKEN_KEY);

String[][] projectList = {{project, project}};
this.setProjectList(projectList);
Expand All @@ -65,6 +76,8 @@ public void UpdateSettings() {
this.DBType = dbType;
this.DataSourceName = dataSource;
this.SchemaName = schema;
this.LoginType = loginType;
this.AccessToken = accessToken;
}

private SQLESettings() {
Expand Down Expand Up @@ -174,4 +187,19 @@ public Map<String, String> getProjectUidMap() {
public void setProjectUidMap(Map<String, String> projectUidMap) {
this.projectUidMap = projectUidMap;
}
public String getLoginType() {
if (LoginType.isEmpty()) {
return PasswordLogin;
}
return LoginType;
}
public void setLoginType(String loginType) {
LoginType = loginType;
}
public String getAccessToekn() {
return AccessToken;
}
public void setAccessToken(String accessToken) {
AccessToken = accessToken;
}
}
Loading

0 comments on commit 076258b

Please sign in to comment.