A much improved (IMHO) CPD viewer that optimizes the display of the various matches.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7266 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2011-09-16 05:49:49 +00:00
parent de631fc97b
commit 11f5efc448
6 changed files with 759 additions and 6 deletions

View File

@ -33,4 +33,5 @@ view.violation = PMD Violations
view.outline = Violations Outline
view.overview = Violations Overview
view.dataflowview = Dataflow View
view.cpd = CPD View
view.cpd = CPD View
view.cpd2 = CPD View2

View File

@ -479,12 +479,20 @@
category="net.sourceforge.pmd.eclipse.ui.views"
name="%view.dataflowview"
id="net.sourceforge.pmd.eclipse.ui.views.dataflowView"/>
<!-- obsolete version
<view
allowMultiple="false"
category="net.sourceforge.pmd.eclipse.ui.views"
class="net.sourceforge.pmd.eclipse.ui.views.cpd.CPDView"
icon="icons/icon_cpd.gif"
id="net.sourceforge.pmd.eclipse.ui.views.CPDView"
name="%view.cpd"/> -->
<view
allowMultiple="false"
category="net.sourceforge.pmd.eclipse.ui.views"
class="net.sourceforge.pmd.eclipse.ui.views.cpd2.CPDView2"
icon="icons/icon_cpd.gif"
id="net.sourceforge.pmd.eclipse.ui.views.br.CPDView2"
name="%view.cpd"/>
<view
allowMultiple="false"

View File

@ -13,7 +13,7 @@ import net.sourceforge.pmd.eclipse.runtime.cmd.DetectCutAndPasteCmd;
import net.sourceforge.pmd.eclipse.ui.PMDUiConstants;
import net.sourceforge.pmd.eclipse.ui.dialogs.CPDCheckDialog;
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
import net.sourceforge.pmd.eclipse.ui.views.cpd.CPDView;
import net.sourceforge.pmd.eclipse.ui.views.cpd2.CPDView2;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IProject;
@ -105,7 +105,7 @@ public class CPDCheckProjectAction extends AbstractUIAction {
final boolean createReport = dialog.isCreateReportSelected();
final Renderer selectedRenderer = this.createRenderer(dialog.getSelectedFormat());
final String fileName = this.createFileName(dialog.getSelectedFormat());
final CPDView view = showView();
final CPDView2 view = showView();
try {
final DetectCutAndPasteCmd detectCmd = new DetectCutAndPasteCmd();
@ -127,11 +127,11 @@ public class CPDCheckProjectAction extends AbstractUIAction {
* Shows the view.
* @param matches
*/
private CPDView showView() {
CPDView view = null;
private CPDView2 showView() {
CPDView2 view = null;
try {
final IWorkbenchPage workbenchPage = targetPartSite().getPage();
view = (CPDView) workbenchPage.showView(PMDUiConstants.ID_CPDVIEW);
view = (CPDView2) workbenchPage.showView(PMDUiConstants.ID_CPDVIEW2);
} catch (PartInitException pie) {
logError( getString(StringKeys.ERROR_VIEW_EXCEPTION), pie);
}

View File

@ -0,0 +1,181 @@
/*
* Created on 13.10.2006
*
* Copyright (c) 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.ui.views.cpd2;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import net.sourceforge.pmd.cpd.Match;
import net.sourceforge.pmd.cpd.TokenEntry;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE.SharedImages;
/**
*
*
* @author Brian Remedios
*/
public class CPDViewLabelProvider2 extends LabelProvider implements ITableLabelProvider {
/*
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
*/
public Image getColumnImage(Object element, int columnIndex) {
Image image = null;
final TreeNode node = (TreeNode) element;
final Object value = node.getValue();
// the second Column gets an Image depending on,
// if the Element is a Match or TokenEntry
switch (columnIndex) {
case 0:
if (value instanceof Match) {
// image = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
} else if (value instanceof TokenEntry) {
image = PlatformUI.getWorkbench().getSharedImages().getImage(SharedImages.IMG_OPEN_MARKER);
}
break;
default:
// let the image null.
}
return image;
}
private int lineCountFor(TreeNode node) {
Object source = node.getValue();
if (source instanceof Match) {
return node.getChildren().length;
}
return -1;
}
public static String pathFor(TokenEntry entry) {
final IPath path = Path.fromOSString(entry.getTokenSrcID());
final IResource resource = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(path);
if (resource != null) {
return resource.getProjectRelativePath().removeFileExtension().toString().replace(IPath.SEPARATOR, '.');
} else {
return "?";
}
}
public static TokenEntry[] entriesFor(Match match) {
Set<TokenEntry> entrySet = match.getMarkSet();
TokenEntry[] entries = new TokenEntry[entrySet.size()];
entries = entrySet.toArray(entries);
Arrays.sort(entries);
return entries;
}
public static String[] sourcesFor(Match match) {
TokenEntry[] entries = entriesFor(match);
String[] classNames = new String[entries.length];
int i = 0;
for (TokenEntry entry : entries) {
classNames[i++] = pathFor(entry);
}
return classNames;
}
public static Map<String, TokenEntry> entriesByClassnameFor(Match match) {
TokenEntry[] entries = entriesFor(match);
Map<String, TokenEntry> entriesByName = new HashMap<String, TokenEntry>(entries.length);
for (TokenEntry entry : entries) {
entriesByName.put( pathFor(entry), entry);
}
return entriesByName;
}
/*
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
*/
public String getColumnText(Object element, int columnIndex) {
final TreeNode node = (TreeNode) element;
final Object value = node.getValue();
String result = "";
switch (columnIndex) {
case 0: int count = lineCountFor(node);
if (count > 0) result = Integer.toString(count);
break;
// show the source
case 1:
if (value instanceof String) {
result = String.valueOf(value);
if (result.endsWith("\r")) result = result.substring(0, result.length()-1);
}
if (value instanceof Match) {
// do nothing, let the painter show it
}
break;
default:
// let text empty
}
return result;
}
}

View File

@ -0,0 +1,192 @@
/*
* Created on 14.10.2006
*
* Copyright (c) 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.ui.views.cpd2;
import net.sourceforge.pmd.cpd.Match;
import net.sourceforge.pmd.cpd.TokenEntry;
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.ITextEditor;
/**
*
*
*/
public class CPDViewTooltipListener2 implements Listener {
private final CPDView2 view;
private Cursor normalCursor;
private Cursor handCursor;
public CPDViewTooltipListener2(CPDView2 view) {
this.view = view;
initialize();
}
private void initialize() {
Display disp = Display.getCurrent();
normalCursor = disp.getSystemCursor(SWT.CURSOR_ARROW);
handCursor = disp.getSystemCursor(SWT.CURSOR_HAND);
}
// open file and jump to the startline
private void highlight(Match match, TokenEntry entry) {
IPath path = Path.fromOSString(entry.getTokenSrcID());
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
if (file == null) return;
try {
// open editor
IWorkbenchPage page = view.getSite().getPage();
IEditorPart part = IDE.openEditor(page, file);
if (part instanceof ITextEditor) {
// select text
ITextEditor textEditor = (ITextEditor) part;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
int offset = document.getLineOffset(entry.getBeginLine()-1);
int length = document.getLineOffset(entry.getBeginLine()-1 + match.getLineCount()) - offset -1;
textEditor.selectAndReveal(offset, length);
}
} catch (PartInitException pie) {
PMDPlugin.getDefault().logError(getString(StringKeys.ERROR_VIEW_EXCEPTION), pie);
} catch (BadLocationException ble) {
PMDPlugin.getDefault().logError(getString(StringKeys.ERROR_VIEW_EXCEPTION), ble);
}
}
private static Match matchAt(TreeItem treeItem) {
Object item = ((TreeNode) treeItem.getData()).getValue();
return item instanceof Match ? (Match)item : null;
}
private TokenEntry itemAt(TreeItem treeItem, Point location, GC gc) {
if (treeItem == null) return null;
Object item = ((TreeNode) treeItem.getData()).getValue();
String[] names = null;
if (item instanceof Match) {
names = CPDViewLabelProvider2.sourcesFor((Match) item);
} else {
return null;
}
location.x -= view.widthOf(0); // subtract width of preceeding columns
int colWidth = view.widthOf(CPDView2.SourceColumnIdx);
int cellWidth = colWidth / names.length;
for (int i=0; i<names.length; i++) {
int rightEdge = colWidth - (cellWidth * i);
int[] widths = view.widthsFor(names[i]);
int classWidth = widths[1];
if (location.x > rightEdge-classWidth && // right of the start?
location.x < rightEdge) { // left of the end?
return CPDViewLabelProvider2.entriesFor((Match)item)[i];
}
}
return null;
}
/*
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
Tree tree = view.getTreeViewer().getTree();
Point location = new Point(event.x, event.y);
Shell shell = tree.getShell();
if (view.inColumn(location) != CPDView2.SourceColumnIdx) {
shell.setCursor(normalCursor);
return;
}
TreeItem item = tree.getItem(location);
TokenEntry entry = itemAt(item, location, event.gc);
if (entry == null) {
shell.setCursor(normalCursor);
return;
}
switch (event.type) {
case SWT.MouseDown:
highlight(matchAt(item), entry);
break;
case SWT.MouseMove:
case SWT.MouseHover:
shell.setCursor(handCursor);
break;
default:
break;
}
}
/**
* Helper method to return an NLS string from its key
*/
private String getString(String key) {
return PMDPlugin.getDefault().getStringTable().getString(key);
}
}