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