-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPreferencesPage.java
More file actions
171 lines (140 loc) · 6 KB
/
Copy pathPreferencesPage.java
File metadata and controls
171 lines (140 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.checkmarx.eclipse.properties;
import java.util.concurrent.CompletableFuture;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
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.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import com.checkmarx.eclipse.Activator;
import com.checkmarx.eclipse.runner.Authenticator;
import com.checkmarx.eclipse.utils.CxLogger;
import com.checkmarx.eclipse.utils.PluginConstants;
import com.checkmarx.eclipse.utils.PluginUtils;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.PartInitException;
import java.net.MalformedURLException;
import java.net.URL;
public class PreferencesPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
public PreferencesPage() {
super(GRID);
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(this::handlePropertyChange);
}
private void handlePropertyChange(PropertyChangeEvent event) {
}
@Override
public void init(IWorkbench workbench) {
setPreferenceStore(Preferences.STORE);
setMessage("Checkmarx One preferences");
}
@Override
protected void createFieldEditors() {
Composite topComposite = new Composite(getFieldEditorParent(), SWT.NONE);
GridData topGridData = new GridData();
topGridData.horizontalAlignment = GridData.FILL;
topGridData.verticalAlignment = GridData.FILL;
topGridData.grabExcessHorizontalSpace = true;
topComposite.setLayoutData(topGridData);
getFieldEditorParent().setLayoutData(topGridData);
GridLayout parentLayout = new GridLayout();
parentLayout.numColumns = 1;
parentLayout.horizontalSpacing = 0;
parentLayout.verticalSpacing = 0;
parentLayout.marginHeight = 0;
parentLayout.marginWidth = 0;
topComposite.setLayout(parentLayout);
StringFieldEditor apiKey = new StringFieldEditor(Preferences.API_KEY, PluginConstants.PREFERENCES_API_KEY, topComposite);
addField(apiKey);
Text textControl = apiKey.getTextControl(topComposite);
textControl.setEchoChar('*');
StringFieldEditor additionalParams = new StringFieldEditor(Preferences.ADDITIONAL_OPTIONS,
PluginConstants.PREFERENCES_ADDITIONAL_OPTIONS, StringFieldEditor.UNLIMITED, StringFieldEditor.VALIDATE_ON_KEY_STROKE, topComposite);
addField(additionalParams);
//set the width for API Key text field
GridData gridData = new GridData(SWT.BEGINNING, SWT.CENTER, true, false);
gridData.widthHint = 500; // Some width
gridData.grabExcessHorizontalSpace = false;
gridData.horizontalAlignment = GridData.FILL;
textControl.setLayoutData(gridData);
addField(space());
Link cliHelp = new Link(getFieldEditorParent(), SWT.NONE);
cliHelp.setText("<a href=\"https://checkmarx.com/resource/documents/en/34965-68626-global-flags.html\">CLI command that supports a set of global flags</a>");
cliHelp.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false));
GridData linkGridData = new GridData(SWT.END, SWT.CENTER, true, false);
cliHelp.setLayoutData(linkGridData);
cliHelp.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench().getBrowserSupport();
try {
browserSupport.getExternalBrowser().openURL(new URL(e.text));
} catch (PartInitException | MalformedURLException e1) {
CxLogger.error("Failed to open CLI help documentation link.", e1);
e1.printStackTrace();
}
}
});
addField(space());
Label connectionLabel = new Label(getFieldEditorParent(), SWT.WRAP);
connectionLabel.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, true, false)
);
Button connectionButton = new Button(topComposite, SWT.PUSH);
connectionButton.setText(PluginConstants.PREFERENCES_TEST_CONNECTION);
connectionButton.setEnabled(!apiKey.getStringValue().trim().isEmpty());
textControl.addModifyListener(e -> {
connectionButton.setEnabled(!textControl.getText().trim().isEmpty());
});
connectionButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String apiKey_str = apiKey.getStringValue();
String additionalParams_str = additionalParams.getStringValue();
connectionButton.setEnabled(false);
connectionLabel.setText(PluginConstants.PREFERENCES_VALIDATING_STATE);
getFieldEditorParent().layout();
CompletableFuture.supplyAsync(() -> {
try {
return Authenticator.INSTANCE.doAuthentication(
apiKey_str, additionalParams_str);
} catch (Throwable t) {
CxLogger.error(PluginConstants.ERROR_AUTHENTICATING_AST, new Exception(t));
return t.getMessage();
}
}).thenAccept((result) -> Display.getDefault().syncExec(() -> {
connectionLabel.setText(mapAuthResult(result));
getFieldEditorParent().layout();
connectionButton.setEnabled(true);
}));
}
});
}
private static String mapAuthResult(String result) {
if (result != null && result.contains(PluginConstants.AUTH_SUCCESS_PATTERN)) {
return PluginConstants.AUTH_SUCCESS_DISPLAY;
}
return result;
}
private FieldEditor space() {
return new LabelFieldEditor("", getFieldEditorParent());
}
@Override
public boolean performOk() {
boolean ok = super.performOk();
if (ok) {
PluginUtils.getEventBroker().post(PluginConstants.TOPIC_APPLY_SETTINGS, PluginConstants.EMPTY_STRING);
}
return ok;
}
}