Disable rules from markers shown in editors

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7671 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2012-05-03 19:10:53 +00:00
parent cfb9d30a4d
commit 7199a1103d
5 changed files with 188 additions and 36 deletions

View File

@ -8,8 +8,11 @@ import java.util.Set;
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
/**
*
@ -31,6 +34,28 @@ public class SWTUtil {
plugin.logError(message, error);
}
/**
* Let the buttons operate as a radio group, with only one button
* selected at a time.
*
* @param buttons
*/
public static void asRadioButtons(final Collection<Button> buttons) {
Listener listener = new Listener() {
public void handleEvent(Event e) {
for (Button button : buttons) {
if (e.widget != button) {
button.setSelection (false);
}
}
((Button) e.widget).setSelection (true);
}
};
for (Button button : buttons) button.addListener(SWT.Selection, listener);
}
// TODO move this to to Collections utility
public static Set<String> asStringSet(String input, char separator) {
List<String> values = Arrays.asList(input.split(""+separator));

View File

@ -45,7 +45,7 @@ public abstract class AbstractPMDAction extends Action {
return PMDPlugin.getDefault().loadPreferences();
}
protected void logErrorByKey(String errorId, Throwable error) {
protected static void logErrorByKey(String errorId, Throwable error) {
PMDPlugin.getDefault().logError(getString(errorId), error);
}
}

View File

@ -1,5 +1,6 @@
package net.sourceforge.pmd.eclipse.ui.views.actions;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -21,8 +22,29 @@ import org.eclipse.jface.viewers.TableViewer;
public class DisableRuleAction extends AbstractViolationSelectionAction {
private IPreferences preferences = PMDPlugin.getDefault().loadPreferences();
private final IPreferences preferences = PMDPlugin.getDefault().loadPreferences();
public static void disableRulesFor(Collection<Rule> rules, IPreferences preferences) {
for (Rule rule : rules) {
preferences.isActive(rule.getName(), false);
}
preferences.sync();
}
public static void removeViolationsOf(Collection<Rule> rules, Set<IProject> projects) {
int deletions = 0;
for (IProject project : projects) {
for (Rule rule : rules) {
deletions += MarkerUtil.deleteViolationsOf(rule.getName(), project);
}
}
System.out.println("Violations deleted: " + deletions);
}
public DisableRuleAction(TableViewer viewer) {
super(viewer);
}
@ -46,53 +68,31 @@ public class DisableRuleAction extends AbstractViolationSelectionAction {
return false;
}
private void removeViolationsOf(List<Rule> rules, Set<IProject> projects) {
int deletions = 0;
for (IProject project : projects) {
for (Rule rule : rules) {
deletions += MarkerUtil.deleteViolationsOf(rule.getName(), project);
}
}
System.out.println("Violations deleted: " + deletions);
}
private List<Rule> disableRulesFor(IMarker[] markers) {
List<Rule> rules = MarkerUtil.rulesFor(markers);
for (Rule rule : rules) {
preferences.isActive(rule.getName(), false);
}
preferences.sync();
return rules;
}
/**
* @see org.eclipse.jface.action.IAction#run()
*/
public void run() {
final IMarker[] markers = getSelectedViolations();
if (markers == null) return;
if (markers == null) return;
runWith(markers, preferences, true);
}
public static void runWith(final IMarker[] markers, final IPreferences preferences, final boolean removeViolations) {
try {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.run(new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
List<Rule> rules = disableRulesFor(markers);
removeViolationsOf(rules, MarkerUtil.commonProjectsOf(markers) );
public void run(IProgressMonitor monitor) throws CoreException {
Collection<Rule> rules = MarkerUtil.rulesFor(markers);
disableRulesFor(rules, preferences);
if (removeViolations) removeViolationsOf(rules, MarkerUtil.commonProjectsOf(markers) );
}
}, null);
} catch (CoreException ce) {
logErrorByKey(StringKeys.ERROR_CORE_EXCEPTION, ce);
}
}
}

View File

@ -0,0 +1,21 @@
package net.sourceforge.pmd.eclipse.ui.views.actions;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;
import org.eclipse.ui.texteditor.ITextEditor;
public class MarkerContributionFactory extends ExtensionContributionFactory {
@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ITextEditor editor = (ITextEditor)page.getActivePart();
additions.addContributionItem(new MarkerMenuFiller(editor), null);
}
}

View File

@ -0,0 +1,106 @@
package net.sourceforge.pmd.eclipse.ui.views.actions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
import net.sourceforge.pmd.eclipse.runtime.PMDRuntimeConstants;
import net.sourceforge.pmd.eclipse.runtime.builder.MarkerUtil;
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferences;
import net.sourceforge.pmd.util.StringUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.text.source.IVerticalRulerInfo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;
public class MarkerMenuFiller extends ContributionItem {
private final ITextEditor editor;
private final IVerticalRulerInfo rulerInfo;
private final List<IMarker> markers;
public MarkerMenuFiller(ITextEditor thEditor) {
editor = thEditor;
rulerInfo = getRulerInfo();
markers = getMarkers();
}
private IVerticalRulerInfo getRulerInfo() {
return (IVerticalRulerInfo) editor.getAdapter(IVerticalRulerInfo.class);
}
private List<IMarker> getMarkers() {
List<IMarker> clickedOnMarkers = new ArrayList<IMarker>();
for (IMarker marker : getAllMarkers()){
if (markerHasBeenClicked(marker)){
clickedOnMarkers.add(marker);
}
}
return clickedOnMarkers;
}
//Determine whether the marker has been clicked using the ruler's mouse listener
private boolean markerHasBeenClicked(IMarker marker) {
return marker.getAttribute(IMarker.LINE_NUMBER, 0) == (rulerInfo.getLineOfLastMouseButtonActivity() + 1);
}
//Get all My Markers for this source file
private IMarker[] getAllMarkers() {
IFile sourceFile = ((FileEditorInput) editor.getEditorInput()).getFile();
try {
return sourceFile.findMarkers(
null, //"defined.in.plugin.xml.mymarker",
true,
IResource.DEPTH_ZERO
);
} catch (CoreException ce) {
return MarkerUtil.EMPTY_MARKERS;
}
}
@Override
public void fill(Menu menu, int index) {
// MenuItem separator = new MenuItem(menu, SWT.SEPARATOR, index);
for (final IMarker marker : markers) {
String ruleName = marker.getAttribute(PMDRuntimeConstants.KEY_MARKERATT_RULENAME, "");
if (StringUtil.isEmpty(ruleName)) continue;
MenuItem menuItem = new MenuItem(menu, SWT.PUSH, index);
menuItem.setText("Disable rule: " + ruleName);
menuItem.addSelectionListener(createDynamicSelectionListener(marker));
}
}
private static SelectionAdapter createDynamicSelectionListener(final IMarker marker) {
return new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
disableRules(marker, true);
}
};
}
private static void disableRules(IMarker marker, boolean removeViolations) {
DisableRuleAction.runWith(
new IMarker[] {marker},
PMDPlugin.getDefault().loadPreferences(),
removeViolations
);
}
}