001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.gui; 021 022import java.util.List; 023 024import com.google.common.collect.ImmutableList; 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.utils.TokenUtils; 027 028/** 029 * Presentation model for CodeSelector. 030 * @author unknown 031 */ 032public class CodeSelectorPModel { 033 /** DetailAST node. */ 034 private final DetailAST ast; 035 /** Mapping. */ 036 private final List<Integer> lines2position; 037 /** Selection start position. */ 038 private int selectionStart; 039 /** Selection end position. */ 040 private int selectionEnd; 041 042 /** 043 * Constructor. 044 * @param ast ast node. 045 * @param lines2position list to map lines. 046 */ 047 public CodeSelectorPModel(DetailAST ast, List<Integer> lines2position) { 048 this.ast = ast; 049 this.lines2position = ImmutableList.copyOf(lines2position); 050 } 051 052 /** 053 * @return selection start position. 054 */ 055 public int getSelectionStart() { 056 return selectionStart; 057 } 058 059 /** 060 * @return selection end position. 061 */ 062 public int getSelectionEnd() { 063 return selectionEnd; 064 } 065 066 /** 067 * Find start and end selection positions from AST line and Column. 068 */ 069 public void findSelectionPositions() { 070 selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo(); 071 072 if (ast.getChildCount() == 0 073 && TokenUtils.getTokenName(ast.getType()).equals(ast.getText())) { 074 selectionEnd = selectionStart; 075 } 076 else { 077 selectionEnd = findLastPosition(ast); 078 } 079 } 080 081 /** 082 * Finds the last position of node without children. 083 * @param astNode DetailAST node. 084 * @return Last position of node without children. 085 */ 086 private int findLastPosition(final DetailAST astNode) { 087 if (astNode.getChildCount() == 0) { 088 return lines2position.get(astNode.getLineNo()) + astNode.getColumnNo() 089 + astNode.getText().length(); 090 } 091 else { 092 return findLastPosition(astNode.getLastChild()); 093 } 094 } 095}