11package sqle .action ;
22
3+ import java .lang .reflect .Field ;
34import java .util .ArrayList ;
5+ import java .util .Iterator ;
46import java .util .List ;
57import java .util .Map ;
68
9+ import org .eclipse .jface .preference .ComboFieldEditor ;
10+ import org .eclipse .jface .preference .FieldEditor ;
711import org .eclipse .jface .preference .FieldEditorPreferencePage ;
12+ import org .eclipse .jface .preference .IPreferenceStore ;
813import org .eclipse .jface .preference .RadioGroupFieldEditor ;
914import org .eclipse .jface .preference .StringFieldEditor ;
15+ import org .eclipse .jface .resource .JFaceResources ;
1016import org .eclipse .swt .SWT ;
1117import org .eclipse .swt .events .SelectionAdapter ;
1218import org .eclipse .swt .events .SelectionEvent ;
1319import org .eclipse .swt .layout .GridData ;
20+ import org .eclipse .swt .layout .GridLayout ;
1421import org .eclipse .swt .widgets .Button ;
22+ import org .eclipse .swt .widgets .Combo ;
1523import org .eclipse .swt .widgets .Composite ;
1624import org .eclipse .swt .widgets .Control ;
1725import org .eclipse .swt .widgets .Display ;
1826import org .eclipse .swt .widgets .Shell ;
1927import org .eclipse .ui .IWorkbench ;
2028import org .eclipse .ui .IWorkbenchPreferencePage ;
2129import org .eclipse .ui .PlatformUI ;
30+ import org .eclipse .swt .widgets .Text ;
2231
2332import sqle .util .HttpClientSQLE ;
2433import sqle .config .SQLESettings ;
@@ -30,6 +39,8 @@ public class HomePreferencePage extends FieldEditorPreferencePage implements IWo
3039 private StringFieldEditor addrInput ;
3140 private StringFieldEditor userInput ;
3241 private StringFieldEditor passwordInput ;
42+ private StringFieldEditor tokenInput ;
43+
3344 private CustomComboFieldEditor projectCombo ;
3445 private CustomComboFieldEditor dbTypeCombo ;
3546 private CustomComboFieldEditor dataSourceCombo ;
@@ -40,6 +51,8 @@ public class HomePreferencePage extends FieldEditorPreferencePage implements IWo
4051 private HttpClientSQLE client ;
4152 private DialogInfo dialog ;
4253
54+ private String loginType ;
55+
4356 public HomePreferencePage () {
4457 super (GRID );
4558 }
@@ -51,84 +64,91 @@ public void init(IWorkbench workbench) {
5164 this .client = new HttpClientSQLE ();
5265
5366 Display display = PlatformUI .getWorkbench ().getDisplay ();
54- Shell activeShell = display .getActiveShell ();
67+ Shell activeShell = display .getActiveShell ();
5568 this .dialog = new DialogInfo (activeShell );
69+ this .loginType = settings .getLoginType ();
5670 }
5771
5872 @ Override
5973 protected void createFieldEditors () {
6074 Composite parent = getFieldEditorParent ();
75+
6176 // 添加sqle地址输入框
6277 addrInput = new StringFieldEditor (SQLESettings .SQLE_ADDR_PREFERENCE_KEY , "SQLE Addr:" , parent );
78+
6379 addField (addrInput );
6480 // 添加http/https选项框
65- String [][] httpHttpsOptions = {{"HTTP" , "http" }, {"HTTPS" , "https" }};
66- httpHttpsRadioGroup = new RadioGroupFieldEditor (
67- SQLESettings .HTTPS_PREFERENCE_KEY ,
68- "HTTP" ,
69- 2 ,
70- httpHttpsOptions ,
71- parent ,
72- true
73- );
74- addField (httpHttpsRadioGroup );
75-
81+ String [][] httpHttpsOptions = { { "HTTP" , "http" }, { "HTTPS" , "https" } };
82+ httpHttpsRadioGroup = new RadioGroupFieldEditor (SQLESettings .HTTPS_PREFERENCE_KEY , "HTTP" , 2 , httpHttpsOptions ,
83+ parent , true );
84+ addField (httpHttpsRadioGroup );
85+
86+ addCustomRadio (parent , SQLESettings .PasswordLogin );
87+ addCustomRadio (parent , SQLESettings .TokenLogin );
88+
7689 // 添加用户输入框
7790 userInput = new StringFieldEditor (SQLESettings .USER_PREFERENCE_KEY , "用户:" , parent );
7891 addField (userInput );
79-
92+
8093 // 添加密码输入框
8194 passwordInput = new StringFieldEditor (SQLESettings .PASSWORD_PREFERENCE_KEY , "密码:" , parent );
8295 passwordInput .getTextControl (parent ).setEchoChar ('*' );
8396
8497 addField (passwordInput );
8598
99+ tokenInput = new StringFieldEditor (SQLESettings .ACCESS_TOKEN_KEY , "token:" , parent );
100+ addField (tokenInput );
101+
102+ changeRadioSelect (settings .getLoginType (), parent );
103+
86104 // 添加按钮
87105 addCustomButtonField (getFieldEditorParent ());
88-
106+
89107 // 初始化选项
90108 String [][] projectOptions = settings .getProjectList ();
91- projectCombo = new CustomComboFieldEditor (SQLESettings .PROJECT_PREFERENCE_KEY , "Project" , projectOptions , parent );
109+ projectCombo = new CustomComboFieldEditor (SQLESettings .PROJECT_PREFERENCE_KEY , "Project" , projectOptions ,
110+ parent );
92111 addField (projectCombo );
93112 projectCombo .fCombo .addSelectionListener (new SelectionAdapter () {
94- @ Override
95- public void widgetSelected (SelectionEvent e ) {
96- settings .setProjectName (projectCombo .fCombo .getText ());
97- addDBSource ();
98- }
99- });
113+ @ Override
114+ public void widgetSelected (SelectionEvent e ) {
115+ settings .setProjectName (projectCombo .fCombo .getText ());
116+ addDBSource ();
117+ }
118+ });
100119
101120 String [][] dbTypeOptions = settings .getDBTypeList ();
102121 dbTypeCombo = new CustomComboFieldEditor (SQLESettings .DBTYPE_PREFERENCE_KEY , "DBType" , dbTypeOptions , parent );
103122 addField (dbTypeCombo );
104123 dbTypeCombo .fCombo .addSelectionListener (new SelectionAdapter () {
105- @ Override
106- public void widgetSelected (SelectionEvent e ) {
107- settings .setDBType (dbTypeCombo .fCombo .getText ());
108- addDBSource ();
109- }
110- });
124+ @ Override
125+ public void widgetSelected (SelectionEvent e ) {
126+ settings .setDBType (dbTypeCombo .fCombo .getText ());
127+ addDBSource ();
128+ }
129+ });
111130
112131 String [][] dbSourceOptions = settings .getDBSourceList ();
113- dataSourceCombo = new CustomComboFieldEditor (SQLESettings .DATASOURCE_PREFERENCE_KEY , "Data Source" , dbSourceOptions , parent );
132+ dataSourceCombo = new CustomComboFieldEditor (SQLESettings .DATASOURCE_PREFERENCE_KEY , "Data Source" ,
133+ dbSourceOptions , parent );
114134 addField (dataSourceCombo );
115135 dataSourceCombo .fCombo .addSelectionListener (new SelectionAdapter () {
116- @ Override
117- public void widgetSelected (SelectionEvent e ) {
118- settings .setDataSourceName (dataSourceCombo .fCombo .getText ());
119- addSchema ();
120- }
121- });
136+ @ Override
137+ public void widgetSelected (SelectionEvent e ) {
138+ settings .setDataSourceName (dataSourceCombo .fCombo .getText ());
139+ addSchema ();
140+ }
141+ });
122142
123143 String [][] schemaOptions = settings .getSchemaList ();
124144 schemaCombo = new CustomComboFieldEditor (SQLESettings .SCHEMA_PREFERENCE_KEY , "Schema" , schemaOptions , parent );
125145 addField (schemaCombo );
126146 schemaCombo .fCombo .addSelectionListener (new SelectionAdapter () {
127- @ Override
128- public void widgetSelected (SelectionEvent e ) {
129- settings .setSchemaName (schemaCombo .fCombo .getText ());
130- }
131- });
147+ @ Override
148+ public void widgetSelected (SelectionEvent e ) {
149+ settings .setSchemaName (schemaCombo .fCombo .getText ());
150+ }
151+ });
132152 }
133153
134154 private void addCustomButtonField (Composite parent ) {
@@ -155,29 +175,35 @@ private void handleButtonClick() {
155175 settings .setSQLEAddr (addrInput .getStringValue ());
156176 settings .setUserName (userInput .getStringValue ());
157177 settings .setPassword (passwordInput .getStringValue ());
158-
178+ settings .setAccessToken (tokenInput .getStringValue ());
179+
159180 Composite radioBoxControl = httpHttpsRadioGroup .getRadioBoxControl (getFieldEditorParent ());
160-
181+
161182 String selectedValue = "http" ;
162183 if (radioBoxControl != null ) {
163- Control [] children = radioBoxControl .getChildren ();
164-
165- for (Control child : children ) {
166- if (child instanceof Button ) {
167- Button button = (Button ) child ;
168-
169- if (button .getSelection ()) {
170- // 获取选中的按钮的数据
171- selectedValue = (String ) button .getData ();
172- break ;
173- }
174- }
175- }
184+ Control [] children = radioBoxControl .getChildren ();
185+
186+ for (Control child : children ) {
187+ if (child instanceof Button ) {
188+ Button button = (Button ) child ;
189+
190+ if (button .getSelection ()) {
191+ // 获取选中的按钮的数据
192+ selectedValue = (String ) button .getData ();
193+ break ;
194+ }
195+ }
196+ }
176197 }
177-
198+
178199 settings .setEnableHttps (selectedValue );
179200 try {
180- client .Login ();
201+ if (settings .getLoginType ().equals (SQLESettings .PasswordLogin ) || settings .getLoginType ().isEmpty ()) {
202+ client .Login ();
203+ } else {
204+ client .GetProjectList ();
205+ }
206+
181207 dialog .displaySuccessDialog ("Test Connect" , "Test Connection Success" );
182208 } catch (Exception e ) {
183209 e .printStackTrace ();
@@ -272,4 +298,43 @@ private static String[][] convertArrayListToArray(ArrayList<String> arrayList) {
272298 }
273299 return resultArray ;
274300 }
301+
302+ private void addCustomRadio (Composite parent , String layout ) {
303+ Button radioButton = new Button (parent , SWT .RADIO );
304+ radioButton .setText (layout );
305+
306+ if (settings .getLoginType ().equals (layout )) {
307+ radioButton .setSelection (true );
308+ }
309+
310+ // 添加选择监听器以处理按钮点击事件
311+ radioButton .addSelectionListener (new SelectionAdapter () {
312+ @ Override
313+ public void widgetSelected (SelectionEvent e ) {
314+ loginType = layout ;
315+ settings .setLoginType (layout );
316+ changeRadioSelect (layout , parent );
317+ }
318+ });
319+ }
320+
321+ private void changeRadioSelect (String layout , Composite parent ) {
322+ if (layout .equals (SQLESettings .PasswordLogin ) || layout .isEmpty ()) {
323+ userInput .setEnabled (true , parent );
324+ passwordInput .setEnabled (true , parent );
325+ tokenInput .setEnabled (false , parent );
326+ } else {
327+ userInput .setEnabled (false , parent );
328+ passwordInput .setEnabled (false , parent );
329+ tokenInput .setEnabled (true , parent );
330+ }
331+ }
332+
333+ @ Override
334+ public boolean performOk () {
335+ super .performOk ();
336+ IPreferenceStore preferenceStore = Activator .getDefault ().getPreferenceStore ();
337+ preferenceStore .setValue (SQLESettings .LOGIN_TYPE_PREFERENCE_KEY , loginType );
338+ return true ;
339+ }
275340}
0 commit comments