]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/SmartTextSelection.java
Adding some JavaDoc and cleaning up a bit.
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / SmartTextSelection.java
1 package no.uio.ifi.refaktor.utils;
2
3 import org.eclipse.jdt.core.ICompilationUnit;
4 import org.eclipse.jdt.core.dom.NodeFinder;
5 import org.eclipse.jface.text.IDocument;
6 import org.eclipse.jface.text.ITextSelection;
7 import org.eclipse.jface.text.TextSelection;
8
9 /**
10  * A custom TextSelection that enforces the presence of
11  * a document to back the selection.
12  */
13 public class SmartTextSelection extends TextSelection implements ITextSelection {
14
15         public SmartTextSelection(IDocument document, int offset, int length) {
16                 super(document, offset, length);
17                 if (document == null)
18                         throw new NullPointerException(this.getClass().getCanonicalName() + ": given document cannot be null");
19         }
20         
21         public SmartTextSelection(IDocument document, ITextSelection selection) {
22                 this(document, selection.getOffset(), selection.getLength());
23         }
24
25         @Override
26         public IDocument getDocument() {
27                 IDocument document = super.getDocument();
28                 assert document != null;
29                 return document;
30         }
31
32         public int getEnd() {
33                 return getOffset() + getLength();
34         }
35
36         public NodeFinder getNodeFinder() {
37                 return ParseUtils.getNodeFinder(getDocument(), this);
38         }
39
40         public NodeFinder getNodeFinder(ICompilationUnit icu) {
41                 return ParseUtils.makeNodeFinder(this, ParseUtils.parse(icu));
42         }
43 }