Skip to content

Commit 3da99f1

Browse files
committed
fix: refactor dialog to class
1 parent e1da0e1 commit 3da99f1

2 files changed

Lines changed: 73 additions & 53 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.snyk.eclipse.plugin.views.snyktoolview;
2+
3+
import java.util.Arrays;
4+
5+
import org.eclipse.core.resources.IProject;
6+
import org.eclipse.swt.SWT;
7+
import org.eclipse.swt.events.SelectionAdapter;
8+
import org.eclipse.swt.events.SelectionEvent;
9+
import org.eclipse.swt.layout.GridData;
10+
import org.eclipse.swt.layout.GridLayout;
11+
import org.eclipse.swt.widgets.Button;
12+
import org.eclipse.swt.widgets.Combo;
13+
import org.eclipse.swt.widgets.Display;
14+
import org.eclipse.swt.widgets.Label;
15+
import org.eclipse.swt.widgets.Shell;
16+
17+
import io.snyk.eclipse.plugin.properties.preferences.EclipsePreferenceState;
18+
import io.snyk.eclipse.plugin.utils.SnykLogger;
19+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
20+
21+
public class BaseBranchDialog {
22+
private EclipsePreferenceState preferenceState = EclipsePreferenceState.getInstance();
23+
24+
public BaseBranchDialog() {
25+
}
26+
27+
public void baseBranchDialog(Display display, String projectPath, IProject project, String[] localBranches) {
28+
Shell shell = new Shell(display, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
29+
shell.setText("Choose base branch for net-new issues scanning");
30+
shell.setLayout(new GridLayout(1, false));
31+
Label label = new Label(shell, SWT.NONE);
32+
label.setText("Base Branch for: " + projectPath);
33+
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
34+
35+
Combo dropdown = new Combo(shell, SWT.DROP_DOWN);
36+
dropdown.setItems(localBranches);
37+
dropdown.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
38+
dropdown.setText(preferenceState.getBaseBranch(projectPath));
39+
40+
Button okButton = new Button(shell, SWT.PUSH);
41+
okButton.setText("OK");
42+
okButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
43+
okButton.addSelectionListener(new SelectionAdapter() {
44+
@Override
45+
public void widgetSelected(SelectionEvent e) {
46+
// Handle OK button press
47+
String selectedBranch = dropdown.getText();
48+
if (Arrays.asList(localBranches).contains(selectedBranch)) {
49+
preferenceState.setBaseBranch(projectPath, selectedBranch);
50+
shell.close();
51+
SnykExtendedLanguageClient.getInstance().triggerScan(project);
52+
} else {
53+
SnykLogger.logInfo("Branch is not a valid local branch for repository: " + projectPath);
54+
}
55+
56+
}
57+
});
58+
59+
shell.pack();
60+
shell.open();
61+
62+
while (!shell.isDisposed()) {
63+
if (!display.readAndDispatch()) {
64+
display.sleep();
65+
}
66+
}
67+
}
68+
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/SnykToolView.java

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.snyk.eclipse.plugin.views.snyktoolview;
22

33
import java.nio.file.Paths;
4-
import java.util.Arrays;
54
import java.util.HashMap;
65
import java.util.Map;
76

@@ -18,17 +17,12 @@
1817
import org.eclipse.swt.SWT;
1918
import org.eclipse.swt.browser.Browser;
2019
import org.eclipse.swt.custom.SashForm;
21-
import org.eclipse.swt.events.SelectionAdapter;
22-
import org.eclipse.swt.events.SelectionEvent;
2320
import org.eclipse.swt.layout.FillLayout;
2421
import org.eclipse.swt.layout.GridData;
2522
import org.eclipse.swt.layout.GridLayout;
26-
import org.eclipse.swt.widgets.Button;
27-
import org.eclipse.swt.widgets.Combo;
2823
import org.eclipse.swt.widgets.Composite;
2924
import org.eclipse.swt.widgets.Display;
3025
import org.eclipse.swt.widgets.Event;
31-
import org.eclipse.swt.widgets.Label;
3226
import org.eclipse.swt.widgets.Listener;
3327
import org.eclipse.swt.widgets.Menu;
3428
import org.eclipse.swt.widgets.Tree;
@@ -41,7 +35,6 @@
4135
import io.snyk.eclipse.plugin.properties.preferences.EclipsePreferenceState;
4236
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
4337
import io.snyk.eclipse.plugin.utils.ResourceUtils;
44-
import io.snyk.eclipse.plugin.utils.SnykLogger;
4538
import io.snyk.eclipse.plugin.views.snyktoolview.providers.TreeContentProvider;
4639
import io.snyk.eclipse.plugin.views.snyktoolview.providers.TreeLabelProvider;
4740
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
@@ -290,18 +283,19 @@ public void enableDelta() {
290283
ContentRootNode node = (ContentRootNode) item.getData();
291284
String projectName = node.getText().toString();
292285
String projectPath = node.getPath().toString();
293-
String baseBranch = preferenceState.getBaseBranch(projectPath);
294286
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
287+
String baseBranch = preferenceState.getBaseBranch(projectPath);
295288
String[] localBranches = preferenceState.getLocalBranches(projectPath).toArray(String[]::new);
296289

297-
item.setText(
298-
String.format("%s - Click here choose base branch [ current: %s ]", projectName, baseBranch));
290+
item.setText(String.format("%s - Click here choose base branch [ current: %s ]", projectName,
291+
baseBranch));
299292

300293
Listener selectionListener = new Listener() {
301294
@Override
302295
public void handleEvent(Event event) {
303296
if (event.item == item) {
304-
baseBranchDialog(event.display, projectPath, project, localBranches);
297+
new BaseBranchDialog().baseBranchDialog(event.display, projectPath, project,
298+
localBranches);
305299
}
306300
}
307301
};
@@ -316,48 +310,6 @@ public void handleEvent(Event event) {
316310
});
317311
}
318312

319-
private void baseBranchDialog(Display display, String projectPath, IProject project, String[] localBranches) {
320-
Shell shell = new Shell(display, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
321-
shell.setText("Choose base branch for net-new issues scanning");
322-
shell.setLayout(new GridLayout(1, false));
323-
Label label = new Label(shell, SWT.NONE);
324-
label.setText("Base Branch for: " + projectPath);
325-
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
326-
327-
Combo dropdown = new Combo(shell, SWT.DROP_DOWN);
328-
dropdown.setItems(localBranches);
329-
dropdown.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
330-
dropdown.setText(preferenceState.getBaseBranch(projectPath));
331-
332-
Button okButton = new Button(shell, SWT.PUSH);
333-
okButton.setText("OK");
334-
okButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
335-
okButton.addSelectionListener(new SelectionAdapter() {
336-
@Override
337-
public void widgetSelected(SelectionEvent e) {
338-
// Handle OK button press
339-
String selectedBranch = dropdown.getText();
340-
if (Arrays.asList(localBranches).contains(selectedBranch)) {
341-
preferenceState.setBaseBranch(projectPath, selectedBranch);
342-
shell.close();
343-
SnykExtendedLanguageClient.getInstance().triggerScan(project);
344-
} else {
345-
SnykLogger.logInfo("Branch is not a valid local branch for repository: " + projectPath);
346-
}
347-
348-
}
349-
});
350-
351-
shell.pack();
352-
shell.open();
353-
354-
while (!shell.isDisposed()) {
355-
if (!display.readAndDispatch()) {
356-
display.sleep();
357-
}
358-
}
359-
}
360-
361313
/*
362314
* Disables Net New Issues scan, and starts a regular scan.
363315
*/

0 commit comments

Comments
 (0)