KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > TextSelection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text;
12
13
14 /**
15  * Standard implementation of {@link org.eclipse.jface.text.ITextSelection}.
16  * <p>
17  * Makes advantage of the weak contract of correctness of its interface. If
18  * generated from a selection provider, it only remembers its offset and length
19  * and computes the remaining information on request.</p>
20  */

21 public class TextSelection implements ITextSelection {
22
23     /** Internal empty text selection */
24     private final static ITextSelection NULL= new TextSelection();
25
26     /**
27      * Returns a shared instance of an empty text selection.
28      *
29      * @return a shared instance of an empty text selection
30      */

31     public static ITextSelection emptySelection() {
32         return NULL;
33     }
34
35     /** Document which delivers the data of the selection */
36     private IDocument fDocument;
37     /** Offset of the selection */
38     private int fOffset;
39     /** Length of the selection */
40     private int fLength;
41
42
43     /**
44      * Creates an empty text selection.
45      */

46     private TextSelection() {
47         this(null, -1, -1);
48     }
49
50     /**
51      * Creates a text selection for the given range. This
52      * selection object describes generically a text range and
53      * is intended to be an argument for the <code>setSelection</code>
54      * method of selection providers.
55      *
56      * @param offset the offset of the range
57      * @param length the length of the range
58      */

59     public TextSelection(int offset, int length) {
60         this(null, offset, length);
61     }
62
63     /**
64      * Creates a text selection for the given range of the given document.
65      * This selection object is created by selection providers in responds
66      * <code>getSelection</code>.
67      *
68      * @param document the document whose text range is selected in a viewer
69      * @param offset the offset of the selected range
70      * @param length the length of the selected range
71      */

72     public TextSelection(IDocument document, int offset, int length) {
73         fDocument= document;
74         fOffset= offset;
75         fLength= length;
76     }
77
78     /**
79      *
80      * Returns true if the offset and length are smaller than 0.
81      * A selection of length 0, is a valid text selection as it
82      * describes, e.g., the cursor position in a viewer.
83      *
84      * @return <code>true</code> if this selection is empty
85      * @see org.eclipse.jface.viewers.ISelection#isEmpty()
86      */

87     public boolean isEmpty() {
88         return fOffset < 0 || fLength < 0;
89     }
90
91     /*
92      * @see org.eclipse.jface.text.ITextSelection#getOffset()
93      */

94     public int getOffset() {
95         return fOffset;
96     }
97
98     /*
99      * @see org.eclipse.jface.text.ITextSelection#getLength()
100      */

101     public int getLength() {
102         return fLength;
103     }
104
105     /*
106      * @see org.eclipse.jface.text.ITextSelection#getStartLine()
107      */

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     /*
120      * @see org.eclipse.jface.text.ITextSelection#getEndLine()
121      */

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     /*
137      * @see org.eclipse.jface.text.ITextSelection#getText()
138      */

139     public String JavaDoc 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     /*
150      * @see java.lang.Object#equals(Object)
151      */

152     public boolean equals(Object JavaDoc 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 JavaDoc sContent= s.fDocument.get(fOffset, fLength);
170                 String JavaDoc content= fDocument.get(fOffset, fLength);
171                 return sContent.equals(content);
172             } catch (BadLocationException x) {
173             }
174         }
175
176         return false;
177     }
178
179     /*
180      * @see java.lang.Object#hashCode()
181      */

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