From 8b4423923796452778892225f21a1b85b0ff9be6 Mon Sep 17 00:00:00 2001 From: Jiger Patel Date: Wed, 16 Apr 2003 06:19:01 +0000 Subject: [PATCH] CPD Duplicate Code Viewer Control for jEdit git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1754 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/jedit/CPDDuplicateCodeViewer.java | 197 ++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 pmd-jedit/src/net/sourceforge/pmd/jedit/CPDDuplicateCodeViewer.java diff --git a/pmd-jedit/src/net/sourceforge/pmd/jedit/CPDDuplicateCodeViewer.java b/pmd-jedit/src/net/sourceforge/pmd/jedit/CPDDuplicateCodeViewer.java new file mode 100644 index 0000000000..a252a7e9cf --- /dev/null +++ b/pmd-jedit/src/net/sourceforge/pmd/jedit/CPDDuplicateCodeViewer.java @@ -0,0 +1,197 @@ +package net.sourceforge.pmd.jedit; + +//Imports +import java.util.*; +import javax.swing.JPanel; +import javax.swing.JTree; +import javax.swing.tree.*; +import javax.swing.event.TreeSelectionListener; +import javax.swing.event.TreeSelectionEvent; +import org.gjt.sp.jedit.jEdit; +import org.gjt.sp.jedit.Buffer; +import org.gjt.sp.jedit.textarea.Selection; +import java.awt.BorderLayout; +import javax.swing.JScrollPane; +import org.gjt.sp.jedit.View; +import org.gjt.sp.jedit.io.VFSManager; +import java.awt.Component; +import javax.swing.JLabel; +import javax.swing.JComponent; +//End of Imports + +/** + * A GUI Component to display Duplicate code. + * + * @created 05 Apr 2003 + * @author Jiger Patel + * + */ + +public class CPDDuplicateCodeViewer extends JPanel +{ + JTree tree; + DefaultTreeModel treeModel = new DefaultTreeModel(new DefaultMutableTreeNode("CPD Results",true)); + View view; + + public CPDDuplicateCodeViewer(View view) + { + this.view = view; + setLayout(new BorderLayout()); + tree = new JTree(treeModel); + tree.getSelectionModel().setSelectionMode + (TreeSelectionModel.SINGLE_TREE_SELECTION); + tree.addTreeSelectionListener(new TreeSelectionListener() + { + public void valueChanged(TreeSelectionEvent e) + { + DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); + if (node == null) return; + + System.out.println("Node is " + node +" class "+ node.getClass()); + if (node.isLeaf() && node instanceof Duplicate) + { + Duplicate duplicate = (Duplicate)node; + gotoDuplicate(duplicate); + System.out.println("Got!! " + duplicate); + } + else + { + System.out.println("Iila something else. Please check "); + } + } + }); + + + add(new JScrollPane(tree)); + + }//End of CPDDuplicateCodeViewer constructor + + + public void refreshTree() + { + treeModel.reload(); + } + + public void gotoDuplicate(final Duplicate duplicate) + { + if(duplicate == null) + { + return; + } + + final Buffer buffer = jEdit.openFile(view,duplicate.getFilename()); + + VFSManager.runInAWTThread(new Runnable() + { + public void run() + { + view.setBuffer(buffer); + int start = buffer.getLineStartOffset(duplicate.getBeginLine()-1); + int end = buffer.getLineEndOffset(duplicate.getEndLine()-1); + view.getTextArea().setSelection(new Selection.Range(start,end)); + view.getTextArea().moveCaretPosition(start); + } + }); + + + } + + public DefaultMutableTreeNode getRoot() + { + return (DefaultMutableTreeNode)treeModel.getRoot(); + } + + public void addDuplicates(Duplicates duplicates) + { + System.out.println("Inside addDuplicates " + duplicates +" Root child count "+ treeModel.getChildCount(treeModel.getRoot())); + getRoot().add(duplicates); + //vecDuplicates.addElement(duplicates); + } + + public class Duplicates extends DefaultMutableTreeNode + { + Vector vecduplicate = new Vector(); + String message, sourcecode; + + public Duplicates(String message, String sourcecode) + { + this.message = message; + this.sourcecode = sourcecode; + } + + public String getSourceCode() + { + return sourcecode; + } + + public void addDuplicate(Duplicate duplicate) + { + add(duplicate); + //vecduplicate.addElement(duplicate); + } + + public String toString() + { + return message; + } + } + + public class Duplicate extends DefaultMutableTreeNode + { + private String filename; + private int beginLine, endLine; + + public Duplicate(String filename,int beginLine, int endLine) + { + this.filename = filename; + this.beginLine = beginLine; + this.endLine = endLine; + } + + public String getFilename() + { + return filename; + } + + public int getBeginLine() + { + return beginLine; + } + + public int getEndLine() + { + return endLine; + } + + public String toString() + { + return filename + ":"+ getBeginLine()+"-"+getEndLine(); + } + } + + public void expandAll() + { + int row = 0; + while (row < tree.getRowCount()) + { + tree.expandRow(row); + row++; + } + } + + public void collapseAll() + { + int row = tree.getRowCount() - 1; + while (row >= 0) { + tree.collapseRow(row); + row--; + } + } + + public void clearDuplicates() + { + getRoot().removeAllChildren(); + } + +}//End of class CPDDuplicateCodeViewer +