1 package no.uio.ifi.refaktor.utils;
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
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;
17 * A custom TextSelection that enforces the presence of
18 * a compilation unit to back the selection.
20 public class CompilationUnitTextSelection extends TextSelection implements ITextSelection {
22 private ICompilationUnit compilationUnit;
23 private NodeFinder nodeFinder;
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;
32 public CompilationUnitTextSelection(ICompilationUnit compilationUnit, ITextSelection selection) {
33 this(compilationUnit, selection.getOffset(), selection.getLength());
37 public IDocument getDocument() {
38 return DocumentUtils.getDocumentFromCompilationUnit(compilationUnit);
42 return getOffset() + getLength();
45 public IProject getProject() {
46 return compilationUnit.getJavaProject().getProject();
49 public ICompilationUnit getCompilationUnit() {
50 return compilationUnit;
53 public ASTNode getCoveringNode() {
54 return getNodeFinder().getCoveringNode();
57 public ASTNode getCoveredNode() {
58 return getNodeFinder().getCoveredNode();
61 private NodeFinder getNodeFinder() {
62 if (nodeFinder == null)
63 nodeFinder = ParseUtils.createNodeFinder(ParseUtils.parse(compilationUnit), this);
67 public boolean surroundsNode(ASTNode node) {
68 return getOffset() <= node.getStartPosition() && endOfNode(node) <= getEnd();
71 private int endOfNode(ASTNode node) {
72 return node.getStartPosition() + node.getLength();
75 public String getPackageName() {
76 return getConcreteCompilationUnit().getPackage().getName().getFullyQualifiedName();
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();
89 private boolean isSurroundedBy(AbstractTypeDeclaration absTypeDecl) {
90 return absTypeDecl.getStartPosition() <= getOffset() && getLength() <= absTypeDecl.getLength();
93 private CompilationUnit getConcreteCompilationUnit() {
94 ASTNode root = getCoveringNode().getRoot();
95 assertThat(root, instanceOf(CompilationUnit.class));
96 return (CompilationUnit) root;
100 public boolean equals(Object obj) {
101 if (obj instanceof CompilationUnitTextSelection)
102 return super.equals(obj) && compilationUnit.equals(((CompilationUnitTextSelection)obj).compilationUnit);
107 public int hashCode() {
108 return (getOffset() << 24) | (getLength() << 16) | compilationUnit.hashCode();
112 public String toString() {
113 return "Offset: " + getOffset() + "; Length: " + getLength() + "; End: " + getEnd() + "; CU handle identifier: " + compilationUnit.getHandleIdentifier();
117 public int getStartLine() {
118 return createTextSelectionWithDocument().getStartLine();
122 public int getEndLine() {
123 return createTextSelectionWithDocument().getEndLine();
127 public String getText() {
128 return createTextSelectionWithDocument().getText();
131 private TextSelection createTextSelectionWithDocument() {
132 return new TextSelection(getDocument(), getOffset(), getLength());