]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/collectors/SelectionValidator.java
ExtractAndMoveMethodAnalysisResult: renaming to ExtractAndMoveMethodCandidate
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / collectors / SelectionValidator.java
CommitLineData
fc35e1b0 1package no.uio.ifi.refaktor.analyze.collectors;
2f82d251
EK
2
3import java.util.Collections;
4import java.util.Comparator;
5import java.util.LinkedList;
6
1d39ea7d 7import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
2f82d251 8
2f82d251 9import org.eclipse.jdt.core.dom.ASTNode;
f10eb047 10import org.eclipse.jdt.core.dom.ASTVisitor;
2f82d251
EK
11import org.eclipse.jdt.core.dom.Block;
12import org.eclipse.jdt.core.dom.MethodDeclaration;
2f82d251
EK
13import org.eclipse.jdt.core.dom.Statement;
14
83148dc0 15public class SelectionValidator extends ASTVisitor {
2f82d251 16
f10eb047 17 private final CompilationUnitTextSelection selection;
520985c2 18 private final LinkedList<ASTNode> selectedStatements;
2f82d251 19
83148dc0 20 public SelectionValidator(CompilationUnitTextSelection selection) {
f10eb047
EK
21 super();
22 this.selection = selection;
2f82d251 23 this.selectedStatements = new LinkedList<ASTNode>();
f10eb047
EK
24 findSelectedStatements();
25 }
26
27 private void findSelectedStatements() {
28 getCoveringNode().accept(this);
2f82d251
EK
29 }
30
57ec7e36 31 private ASTNode getCoveringNode() {
f10eb047 32 return selection.getCoveringNode();
2f82d251
EK
33 }
34
35 private ASTNode getCoveredNode() {
f10eb047 36 return selection.getCoveredNode();
2f82d251
EK
37 }
38
2f82d251
EK
39 @Override
40 public boolean preVisit2(ASTNode node) {
f10eb047 41 if (node instanceof Statement && selection.surroundsNode(node)) {
2f82d251
EK
42 selectedStatements.add(node);
43 // Only visit top-level statements
44 return false;
45 }
46
47 if (node instanceof MethodDeclaration || node instanceof Block )
48 return true;
49
50 return false;
51 }
52
83148dc0 53 public static void checkIfSelectionIsValid(CompilationUnitTextSelection selection) {
1c2fad9b
EK
54 if (!selectionIsValid(selection))
55 throw new SelectionInvalidException();
83148dc0
EK
56 }
57
1c2fad9b 58 private static boolean selectionIsValid(CompilationUnitTextSelection selection) {
83148dc0 59 return new SelectionValidator(selection).selectionIsValid();
f10eb047
EK
60 }
61
62 private boolean selectionIsValid() {
63 if (noStatementsSelected())
64 return false;
65
66 if (selectedNodeHasBlockStructure())
67 return true;
68
69 return selectionMatchesStatementsStartAndEnd();
70 }
71
72 private boolean noStatementsSelected() {
73 return selectedStatements.isEmpty();
74 }
75
76 private boolean selectedNodeHasBlockStructure() {
2f82d251
EK
77 return isSingleNodeSelection() &&
78 (getSelectedNode() instanceof MethodDeclaration || getSelectedNode() instanceof Block);
79 }
80
f10eb047
EK
81 private boolean selectionMatchesStatementsStartAndEnd() {
82 sortSelectedStatements();
83 return selectedStatements.getFirst().getStartPosition() == selection.getOffset()
84 && nodeEnd(selectedStatements.getLast()) == selection.getEnd();
85 }
86
87 private void sortSelectedStatements() {
88 Collections.sort(selectedStatements, new Comparator<ASTNode>(){
89 @Override
90 public int compare(ASTNode node1, ASTNode node2) {
91 if (node1.equals(node2))
92 return 0;
93 else if (node1.getStartPosition() < node2.getStartPosition())
94 return -1;
95 else if (node1.getStartPosition() > node2.getStartPosition())
96 return 1;
97 else
98 return 0;
99 }});
100 }
101
102 private boolean isSingleNodeSelection() {
103 return getCoveringNode() == getCoveredNode();
104 }
105
57ec7e36
EK
106 private ASTNode getSelectedNode() {
107 if (!isSingleNodeSelection())
108 return null;
109 return getCoveredNode();
110 }
111
2f82d251
EK
112 private int nodeEnd(ASTNode astNode) {
113 return astNode.getStartPosition() + astNode.getLength();
114 }
115}