001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.DetailNode; 026import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 027 028/** 029 * Presentation model for CodeSelector. 030 */ 031public class CodeSelectorPresentation { 032 033 /** DetailAST or DetailNode node. */ 034 private final Object node; 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 * @noinspection AssignmentOrReturnOfFieldWithMutableType 047 */ 048 public CodeSelectorPresentation(DetailAST ast, List<Integer> lines2position) { 049 node = ast; 050 this.lines2position = lines2position; 051 } 052 053 /** 054 * Constructor. 055 * @param node DetailNode node. 056 * @param lines2position list to map lines. 057 * @noinspection AssignmentOrReturnOfFieldWithMutableType 058 */ 059 public CodeSelectorPresentation(DetailNode node, List<Integer> lines2position) { 060 this.node = node; 061 this.lines2position = lines2position; 062 } 063 064 /** 065 * Returns selection start position. 066 * @return selection start position. 067 */ 068 public int getSelectionStart() { 069 return selectionStart; 070 } 071 072 /** 073 * Returns selection end position. 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 && TokenUtil.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 155}