View Javadoc
1   package com.puppycrawl.tools.checkstyle.gui;
2   
3   import com.puppycrawl.tools.checkstyle.api.DetailAST;
4   import java.awt.Color;
5   import java.util.List;
6   import javax.swing.JTextArea;
7   
8   public class CodeSelector
9   {
10      private final DetailAST ast;
11      private final JTextArea editor;
12      private final List<Integer> lines2position;
13  
14      public CodeSelector(final DetailAST ast, final JTextArea editor,
15                          final List<Integer> lines2position)
16      {
17          this.ast = ast;
18          this.editor = editor;
19          this.lines2position = lines2position;
20      }
21  
22      public void select() {
23          int start = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
24          int end = findLastPosition(ast);
25  
26          editor.setSelectedTextColor(Color.blue);
27          editor.requestFocusInWindow();
28          editor.setSelectionStart(start);
29          editor.setSelectionEnd(end);
30          editor.transferFocusBackward();
31      }
32  
33      private int findLastPosition(final DetailAST ast)
34      {
35          if (ast.getChildCount() == 0)
36          {
37              return lines2position.get(ast.getLineNo()) + ast.getColumnNo()
38                  + ast.getText().length();
39          }
40          else
41          {
42              return findLastPosition(ast.getLastChild());
43          }
44      }
45  }