1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.awt.Color;
23 import java.util.ArrayList;
24 import java.util.Collection;
25
26 import javax.swing.JTextArea;
27
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.DetailNode;
30
31 /**
32 * Helper class to select a code.
33 */
34 public class CodeSelector {
35
36 /** Editor. */
37 private final JTextArea editor;
38 /** Presentation model. */
39 private final CodeSelectorPresentation pModel;
40
41 /**
42 * Constructor.
43 *
44 * @param node ast node.
45 * @param editor text area editor.
46 * @param lines2position positions of lines.
47 */
48 public CodeSelector(final Object node, final JTextArea editor,
49 final Collection<Integer> lines2position) {
50 this.editor = editor;
51 if (node instanceof DetailAST detailAst) {
52 pModel = new CodeSelectorPresentation(detailAst,
53 new ArrayList<>(lines2position));
54 }
55 else {
56 pModel = new CodeSelectorPresentation((DetailNode) node,
57 new ArrayList<>(lines2position));
58 }
59 }
60
61 /**
62 * Set selection.
63 */
64 public void select() {
65 pModel.findSelectionPositions();
66 editor.setSelectedTextColor(Color.blue);
67 editor.requestFocusInWindow();
68 editor.setCaretPosition(pModel.getSelectionStart());
69 editor.moveCaretPosition(pModel.getSelectionEnd());
70 }
71
72 }