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