001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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.ArrayList; 023import java.util.Collections; 024import java.util.List; 025 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.DetailNode; 028import com.puppycrawl.tools.checkstyle.utils.TokenUtils; 029 030/** 031 * Presentation model for CodeSelector. 032 * @author unknown 033 */ 034public class CodeSelectorPresentation { 035 /** DetailAST or DetailNode node. */ 036 private final Object node; 037 /** Mapping. */ 038 private final List<Integer> lines2position; 039 /** Selection start position. */ 040 private int selectionStart; 041 /** Selection end position. */ 042 private int selectionEnd; 043 044 /** 045 * Constructor. 046 * @param ast ast node. 047 * @param lines2position list to map lines. 048 */ 049 public CodeSelectorPresentation(DetailAST ast, List<Integer> lines2position) { 050 node = ast; 051 final List<Integer> copy = new ArrayList<>(lines2position); 052 this.lines2position = Collections.unmodifiableList(copy); 053 } 054 055 /** 056 * Constructor. 057 * @param node DetailNode node. 058 * @param lines2position list to map lines. 059 */ 060 public CodeSelectorPresentation(DetailNode node, List<Integer> lines2position) { 061 this.node = node; 062 final List<Integer> copy = new ArrayList<>(lines2position); 063 this.lines2position = Collections.unmodifiableList(copy); 064 } 065 066 /** 067 * @return selection start position. 068 */ 069 public int getSelectionStart() { 070 return selectionStart; 071 } 072 073 /** 074 * @return selection end position. 075 */ 076 public int getSelectionEnd() { 077 return selectionEnd; 078 } 079 080 /** 081 * Find start and end selection positions from AST line and Column. 082 */ 083 public void findSelectionPositions() { 084 if (node instanceof DetailAST) { 085 findSelectionPositions((DetailAST) node); 086 } 087 else { 088 findSelectionPositions((DetailNode) node); 089 } 090 } 091 092 /** 093 * Find start and end selection positions from AST line and Column. 094 * @param ast DetailAST node for which selection finds 095 */ 096 private void findSelectionPositions(DetailAST ast) { 097 selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo(); 098 099 if (ast.getChildCount() == 0 100 && TokenUtils.getTokenName(ast.getType()).equals(ast.getText())) { 101 selectionEnd = selectionStart; 102 } 103 else { 104 selectionEnd = findLastPosition(ast); 105 } 106 } 107 108 /** 109 * Find start and end selection positions from DetailNode line and Column. 110 * @param detailNode DetailNode node for which selection finds 111 */ 112 private void findSelectionPositions(DetailNode detailNode) { 113 selectionStart = lines2position.get(detailNode.getLineNumber()) 114 + detailNode.getColumnNumber(); 115 116 selectionEnd = findLastPosition(detailNode); 117 } 118 119 /** 120 * Finds the last position of node without children. 121 * @param astNode DetailAST node. 122 * @return Last position of node without children. 123 */ 124 private int findLastPosition(final DetailAST astNode) { 125 final int lastPosition; 126 if (astNode.getChildCount() == 0) { 127 lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo() 128 + astNode.getText().length(); 129 } 130 else { 131 lastPosition = findLastPosition(astNode.getLastChild()); 132 } 133 return lastPosition; 134 } 135 136 /** 137 * Finds the last position of node without children. 138 * @param detailNode DetailNode node. 139 * @return Last position of node without children. 140 */ 141 private int findLastPosition(final DetailNode detailNode) { 142 final int lastPosition; 143 if (detailNode.getChildren().length == 0) { 144 lastPosition = lines2position.get(detailNode.getLineNumber()) 145 + detailNode.getColumnNumber() + detailNode.getText().length(); 146 } 147 else { 148 final DetailNode lastChild = 149 detailNode.getChildren()[detailNode.getChildren().length - 1]; 150 lastPosition = findLastPosition(lastChild); 151 } 152 return lastPosition; 153 } 154}