1 11 package org.eclipse.jface.text; 12 13 14 21 public class TextSelection implements ITextSelection { 22 23 24 private final static ITextSelection NULL= new TextSelection(); 25 26 31 public static ITextSelection emptySelection() { 32 return NULL; 33 } 34 35 36 private IDocument fDocument; 37 38 private int fOffset; 39 40 private int fLength; 41 42 43 46 private TextSelection() { 47 this(null, -1, -1); 48 } 49 50 59 public TextSelection(int offset, int length) { 60 this(null, offset, length); 61 } 62 63 72 public TextSelection(IDocument document, int offset, int length) { 73 fDocument= document; 74 fOffset= offset; 75 fLength= length; 76 } 77 78 87 public boolean isEmpty() { 88 return fOffset < 0 || fLength < 0; 89 } 90 91 94 public int getOffset() { 95 return fOffset; 96 } 97 98 101 public int getLength() { 102 return fLength; 103 } 104 105 108 public int getStartLine() { 109 110 try { 111 if (fDocument != null) 112 return fDocument.getLineOfOffset(fOffset); 113 } catch (BadLocationException x) { 114 } 115 116 return -1; 117 } 118 119 122 public int getEndLine() { 123 try { 124 if (fDocument != null) { 125 int endOffset= fOffset + fLength; 126 if (fLength != 0) 127 endOffset--; 128 return fDocument.getLineOfOffset(endOffset); 129 } 130 } catch (BadLocationException x) { 131 } 132 133 return -1; 134 } 135 136 139 public String getText() { 140 try { 141 if (fDocument != null) 142 return fDocument.get(fOffset, fLength); 143 } catch (BadLocationException x) { 144 } 145 146 return null; 147 } 148 149 152 public boolean equals(Object obj) { 153 if (obj == this) 154 return true; 155 156 if (obj == null || getClass() != obj.getClass()) 157 return false; 158 159 TextSelection s= (TextSelection) obj; 160 boolean sameRange= (s.fOffset == fOffset && s.fLength == fLength); 161 if (sameRange) { 162 163 if (s.fDocument == null && fDocument == null) 164 return true; 165 if (s.fDocument == null || fDocument == null) 166 return false; 167 168 try { 169 String sContent= s.fDocument.get(fOffset, fLength); 170 String content= fDocument.get(fOffset, fLength); 171 return sContent.equals(content); 172 } catch (BadLocationException x) { 173 } 174 } 175 176 return false; 177 } 178 179 182 public int hashCode() { 183 int low= fDocument != null ? fDocument.hashCode() : 0; 184 return (fOffset << 24) | (fLength << 16) | low; 185 } 186 } 187 188 | Popular Tags |