001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.hasChildren() || !TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) {
100            selectionEnd = findLastPosition(ast);
101        }
102        else {
103            selectionEnd = selectionStart;
104        }
105    }
106
107    /**
108     * Find start and end selection positions from DetailNode line and Column.
109     * @param detailNode DetailNode node for which selection finds
110     */
111    private void findSelectionPositions(DetailNode detailNode) {
112        selectionStart = lines2position.get(detailNode.getLineNumber())
113                            + detailNode.getColumnNumber();
114
115        selectionEnd = findLastPosition(detailNode);
116    }
117
118    /**
119     * Finds the last position of node without children.
120     * @param astNode DetailAST node.
121     * @return Last position of node without children.
122     */
123    private int findLastPosition(final DetailAST astNode) {
124        final int lastPosition;
125        if (astNode.hasChildren()) {
126            lastPosition = findLastPosition(astNode.getLastChild());
127        }
128        else {
129            lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
130                    + astNode.getText().length();
131        }
132        return lastPosition;
133    }
134
135    /**
136     * Finds the last position of node without children.
137     * @param detailNode DetailNode node.
138     * @return Last position of node without children.
139     */
140    private int findLastPosition(final DetailNode detailNode) {
141        final int lastPosition;
142        if (detailNode.getChildren().length == 0) {
143            lastPosition = lines2position.get(detailNode.getLineNumber())
144                    + detailNode.getColumnNumber() + detailNode.getText().length();
145        }
146        else {
147            final DetailNode lastChild =
148                    detailNode.getChildren()[detailNode.getChildren().length - 1];
149            lastPosition = findLastPosition(lastChild);
150        }
151        return lastPosition;
152    }
153
154}