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     *
045     * @param ast ast node.
046     * @param lines2position list to map lines.
047     * @noinspection AssignmentOrReturnOfFieldWithMutableType
048     */
049    public CodeSelectorPresentation(DetailAST ast, List<Integer> lines2position) {
050        node = ast;
051        this.lines2position = lines2position;
052    }
053
054    /**
055     * Constructor.
056     *
057     * @param node DetailNode node.
058     * @param lines2position list to map lines.
059     * @noinspection AssignmentOrReturnOfFieldWithMutableType
060     */
061    public CodeSelectorPresentation(DetailNode node, List<Integer> lines2position) {
062        this.node = node;
063        this.lines2position = lines2position;
064    }
065
066    /**
067     * Returns selection start position.
068     *
069     * @return selection start position.
070     */
071    public int getSelectionStart() {
072        return selectionStart;
073    }
074
075    /**
076     * Returns selection end position.
077     *
078     * @return selection end position.
079     */
080    public int getSelectionEnd() {
081        return selectionEnd;
082    }
083
084    /**
085     * Find start and end selection positions from AST line and Column.
086     */
087    public void findSelectionPositions() {
088        if (node instanceof DetailAST) {
089            findSelectionPositions((DetailAST) node);
090        }
091        else {
092            findSelectionPositions((DetailNode) node);
093        }
094    }
095
096    /**
097     * Find start and end selection positions from AST line and Column.
098     *
099     * @param ast DetailAST node for which selection finds
100     */
101    private void findSelectionPositions(DetailAST ast) {
102        selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
103
104        if (ast.hasChildren() || !TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) {
105            selectionEnd = findLastPosition(ast);
106        }
107        else {
108            selectionEnd = selectionStart;
109        }
110    }
111
112    /**
113     * Find start and end selection positions from DetailNode line and Column.
114     *
115     * @param detailNode DetailNode node for which selection finds
116     */
117    private void findSelectionPositions(DetailNode detailNode) {
118        selectionStart = lines2position.get(detailNode.getLineNumber())
119                            + detailNode.getColumnNumber();
120
121        selectionEnd = findLastPosition(detailNode);
122    }
123
124    /**
125     * Finds the last position of node without children.
126     *
127     * @param astNode DetailAST node.
128     * @return Last position of node without children.
129     */
130    private int findLastPosition(final DetailAST astNode) {
131        final int lastPosition;
132        if (astNode.hasChildren()) {
133            lastPosition = findLastPosition(astNode.getLastChild());
134        }
135        else {
136            lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
137                    + astNode.getText().length();
138        }
139        return lastPosition;
140    }
141
142    /**
143     * Finds the last position of node without children.
144     *
145     * @param detailNode DetailNode node.
146     * @return Last position of node without children.
147     */
148    private int findLastPosition(final DetailNode detailNode) {
149        final int lastPosition;
150        if (detailNode.getChildren().length == 0) {
151            lastPosition = lines2position.get(detailNode.getLineNumber())
152                    + detailNode.getColumnNumber() + detailNode.getText().length();
153        }
154        else {
155            final DetailNode lastChild =
156                    detailNode.getChildren()[detailNode.getChildren().length - 1];
157            lastPosition = findLastPosition(lastChild);
158        }
159        return lastPosition;
160    }
161
162}