From b3e7e97d30769de05a61c539db3b244fe1e342f4 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Fri, 20 Apr 2012 20:22:12 +1000 Subject: [PATCH] Patch# 3199838: selecting a node in the GUI moves the code pane to the corresponding line. Very nice --- .../tools/checkstyle/gui/CodeSelector.java | 45 +++++++++++++++++++ .../tools/checkstyle/gui/JTreeTable.java | 21 +++++++++ .../checkstyle/gui/ParseTreeInfoPanel.java | 21 +++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/gui/CodeSelector.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/CodeSelector.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/CodeSelector.java new file mode 100644 index 000000000..c1ed6632e --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/CodeSelector.java @@ -0,0 +1,45 @@ +package com.puppycrawl.tools.checkstyle.gui; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import java.awt.Color; +import java.util.List; +import javax.swing.JTextArea; + +public class CodeSelector +{ + private final DetailAST ast; + private final JTextArea editor; + private final List lines2position; + + public CodeSelector(final DetailAST ast, final JTextArea editor, + final List lines2position) + { + this.ast = ast; + this.editor = editor; + this.lines2position = lines2position; + } + + public void select() { + int start = lines2position.get(ast.getLineNo()) + ast.getColumnNo(); + int end = findLastPosition(ast); + + editor.setSelectedTextColor(Color.blue); + editor.requestFocusInWindow(); + editor.setSelectionStart(start); + editor.setSelectionEnd(end); + editor.transferFocusBackward(); + } + + private int findLastPosition(final DetailAST ast) + { + if (ast.getChildCount() == 0) + { + return lines2position.get(ast.getLineNo()) + ast.getColumnNo() + + ast.getText().length(); + } + else + { + return findLastPosition(ast.getLastChild()); + } + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/JTreeTable.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/JTreeTable.java index c9e4d1aeb..e68f17508 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/JTreeTable.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/JTreeTable.java @@ -65,9 +65,12 @@ import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.util.EventObject; +import java.util.List; + import javax.swing.Action; import javax.swing.AbstractAction; import javax.swing.JTable; +import javax.swing.JTextArea; import javax.swing.JTree; import javax.swing.KeyStroke; import javax.swing.ListSelectionModel; @@ -83,6 +86,8 @@ import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; +import com.puppycrawl.tools.checkstyle.api.DetailAST; + /** * This example shows how to create a simple JTreeTable component, * by using a JTree as a renderer (and editor) for the cells in a @@ -100,6 +105,8 @@ public class JTreeTable extends JTable private static final long serialVersionUID = -8493693409423365387L; /** A subclass of JTree. */ protected TreeTableCellRenderer tree; + private JTextArea editor; + private List lines2position; public JTreeTable(TreeTableModel treeTableModel) { @@ -142,6 +149,10 @@ public class JTreeTable extends JTable public void actionPerformed(ActionEvent e) { final TreePath selected = tree.getSelectionPath(); + + DetailAST ast = (DetailAST) selected.getLastPathComponent(); + new CodeSelector(ast, editor, lines2position).select(); + if (tree.isExpanded(selected)) { tree.collapsePath(selected); } @@ -480,4 +491,14 @@ public class JTreeTable extends JTable } } } + + public void setEditor(JTextArea mJTextArea) + { + this.editor = mJTextArea; + } + + public void setLinePositionMap(List lines2position) + { + this.lines2position = lines2position; + } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java index 67304e4a8..4d743c055 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java @@ -26,6 +26,8 @@ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.TooManyListenersException; import javax.swing.AbstractAction; import javax.swing.Action; @@ -60,6 +62,7 @@ public class ParseTreeInfoPanel extends JPanel private File mLastDirectory = null; private File mCurrentFile = null; private final Action reloadAction; + private final List lines2position = new ArrayList(); private static class JavaFileFilter extends FileFilter { @@ -162,6 +165,18 @@ public class ParseTreeInfoPanel extends JPanel reloadAction.setEnabled(true); final String[] sourceLines = text.toLinesArray(); + + // clear for each new file + getLines2position().clear(); + // starts line counting at 1 + getLines2position().add(0); + // insert the contents of the file to the text area + for (String element : sourceLines) + { + getLines2position().add(mJTextArea.getText().length()); + mJTextArea.append(element + "\n"); + } + //clean the text area before inserting the lines of the new file if (mJTextArea.getText().length() != 0) { mJTextArea.replaceRange("", 0, mJTextArea.getText() @@ -251,6 +266,8 @@ public class ParseTreeInfoPanel extends JPanel mJTextArea = new JTextArea(20, 15); mJTextArea.setEditable(false); + mTreeTable.setEditor(mJTextArea); + mTreeTable.setLinePositionMap(lines2position); final JScrollPane sp2 = new JScrollPane(mJTextArea); this.add(sp2, BorderLayout.CENTER); @@ -284,5 +301,9 @@ public class ParseTreeInfoPanel extends JPanel SwingUtilities.invokeLater(showError); } + public List getLines2position() + { + return lines2position; + } }