11package io .snyk .eclipse .plugin .views .snyktoolview ;
22
3+ import java .nio .file .Path ;
34import java .nio .file .Paths ;
5+ import java .util .HashMap ;
6+ import java .util .List ;
7+ import java .util .Map ;
48
9+ import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
10+ import org .eclipse .core .runtime .preferences .InstanceScope ;
511import org .eclipse .jface .action .IMenuManager ;
612import org .eclipse .jface .action .MenuManager ;
713import org .eclipse .jface .viewers .ISelectionChangedListener ;
1319import org .eclipse .swt .SWT ;
1420import org .eclipse .swt .browser .Browser ;
1521import org .eclipse .swt .custom .SashForm ;
22+ import org .eclipse .swt .events .SelectionAdapter ;
23+ import org .eclipse .swt .events .SelectionEvent ;
1624import org .eclipse .swt .layout .FillLayout ;
1725import org .eclipse .swt .layout .GridData ;
1826import org .eclipse .swt .layout .GridLayout ;
27+ import org .eclipse .swt .widgets .Button ;
28+ import org .eclipse .swt .widgets .Combo ;
1929import org .eclipse .swt .widgets .Composite ;
2030import org .eclipse .swt .widgets .Display ;
31+ import org .eclipse .swt .widgets .Event ;
32+ import org .eclipse .swt .widgets .Label ;
33+ import org .eclipse .swt .widgets .Listener ;
2134import org .eclipse .swt .widgets .Menu ;
2235import org .eclipse .swt .widgets .Tree ;
36+ import org .eclipse .swt .widgets .TreeItem ;
2337import org .eclipse .ui .PlatformUI ;
2438import org .eclipse .ui .menus .CommandContributionItem ;
2539import org .eclipse .ui .menus .CommandContributionItemParameter ;
2640import org .eclipse .ui .part .ViewPart ;
2741
42+ import com .google .gson .Gson ;
43+
2844import io .snyk .eclipse .plugin .properties .preferences .Preferences ;
2945import io .snyk .eclipse .plugin .utils .ResourceUtils ;
3046import io .snyk .eclipse .plugin .views .snyktoolview .providers .TreeContentProvider ;
3147import io .snyk .eclipse .plugin .views .snyktoolview .providers .TreeLabelProvider ;
48+ import io .snyk .languageserver .protocolextension .messageObjects .FolderConfig ;
3249
3350/**
3451 * TODO This view will replace the old SnykView. Move the snyktoolview classes
@@ -47,6 +64,10 @@ public class SnykToolView extends ViewPart implements ISnykToolView {
4764 private TreeViewer treeViewer ;
4865 private Browser browser ;
4966 private BrowserHandler browserHandler ;
67+ 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 ();
5071
5172 @ Override
5273 public void createPartControl (Composite parent ) {
@@ -255,6 +276,123 @@ public void toggleIgnoresButtons() {
255276
256277 }
257278
279+ 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+
287+ item .setText (String .format ("Click to choose base branch for: %s [ current: %s ]" , project , baseBranch ));
288+
289+ Listener selectionListener = new Listener () {
290+ @ Override
291+ public void handleEvent (Event event ) {
292+ if (event .item == item ) {
293+ showPopup (event .display , item );
294+ }
295+ }
296+ };
297+
298+ // Store the listener in the map
299+ itemListeners .put (item , selectionListener );
300+
301+ // Add the listener to the item's parent
302+ item .getParent ().addListener (SWT .Selection , selectionListener );
303+ }
304+ }
305+
306+ private void showPopup (Display display , TreeItem item ) {
307+ Shell shell = new Shell (display , SWT .APPLICATION_MODAL | SWT .DIALOG_TRIM );
308+ shell .setText ("Choose base branch for net-new issues scanning" );
309+ shell .setLayout (new GridLayout (1 , false ));
310+
311+ if (!(item .getData () instanceof ContentRootNode ))
312+ return ;
313+
314+ ContentRootNode node = (ContentRootNode ) item .getData ();
315+ Path project = node .getPath ();
316+
317+ Label label = new Label (shell , SWT .NONE );
318+ label .setText ("Base Branch for: " + project );
319+ label .setLayoutData (new GridData (SWT .FILL , SWT .CENTER , true , false ));
320+
321+ List <String > dropdownItems = getLocalBranches (project .toString ());
322+
323+ Combo dropdown = new Combo (shell , SWT .DROP_DOWN );
324+ dropdown .setItems (dropdownItems .toArray (new String [0 ]));
325+ dropdown .setLayoutData (new GridData (SWT .FILL , SWT .CENTER , true , false ));
326+
327+ Button okButton = new Button (shell , SWT .PUSH );
328+ okButton .setText ("OK" );
329+ okButton .setLayoutData (new GridData (SWT .END , SWT .CENTER , false , false ));
330+ okButton .addSelectionListener (new SelectionAdapter () {
331+ @ Override
332+ public void widgetSelected (SelectionEvent e ) {
333+ // Handle OK button press
334+ shell .close ();
335+ }
336+ });
337+
338+ shell .pack ();
339+ shell .open ();
340+
341+ while (!shell .isDisposed ()) {
342+ if (!display .readAndDispatch ()) {
343+ display .sleep ();
344+ }
345+ }
346+ }
347+
348+ public List <String > getLocalBranches (String folderPath ) {
349+
350+ IEclipsePreferences state = InstanceScope .INSTANCE .getNode ("io.snyk.eclipse.plugin" );
351+ // Retrieve the JSON string from the preferences state
352+ String json = state .get (folderPath , null );
353+
354+ if (json != null ) {
355+ // Deserialize the JSON string back to a FolderConfig object
356+ FolderConfig folderConfig = gson .fromJson (json , FolderConfig .class );
357+ // Return the list of local branches
358+ return folderConfig .getLocalBranches ();
359+ }
360+
361+ return List .of (); // Return an empty list if no data is found
362+ }
363+
364+ public String getBaseBranch (String folderPath ) {
365+
366+ IEclipsePreferences state = InstanceScope .INSTANCE .getNode ("io.snyk.eclipse.plugin" );
367+ // Retrieve the JSON string from the preferences state
368+ String json = state .get (folderPath , null );
369+
370+ if (json == null )
371+ return null ;
372+
373+ // Deserialize the JSON string back to a FolderConfig object
374+ FolderConfig folderConfig = gson .fromJson (json , FolderConfig .class );
375+ // Return the list of local branches
376+ return folderConfig .getBaseBranch ();
377+
378+ }
379+
380+ public void disableDelta () {
381+ for (Map .Entry <TreeItem , Listener > entry : itemListeners .entrySet ()) {
382+ TreeItem item = entry .getKey ();
383+ Listener listener = entry .getValue ();
384+
385+ // Revert text to original
386+ item .setText ("Project name" );
387+
388+ // Remove listener from the item's parent
389+ item .getParent ().removeListener (SWT .Selection , listener );
390+ }
391+
392+ // Clear the map after removing all listeners
393+ itemListeners .clear ();
394+ }
395+
258396 // Helper method to add a command if it's not already present
259397 private void addCommandIfNotPresent (IMenuManager menu , String commandId ) {
260398 if (menu .find (commandId ) == null ) {
0 commit comments