comparison dwtx/jface/text/TextSelection.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.TextSelection;
14
15 import dwt.dwthelper.utils;
16
17
18 /**
19 * Standard implementation of {@link dwtx.jface.text.ITextSelection}.
20 * <p>
21 * Makes advantage of the weak contract of correctness of its interface. If
22 * generated from a selection provider, it only remembers its offset and length
23 * and computes the remaining information on request.</p>
24 */
25 public class TextSelection : ITextSelection {
26
27 /** Internal empty text selection */
28 private final static ITextSelection NULL= new TextSelection();
29
30 /**
31 * Returns a shared instance of an empty text selection.
32 *
33 * @return a shared instance of an empty text selection
34 */
35 public static ITextSelection emptySelection() {
36 return NULL;
37 }
38
39 /** Document which delivers the data of the selection */
40 private IDocument fDocument;
41 /** Offset of the selection */
42 private int fOffset;
43 /** Length of the selection */
44 private int fLength;
45
46
47 /**
48 * Creates an empty text selection.
49 */
50 private TextSelection() {
51 this(null, -1, -1);
52 }
53
54 /**
55 * Creates a text selection for the given range. This
56 * selection object describes generically a text range and
57 * is intended to be an argument for the <code>setSelection</code>
58 * method of selection providers.
59 *
60 * @param offset the offset of the range
61 * @param length the length of the range
62 */
63 public TextSelection(int offset, int length) {
64 this(null, offset, length);
65 }
66
67 /**
68 * Creates a text selection for the given range of the given document.
69 * This selection object is created by selection providers in responds
70 * <code>getSelection</code>.
71 *
72 * @param document the document whose text range is selected in a viewer
73 * @param offset the offset of the selected range
74 * @param length the length of the selected range
75 */
76 public TextSelection(IDocument document, int offset, int length) {
77 fDocument= document;
78 fOffset= offset;
79 fLength= length;
80 }
81
82 /**
83 *
84 * Returns true if the offset and length are smaller than 0.
85 * A selection of length 0, is a valid text selection as it
86 * describes, e.g., the cursor position in a viewer.
87 *
88 * @return <code>true</code> if this selection is empty
89 * @see dwtx.jface.viewers.ISelection#isEmpty()
90 */
91 public bool isEmpty() {
92 return fOffset < 0 || fLength < 0;
93 }
94
95 /*
96 * @see dwtx.jface.text.ITextSelection#getOffset()
97 */
98 public int getOffset() {
99 return fOffset;
100 }
101
102 /*
103 * @see dwtx.jface.text.ITextSelection#getLength()
104 */
105 public int getLength() {
106 return fLength;
107 }
108
109 /*
110 * @see dwtx.jface.text.ITextSelection#getStartLine()
111 */
112 public int getStartLine() {
113
114 try {
115 if (fDocument !is null)
116 return fDocument.getLineOfOffset(fOffset);
117 } catch (BadLocationException x) {
118 }
119
120 return -1;
121 }
122
123 /*
124 * @see dwtx.jface.text.ITextSelection#getEndLine()
125 */
126 public int getEndLine() {
127 try {
128 if (fDocument !is null) {
129 int endOffset= fOffset + fLength;
130 if (fLength !is 0)
131 endOffset--;
132 return fDocument.getLineOfOffset(endOffset);
133 }
134 } catch (BadLocationException x) {
135 }
136
137 return -1;
138 }
139
140 /*
141 * @see dwtx.jface.text.ITextSelection#getText()
142 */
143 public String getText() {
144 try {
145 if (fDocument !is null)
146 return fDocument.get(fOffset, fLength);
147 } catch (BadLocationException x) {
148 }
149
150 return null;
151 }
152
153 /*
154 * @see java.lang.Object#equals(Object)
155 */
156 public bool equals(Object obj) {
157 if (obj is this)
158 return true;
159
160 if (obj is null || getClass() !is obj.getClass())
161 return false;
162
163 TextSelection s= (TextSelection) obj;
164 bool sameRange= (s.fOffset is fOffset && s.fLength is fLength);
165 if (sameRange) {
166
167 if (s.fDocument is null && fDocument is null)
168 return true;
169 if (s.fDocument is null || fDocument is null)
170 return false;
171
172 try {
173 String sContent= s.fDocument.get(fOffset, fLength);
174 String content= fDocument.get(fOffset, fLength);
175 return sContent.equals(content);
176 } catch (BadLocationException x) {
177 }
178 }
179
180 return false;
181 }
182
183 /*
184 * @see java.lang.Object#hashCode()
185 */
186 public int hashCode() {
187 int low= fDocument !is null ? fDocument.hashCode() : 0;
188 return (fOffset << 24) | (fLength << 16) | low;
189 }
190 }
191