]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/CompilationUnitTextSelection.java
Adding some logging capabilities to RefaktorDebug.
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / CompilationUnitTextSelection.java
1 package no.uio.ifi.refaktor.utils;
2
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
5
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.jdt.core.ICompilationUnit;
8 import org.eclipse.jdt.core.dom.ASTNode;
9 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
10 import org.eclipse.jdt.core.dom.CompilationUnit;
11 import org.eclipse.jdt.core.dom.NodeFinder;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.ITextSelection;
14 import org.eclipse.jface.text.TextSelection;
15
16 /**
17  * A custom TextSelection that enforces the presence of
18  * a compilation unit to back the selection.
19  */
20 public class CompilationUnitTextSelection extends TextSelection implements ITextSelection {
21
22         private ICompilationUnit compilationUnit;
23         private NodeFinder nodeFinder;
24
25         public CompilationUnitTextSelection(ICompilationUnit compilationUnit, int offset, int length) {
26                 super(offset, length);
27                 if (compilationUnit == null)
28                         throw new NullPointerException(this.getClass().getCanonicalName() + ": given compilation unit cannot be null");
29                 this.compilationUnit = compilationUnit;
30         }
31
32         public CompilationUnitTextSelection(ICompilationUnit compilationUnit, ITextSelection selection) {
33                 this(compilationUnit, selection.getOffset(), selection.getLength());
34         }
35
36         @Override
37         public IDocument getDocument() {
38                 return DocumentUtils.getDocumentFromCompilationUnit(compilationUnit);
39         }
40
41         public int getEnd() {
42                 return getOffset() + getLength();
43         }
44
45         public IProject getProject() {
46                 return compilationUnit.getJavaProject().getProject();
47         }
48
49         public ICompilationUnit getCompilationUnit() {
50                 return compilationUnit;
51         }
52
53         public ASTNode getCoveringNode() {
54                 return getNodeFinder().getCoveringNode();
55         }
56
57         public ASTNode getCoveredNode() {
58                 return getNodeFinder().getCoveredNode();
59         }
60
61         private NodeFinder getNodeFinder() {
62                 if (nodeFinder == null)
63                         nodeFinder = ParseUtils.createNodeFinder(ParseUtils.parse(compilationUnit), this);
64                 return nodeFinder;
65         }
66
67         public boolean surroundsNode(ASTNode node) {
68                 return getOffset() <= node.getStartPosition() && endOfNode(node) <= getEnd();
69         }
70
71         private int endOfNode(ASTNode node) {
72                 return node.getStartPosition() + node.getLength();
73         }
74
75         public String getPackageName() {
76                 return getConcreteCompilationUnit().getPackage().getName().getFullyQualifiedName();
77         }
78
79         public String getNameOfSurroundingType() {
80                 for (Object type: getConcreteCompilationUnit().types()) {
81                         assertThat(type, instanceOf(AbstractTypeDeclaration.class));
82                         AbstractTypeDeclaration absTypeDecl = (AbstractTypeDeclaration) type;
83                         if (isSurroundedBy(absTypeDecl))
84                                 return absTypeDecl.getName().getFullyQualifiedName();
85                 }
86                 return null;
87         }
88
89         private boolean isSurroundedBy(AbstractTypeDeclaration absTypeDecl) {
90                 return absTypeDecl.getStartPosition() <= getOffset() && getLength() <= absTypeDecl.getLength();
91         }
92
93         private CompilationUnit getConcreteCompilationUnit() {
94                 ASTNode root = getCoveringNode().getRoot();
95                 assertThat(root, instanceOf(CompilationUnit.class));
96                 return (CompilationUnit) root;
97         }
98
99         @Override
100         public boolean equals(Object obj) {
101                 if (obj instanceof CompilationUnitTextSelection)
102                         return super.equals(obj) && compilationUnit.equals(((CompilationUnitTextSelection)obj).compilationUnit);
103                 return false;
104         }
105         
106         @Override
107         public int hashCode() {
108                 return (getOffset() << 24) | (getLength() << 16) | compilationUnit.hashCode();
109         }
110         
111         @Override
112         public String toString() {
113                 return "Offset: " + getOffset() + "; Length: " + getLength() + "; End: " + getEnd() + "; CU handle identifier: " + compilationUnit.getHandleIdentifier();
114         }
115         
116         @Override
117         public int getStartLine() {
118                 return createTextSelectionWithDocument().getStartLine();
119         }
120         
121         @Override
122         public int getEndLine() {
123                 return createTextSelectionWithDocument().getEndLine();
124         }
125         
126         @Override
127         public String getText() {
128                 return createTextSelectionWithDocument().getText();
129         }
130         
131         private TextSelection createTextSelectionWithDocument() {
132                 return new TextSelection(getDocument(), getOffset(), getLength());
133         }
134 }