11package io .snyk .eclipse .plugin .views .snyktoolview ;
22
3- import java .nio .file .Path ;
43import java .nio .file .Paths ;
4+ import java .util .Arrays ;
55import java .util .HashMap ;
6- import java .util .List ;
76import java .util .Map ;
87
9- import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
10- import org .eclipse .core .runtime .preferences .InstanceScope ;
118import org .eclipse .jface .action .IMenuManager ;
129import org .eclipse .jface .action .MenuManager ;
1310import org .eclipse .jface .viewers .ISelectionChangedListener ;
3936import org .eclipse .ui .menus .CommandContributionItemParameter ;
4037import org .eclipse .ui .part .ViewPart ;
4138
42- import com .google .gson .Gson ;
43-
39+ import io .snyk .eclipse .plugin .properties .preferences .EclipsePreferenceState ;
4440import io .snyk .eclipse .plugin .properties .preferences .Preferences ;
4541import io .snyk .eclipse .plugin .utils .ResourceUtils ;
42+ import io .snyk .eclipse .plugin .utils .SnykLogger ;
4643import io .snyk .eclipse .plugin .views .snyktoolview .providers .TreeContentProvider ;
4744import io .snyk .eclipse .plugin .views .snyktoolview .providers .TreeLabelProvider ;
48- import io .snyk .languageserver .protocolextension .messageObjects . FolderConfig ;
45+ import io .snyk .languageserver .protocolextension .SnykExtendedLanguageClient ;
4946
5047/**
5148 * TODO This view will replace the old SnykView. Move the snyktoolview classes
@@ -65,9 +62,7 @@ public class SnykToolView extends ViewPart implements ISnykToolView {
6562 private Browser browser ;
6663 private BrowserHandler browserHandler ;
6764 private Map <TreeItem , Listener > itemListeners = new HashMap <>();
68- private final Gson gson = new Gson ();
69-
70- private final static Shell SHELL = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getShell ();
65+ private EclipsePreferenceState preferenceState = EclipsePreferenceState .getInstance ();
7166
7267 @ Override
7368 public void createPartControl (Composite parent ) {
@@ -120,6 +115,9 @@ public void selectionChanged(SelectionChangedEvent event) {
120115 });
121116 }
122117 });
118+
119+ if (Preferences .getInstance ().getBooleanPref (Preferences .FILTER_DELTA_NEW_ISSUES ))
120+ this .enableDelta ();
123121 }
124122
125123 private void registerTreeContextMeny (Composite parent ) {
@@ -276,32 +274,43 @@ public void toggleIgnoresButtons() {
276274
277275 }
278276
277+ /*
278+ * Sets up for doing a Net New Issues scan.
279+ */
279280 public void enableDelta () {
280- TreeItem [] rootItems = getTreeViewer ().getTree ().getItems ();
281- for (TreeItem item : rootItems ) {
282- ContentRootNode node = (ContentRootNode ) item .getData ();
283- String project = node .getText ().toString ();
284- String projectPath = node .getPath ().toString ();
285- String baseBranch = getBaseBranch (projectPath .toString ());
286- String [] localBranches = getLocalBranches (projectPath ).toArray (String []::new );
287-
288- item .setText (String .format ("Click to choose base branch for: %s [ current: %s ]" , project , baseBranch ));
289-
290- Listener selectionListener = new Listener () {
291- @ Override
292- public void handleEvent (Event event ) {
293- if (event .item == item ) {
294- baseBranchDialog (event .display , projectPath , localBranches );
295- }
296- }
297- };
281+ clearDeltaNodeListeners ();
298282
299- // Store the listener in the map
300- itemListeners . put ( item , selectionListener );
283+ Display . getDefault (). asyncExec (() -> {
284+ if ( this . treeViewer != null && ! this . treeViewer . getTree (). isDisposed ()) {
301285
302- // Add the listener to the item's parent
303- item .getParent ().addListener (SWT .Selection , selectionListener );
304- }
286+ TreeItem [] rootItems = getTreeViewer ().getTree ().getItems ();
287+ for (TreeItem item : rootItems ) {
288+ ContentRootNode node = (ContentRootNode ) item .getData ();
289+ String project = node .getText ().toString ();
290+ String projectPath = node .getPath ().toString ();
291+ String baseBranch = preferenceState .getBaseBranch (projectPath );
292+ String [] localBranches = preferenceState .getLocalBranches (projectPath ).toArray (String []::new );
293+
294+ item .setText (
295+ String .format ("Click to choose base branch for: %s [ current: %s ]" , project , baseBranch ));
296+
297+ Listener selectionListener = new Listener () {
298+ @ Override
299+ public void handleEvent (Event event ) {
300+ if (event .item == item ) {
301+ baseBranchDialog (event .display , projectPath , localBranches );
302+ }
303+ }
304+ };
305+
306+ // Store the listener in the map
307+ itemListeners .put (item , selectionListener );
308+
309+ // Add the listener to the item's parent
310+ item .getParent ().addListener (SWT .Selection , selectionListener );
311+ }
312+ }
313+ });
305314 }
306315
307316 private void baseBranchDialog (Display display , String projectPath , String [] localBranches ) {
@@ -315,6 +324,7 @@ private void baseBranchDialog(Display display, String projectPath, String[] loca
315324 Combo dropdown = new Combo (shell , SWT .DROP_DOWN );
316325 dropdown .setItems (localBranches );
317326 dropdown .setLayoutData (new GridData (SWT .FILL , SWT .CENTER , true , false ));
327+ dropdown .setText (preferenceState .getBaseBranch (projectPath ));
318328
319329 Button okButton = new Button (shell , SWT .PUSH );
320330 okButton .setText ("OK" );
@@ -323,7 +333,15 @@ private void baseBranchDialog(Display display, String projectPath, String[] loca
323333 @ Override
324334 public void widgetSelected (SelectionEvent e ) {
325335 // Handle OK button press
326- shell .close ();
336+ String selectedBranch = dropdown .getText ();
337+ if (Arrays .asList (localBranches ).contains (selectedBranch )) {
338+ preferenceState .setBaseBranch (projectPath , selectedBranch );
339+ shell .close ();
340+ SnykExtendedLanguageClient .getInstance ().triggerScan (null );
341+ } else {
342+ SnykLogger .logInfo ("Branch is not a valid local branch for repository: " + projectPath );
343+ }
344+
327345 }
328346 });
329347
@@ -337,55 +355,36 @@ public void widgetSelected(SelectionEvent e) {
337355 }
338356 }
339357
340- public List <String > getLocalBranches (String folderPath ) {
341-
342- IEclipsePreferences state = InstanceScope .INSTANCE .getNode ("io.snyk.eclipse.plugin" );
343- // Retrieve the JSON string from the preferences state
344- String json = state .get (folderPath , null );
345-
346- if (json != null ) {
347- // Deserialize the JSON string back to a FolderConfig object
348- FolderConfig folderConfig = gson .fromJson (json , FolderConfig .class );
349- // Return the list of local branches
350- return folderConfig .getLocalBranches ();
351- }
352-
353- return List .of (); // Return an empty list if no data is found
354- }
355-
356- public String getBaseBranch (String folderPath ) {
357-
358- IEclipsePreferences state = InstanceScope .INSTANCE .getNode ("io.snyk.eclipse.plugin" );
359- // Retrieve the JSON string from the preferences state
360- String json = state .get (folderPath , null );
361-
362- if (json == null )
363- return null ;
364-
365- // Deserialize the JSON string back to a FolderConfig object
366- FolderConfig folderConfig = gson .fromJson (json , FolderConfig .class );
367- // Return the list of local branches
368- return folderConfig .getBaseBranch ();
358+ /*
359+ * Disables Net New Issues scan, and starts a regular scan.
360+ */
361+ public void disableDelta () {
362+ clearDeltaNodeListeners ();
369363
364+ SnykExtendedLanguageClient .getInstance ().triggerScan (null );
370365 }
371366
372- public void disableDelta () {
373- for (Map .Entry <TreeItem , Listener > entry : itemListeners .entrySet ()) {
374- TreeItem item = entry .getKey ();
375- Listener listener = entry .getValue ();
367+ private void clearDeltaNodeListeners () {
368+ Display .getDefault ().asyncExec (() -> {
369+ if (this .treeViewer != null && !this .treeViewer .getTree ().isDisposed ()) {
370+ for (Map .Entry <TreeItem , Listener > entry : itemListeners .entrySet ()) {
371+ TreeItem item = entry .getKey ();
372+ Listener listener = entry .getValue ();
376373
377- ContentRootNode node = (ContentRootNode ) item .getData ();
378- String project = node .getText ().toString ();
374+ ContentRootNode node = (ContentRootNode ) item .getData ();
375+ String project = node .getText ().toString ();
379376
380- // Revert text to original
381- item .setText (project );
377+ // Revert text to original
378+ item .setText (project );
382379
383- // Remove listener from the item's parent
384- item .getParent ().removeListener (SWT .Selection , listener );
385- }
380+ // Remove listener from the item's parent
381+ item .getParent ().removeListener (SWT .Selection , listener );
382+ }
386383
387- // Clear the map after removing all listeners
388- itemListeners .clear ();
384+ // Clear the map after removing all listeners
385+ itemListeners .clear ();
386+ }
387+ });
389388 }
390389
391390 // Helper method to add a command if it's not already present
0 commit comments