From 63b1270165083284dffb93f36164f27ebfcb1bb8 Mon Sep 17 00:00:00 2001 From: Philippe Herlin Date: Mon, 17 Mar 2003 23:32:05 +0000 Subject: [PATCH] minor cleaning git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1542 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/eclipse/CPDReportWindow.java | 12 +- .../sourceforge/pmd/eclipse/CPDVisitor.java | 73 ++-- .../actions/CPDCheckProjectAction.java | 313 +++++++++--------- 3 files changed, 200 insertions(+), 198 deletions(-) diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDReportWindow.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDReportWindow.java index a58d60ad71..3ec3342c4b 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDReportWindow.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDReportWindow.java @@ -10,12 +10,16 @@ import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; /** + * A window to show the CPD report + * * @author David Craine + * @author Philippe Herlin + * @version $Revision$ + * + * $Log$ + * Revision 1.3 2003/03/17 23:31:44 phherlin + * minor cleaning * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>Templates. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generat ion. */ public class CPDReportWindow extends ApplicationWindow { private Label label; diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDVisitor.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDVisitor.java index 8b233d5315..fbb95203f0 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDVisitor.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/CPDVisitor.java @@ -1,55 +1,54 @@ package net.sourceforge.pmd.eclipse; import net.sourceforge.pmd.cpd.CPD; - import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceVisitor; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.dialogs.MessageDialog; -import java.io.File; - /** + * A visitor to process IFile resource against CPD + * * @author David Craine + * @author Philippe Herlin + * @version $Revision$ + * + * $Log$ + * Revision 1.2 2003/03/17 23:32:05 phherlin + * minor cleaning * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>Templates. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generation. */ public class CPDVisitor implements IResourceVisitor { - CPD cpd; - - /** - * Constructor for CPDVisitor. - */ - public CPDVisitor(CPD cpd) { - super(); - this.cpd = cpd; - } + CPD cpd; - /** - * @see org.eclipse.core.resources.IResourceVisitor#visit(IResource) - * Add java files into the CPD object - */ - public boolean visit(IResource resource) throws CoreException { - if ((resource instanceof IFile) && - (((IFile) resource).getFileExtension() != null) && - ((IFile) resource).getFileExtension().equals("java")) { - try { - cpd.add(((IFile)resource).getLocation().toFile()); - } - catch (Exception e){ - MessageDialog.openError(null, "CPD", e.toString()); - } - return false; - } - else { - return true; - } + /** + * Constructor for CPDVisitor. + */ + public CPDVisitor(CPD cpd) { + super(); + this.cpd = cpd; + } - } + /** + * @see org.eclipse.core.resources.IResourceVisitor#visit(IResource) + * Add java files into the CPD object + */ + public boolean visit(IResource resource) throws CoreException { + boolean result = true; + if ((resource instanceof IFile) + && (((IFile) resource).getFileExtension() != null) + && ((IFile) resource).getFileExtension().equals("java")) { + try { + cpd.add(((IFile) resource).getLocation().toFile()); + } catch (Exception e) { + MessageDialog.openError(null, "CPD", e.toString()); + } + result = false; + } + + return result; + + } } diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/CPDCheckProjectAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/CPDCheckProjectAction.java index b84eaedbda..81debf3ab9 100644 --- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/CPDCheckProjectAction.java +++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/CPDCheckProjectAction.java @@ -4,6 +4,14 @@ import java.io.File; import java.lang.reflect.InvocationTargetException; import java.util.Iterator; +import net.sourceforge.pmd.cpd.CPD; +import net.sourceforge.pmd.cpd.CPDListener; +import net.sourceforge.pmd.cpd.Results; +import net.sourceforge.pmd.cpd.Tile; +import net.sourceforge.pmd.cpd.TokenEntry; +import net.sourceforge.pmd.eclipse.CPDReportWindow; +import net.sourceforge.pmd.eclipse.CPDVisitor; +import net.sourceforge.pmd.eclipse.PMDPlugin; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.action.IAction; @@ -13,183 +21,174 @@ import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TreeViewer; -import org.eclipse.jface.window.WindowManager; -import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; -import net.sourceforge.pmd.cpd.*; -import net.sourceforge.pmd.eclipse.CPDReportWindow; -import net.sourceforge.pmd.eclipse.CPDVisitor; -import net.sourceforge.pmd.eclipse.PMDPlugin; /** + * Process CPD action menu. Run CPD against the selected project. + * * @author David Craine + * @author Philippe Herlin + * @version $Revision$ + * + * $Log$ + * Revision 1.3 2003/03/17 23:31:26 phherlin + * minor cleaning * - * To change this generated comment edit the template variable "typecomment": - * Window>Preferences>Java>Templates. - * To enable and disable the creation of type comments go to - * Window>Preferences>Java>Code Generation. */ -public class CPDCheckProjectAction - implements IObjectActionDelegate, IRunnableWithProgress { - IWorkbenchPart targetPart; +public class CPDCheckProjectAction implements IObjectActionDelegate, IRunnableWithProgress { + IWorkbenchPart targetPart; - /** - * Constructor for CPDCheckProjectAction. - */ - public CPDCheckProjectAction() { - super(); - } + /** + * Constructor for CPDCheckProjectAction. + */ + public CPDCheckProjectAction() { + super(); + } - /** - * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) - */ - public void setActivePart(IAction action, IWorkbenchPart targetPart) { - this.targetPart = targetPart; - } + /** + * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) + */ + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + this.targetPart = targetPart; + } - public void run(IProgressMonitor monitor) - throws InvocationTargetException, InterruptedException { - try { - CPD cpd = new CPD(); - //cpd.setListener(new CPDActivityListener(monitor)); //for some reason CPD dies when using this listener - PMDPlugin.getDefault().getPreferenceStore().setDefault( - PMDPlugin.MIN_TILE_SIZE_PREFERENCE, - 25); - cpd.setMinimumTileSize( - PMDPlugin.getDefault().getPreferenceStore().getInt( - PMDPlugin.MIN_TILE_SIZE_PREFERENCE)); - CPDVisitor visitor = new CPDVisitor(cpd); - Object sel = targetPart.getSite().getSelectionProvider(); - CPDReportWindow crw = - new CPDReportWindow(targetPart.getSite().getShell()); - crw.create(); + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + try { + CPD cpd = new CPD(); + //cpd.setListener(new CPDActivityListener(monitor)); //for some reason CPD dies when using this listener + PMDPlugin.getDefault().getPreferenceStore().setDefault(PMDPlugin.MIN_TILE_SIZE_PREFERENCE, 25); + cpd.setMinimumTileSize(PMDPlugin.getDefault().getPreferenceStore().getInt(PMDPlugin.MIN_TILE_SIZE_PREFERENCE)); + CPDVisitor visitor = new CPDVisitor(cpd); + Object sel = targetPart.getSite().getSelectionProvider(); + CPDReportWindow crw = new CPDReportWindow(targetPart.getSite().getShell()); + crw.create(); - if (sel instanceof TreeViewer) { - TreeViewer tv = (TreeViewer) sel; - ISelection sel2 = tv.getSelection(); - if (sel2 instanceof StructuredSelection) { - monitor.beginTask("Searhing for files...", IProgressMonitor.UNKNOWN); - StructuredSelection ss = (StructuredSelection) sel2; - for (Iterator iter = ss.iterator(); iter.hasNext();) { - Object obj = iter.next(); - if (obj instanceof IProject) { - ((IProject) obj).accept(visitor); - } - } - monitor.beginTask("Running Cut & Paste Detector...", IProgressMonitor.UNKNOWN); - cpd.go(); - monitor.beginTask("Building result set...", IProgressMonitor.UNKNOWN); - Results results = cpd.getResults(); - for (Iterator iter = results.getTiles(); iter.hasNext();) { - Tile tile = (Tile) iter.next(); - int dups = results.getOccurrenceCountFor(tile); - crw.addEntry(String.valueOf(dups) + " duplicates found.\n"); - for (Iterator iter2 = results.getOccurrences(tile); - iter2.hasNext(); - ) { - TokenEntry te = (TokenEntry) iter2.next(); - crw.addEntry("\t"+te.getTokenSrcID()+": "+te.getBeginLine()+"\n"); - } - crw.addEntry("\n"); - } - monitor.done(); - crw.open(); - crw.getShell().setSize(500, 500); - } - } - } catch (Exception e) { - MessageDialog.openError(null, "Error running CPD", e.toString()); - monitor.done(); - } + if (sel instanceof TreeViewer) { + TreeViewer tv = (TreeViewer) sel; + ISelection sel2 = tv.getSelection(); + if (sel2 instanceof StructuredSelection) { + monitor.beginTask("Searhing for files...", IProgressMonitor.UNKNOWN); + StructuredSelection ss = (StructuredSelection) sel2; + for (Iterator iter = ss.iterator(); iter.hasNext();) { + Object obj = iter.next(); + if (obj instanceof IProject) { + ((IProject) obj).accept(visitor); + } + } + monitor.beginTask("Running Cut & Paste Detector...", IProgressMonitor.UNKNOWN); + cpd.go(); + monitor.beginTask("Building result set...", IProgressMonitor.UNKNOWN); + Results results = cpd.getResults(); + for (Iterator iter = results.getTiles(); iter.hasNext();) { + Tile tile = (Tile) iter.next(); + int dups = results.getOccurrenceCountFor(tile); + crw.addEntry(String.valueOf(dups) + " duplicates found.\n"); + for (Iterator iter2 = results.getOccurrences(tile); iter2.hasNext();) { + TokenEntry te = (TokenEntry) iter2.next(); + crw.addEntry("\t" + te.getTokenSrcID() + ": " + te.getBeginLine() + "\n"); + } + crw.addEntry("\n"); + } + monitor.done(); + crw.open(); + crw.getShell().setSize(500, 500); + } + } + } catch (Exception e) { + MessageDialog.openError(null, "Error running CPD", e.toString()); + monitor.done(); + } - } + } - /** - * @see org.eclipse.ui.IActionDelegate#run(IAction) - * Find all the selected projects and add their respsective java files into the - * CPD utility via the CPDVisitor. Then run the CPD utility and display - * the results. - */ - public void run(IAction action) { - try { - new ProgressMonitorDialog(targetPart.getSite().getShell()).run( - false, - false, - this); - } catch (Exception e) { - } - } + /** + * @see org.eclipse.ui.IActionDelegate#run(IAction) + * Find all the selected projects and add their respsective java files into the + * CPD utility via the CPDVisitor. Then run the CPD utility and display + * the results. + */ + public void run(IAction action) { + try { + new ProgressMonitorDialog(targetPart.getSite().getShell()).run(false, false, this); + } catch (InvocationTargetException e) { + PMDPlugin.getDefault().logError("Error while executing CPD", e); + } catch (InterruptedException e) { + PMDPlugin.getDefault().logError("CPD interrupted", e); + } + } - /** - * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection) - */ - public void selectionChanged(IAction action, ISelection selection) { - } + /** + * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection) + */ + public void selectionChanged(IAction action, ISelection selection) { + } - class CPDActivityListener implements CPDListener { - IProgressMonitor monitor; - private boolean firstFile = true; - private boolean firstToken = true; - private boolean firstTile = true; - private int tokenTracker = 0; - private static final int TOKEN_LIMIT = 100; + class CPDActivityListener implements CPDListener { + IProgressMonitor monitor; + private boolean firstFile = true; + private boolean firstToken = true; + private boolean firstTile = true; + private int tokenTracker = 0; + private static final int TOKEN_LIMIT = 100; - public CPDActivityListener(IProgressMonitor monitor) { - this.monitor = monitor; - } + public CPDActivityListener(IProgressMonitor monitor) { + this.monitor = monitor; + } - /** - * @see net.sourceforge.pmd.cpd.CPDListener#addedFile(int, File) - */ - public boolean addedFile(int fileCount, File file) { - if (firstFile) { - monitor.beginTask("Adding Files", fileCount); - firstFile = false; - } - if (file != null) - monitor.subTask(file.getName()); - monitor.worked(1); - return (file!=null); - } + /** + * @see net.sourceforge.pmd.cpd.CPDListener#addedFile(int, File) + */ + public boolean addedFile(int fileCount, File file) { + if (firstFile) { + monitor.beginTask("Adding Files", fileCount); + firstFile = false; + } + if (file != null) { + monitor.subTask(file.getName()); + } + monitor.worked(1); + return (file != null); + } - /** - * @see net.sourceforge.pmd.cpd.CPDListener#addingTokens(int, int, String) - */ - public boolean addingTokens(int tokenSetCount, int doneSoFar,String tokenSrcId) { - if (firstToken) { - monitor.beginTask("Adding Tokens", tokenSetCount); - firstToken = false; - } - if (tokenSrcId != null && tokenTracker++ == TOKEN_LIMIT) { - monitor.subTask(tokenSrcId); - tokenTracker = 0; - monitor.worked(TOKEN_LIMIT); - } - return true; + /** + * @see net.sourceforge.pmd.cpd.CPDListener#addingTokens(int, int, String) + */ + public boolean addingTokens(int tokenSetCount, int doneSoFar, String tokenSrcId) { + if (firstToken) { + monitor.beginTask("Adding Tokens", tokenSetCount); + firstToken = false; + } + if (tokenSrcId != null && tokenTracker++ == TOKEN_LIMIT) { + monitor.subTask(tokenSrcId); + tokenTracker = 0; + monitor.worked(TOKEN_LIMIT); + } + return true; - } + } - /** - * @see net.sourceforge.pmd.cpd.CPDListener#update(String) - */ - public boolean update(String arg0) { - return true; - } + /** + * @see net.sourceforge.pmd.cpd.CPDListener#update(String) + */ + public boolean update(String arg0) { + return true; + } - /** - * @see net.sourceforge.pmd.cpd.CPDListener#addedNewTile(Tile, int, int) - */ - public boolean addedNewTile(Tile tile, int tilesSoFar, int totalTiles) { - if (firstTile) { - monitor.beginTask("Adding Tiles", totalTiles); - firstTile = false; - } - if (tile != null) - monitor.subTask(tile.getImage()); - monitor.worked(1); - return true; - } + /** + * @see net.sourceforge.pmd.cpd.CPDListener#addedNewTile(Tile, int, int) + */ + public boolean addedNewTile(Tile tile, int tilesSoFar, int totalTiles) { + if (firstTile) { + monitor.beginTask("Adding Tiles", totalTiles); + firstTile = false; + } + if (tile != null) { + monitor.subTask(tile.getImage()); + } + monitor.worked(1); + return true; + } - } + } }