From a8d15c98ac1a41985ec65edc71daf50372ec362b Mon Sep 17 00:00:00 2001 From: Dale Anson Date: Tue, 8 Apr 2008 21:57:00 +0000 Subject: [PATCH] Better fix for 1937876. Previously, any problem with loading a custom ruleset would cause the option pane to not show any rulesets. This fix traps any exception when loading custom rulesets and allows the checkbox tree model to be build with the standard PMD rulesets. Any problem with loading the standard PMD rulesets still throws an exception, which is handled by my previous fix. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5989 51baf565-9d33-0410-a72c-fc3788e3496d --- .../sourceforge/pmd/jedit/SelectedRules.java | 59 +++++++++++++++---- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/SelectedRules.java b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/SelectedRules.java index df673bb32b..c2f6cb5cfe 100644 --- a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/SelectedRules.java +++ b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/SelectedRules.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.jedit; import java.util.*; +import javax.swing.JOptionPane; import javax.swing.tree.*; import net.sourceforge.pmd.Rule; @@ -30,6 +31,24 @@ public class SelectedRules { private Comparator ruleSorter = new Comparator() { public int compare( Rule r1, Rule r2 ) { + if (r1 == null) { + return 1; + } + if (r2 == null) { + return -1; + } + return r1.getName().compareTo( r2.getName() ); + } + }; + + private Comparator rulesetSorter = new Comparator() { + public int compare( RuleSet r1, RuleSet r2 ) { + if (r1 == null) { + return 1; + } + if (r2 == null) { + return -1; + } return r1.getName().compareTo( r2.getName() ); } }; @@ -44,29 +63,45 @@ public class SelectedRules { private RuleSets selectedRules = null; /** - * Constructor for the SelectedRules object + * Loads PMD standard rulesets and any user defined custom rulesets, creates + * a checkbox tree model and root tree node for the rules. * - * @exception RuleSetNotFoundException Description of the Exception + * @exception RuleSetNotFoundException Only thrown when loading PMD standard + * rulesets. Any exception found while attempting to load a custom ruleset is + * caught and a message is displayed to the user. */ public SelectedRules() throws RuleSetNotFoundException { + List rulesets = new ArrayList(); + RuleSetFactory rsf = new RuleSetFactory(); for ( Iterator i = rsf.getRegisteredRuleSets(); i.hasNext(); ) { RuleSet rs = i.next(); //System.out.println("Added RuleSet " + rs.getName() + " descriprion "+ rs.getDescription() +" language "+ rs.getLanguage()); - addRuleSet2Rules( rs ); + rulesets.add(rs); } - //Load custom RuleSets if any. - String customRuleSetPath = jEdit.getProperty( "pmd.customRulesPath" ); - if ( !( customRuleSetPath == null ) ) { - RuleSets ruleSets = rsf.createRuleSets( customRuleSetPath ); - if ( ruleSets.getAllRuleSets() != null ) { - for ( int i = 0;i < ruleSets.getAllRuleSets().length;i++ ) { - RuleSet rs = ruleSets.getAllRuleSets() [ i ]; - addRuleSet2Rules( rs ); + // Load custom RuleSets if any, but do not die if there is any problem + // a custom ruleset. + try { + String customRuleSetPath = jEdit.getProperty( "pmd.customRulesPath" ); + if ( !( customRuleSetPath == null ) ) { + RuleSets ruleSets = rsf.createRuleSets( customRuleSetPath ); // rsnfe + if ( ruleSets.getAllRuleSets() != null ) { + for (RuleSet rs : ruleSets.getAllRuleSets()) { + rulesets.add( rs ); + } } } } + catch(RuleSetNotFoundException e) { + JOptionPane.showMessageDialog(null, "There was an error loading one or more custom rulesets, so no custom rulesets were loaded", "Error Loading Custom Ruleset", JOptionPane.ERROR_MESSAGE); + } + + // sort all rulesets by name and load the tree model and root node + Collections.sort( rulesets, rulesetSorter ); + for (RuleSet rs : rulesets) { + addRuleSet2Rules( rs ); + } TreeModel treeModel = new DefaultTreeModel( root ); checkingModel = new DefaultTreeCheckingModel( treeModel ); @@ -152,7 +187,7 @@ public class SelectedRules { List rules = new ArrayList( rs.getRules() ); Collections.sort( rules, ruleSorter ); - for ( Rule rule: rules ) { + for ( Rule rule : rules ) { DefaultMutableTreeNode ruleNode = new DefaultMutableTreeNode( new RuleNode( rule ) ); node.add( ruleNode ); }