1111import java .util .Optional ;
1212import java .util .Set ;
1313import java .util .UUID ;
14+ import java .util .stream .Collectors ;
1415
1516import org .apache .commons .lang3 .StringUtils ;
1617import org .eclipse .core .resources .IFile ;
9899import com .google .common .base .Strings ;
99100import com .google .common .eventbus .EventBus ;
100101import com .google .common .eventbus .Subscribe ;
102+ import java .util .Timer ;
103+ import java .util .TimerTask ;
101104
102105public class CheckmarxView extends ViewPart implements EventHandler {
103106
@@ -112,6 +115,11 @@ public class CheckmarxView extends ViewPart implements EventHandler {
112115 private static final String FORMATTED_SCAN_LABEL = "%s %s" ;
113116 private static final String FORMATTED_SCAN_LABEL_LATEST = "%s %s (%s)" ;
114117
118+ private Timer debounceTimer = new Timer ("ProjectSearchDebounce" , true );
119+ private TimerTask pendingSearchTask ;
120+ private static final int DEBOUNCE_DELAY_MS = 400 ;
121+ private volatile String latestProjectSearchTerm = "" ;
122+
115123 private static final int SCROLL_WIDTH = 30 ;
116124 /**
117125 * The ID of the view as specified by the extension.
@@ -805,18 +813,58 @@ protected IStatus run(IProgressMonitor arg0) {
805813
806814 // Add ModifyListener to handle manual text input for projects
807815 projectComboViewer .getCombo ().addModifyListener (e -> {
808- String enteredProject = projectComboViewer .getCombo ().getText ();
809-
810- // Check if text was modified and project doesn't exist
811- boolean projectExists = currentProjects .stream ()
812- .anyMatch (p -> p .getName ().equals (enteredProject ));
813-
814- if (!projectExists ) {
816+ String enteredProject = projectComboViewer .getCombo ().getText ().trim ();
817+ // Skip search if the text is the default instruction
818+ if (enteredProject .equals (PROJECT_COMBO_VIEWER_TEXT )) {
815819 updateStartScanButton (false ); // Disable scan button
816- } else {
817- // Only enable if we also have a valid branch
818- boolean validBranch = !currentBranch .isEmpty () && currentBranches .contains (currentBranch );
819- updateStartScanButton (validBranch );
820+ return ;
821+ }
822+
823+ latestProjectSearchTerm = enteredProject ; // Track the latest term
824+ List <String > matchedProjects ;
825+ matchedProjects = currentProjects .stream ().map (Project ::getName )
826+ .filter (name -> name != null && name .toLowerCase ().contains (enteredProject .toLowerCase ())).limit (100 )
827+ .collect (Collectors .toList ());
828+
829+ if (matchedProjects .isEmpty ()) {
830+ CxLogger .info ("Entered project is not exist in current projects list" );
831+ // Cancel any pending search
832+ if (pendingSearchTask != null ) {
833+ pendingSearchTask .cancel ();
834+ }
835+ // Schedule a new search after the debounce delay
836+ pendingSearchTask = new java .util .TimerTask () {
837+ @ Override
838+ public void run () {
839+ final String searchTerm = latestProjectSearchTerm ; // Capture the term for this search
840+ // Schedule a background job for the server search
841+ Job job = new Job ("Checkmarx: Searching for project on server..." ) {
842+ @ Override
843+ protected IStatus run (IProgressMonitor monitor ) {
844+ List <Project > searchedProjects ;
845+ try {
846+ searchedProjects = DataProvider .getInstance ().getProjects (searchTerm );
847+ Display .getDefault ().asyncExec (() -> {
848+ if (searchTerm .equals (latestProjectSearchTerm )) {
849+ // Update UI in UI thread
850+ if (searchedProjects != null && !searchedProjects .isEmpty ()) {
851+ projectComboViewer .setInput (searchedProjects );
852+ currentProjects = searchedProjects ;
853+ } else {
854+ updateStartScanButton (false ); // Disable scan button
855+ }
856+ }
857+ });
858+ } catch (Exception ex ) {
859+ ex .printStackTrace ();
860+ }
861+ return Status .OK_STATUS ;
862+ }
863+ };
864+ job .schedule ();
865+ }
866+ };
867+ debounceTimer .schedule (pendingSearchTask , DEBOUNCE_DELAY_MS );
820868 }
821869 });
822870 }
@@ -1806,20 +1854,20 @@ private void layoutAttackVectorItemComposite() {
18061854 }
18071855
18081856 private void drawPackageData (DisplayModel selectedItem ) {
1809- ScrolledComposite sc = new ScrolledComposite (attackVectorCompositePanel , SWT .H_SCROLL | SWT .V_SCROLL );
1857+ ScrolledComposite sc = new ScrolledComposite (attackVectorCompositePanel , SWT .H_SCROLL | SWT .V_SCROLL );
18101858
1811- Composite child = new Composite (sc , SWT .NONE );
1812- child .setLayoutData (new GridData (GridData .FILL , GridData .BEGINNING , true , true ));
1813- child .setLayout (new GridLayout (1 , false ));
1814- child .setBackground (attackVectorCompositePanel .getBackground ());
1859+ Composite child = new Composite (sc , SWT .NONE );
1860+ child .setLayoutData (new GridData (GridData .FILL , GridData .BEGINNING , true , true ));
1861+ child .setLayout (new GridLayout (1 , false ));
1862+ child .setBackground (attackVectorCompositePanel .getBackground ());
18151863
1816- drawAttackVectorTitle (child , PluginConstants .PACKAGE_DATA );
1817- drawIndividualPackageData (child , selectedItem .getResult ().getData ().getPackageData ());
1864+ drawAttackVectorTitle (child , PluginConstants .PACKAGE_DATA );
1865+ drawIndividualPackageData (child , selectedItem .getResult ().getData ().getPackageData ());
18181866
1819- sc .setContent (child );
1820- sc .setMinSize (child .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
1821- sc .setExpandHorizontal (true );
1822- sc .setExpandVertical (true );
1867+ sc .setContent (child );
1868+ sc .setMinSize (child .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
1869+ sc .setExpandHorizontal (true );
1870+ sc .setExpandVertical (true );
18231871 }
18241872
18251873 /**
@@ -1930,18 +1978,18 @@ private void drawSASTLearnMore(DisplayModel selectedItem, TabFolder folder, TabI
19301978 final ScrolledComposite learnMoreScrolledComposite = new ScrolledComposite (folder , SWT .V_SCROLL );
19311979 learnMoreScrolledComposite .setExpandHorizontal (true );
19321980 learnMoreScrolledComposite .setExpandVertical (true );
1933-
1934- final Composite learnMoreComposite = new Composite (learnMoreScrolledComposite , SWT .NONE );
1935- learnMoreComposite .setLayout (new GridLayout ());
1936-
1937- learnMoreScrolledComposite .setContent (learnMoreComposite );
1938- learnMoreScrolledComposite .setMinSize (learnMoreComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
19391981
1940- if (learnMoreData == null ) {
1941- CLabel loadingLabel = new CLabel (learnMoreComposite , SWT .NONE );
1982+ final Composite learnMoreComposite = new Composite (learnMoreScrolledComposite , SWT .NONE );
1983+ learnMoreComposite .setLayout (new GridLayout ());
1984+
1985+ learnMoreScrolledComposite .setContent (learnMoreComposite );
1986+ learnMoreScrolledComposite .setMinSize (learnMoreComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
1987+
1988+ if (learnMoreData == null ) {
1989+ CLabel loadingLabel = new CLabel (learnMoreComposite , SWT .NONE );
19421990 loadingLabel .setText (PluginConstants .LEARN_MORE_LOADING );
1943- }
1944-
1991+ }
1992+
19451993 learnMoreTab .setControl (learnMoreScrolledComposite );
19461994
19471995 Job job = new Job (PluginConstants .GETTING_LEARN_MORE_JOB ) {
@@ -1975,8 +2023,8 @@ protected IStatus run(IProgressMonitor arg0) {
19752023 });
19762024 }
19772025
1978- learnMoreScrolledComposite .setMinSize (learnMoreComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
1979- learnMoreComposite .layout ();
2026+ learnMoreScrolledComposite .setMinSize (learnMoreComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
2027+ learnMoreComposite .layout ();
19802028 }
19812029 } catch (Exception e ) {
19822030 CxLogger .error (String .format (PluginConstants .ERROR_GETTING_LEARN_MORE , e .getMessage ()), e );
@@ -2024,17 +2072,17 @@ private void drawSASTRemediationExamples(DisplayModel selectedItem, TabFolder fo
20242072 final ScrolledComposite remediationExamplesScrolledComposite = new ScrolledComposite (folder , SWT .V_SCROLL | SWT .BORDER );
20252073 remediationExamplesScrolledComposite .setExpandHorizontal (true );
20262074 remediationExamplesScrolledComposite .setExpandVertical (true );
2027-
2028- final Composite remediationExamplesComposite = new Composite (remediationExamplesScrolledComposite , SWT .NONE );
2029- remediationExamplesComposite .setLayout (new GridLayout ());
2030-
2031- remediationExamplesScrolledComposite .setContent (remediationExamplesComposite );
2032- remediationExamplesScrolledComposite .setMinSize (remediationExamplesComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
20332075
2034- if (learnMoreData == null ) {
2035- Label loadingLabel = new Label (remediationExamplesComposite , SWT .NONE );
2076+ final Composite remediationExamplesComposite = new Composite (remediationExamplesScrolledComposite , SWT .NONE );
2077+ remediationExamplesComposite .setLayout (new GridLayout ());
2078+
2079+ remediationExamplesScrolledComposite .setContent (remediationExamplesComposite );
2080+ remediationExamplesScrolledComposite .setMinSize (remediationExamplesComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
2081+
2082+ if (learnMoreData == null ) {
2083+ Label loadingLabel = new Label (remediationExamplesComposite , SWT .NONE );
20362084 loadingLabel .setText (PluginConstants .LEARN_MORE_LOADING );
2037- }
2085+ }
20382086
20392087 remediationExamplesTab .setControl (remediationExamplesScrolledComposite );
20402088
@@ -2062,14 +2110,14 @@ protected IStatus run(IProgressMonitor arg0) {
20622110 for (Sample sample : samples ) {
20632111 StyledText sampleTitle = new StyledText (remediationExamplesComposite , SWT .WRAP );
20642112 sampleTitle .setText (String .format (PluginConstants .REMEDIATION_EXAMPLE_TITLE_FORMAT , sample .getTitle (), sample .getProgLanguage ()));
2065- GridData titleLayoutData = new GridData ( GridData .FILL_HORIZONTAL ) ;
2066- titleLayoutData .grabExcessHorizontalSpace = true ;
2067- titleLayoutData .horizontalAlignment = SWT .FILL ;
2068- titleLayoutData .widthHint = remediationExamplesScrolledComposite .getClientArea ().width - SCROLL_WIDTH ;
2069- titleLayoutData .horizontalSpan = 2 ;
2070- sampleTitle .setLayoutData (titleLayoutData );
2071- sampleTitle .setMargins (2 , 5 , 2 , 5 );
2072-
2113+ GridData titleLayoutData = new GridData ( GridData .FILL_HORIZONTAL ) ;
2114+ titleLayoutData .grabExcessHorizontalSpace = true ;
2115+ titleLayoutData .horizontalAlignment = SWT .FILL ;
2116+ titleLayoutData .widthHint = remediationExamplesScrolledComposite .getClientArea ().width - SCROLL_WIDTH ;
2117+ titleLayoutData .horizontalSpan = 2 ;
2118+ sampleTitle .setLayoutData (titleLayoutData );
2119+ sampleTitle .setMargins (2 , 5 , 2 , 5 );
2120+
20732121 Composite sampleExampleComposite = new Composite (remediationExamplesComposite , SWT .NONE );
20742122 sampleExampleComposite .setBackground (remediationExamplesComposite .getBackground ());
20752123 GridLayout layout = new GridLayout ();
@@ -2080,12 +2128,12 @@ protected IStatus run(IProgressMonitor arg0) {
20802128
20812129 Label sampleExample = new Label (sampleExampleComposite , SWT .WRAP );
20822130 sampleExample .setText (sample .getCode ());
2083- GridData exampleLayoutData = new GridData (GridData .FILL_HORIZONTAL ) ;
2084- exampleLayoutData .grabExcessHorizontalSpace = true ;
2085- exampleLayoutData .horizontalAlignment = SWT .FILL ;
2086- exampleLayoutData .widthHint = remediationExamplesScrolledComposite .getClientArea ().width - SCROLL_WIDTH ;
2087- exampleLayoutData .horizontalSpan = 2 ;
2088- sampleExample .setLayoutData (exampleLayoutData );
2131+ GridData exampleLayoutData = new GridData (GridData .FILL_HORIZONTAL ) ;
2132+ exampleLayoutData .grabExcessHorizontalSpace = true ;
2133+ exampleLayoutData .horizontalAlignment = SWT .FILL ;
2134+ exampleLayoutData .widthHint = remediationExamplesScrolledComposite .getClientArea ().width - SCROLL_WIDTH ;
2135+ exampleLayoutData .horizontalSpan = 2 ;
2136+ sampleExample .setLayoutData (exampleLayoutData );
20892137
20902138 remediationExamplesScrolledComposite .setMinSize (remediationExamplesComposite .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
20912139 remediationExamplesComposite .layout ();
@@ -2134,14 +2182,14 @@ private void addLearnMoreSectionsToComposite(Composite composite, String title,
21342182 titleLabel .setFont (boldFont );
21352183
21362184 StyledText descriptionLabel = new StyledText (composite , SWT .WRAP );
2137- descriptionLabel .setText (description );
2138- GridData descriptionLayout = new GridData (GridData .FILL_HORIZONTAL );
2139- descriptionLayout .grabExcessHorizontalSpace = true ;
2140- descriptionLayout .horizontalAlignment = SWT .FILL ;
2141- descriptionLayout .widthHint = composite .getClientArea ().width - SCROLL_WIDTH ;
2142- descriptionLayout .horizontalSpan = 2 ;
2143- descriptionLabel .setLayoutData (descriptionLayout );
2144- descriptionLabel .setBottomMargin (20 );
2185+ descriptionLabel .setText (description );
2186+ GridData descriptionLayout = new GridData (GridData .FILL_HORIZONTAL );
2187+ descriptionLayout .grabExcessHorizontalSpace = true ;
2188+ descriptionLayout .horizontalAlignment = SWT .FILL ;
2189+ descriptionLayout .widthHint = composite .getClientArea ().width - SCROLL_WIDTH ;
2190+ descriptionLayout .horizontalSpan = 2 ;
2191+ descriptionLabel .setLayoutData (descriptionLayout );
2192+ descriptionLabel .setBottomMargin (20 );
21452193 }
21462194
21472195 /*private void populateBFLMessage(Image image, String bflMessage) {
@@ -2220,18 +2268,18 @@ protected IStatus run(IProgressMonitor arg0) {
22202268 private void drawVulnerabilityLocation (DisplayModel selectedItem ) {
22212269 ScrolledComposite sc = new ScrolledComposite (attackVectorCompositePanel , SWT .H_SCROLL | SWT .V_SCROLL );
22222270
2223- Composite child = new Composite (sc , SWT .NONE );
2224- child .setLayoutData (new GridData (GridData .FILL , GridData .BEGINNING , true , true ));
2225- child .setLayout (new GridLayout (1 , false ));
2226- child .setBackground (attackVectorCompositePanel .getBackground ());
2271+ Composite child = new Composite (sc , SWT .NONE );
2272+ child .setLayoutData (new GridData (GridData .FILL , GridData .BEGINNING , true , true ));
2273+ child .setLayout (new GridLayout (1 , false ));
2274+ child .setBackground (attackVectorCompositePanel .getBackground ());
22272275
2228- drawAttackVectorTitle (child , PluginConstants .LOCATION );
2276+ drawAttackVectorTitle (child , PluginConstants .LOCATION );
22292277 drawIndividualLocationData (child , selectedItem );
22302278
2231- sc .setContent (child );
2232- sc .setMinSize (child .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
2233- sc .setExpandHorizontal (true );
2234- sc .setExpandVertical (true );
2279+ sc .setContent (child );
2280+ sc .setMinSize (child .computeSize (SWT .DEFAULT , SWT .DEFAULT ));
2281+ sc .setExpandHorizontal (true );
2282+ sc .setExpandVertical (true );
22352283 }
22362284
22372285 private void drawIndividualLocationData (Composite parent , DisplayModel selectedItem ) {
0 commit comments