From 52a51bf67cab1fba0f585bb9953d289e54531dec Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Sun, 11 Nov 2007 03:37:27 +0000 Subject: [PATCH] fixes to make it work with 4.1rc1 (4.0+ in fact): JEditPMDRule removed to resolve typecast problems with XPath rules handling of selected rules modified git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5642 51baf565-9d33-0410-a72c-fc3788e3496d --- .../sourceforge/pmd/jedit/JEditPMDRule.java | 287 ------------------ .../sourceforge/pmd/jedit/PMDOptionPane.java | 10 +- .../sourceforge/pmd/jedit/RuleCheckBox.java | 40 +++ .../sourceforge/pmd/jedit/SelectedRules.java | 119 ++------ 4 files changed, 69 insertions(+), 387 deletions(-) delete mode 100644 pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/JEditPMDRule.java create mode 100644 pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/RuleCheckBox.java diff --git a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/JEditPMDRule.java b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/JEditPMDRule.java deleted file mode 100644 index d7fc78b9fd..0000000000 --- a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/JEditPMDRule.java +++ /dev/null @@ -1,287 +0,0 @@ -package net.sourceforge.pmd.jedit; - -import java.util.List; -import java.util.Properties; - -import net.sourceforge.pmd.Rule; -import net.sourceforge.pmd.RuleContext; -import net.sourceforge.pmd.RuleSet; -import net.sourceforge.pmd.PropertyDescriptor; - - -public class JEditPMDRule implements Rule -{ - private final Rule rule; - private final RuleSet rs; - - public JEditPMDRule(Rule rule, RuleSet rs) - { - this.rule = rule; - this.rs = rs; - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#addProperties(java.util.Properties) - */ - public void addProperties(Properties properties) - { - rule.addProperties(properties); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#addProperty(java.lang.String, java.lang.String) - */ - public void addProperty(String name, String property) - { - rule.addProperty(name, property); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#apply(java.util.List, net.sourceforge.pmd.RuleContext) - */ - public void apply(List astCompilationUnits, RuleContext ctx) - { - rule.apply(astCompilationUnits, ctx); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getBooleanProperty(java.lang.String) - */ - public boolean getBooleanProperty(String name) - { - return rule.getBooleanProperty(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getDescription() - */ - public String getDescription() - { - return rule.getDescription(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getDoubleProperty(java.lang.String) - */ - public double getDoubleProperty(String name) - { - return rule.getDoubleProperty(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getExternalInfoUrl() - */ - public String getExternalInfoUrl() - { - return rule.getExternalInfoUrl(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getIntProperty(java.lang.String) - */ - public int getIntProperty(String name) - { - return rule.getIntProperty(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getMessage() - */ - public String getMessage() - { - return rule.getMessage(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getName() - */ - public String getName() - { - return rule.getName(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getPriority() - */ - public int getPriority() - { - return rule.getPriority(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getPriorityName() - */ - public String getPriorityName() - { - return rule.getPriorityName(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getProperties() - */ - public Properties getProperties() - { - return rule.getProperties(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getRuleSetName() - */ - public String getRuleSetName() - { - return rule.getRuleSetName(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#getStringProperty(java.lang.String) - */ - public String getStringProperty(String name) - { - return rule.getStringProperty(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#hasProperty(java.lang.String) - */ - public boolean hasProperty(String name) - { - return rule.hasProperty(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#include() - */ - public boolean include() - { - return rule.include(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setDescription(java.lang.String) - */ - public void setDescription(String description) - { - rule.setDescription(description); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setExternalInfoUrl(java.lang.String) - */ - public void setExternalInfoUrl(String url) - { - rule.setExternalInfoUrl(url); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setInclude(boolean) - */ - public void setInclude(boolean include) - { - rule.setInclude(include); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setMessage(java.lang.String) - */ - public void setMessage(String message) - { - rule.setMessage(message); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setName(java.lang.String) - */ - public void setName(String name) - { - rule.setName(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setPriority(int) - */ - public void setPriority(int priority) - { - rule.setPriority(priority); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setRuleSetName(java.lang.String) - */ - public void setRuleSetName(String name) - { - rule.setRuleSetName(name); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#setUsesDFA() - */ - public void setUsesDFA() - { - rule.setUsesDFA(); - } - - /* (non-Javadoc) - * @see net.sourceforge.pmd.Rule#usesDFA() - */ - public boolean usesDFA() - { - return rule.usesDFA(); - } - - /** - * @return Returns the rs. - */ - public RuleSet getRs() - { - return rs; - } - - public boolean usesTypeResolution() - { - return rule.usesTypeResolution(); - } - - public void setUsesTypeResolution() - { - rule.setUsesTypeResolution(); - } - - public PropertyDescriptor propertyDescriptorFor(String propDesc) - { - return rule.propertyDescriptorFor(propDesc); - } - - public void addExample(String example) - { - rule.addExample(example); - } - - public void addRuleChainVisit(String astNodeName) - { - rule.addRuleChainVisit(astNodeName); - } - - public List getExamples() - { - return rule.getExamples(); - } - - public List getRuleChainVisits() - { - return rule.getRuleChainVisits(); - } - - public boolean usesRuleChain() - { - return rule.usesRuleChain(); - - } - /** - * @deprecated - use getExamples() since we support multiple examples - */ - public String getExample() - { - return rule.getExample(); - } -} - diff --git a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/PMDOptionPane.java b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/PMDOptionPane.java index aae35c0fc5..4c32711428 100644 --- a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/PMDOptionPane.java +++ b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/PMDOptionPane.java @@ -60,10 +60,10 @@ public class PMDOptionPane extends AbstractOptionPane implements OptionPane int index = locationToIndex(e.getPoint()); if (index != -1) { - JCheckBox box = (JCheckBox) getModel().getElementAt(index); + RuleCheckBox box = (RuleCheckBox) getModel().getElementAt(index); // String example = // rules.getRule(box).getExample(); - List examples = rules.getRule(box).getExamples(); + List examples = box.getRule().getExamples(); exampleTextArea.setText(StringList.join(examples, "\n---------\n")); exampleTextArea.setCaretPosition(0); } @@ -75,7 +75,7 @@ public class PMDOptionPane extends AbstractOptionPane implements OptionPane public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { - JCheckBox box = (JCheckBox) value; + RuleCheckBox box = (RuleCheckBox) value; box.setEnabled(isEnabled()); box.setFont(getFont()); box.setFocusPainted(false); @@ -83,12 +83,12 @@ public class PMDOptionPane extends AbstractOptionPane implements OptionPane box.setBorder(isSelected ? UIManager .getBorder("List.focusCellHighlightBorder") : new EmptyBorder(1, 1, 1, 1)); - box.setToolTipText(rules.getRule(box).getRuleSetName()); + box.setToolTipText(box.getRule().getRuleSetName()); return box; } } - public CheckboxList(Object[] args) + public CheckboxList(RuleCheckBox[] args) { super(args); setCellRenderer(new CheckboxListCellRenderer()); diff --git a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/RuleCheckBox.java b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/RuleCheckBox.java new file mode 100644 index 0000000000..f5482f6093 --- /dev/null +++ b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/RuleCheckBox.java @@ -0,0 +1,40 @@ +package net.sourceforge.pmd.jedit; + +import javax.swing.JCheckBox; + +import org.gjt.sp.jedit.jEdit; + +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.RuleSet; + +public class RuleCheckBox extends JCheckBox { + + private static final long serialVersionUID = 4779236734852745643L; + + private final Rule rule; + + private final RuleSet ruleset; + + public RuleCheckBox(Rule rule, RuleSet rs) { + super(rule.getName()); + this.rule = rule; + this.ruleset = rs; + + setSelected(jEdit.getBooleanProperty(PMDJEditPlugin.OPTION_RULES_PREFIX + rule.getName(), true)); + } + + /** + * @return the rule + */ + public Rule getRule() { + return rule; + } + + /** + * @return the ruleset + */ + public RuleSet getRuleset() { + return ruleset; + } + +} 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 65462ae9bb..7a245eb980 100644 --- a/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/SelectedRules.java +++ b/pmd-jedit/PMDPlugin/src/net/sourceforge/pmd/jedit/SelectedRules.java @@ -5,6 +5,13 @@ */ package net.sourceforge.pmd.jedit; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; @@ -13,13 +20,6 @@ import net.sourceforge.pmd.RuleSets; import org.gjt.sp.jedit.jEdit; -import javax.swing.JCheckBox; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.TreeMap; - /** * Description of the Class @@ -30,15 +30,12 @@ import java.util.TreeMap; public class SelectedRules { - // Rule -> JCheckBox - private final Map rules = new TreeMap( - new Comparator() + private final Set checkboxes = new TreeSet( + new Comparator() { - public int compare(Object o1, Object o2) + public int compare(RuleCheckBox r1, RuleCheckBox r2) { - Rule r1 = (Rule)o1; - Rule r2 = (Rule)o2; - return r1.getName().compareTo(r2.getName()); + return r1.getRule().getName().compareTo(r2.getRule().getName()); } }); @@ -51,9 +48,9 @@ public class SelectedRules public SelectedRules() throws RuleSetNotFoundException { RuleSetFactory rsf = new RuleSetFactory(); - for(Iterator i = rsf.getRegisteredRuleSets(); i.hasNext(); ) + for(Iterator i = rsf.getRegisteredRuleSets(); i.hasNext(); ) { - RuleSet rs = (RuleSet)i.next(); + RuleSet rs = i.next(); //System.out.println("Added RuleSet " + rs.getName() + " descriprion "+ rs.getDescription() +" language "+ rs.getLanguage()); addRuleSet2Rules(rs); } @@ -78,75 +75,23 @@ public class SelectedRules } - /** - * Description of the Method - * - * @return Description of the Return Value - */ - public int size() - { - return rules.size(); - } - - - /** - * Gets the rule attribute of the SelectedRules object - * - * @param candidate Description of the Parameter - * @return The rule value - */ - public Rule getRule(JCheckBox candidate) - { - for(Iterator i = rules.keySet().iterator(); i.hasNext(); ) - { - Rule rule = (Rule)i.next(); - JCheckBox box = (JCheckBox)rules.get(rule); - if(box.equals(candidate)) - { - return rule; - } - } - throw new RuntimeException("Couldn't find a rule that mapped to the passed in JCheckBox " + candidate); - } - - - /** - * Description of the Method - * - * @param key Description of the Parameter - * @return Description of the Return Value - */ - public JCheckBox get(Object key) - { - return (JCheckBox)rules.get(key); - } - - /** * Gets the allBoxes attribute of the SelectedRules object * * @return The allBoxes value */ - public Object[] getAllBoxes() + public RuleCheckBox[] getAllBoxes() { - Object[] foo = new Object[rules.size()]; - int idx = 0; - for(Iterator i = rules.values().iterator(); i.hasNext(); ) - { - foo[idx] = i.next(); - idx++; - } - return foo; + return checkboxes.toArray(new RuleCheckBox[checkboxes.size()]); } /** Description of the Method */ public void save() { - for(Iterator i = rules.keySet().iterator(); i.hasNext(); ) + for(RuleCheckBox box: checkboxes) { - Rule rule = (Rule)i.next(); - jEdit.setBooleanProperty(PMDJEditPlugin.OPTION_RULES_PREFIX + rule.getName(), get(rule).isSelected()); + jEdit.setBooleanProperty(PMDJEditPlugin.OPTION_RULES_PREFIX + box.getRule().getName(), box.isSelected()); } } @@ -160,14 +105,14 @@ public class SelectedRules { RuleSets newRuleSets = new RuleSets(); - Map rulesetmap = new HashMap(); + Map rulesetmap = new HashMap(); - for(Iterator i = rules.keySet().iterator(); i.hasNext(); ) + for(RuleCheckBox box: checkboxes) { - Rule rule = (Rule)i.next(); - if(get(rule).isSelected()) + if(box.isSelected()) { - RuleSet rs = ((JEditPMDRule)rule).getRs(); + Rule rule = box.getRule(); + RuleSet rs = box.getRuleset(); if(rulesetmap.get(rs.getName()) != null) { RuleSet existingRs = (RuleSet) rulesetmap.get(rs.getName()); @@ -190,21 +135,6 @@ public class SelectedRules return newRuleSets; } - - /** - * Description of the Method - * - * @param name Description of the Parameter - * @return Description of the Return Value - */ - private JCheckBox createCheckBox(String name) - { - JCheckBox box = new JCheckBox(name); - box.setSelected(jEdit.getBooleanProperty(PMDJEditPlugin.OPTION_RULES_PREFIX + name, true)); - return box; - } - - /** * Adds a feature to the RuleSet2Rules attribute of the SelectedRules object * @@ -212,10 +142,9 @@ public class SelectedRules */ private void addRuleSet2Rules(RuleSet rs) { - for(Iterator j = rs.getRules().iterator(); j.hasNext(); ) + for(Rule rule: rs.getRules()) { - Rule rule = (Rule)j.next(); - rules.put(new JEditPMDRule(rule, rs), createCheckBox(rule.getName())); + checkboxes.add(new RuleCheckBox(rule, rs)); } } }