44import com .intellij .openapi .fileChooser .FileChooser ;
55import com .intellij .openapi .fileChooser .FileChooserDescriptor ;
66import com .intellij .openapi .project .Project ;
7+ import com .intellij .openapi .project .ProjectUtil ;
8+ import com .intellij .openapi .ui .ComboBox ;
79import com .intellij .openapi .ui .DialogBuilder ;
810import com .intellij .openapi .ui .DialogWrapper ;
911import com .intellij .openapi .vfs .VfsUtilCore ;
1820import org .jetbrains .annotations .NotNull ;
1921
2022import javax .swing .*;
21- import javax .swing .event .*;
23+ import javax .swing .event .ChangeEvent ;
24+ import javax .swing .event .ChangeListener ;
25+ import javax .swing .event .ListSelectionEvent ;
26+ import javax .swing .event .ListSelectionListener ;
2227import javax .swing .table .DefaultTableModel ;
2328import javax .swing .table .TableModel ;
2429import java .awt .*;
25- import java .awt .event .ActionEvent ;
26- import java .awt .event .ActionListener ;
2730import java .awt .event .KeyEvent ;
2831import java .io .File ;
2932import java .io .IOException ;
30- import java .util .List ;
3133import java .util .*;
34+ import java .util .List ;
3235
3336import static com .intellij .plugins .bodhi .pmd .actions .PreDefinedMenuGroup .RULESETS_FILENAMES_KEY ;
3437import static com .intellij .plugins .bodhi .pmd .actions .PreDefinedMenuGroup .RULESETS_PROPERTY_FILE ;
@@ -174,7 +177,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
174177 DialogBuilder db = new DialogBuilder (PMDUtil .getProjectComponent (e ).getCurrentProject ());
175178 db .addOkAction ();
176179 db .addCancelAction ();
177- db .setTitle ("Choose Custom RuleSet from Drop-down, File or paste URL" );
180+ db .setTitle ("Choose Custom RuleSet from Drop-down, File or Paste URL" );
178181 final BrowsePanel panel = new BrowsePanel (defaultValue , db , project );
179182 db .show ();
180183 //If ok is selected add the selected ruleset
@@ -189,7 +192,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
189192 ruleSetPathJList .setSelectedIndex (listModel .getSize ());
190193 }
191194 String err ;
192- if ((err = PMDResultCollector .isValidRuleSet (rulesPath )).length () > 0 ) {
195+ if (! (err = PMDResultCollector .isValidRuleSet (rulesPath )).isEmpty () ) {
193196 String message = "The selected file/URL is not valid for PMD 7." ;
194197 if (err .contains ("XML validation errors occurred" )) {
195198 message += " XML validation errors occurred." ;
@@ -209,7 +212,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
209212 listModel .set (selectedIndex , rulesPath .trim ()); // trigger menu update
210213 return ;
211214 }
212- if (defaultValue != null && defaultValue .trim ().length () > 0 && selectedIndex >= 0 ) {
215+ if (defaultValue != null && ! defaultValue .trim ().isEmpty () && selectedIndex >= 0 ) {
213216 listModel .set (selectedIndex , rulesPath );
214217 return ;
215218 }
@@ -321,10 +324,10 @@ private void validateJavaVersion(String versionInput, int row, int column, Objec
321324 if (versionInput .equals (orig )) {
322325 return ;
323326 }
324- Language java = LanguageRegistry .PMD .getLanguageById ("java" );
327+ Language java = Objects . requireNonNull ( LanguageRegistry .PMD .getLanguageById ("java" ) );
325328 boolean isRegistered = java .hasVersion (versionInput );
326329 if (isRegistered ) {
327- String registeredVersion = java .getVersion (versionInput ).getVersion ();
330+ String registeredVersion = Objects . requireNonNull ( java .getVersion (versionInput ) ).getVersion ();
328331 optionsTable .setToolTipText ("Java version " + registeredVersion );
329332 }
330333 else {
@@ -352,23 +355,21 @@ private void validateStatUrl(String urlInput, int row, int column, Object orig,
352355 return ;
353356 }
354357 if (!urlInput .isEmpty ()) {
355- if (!PMDUtil .isValidUrl (urlInput )) {
356- optionsTable .setToolTipText ("Previous input - Invalid URL: '" + urlInput + "'" );
357- super .setValueAt (orig , row , column );
358- isModified = origIsMod ;
359- }
360- else {
358+ if (PMDUtil .isValidUrl (urlInput )) {
361359 String content = "{\" test connection\" }\n " ;
362360 String exportMsg = PMDJsonExportingRenderer .tryJsonExport (content , urlInput );
363361 if (!exportMsg .isEmpty ()) {
364362 optionsTable .setToolTipText ("Previous input - Failure for '" + urlInput + "': " + exportMsg );
365363 super .setValueAt (orig , row , column );
366364 isModified = origIsMod ;
367- }
368- else {
365+ } else {
369366 isModified = true ;
370367 optionsTable .setToolTipText (STAT_URL_MSG_SUCCESS );
371368 }
369+ } else {
370+ optionsTable .setToolTipText ("Previous input - Invalid URL: '" + urlInput + "'" );
371+ super .setValueAt (orig , row , column );
372+ isModified = origIsMod ;
372373 }
373374 }
374375 }
@@ -476,44 +477,46 @@ public BrowsePanel(String defaultValue, final DialogBuilder db, final Project pr
476477 final Vector <String > elements = new Vector <>();
477478 elements .add (defaultValue );
478479 Set <String > ruleSetNames = PMDUtil .getValidKnownCustomRules ().keySet ();
479- for (String ruleSetName : ruleSetNames ) {
480- elements .add (ruleSetName );
481- }
480+ elements .addAll (ruleSetNames );
481+
482482 ComboBoxModel <String > model = new DefaultComboBoxModel <>(elements );
483483 model .setSelectedItem (defaultValue );
484- pathComboBox = new JComboBox <>(model );
484+ pathComboBox = new ComboBox <>(model );
485485 pathComboBox .setEditable (true );
486486 pathComboBox .setMinimumSize (new Dimension (200 , 26 ));
487487 pathComboBox .setMaximumSize (new Dimension (800 , 28 ));
488488 pathComboBox .setPreferredSize (new Dimension (230 , 28 ));
489489 add (pathComboBox );
490490 add (Box .createHorizontalStrut (5 ));
491+ JButton open = getJButton (project );
492+ add (open );
493+ add (Box .createVerticalGlue ());
494+ db .setCenterPanel (this );
495+ }
496+
497+ private @ NotNull JButton getJButton (Project project ) {
491498 JButton open = new JButton ("Browse" );
492499 open .setPreferredSize (new Dimension (80 , 20 ));
493- open .addActionListener (new ActionListener () {
494- public void actionPerformed (ActionEvent e ) {
495- final VirtualFile toSelect = project .getBaseDir ();
496- // file system access takes some time, IntelliJ sometimes gives an exception that
497- // and EDT thread should not take long. Should be solved by using a BGT thread, but how?
498- final FileChooserDescriptor descriptor = new FileChooserDescriptor (true , false , false , false , false , false );
499- descriptor .withFileFilter (virtualFile -> virtualFile .getName ().endsWith (".xml" ));
500-
501- final VirtualFile chosen = FileChooser .chooseFile (descriptor , BrowsePanel .this , project , toSelect );
502- if (chosen != null ) {
503- final File newConfigFile = VfsUtilCore .virtualToIoFile (chosen );
504- String ioFile = newConfigFile .getAbsolutePath ();
505- final Vector <String > elem = new Vector <>();
506- elem .add (ioFile );
507- ComboBoxModel <String > newModel = new DefaultComboBoxModel <>(elem );
508- pathComboBox .setModel (newModel );
509- pathComboBox .setSelectedItem (ioFile );
510- pathComboBox .setEditable (false );
511- }
500+ open .addActionListener (e -> {
501+ final VirtualFile toSelect = ProjectUtil .guessProjectDir (project );
502+ // file system access takes some time, IntelliJ sometimes gives an exception that
503+ // and EDT thread should not take long. Should be solved by using a BGT thread, but how?
504+ final FileChooserDescriptor descriptor = new FileChooserDescriptor (true , false , false , false , false , false );
505+ descriptor .withFileFilter (virtualFile -> virtualFile .getName ().endsWith (".xml" ));
506+
507+ final VirtualFile chosen = FileChooser .chooseFile (descriptor , this , project , toSelect );
508+ if (chosen != null ) {
509+ final File newConfigFile = VfsUtilCore .virtualToIoFile (chosen );
510+ String ioFile = newConfigFile .getAbsolutePath ();
511+ final Vector <String > elem = new Vector <>();
512+ elem .add (ioFile );
513+ ComboBoxModel <String > newModel = new DefaultComboBoxModel <>(elem );
514+ pathComboBox .setModel (newModel );
515+ pathComboBox .setSelectedItem (ioFile );
516+ pathComboBox .setEditable (false );
512517 }
513518 });
514- add (open );
515- add (Box .createVerticalGlue ());
516- db .setCenterPanel (this );
519+ return open ;
517520 }
518521
519522 public String getText () {
0 commit comments