Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4165 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2006-01-27 00:03:11 +00:00
parent 794aadc38a
commit deb944f7c5
5 changed files with 355 additions and 145 deletions

@ -11,6 +11,7 @@ Runtime
. Update to PMD v3.5
. Fix NullPointerException on PriorityFilter class
. Fix NullPointerException and InvocationTargetException on ASTWriterImpl class
. Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3
----------------------------------------------------------------------------------------

@ -1,24 +1,35 @@
/*
* <copyright>
* Copyright 1997-2003 PMD for Eclipse Development team
* under sponsorship of the Defense Advanced Research Projects
* Agency (DARPA).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Cougaar Open Source License as published by
* DARPA on the Cougaar Open Source Website (www.cougaar.org).
*
* THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
* PROVIDED "AS IS" WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
* IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
* ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
* TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THE COUGAAR SOFTWARE.
*
* </copyright>
/*
* Copyright (c) 2005,2006 PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.actions;
@ -51,6 +62,9 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
@ -63,43 +77,40 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.3 2005/10/24 22:39:00 phherlin
* Integrating Sebastian Raffel's work
* Refactor command processing
* Revision 1.2 2003/11/30 22:57:37 phherlin
* Merging from eclipse-v2 development branch
* Revision 1.4 2006/01/27 00:03:11 phherlin
* Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3
* Revision 1.3 2005/10/24 22:39:00 phherlin Integrating Sebastian Raffel's work Refactor command
* processing Revision 1.2 2003/11/30 22:57:37 phherlin Merging from eclipse-v2 development branch
*
* Revision 1.1.2.1 2003/11/04 16:27:19 phherlin Refactor to use the adaptable
* framework instead of downcasting
* Revision 1.1.2.1 2003/11/04 16:27:19 phherlin Refactor to use the adaptable framework instead of downcasting
*
* Revision 1.1 2003/08/14 16:10:41 phherlin Implementing Review feature
* (RFE#787086)
* Revision 1.1 2003/08/14 16:10:41 phherlin Implementing Review feature (RFE#787086)
*
*/
public class ClearReviewsAction implements IObjectActionDelegate, IResourceVisitor, IViewActionDelegate {
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.actions.ClearReviewsAction");
private IWorkbenchPart activePart;
private IWorkbenchPart targetPart;
private IProgressMonitor monitor;
/**
* @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
*/
public void init(IViewPart view) {
this.activePart = view.getSite().getPage().getActivePart();
this.targetPart = view.getSite().getPage().getActivePart();
}
/**
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
* org.eclipse.ui.IWorkbenchPart)
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.activePart = targetPart;
this.targetPart = targetPart;
}
/**
* @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
*/
public void run(IAction action) {
log.info("Remove violation reviews requested.");
ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
try {
monitorDialog.run(false, false, new IRunnableWithProgress() {
@ -117,8 +128,7 @@ public class ClearReviewsAction implements IObjectActionDelegate, IResourceVisit
}
/**
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
* org.eclipse.jface.viewers.ISelection)
* @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
}
@ -165,41 +175,62 @@ public class ClearReviewsAction implements IObjectActionDelegate, IResourceVisit
* Process the clear review action
*/
protected void clearReviews() {
ISelection selection = activePart.getSite().getSelectionProvider().getSelection();
if ((selection != null) && (selection instanceof IStructuredSelection)) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
if (getMonitor() != null) {
getMonitor().beginTask(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_MONITOR_REMOVE_REVIEWS),
IProgressMonitor.UNKNOWN);
try {
// If action is started from a view, the process all selected resource
if (this.targetPart instanceof IViewPart) {
ISelection selection = targetPart.getSite().getSelectionProvider().getSelection();
Iterator i = structuredSelection.iterator();
try {
while (i.hasNext()) {
Object object = (Object) i.next();
IResource resource = null;
if ((selection != null) && (selection instanceof IStructuredSelection)) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
if (getMonitor() != null) {
getMonitor().beginTask(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_MONITOR_REMOVE_REVIEWS),
IProgressMonitor.UNKNOWN);
if (object instanceof IMarker) {
resource = ((IMarker) object).getResource();
} else if (object instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) object;
resource = (IResource) adaptable.getAdapter(IResource.class);
} else {
log.warn("The selected object is not adaptable");
log.debug(" -> selected object = " + object);
}
Iterator i = structuredSelection.iterator();
while (i.hasNext()) {
Object object = (Object) i.next();
IResource resource = null;
if (resource != null) {
resource.accept(this);
} else {
log.warn("The selected object cannot adapt to a resource.");
log.debug(" -> selected object" + object);
if (object instanceof IMarker) {
resource = ((IMarker) object).getResource();
} else if (object instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) object;
resource = (IResource) adaptable.getAdapter(IResource.class);
} else {
log.warn("The selected object is not adaptable");
log.debug(" -> selected object = " + object);
}
if (resource != null) {
resource.accept(this);
} else {
log.warn("The selected object cannot adapt to a resource.");
log.debug(" -> selected object" + object);
}
}
}
} catch (CoreException e) {
PMDPlugin.getDefault().logError(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION, e);
}
}
// If action is started from an editor, process the file currently edited
if (this.targetPart instanceof IEditorPart) {
IEditorInput editorInput = ((IEditorPart) this.targetPart).getEditorInput();
if (editorInput instanceof IFileEditorInput) {
((IFileEditorInput) editorInput).getFile().accept(this);
} else {
log.debug("The kind of editor input is not supported. The editor input if of type: "
+ editorInput.getClass().getName());
}
}
// else this is not supported
else {
log.debug("This action is not supported on this kind of part. This part type is: "
+ this.targetPart.getClass().getName());
}
} catch (CoreException e) {
PMDPlugin.getDefault().logError(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION, e);
}
}

@ -1,3 +1,36 @@
/*
* Copyright (c) 2005,2006 PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.actions;
import java.util.Iterator;
@ -17,7 +50,11 @@ import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
/**
@ -27,38 +64,32 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.13 2005/12/30 16:22:31 phherlin
* Add a debug log to check what happens when this action is launched in an editor pane
*
* Revision 1.12 2005/10/24 22:39:00 phherlin
* Integrating Sebastian Raffel's work
* Refactor command processing
* Revision 1.11 2005/05/07 13:32:06 phherlin
* Continuing refactoring Fix some PMD violations Fix Bug 1144793 Fix Bug
* 1190624 (at least try)
* Revision 1.14 2006/01/27 00:03:11 phherlin
* Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3
* Revision 1.13 2005/12/30 16:22:31 phherlin Add a debug log to check what happens when this action
* is launched in an editor pane
*
* Revision 1.10 2003/11/30 22:57:37 phherlin Merging from eclipse-v2
* development branch
* Revision 1.12 2005/10/24 22:39:00 phherlin Integrating Sebastian Raffel's work Refactor command processing Revision 1.11
* 2005/05/07 13:32:06 phherlin Continuing refactoring Fix some PMD violations Fix Bug 1144793 Fix Bug 1190624 (at least try)
*
* Revision 1.8.2.2 2003/11/04 16:27:19 phherlin Refactor to use the adaptable
* framework instead of downcasting
* Revision 1.10 2003/11/30 22:57:37 phherlin Merging from eclipse-v2 development branch
*
* Revision 1.8.2.1 2003/10/30 22:09:51 phherlin Simplify the code : moving the
* deep nested CountVisitor class as a first level nested inner class. This also
* correct a rule violation from PMD.
* Revision 1.8.2.2 2003/11/04 16:27:19 phherlin Refactor to use the adaptable framework instead of downcasting
*
* Revision 1.8 2003/08/13 20:08:40 phherlin Refactoring private->protected to
* remove warning about non accessible member access in enclosing types
* Revision 1.8.2.1 2003/10/30 22:09:51 phherlin Simplify the code : moving the deep nested CountVisitor class as a first level
* nested inner class. This also correct a rule violation from PMD.
*
* Revision 1.7 2003/07/01 20:21:37 phherlin Correcting some PMD violations !
* (empty if stmt)
* Revision 1.8 2003/08/13 20:08:40 phherlin Refactoring private->protected to remove warning about non accessible member access in
* enclosing types
*
* Revision 1.7 2003/07/01 20:21:37 phherlin Correcting some PMD violations ! (empty if stmt)
*
* Revision 1.6 2003/06/19 20:58:13 phherlin Improve progress indicator accuracy
*
* Revision 1.5 2003/05/19 22:27:33 phherlin Refactoring to improve performance
*
* Revision 1.4 2003/03/30 20:48:19 phherlin Adding logging Displaying error
* dialog in a thread safe way Adding support for folders and package
* Revision 1.4 2003/03/30 20:48:19 phherlin Adding logging Displaying error dialog in a thread safe way Adding support for folders
* and package
*
*/
public class PMDCheckAction implements IObjectActionDelegate {
@ -66,8 +97,7 @@ public class PMDCheckAction implements IObjectActionDelegate {
private IWorkbenchPart targetPart;
/**
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction,
* IWorkbenchPart)
* @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.targetPart = targetPart;
@ -80,12 +110,35 @@ public class PMDCheckAction implements IObjectActionDelegate {
log.info("Check PMD action requested");
try {
ISelection selection = this.targetPart.getSite().getSelectionProvider().getSelection();
if (selection instanceof IStructuredSelection) {
reviewSelectedResources((IStructuredSelection) selection);
} else {
log.debug("The selection is not an instance of IStructuredSelection. This is not supported: " + selection.getClass().getName());
// Execute PMD on a range of selected resource if action selected from a view part
if (this.targetPart instanceof IViewPart) {
ISelection selection = this.targetPart.getSite().getSelectionProvider().getSelection();
if (selection instanceof IStructuredSelection) {
reviewSelectedResources((IStructuredSelection) selection);
} else {
log.debug("The selection is not an instance of IStructuredSelection. This is not supported: "
+ selection.getClass().getName());
}
}
// If action is selected from an editor, run PMD on the file currently edited
else if (this.targetPart instanceof IEditorPart) {
IEditorInput editorInput = ((IEditorPart) this.targetPart).getEditorInput();
if (editorInput instanceof IFileEditorInput) {
reviewSingleResource(((IFileEditorInput) editorInput).getFile());
} else {
log.debug("The kind of editor input is not supported. The editor input if of type: "
+ editorInput.getClass().getName());
}
}
// Else, this is not supported for now
else {
log.debug("Running PMD from this kind of part is not supported. Part is of type "
+ this.targetPart.getClass().getName());
}
} catch (CommandException e) {
PMDPlugin.getDefault().showError(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION), e);
}
@ -98,11 +151,27 @@ public class PMDCheckAction implements IObjectActionDelegate {
public void selectionChanged(IAction action, ISelection selection) {
}
/**
* Run the reviewCode command on a single resource
*
* @param resource
* @throws CommandException
*/
private void reviewSingleResource(IResource resource) throws CommandException {
ReviewCodeCmd cmd = new ReviewCodeCmd();
cmd.addResource(resource);
cmd.setStepsCount(1);
cmd.setTaskMarker(true);
cmd.setOpenPmdPerspective(PMDPlugin.getDefault().getPreferenceStore()
.getInt(PMDPlugin.SHOW_PERSPECTIVE_ON_CHECK_PREFERENCE) == 1);
cmd.setUserInitiated(true);
cmd.performExecute();
}
/**
* Prepare and run the reviewCode command for all selected resources
*
* @param selection
* the selected resources
* @param selection the selected resources
*/
private void reviewSelectedResources(IStructuredSelection selection) throws CommandException {
ReviewCodeCmd cmd = new ReviewCodeCmd();
@ -139,8 +208,7 @@ public class PMDCheckAction implements IObjectActionDelegate {
/**
* Count the number of resources of a selection
*
* @param selection
* a selection
* @param selection a selection
* @return the element count
*/
private int countElement(IStructuredSelection selection) {

@ -1,3 +1,36 @@
/*
* Copyright (c) 2005,2006 PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.actions;
import java.io.ByteArrayInputStream;
@ -31,7 +64,11 @@ import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
@ -43,6 +80,9 @@ import org.eclipse.ui.PlatformUI;
* @version $Revision$
*
* $Log$
* Revision 1.10 2006/01/27 00:03:11 phherlin
* Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3
*
* Revision 1.9 2004/04/29 21:15:08 phherlin
* Upgrading to PMD v1.7
*
@ -86,23 +126,43 @@ public class PMDGenerateASTAction implements IObjectActionDelegate, IRunnableWit
*/
public void run(IAction action) {
log.info("Generation AST action requested");
ISelection sel = targetPart.getSite().getSelectionProvider().getSelection();
if (sel instanceof IStructuredSelection) {
this.structuredSelection = (IStructuredSelection) sel;
ProgressMonitorDialog dialog =
new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
try {
dialog.run(false, false, this);
} catch (InvocationTargetException e) {
PMDPlugin.getDefault().showError(
PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_INVOCATIONTARGET_EXCEPTION),
e);
} catch (InterruptedException e) {
PMDPlugin.getDefault().showError(
PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_INTERRUPTED_EXCEPTION),
e);
// If action is selected from a view, process the selection
if (this.targetPart instanceof IViewPart) {
ISelection sel = targetPart.getSite().getSelectionProvider().getSelection();
if (sel instanceof IStructuredSelection) {
this.structuredSelection = (IStructuredSelection) sel;
ProgressMonitorDialog dialog =
new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
try {
dialog.run(false, false, this);
} catch (InvocationTargetException e) {
PMDPlugin.getDefault().showError(
PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_INVOCATIONTARGET_EXCEPTION),
e);
} catch (InterruptedException e) {
PMDPlugin.getDefault().showError(
PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_INTERRUPTED_EXCEPTION),
e);
}
}
}
// If action is selected from an editor, process the file currently edited
if (this.targetPart instanceof IEditorPart) {
IEditorInput editorInput = ((IEditorPart) this.targetPart).getEditorInput();
if (editorInput instanceof IFileEditorInput) {
generateAST(((IFileEditorInput) editorInput).getFile());
} else {
log.debug("The kind of editor input is not supported. The editor input if of type: "
+ editorInput.getClass().getName());
}
}
// else this is not supported
else {
log.debug("This action is not supported on this kind of part. This part type is: " + this.targetPart.getClass().getName());
}
}
/**

@ -1,3 +1,36 @@
/*
* Copyright (c) 2005,2006 PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.actions;
import java.util.Iterator;
@ -14,6 +47,9 @@ import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
@ -26,20 +62,17 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
* Revision 1.5 2003/11/30 22:57:37 phherlin
* Merging from eclipse-v2 development branch
*
* Revision 1.4.2.1 2003/11/04 16:27:19 phherlin
* Refactor to use the adaptable framework instead of downcasting
*
* Revision 1.4 2003/05/19 22:27:33 phherlin
* Refactoring to improve performance
*
* Revision 1.3 2003/03/30 20:49:37 phherlin
* Adding logging
* Displaying error dialog in a thread safe way
* Adding support for folders and package
*
* Revision 1.6 2006/01/27 00:03:11 phherlin
* Fix BUG#1365407 Problems with PMD in Eclipse/Issue 3
* Revision 1.5 2003/11/30 22:57:37 phherlin Merging from eclipse-v2 development branch
*
* Revision 1.4.2.1 2003/11/04 16:27:19 phherlin Refactor to use the adaptable framework instead of downcasting
*
* Revision 1.4 2003/05/19 22:27:33 phherlin Refactoring to improve performance
*
* Revision 1.3 2003/03/30 20:49:37 phherlin Adding logging Displaying error dialog in a thread safe way Adding support for folders
* and package
*
*/
public class PMDRemoveMarkersAction implements IViewActionDelegate, IObjectActionDelegate {
private static final String VIEW_ACTION = "net.sourceforge.pmd.eclipse.pmdRemoveAllMarkersAction";
@ -88,33 +121,50 @@ public class PMDRemoveMarkersAction implements IViewActionDelegate, IObjectActio
* Process removing of makers on a resource selection (project or file)
*/
private void processResource() {
ISelection sel = targetPart.getSite().getSelectionProvider().getSelection();
try {
// if action is run from a view, process the selected resources
if (this.targetPart instanceof IViewPart) {
ISelection sel = targetPart.getSite().getSelectionProvider().getSelection();
if (sel instanceof IStructuredSelection) {
IStructuredSelection structuredSel = (IStructuredSelection) sel;
for (Iterator i = structuredSel.iterator(); i.hasNext();) {
Object element = i.next();
if (sel instanceof IStructuredSelection) {
IStructuredSelection structuredSel = (IStructuredSelection) sel;
for (Iterator i = structuredSel.iterator(); i.hasNext();) {
Object element = i.next();
try {
if (element instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) element;
IResource resource = (IResource) adaptable.getAdapter(IResource.class);
if (resource != null) {
resource.deleteMarkers(PMDPlugin.PMD_MARKER, true, IResource.DEPTH_INFINITE);
if (element instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) element;
IResource resource = (IResource) adaptable.getAdapter(IResource.class);
if (resource != null) {
resource.deleteMarkers(PMDPlugin.PMD_MARKER, true, IResource.DEPTH_INFINITE);
} else {
log.warn("The selected object cannot adapt to a resource");
log.debug(" -> selected object : " + element);
}
} else {
log.warn("The selected object cannot adapt to a resource");
log.warn("The selected object is not adaptable");
log.debug(" -> selected object : " + element);
}
} else {
log.warn("The selected object is not adaptable");
log.debug(" -> selected object : " + element);
}
} catch (CoreException e) {
PMDPlugin.getDefault().showError(
PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION),
e);
}
}
// if action is run from an editor, process the file currently edited
else if (this.targetPart instanceof IEditorPart) {
IEditorInput editorInput = ((IEditorPart) this.targetPart).getEditorInput();
if (editorInput instanceof IFileEditorInput) {
((IFileEditorInput) editorInput).getFile().deleteMarkers(PMDPlugin.PMD_MARKER, true, IResource.DEPTH_INFINITE);
} else {
log.debug("The kind of editor input is not supported. The editor input if of type: "
+ editorInput.getClass().getName());
}
}
// else, this is not supported
else {
log.debug("This action is not supported on that part. This part type is: " + this.targetPart.getClass().getName());
}
} catch (CoreException e) {
PMDPlugin.getDefault().showError(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION), e);
}
}