1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2019 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.List; 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 * @param node ast node. 44 * @param editor text area editor. 45 * @param lines2position list to map lines. 46 */ 47 public CodeSelector(final Object node, final JTextArea editor, 48 final List<Integer> lines2position) { 49 this.editor = editor; 50 if (node instanceof DetailAST) { 51 pModel = new CodeSelectorPresentation((DetailAST) node, 52 new ArrayList<>(lines2position)); 53 } 54 else { 55 pModel = new CodeSelectorPresentation((DetailNode) node, 56 new ArrayList<>(lines2position)); 57 } 58 } 59 60 /** 61 * Set selection. 62 */ 63 public void select() { 64 pModel.findSelectionPositions(); 65 editor.setSelectedTextColor(Color.blue); 66 editor.requestFocusInWindow(); 67 editor.setCaretPosition(pModel.getSelectionStart()); 68 editor.moveCaretPosition(pModel.getSelectionEnd()); 69 } 70 71 }