diff --git a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/PMDPreferencePage.java b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/PMDPreferencePage.java index f7db9f5a26..b6904a3b1a 100644 --- a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/PMDPreferencePage.java +++ b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/PMDPreferencePage.java @@ -511,7 +511,7 @@ public class PMDPreferencePage extends PreferencePage implements IWorkbenchPrefe button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { - RuleSetSelectionDialog dialog = new RuleSetSelectionDialog(getShell()); + RuleSetSelectionDialog dialog = new RuleSetSelectionDialog(getShell(), "Import rules"); dialog.open(); if (dialog.getReturnCode() == RuleSetSelectionDialog.OK) { try { diff --git a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/RuleSetSelectionDialog.java b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/RuleSetSelectionDialog.java index 9b276b17e1..896f901fe8 100644 --- a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/RuleSetSelectionDialog.java +++ b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/RuleSetSelectionDialog.java @@ -43,6 +43,7 @@ public class RuleSetSelectionDialog extends Dialog { private RuleSet selectedRuleSet; private boolean importByReference; + private final String title; private final RuleSet[] ruleSets; private final String[] ruleSetNames; @@ -55,8 +56,11 @@ public class RuleSetSelectionDialog extends Dialog { * Constructor for RuleSetSelectionDialog. * @param parentdlgArea */ - public RuleSetSelectionDialog(Shell parent) { + public RuleSetSelectionDialog(Shell parent, String theTitle) { super(parent); + + title = theTitle; + Set registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets(); SortedSet sortedRuleSets = new TreeSet(new Comparator() { public int compare(RuleSet ruleSet1, RuleSet ruleSet2) { @@ -115,17 +119,14 @@ public class RuleSetSelectionDialog extends Dialog { data.horizontalSpan = 2; data.grabExcessHorizontalSpace = true; copyButton.setLayoutData(data); - - // Set the window title - getShell().setText(getMessage(StringKeys.PREF_RULESET_DIALOG_TITLE)); + getShell().setText(title); return dlgArea; } - protected Control createContents(Composite parent) { - Control ctrl = super.createContents(parent); - updateControls(); - return ctrl; + public void create() { + super.create(); + updateControls(); } /** @@ -233,7 +234,7 @@ public class RuleSetSelectionDialog extends Dialog { } private void updateControls() { - boolean hasItem = inputCombo.getSelectionIndex() > 0; + boolean hasItem = inputCombo.getSelectionIndex() > 0 || StringUtil.isNotEmpty(inputCombo.getText()); getButton(IDialogConstants.OK_ID).setEnabled(hasItem); } diff --git a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/AbstractTreeTableManager.java b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/AbstractTreeTableManager.java index 6ec549b64d..035622e45f 100755 --- a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/AbstractTreeTableManager.java +++ b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/AbstractTreeTableManager.java @@ -451,10 +451,10 @@ public abstract class AbstractTreeTableManager extends Abstra if (modifyListener != null) modifyListener.setModified(); } - protected void updateButtonsFor(int[] selectionRatio) { + protected void updateButtonsFor(int selections, int totalSelections) { - selectAllButton.setEnabled( selectionRatio[0] < selectionRatio[1]); - unSelectAllButton.setEnabled( selectionRatio[0] > 0); + selectAllButton.setEnabled(selections < totalSelections); + unSelectAllButton.setEnabled(selections > 0); } protected abstract void updateCheckControls(); diff --git a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleSetTreeItemProvider.java b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleSetTreeItemProvider.java index 5d67f90016..011e2ab466 100644 --- a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleSetTreeItemProvider.java +++ b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleSetTreeItemProvider.java @@ -116,12 +116,15 @@ public class RuleSetTreeItemProvider implements ITreeContentProvider { private RuleGroup groupFor(Rule rule) { + if (fieldAccessor == null) return null; + Comparable groupId = fieldAccessor.valueFor(rule); return ruleGroups.get(groupId); } /** - * Method getParent. + * Return the effective parent of the element if we can figure it out. + * * @param element Object * @return Object * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object) @@ -136,7 +139,9 @@ public class RuleSetTreeItemProvider implements ITreeContentProvider { } /** - * Method hasChildren. + * Return whether the element has kids depending on what kind + * of parent it might be. + * * @param element Object * @return boolean * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object) diff --git a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleTableManager.java b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleTableManager.java index 12f4985e31..f3c566ea89 100755 --- a/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleTableManager.java +++ b/pmd-eclipse-plugin/plugins/net.sourceforge.pmd.eclipse.plugin/src/net/sourceforge/pmd/eclipse/ui/preferences/br/RuleTableManager.java @@ -373,7 +373,8 @@ public class RuleTableManager extends AbstractTreeTableManager implements button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { - RuleSetSelectionDialog dialog = new RuleSetSelectionDialog(parent.getShell()); + //String title = getMessage(StringKeys.PREF_RULESET_DIALOG_TITLE); + RuleSetSelectionDialog dialog = new RuleSetSelectionDialog(parent.getShell(), "Import rules"); dialog.open(); if (dialog.getReturnCode() == Window.OK) { doImport(dialog.getSelectedRuleSet(), dialog.isImportByReference()); @@ -384,20 +385,41 @@ public class RuleTableManager extends AbstractTreeTableManager implements return button; } + private boolean addWithoutDupes(Rule newRule, RuleSet ruleSet) { + + for (Rule rule : ruleSet.getRules()) { + if (rule.equals(newRule)) { + return false; + } + } + + ruleSet.addRule(newRule); + return true; + } + + private void add(RuleSet incomingRuleSet) { + + Iterator iter = incomingRuleSet.getRules().iterator(); + + while (iter.hasNext()) { + Rule rule = iter.next(); + if (addWithoutDupes(rule, ruleSet)) { + rule.setRuleSetName(ruleSet.getName()); + added(rule); + } + } + + ruleSet.addIncludePatterns( incomingRuleSet.getIncludePatterns() ); + ruleSet.addExcludePatterns( incomingRuleSet.getExcludePatterns() ); + } + private void doImport(RuleSet selectedRuleSet, boolean doByReference) { try { if (doByReference) { ruleSet.addRuleSetByReference(selectedRuleSet, false); } else { - // Set pmd-eclipse as new RuleSet name and add the Rule - Iterator iter = selectedRuleSet.getRules().iterator(); - while (iter.hasNext()) { - Rule rule = iter.next(); - rule.setRuleSetName("pmd-elipse"); // FIXME - ruleSet.addRule(rule); - added(rule); - } + add(selectedRuleSet); } setModified(); try { @@ -629,8 +651,8 @@ public class RuleTableManager extends AbstractTreeTableManager implements return isActive(((Rule)item).getName()); } else { if (item instanceof RuleGroup) { - int[] fraction = selectionRatioIn(((RuleGroup)item).rules()); - return (fraction[0] > 0) && (fraction[0] == fraction[1]); + SelectionStats stats = selectionRatioIn(((RuleGroup)item).rules()); + return (stats.selectedCount > 0) && stats.allSelected(); } } return false; // should never get here @@ -640,8 +662,8 @@ public class RuleTableManager extends AbstractTreeTableManager implements if (item instanceof Rule) return false; if (item instanceof RuleGroup) { - int[] fraction = selectionRatioIn(((RuleGroup)item).rules()); - return (fraction[0] > 0) && (fraction[0] != fraction[1]); + SelectionStats stats = selectionRatioIn(((RuleGroup)item).rules()); + return (stats.selectedCount > 0) && (!stats.allSelected()); } return false; } @@ -810,7 +832,21 @@ public class RuleTableManager extends AbstractTreeTableManager implements if (removeRuleButton != null) removeRuleButton.setEnabled(items.length > 0); } - private int[] selectionRatioIn(Rule[] rules) { + private class SelectionStats { + public int selectedCount; + public int totalCount; + public int dysfunctionCount; + + public SelectionStats(int theSelectedCount, int theTotalCount, int theDysfunctionCount) { + selectedCount = theSelectedCount; + totalCount = theTotalCount; + dysfunctionCount = theDysfunctionCount; + } + + public boolean allSelected() { return selectedCount == totalCount; } + } + + private SelectionStats selectionRatioIn(Rule[] rules) { int selectedCount = 0; int dysfunctionCount = 0; @@ -820,7 +856,7 @@ public class RuleTableManager extends AbstractTreeTableManager implements if (StringUtil.isNotEmpty(rule.dysfunctionReason())) dysfunctionCount++; } } - return new int[] { selectedCount , rules.length, dysfunctionCount }; + return new SelectionStats(selectedCount , rules.length, dysfunctionCount) ; } protected void setAllItemsActive() { @@ -907,9 +943,9 @@ public class RuleTableManager extends AbstractTreeTableManager implements Rule[] rules = new Rule[ruleSet.size()]; rules = ruleSet.getRules().toArray(rules); - int[] selectionRatio = selectionRatioIn(rules); - boolean hasIssues = selectionRatio[2] > 0; - updateButtonsFor(selectionRatio); + SelectionStats stats = selectionRatioIn(rules); + boolean hasIssues = stats.dysfunctionCount > 0; + updateButtonsFor(stats.selectedCount, stats.totalCount); String label = SWTUtil.stringFor(StringKeys.PREF_RULESET_ACTIVE_RULE_COUNT); activeCountDetails(