Made rule properties editable in the rule enabler dialog.
This is done as an "override" on top of the rule properties configured for each rule in the ruleset XML files. Only existing properties can be edited; new ones can't be added, and existing ones can't be removed. Implementation is slightly quick-n-dirty; I would prefer to refactor PMD configuration to store rulesets defined in XML files in the system filesystem, and allow import into that set of rulesets from external files or the rulesets/* stuff in the classpath. That would require a bit more surgery :) and also some discussion to make sure my idea is not significantly brain-damaged. So this will do for now. Completes task 75427. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1951 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -32,6 +32,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleSet;
|
||||
import net.sourceforge.pmd.RuleSetFactory;
|
||||
@ -42,6 +43,7 @@ import pmd.custom.RuleClassLoader;
|
||||
|
||||
/**
|
||||
* @author ole martin mørk
|
||||
* @author Gunnlaugur Þór Briem
|
||||
* @created 26. november 2002
|
||||
*/
|
||||
public abstract class ConfigUtils {
|
||||
@ -54,10 +56,20 @@ public abstract class ConfigUtils {
|
||||
*/
|
||||
public static List createRuleList( String rules ) {
|
||||
Iterator iterator = getAllAvailableRules().iterator();
|
||||
Map propOverrides = PMDOptionsSettings.getDefault().getRuleProperties();
|
||||
List list = new ArrayList();
|
||||
while( iterator.hasNext() ) {
|
||||
Rule rule = ( Rule )iterator.next();
|
||||
if( rules.indexOf( rule.getName() + ", " ) > -1 ) {
|
||||
// add it, but first check for property overrides.
|
||||
Map rulePropOverrides = (Map)propOverrides.get( rule.getName() );
|
||||
if(rulePropOverrides != null) {
|
||||
Iterator iter = rulePropOverrides.entrySet().iterator();
|
||||
while( iter.hasNext() ) {
|
||||
Map.Entry entry = (Map.Entry)iter.next();
|
||||
rule.addProperty( (String)entry.getKey(), (String)entry.getValue() );
|
||||
}
|
||||
}
|
||||
list.add( rule );
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,8 @@
|
||||
*/
|
||||
package pmd.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.openide.options.SystemOption;
|
||||
import org.openide.util.HelpCtx;
|
||||
import org.openide.util.NbBundle;
|
||||
@ -34,6 +36,7 @@ import org.openide.util.NbBundle;
|
||||
* Options for PMD netbeans
|
||||
*
|
||||
* @author Ole-Martin Mørk
|
||||
* @author Gunnlaugur Þór Briem
|
||||
* @created 24. oktober 2002
|
||||
*/
|
||||
public class PMDOptionsSettings extends SystemOption {
|
||||
@ -41,13 +44,37 @@ public class PMDOptionsSettings extends SystemOption {
|
||||
/** The serialVersionUID. Don't change! */
|
||||
private final static long serialVersionUID = 8418202279282091070L;
|
||||
|
||||
/** The constant for the rulesets property */
|
||||
/** The constant for the rules property. The String value of this property is a comma-separated list of
|
||||
* names of currently enabled rules. The names refer to the rule definitions in all rulesets returned by
|
||||
* {@link RuleSetFactory#getRegisteredRuleSets}.
|
||||
*/
|
||||
public final static String PROP_RULES = "rules";
|
||||
|
||||
/** The constant for the rule properties property. Please excuse the name! The value of this property is
|
||||
* a <code>Map</code>, whose keys are Strings (rule names) and whose values are instances of
|
||||
* <code>Map</code> containing rule properties (keys and values are <code>String</code>s). These rule
|
||||
* properties override the rules configured for a given rule in its ruleset definition. This is to
|
||||
* enable the NetBeans user to set rule properties within NetBeans, since the ruleset definitions
|
||||
* themselves are locked inside a jar somewhere.
|
||||
* <p>
|
||||
* This property does not show up in the standard beans property editor in NetBeans Options dialog;
|
||||
* rather, it is set by the custom property editor for {@link #PROP_RULES}.
|
||||
*/
|
||||
public final static String PROP_RULE_PROPERTIES = "ruleproperties";
|
||||
|
||||
/** The constant for the rulesetz property. The value of this property is an instance of
|
||||
* {@link CustomRuleSetSettings}, representing the custom ruleset settings currently in effect.
|
||||
*/
|
||||
public final static String PROP_RULESETS = "rulesetz";
|
||||
|
||||
/** The constant for the interval property. The value of this property is the interval at which
|
||||
* source code in the active editor document should be automatically PMD-scanned, in seconds.
|
||||
*/
|
||||
public final static String PROP_SCAN_INTERVAL = "interval";
|
||||
|
||||
/** The constant for the EnableScan property. This property defines whether automatic PMD source code
|
||||
* scanning is enabled or not.
|
||||
*/
|
||||
public final static String PROP_ENABLE_SCAN = "EnableScan";
|
||||
|
||||
/** Default interval for scanning, 10 seconds. **/
|
||||
@ -66,13 +93,13 @@ public class PMDOptionsSettings extends SystemOption {
|
||||
"AvoidReassigningParametersRule, OnlyOneReturn, UseSingletonRule, " +
|
||||
"DontImportJavaLang, UnusedImports, DuplicateImports, ";
|
||||
|
||||
|
||||
// No constructor please!
|
||||
|
||||
/** Sets the default rulesets and initializes the option */
|
||||
protected void initialize() {
|
||||
super.initialize();
|
||||
setRules( DEFAULT_RULES );
|
||||
setRuleProperties( new HashMap() );
|
||||
setRulesets(new CustomRuleSetSettings());
|
||||
setScanEnabled(Boolean.FALSE);
|
||||
setScanInterval(new Integer(DEFAULT_SCAN_INTERVAL));
|
||||
@ -119,7 +146,6 @@ public class PMDOptionsSettings extends SystemOption {
|
||||
return ( String )getProperty( PROP_RULES );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the rulesets property
|
||||
*
|
||||
@ -129,6 +155,24 @@ public class PMDOptionsSettings extends SystemOption {
|
||||
putProperty( PROP_RULES, rules, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the rule properties property (sorry). See {@link #PROP_RULE_PROPERTIES}.
|
||||
*
|
||||
* @return the rule properties, not null.
|
||||
*/
|
||||
public Map getRuleProperties() {
|
||||
return ( Map )getProperty( PROP_RULE_PROPERTIES );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the rule properties property (sorry). See {@link #PROP_RULE_PROPERTIES}.
|
||||
*
|
||||
* @param ruleProperties The new rule properties, not null.
|
||||
*/
|
||||
public void setRuleProperties( Map ruleProperties ) {
|
||||
putProperty( PROP_RULE_PROPERTIES, ruleProperties, true );
|
||||
}
|
||||
|
||||
/** Getter for property rulesets.
|
||||
* @return Value of property rulesets.
|
||||
*
|
||||
|
@ -26,13 +26,18 @@
|
||||
*/
|
||||
package pmd.config.ui;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleProperties;
|
||||
import pmd.config.PMDOptionsSettings;
|
||||
|
||||
/** The datamodel for the properties table
|
||||
* @author ole martin mørk
|
||||
* @author Gunnlaugur Þór Briem
|
||||
* @created 25. november 2002
|
||||
*/
|
||||
public class PropertiesModel extends AbstractTableModel {
|
||||
@ -40,7 +45,6 @@ public class PropertiesModel extends AbstractTableModel {
|
||||
/** The values used in the table */
|
||||
private final String values[][];
|
||||
|
||||
|
||||
/** Creates a new instance of PropertiesModel
|
||||
* @param rule The rule the table should be based upon
|
||||
*/
|
||||
@ -50,19 +54,27 @@ public class PropertiesModel extends AbstractTableModel {
|
||||
return;
|
||||
}
|
||||
RuleProperties properties = rule.getProperties();
|
||||
Map propertyOverrides = (Map)PMDOptionsSettings.getDefault().getRuleProperties().get(rule.getName());
|
||||
if(propertyOverrides == null) {
|
||||
propertyOverrides = Collections.EMPTY_MAP;
|
||||
}
|
||||
values = new String[properties.size()][2];
|
||||
Enumeration keys = properties.keys();
|
||||
int counter = 0;
|
||||
while( keys.hasMoreElements() ) {
|
||||
String key = ( String )keys.nextElement();
|
||||
values[counter][0] = key;
|
||||
values[counter][1] = properties.getProperty( key );
|
||||
|
||||
if( propertyOverrides.containsKey( key ) ) {
|
||||
values[counter][1] = (String)propertyOverrides.get( key );
|
||||
} else {
|
||||
values[counter][1] = properties.getProperty( key );
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Gets the number of columns in the table
|
||||
* @return the number of columns
|
||||
*/
|
||||
@ -101,10 +113,33 @@ public class PropertiesModel extends AbstractTableModel {
|
||||
/** Says if the cell is editable
|
||||
* @param rowIndex The row
|
||||
* @param columnIndex The column
|
||||
* @return false
|
||||
* @return true if and only if <code>columnIndex == 1</code>.
|
||||
*/
|
||||
public boolean isCellEditable( int rowIndex, int columnIndex ) {
|
||||
return false;
|
||||
return columnIndex == 1;
|
||||
}
|
||||
|
||||
/** Sets the value in the cell at the given coordinates in the table.
|
||||
*
|
||||
* @param obj the new value of the cell.
|
||||
* @param rowIndex The row
|
||||
* @param columnIndex The column
|
||||
* @throws IllegalArgumentException if row is out of range, column is not 1, or obj is not a String.
|
||||
*/
|
||||
public void setValueAt(Object obj, int rowIndex, int columnIndex) {
|
||||
if(columnIndex != 1) {
|
||||
throw new IllegalArgumentException("Can only edit property values, not property names");
|
||||
}
|
||||
if(rowIndex < 0 || rowIndex >= values.length) {
|
||||
throw new IllegalArgumentException("Row " + rowIndex + " out of range 0.." + (values.length - 1));
|
||||
}
|
||||
if(obj instanceof String) {
|
||||
String value = (String)obj;
|
||||
values[rowIndex][columnIndex] = value;
|
||||
fireTableCellUpdated(rowIndex, columnIndex);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Property value must be String, was: " + String.valueOf(obj));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,18 +26,28 @@
|
||||
*/
|
||||
package pmd.config.ui;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import javax.swing.JPanel;
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import java.awt.event.MouseEvent;
|
||||
import org.apache.oro.text.perl.Perl5Util;
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.event.TableModelEvent;
|
||||
import javax.swing.event.TableModelListener;
|
||||
|
||||
/** The JPanel used to edit the Rule property
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import org.apache.oro.text.perl.Perl5Util;
|
||||
import pmd.config.PMDOptionsSettings;
|
||||
|
||||
/**
|
||||
* The UI for editing the Rule and RuleProperties properties.
|
||||
*
|
||||
* @author ole martin mørk
|
||||
* @author Gunnlaugur Þór Briem
|
||||
*/
|
||||
public class RuleEnabler extends JPanel {
|
||||
public class RuleEnabler extends JPanel implements TableModelListener {
|
||||
|
||||
private final PropertyEditorSupport editor;
|
||||
private String currentRuleName = null;
|
||||
private final String REGEX = "s/ +/ /g";
|
||||
private final static Perl5Util regex = new Perl5Util();
|
||||
/** Creates a new editor
|
||||
@ -397,7 +407,7 @@ public class RuleEnabler extends JPanel {
|
||||
if( rule != null ) {
|
||||
example.setText( regex.substitute( REGEX, rule.getExample().trim() ) );
|
||||
information.setText( regex.substitute( REGEX, rule.getDescription().trim() ) );
|
||||
properties.setModel( new PropertiesModel( rule ) );
|
||||
updatePropertiesModel( rule );
|
||||
}
|
||||
}//GEN-LAST:event_chosenListValueChanged
|
||||
|
||||
@ -409,7 +419,7 @@ public class RuleEnabler extends JPanel {
|
||||
if( rule != null ) {
|
||||
example.setText( regex.substitute( REGEX, rule.getExample().trim() ) );
|
||||
information.setText( regex.substitute( REGEX, rule.getDescription().trim() ) );
|
||||
properties.setModel( new PropertiesModel( rule ) );
|
||||
updatePropertiesModel( rule );
|
||||
}
|
||||
}//GEN-LAST:event_availableListValueChanged
|
||||
|
||||
@ -477,7 +487,40 @@ public class RuleEnabler extends JPanel {
|
||||
}
|
||||
|
||||
}//GEN-LAST:event_chooseOneActionPerformed
|
||||
|
||||
public void tableChanged(TableModelEvent evt) {
|
||||
if(evt.getType() == TableModelEvent.UPDATE && evt.getColumn() == 1) {
|
||||
int row = evt.getFirstRow();
|
||||
PropertiesModel model = (PropertiesModel)properties.getModel();
|
||||
PMDOptionsSettings settings = PMDOptionsSettings.getDefault();
|
||||
String propName = (String)model.getValueAt(row, 0);
|
||||
String newValue = (String)model.getValueAt(row, 1);
|
||||
Map rulePropOverrides = (Map)settings.getRuleProperties().get(this.currentRuleName);
|
||||
if(rulePropOverrides == null) {
|
||||
rulePropOverrides = new HashMap();
|
||||
settings.getRuleProperties().put(this.currentRuleName, rulePropOverrides);
|
||||
}
|
||||
rulePropOverrides.put(propName, newValue);
|
||||
editor.firePropertyChange();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reinitializes the rule properties table with a model based on the given rule.
|
||||
* Called when a new rule is selected in the available list or the selected list.
|
||||
*
|
||||
* @param rule the rule to base the new table model on.
|
||||
*/
|
||||
private void updatePropertiesModel( Rule rule ) {
|
||||
PropertiesModel oldModel = (PropertiesModel)properties.getModel();
|
||||
if(oldModel != null) {
|
||||
oldModel.removeTableModelListener(this);
|
||||
}
|
||||
this.currentRuleName = rule.getName();
|
||||
PropertiesModel newModel = new PropertiesModel( rule );
|
||||
newModel.addTableModelListener(this);
|
||||
properties.setModel( newModel );
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
private javax.swing.JSeparator topSeparator;
|
||||
|
Reference in New Issue
Block a user