Patch# 3199838: selecting a node in the GUI moves the code pane to the corresponding line. Very nice
This commit is contained in:
parent
2e3654c59b
commit
b3e7e97d30
|
|
@ -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<Integer> lines2position;
|
||||
|
||||
public CodeSelector(final DetailAST ast, final JTextArea editor,
|
||||
final List<Integer> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Integer> 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<Integer> lines2position)
|
||||
{
|
||||
this.lines2position = lines2position;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Integer> lines2position = new ArrayList<Integer>();
|
||||
|
||||
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<Integer> getLines2position()
|
||||
{
|
||||
return lines2position;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue