Additional import dialog improvements.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7554 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2011-12-09 07:59:01 +00:00
parent ff59234ff4
commit 4808f52509
5 changed files with 75 additions and 33 deletions

View File

@ -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 {

View File

@ -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<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
SortedSet<RuleSet> sortedRuleSets = new TreeSet<RuleSet>(new Comparator<RuleSet>() {
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);
}

View File

@ -451,10 +451,10 @@ public abstract class AbstractTreeTableManager <T extends Object> 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();

View File

@ -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)

View File

@ -373,7 +373,8 @@ public class RuleTableManager extends AbstractTreeTableManager<Rule> 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<Rule> 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<Rule> 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<Rule> 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<Rule> 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<Rule> 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<Rule> 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<Rule> 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<Rule> 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(