129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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
|
|
14 module dwtx.jface.text.DefaultTextDoubleClickStrategy;
|
|
15
|
|
16 import dwt.dwthelper.utils;
|
|
17
|
|
18 import java.text.CharacterIterator;
|
|
19
|
|
20 import com.ibm.icu.text.BreakIterator;
|
|
21
|
|
22 /**
|
|
23 * Standard implementation of
|
|
24 * {@link dwtx.jface.text.ITextDoubleClickStrategy}.
|
|
25 * <p>
|
|
26 * Selects words using <code>java.text.BreakIterator</code> for the default
|
|
27 * locale.</p>
|
|
28 * <p>
|
|
29 * This class is not intended to be subclassed.
|
|
30 * </p>
|
|
31 *
|
|
32 * @see java.text.BreakIterator
|
|
33 * @noextend This class is not intended to be subclassed by clients.
|
|
34 */
|
|
35 public class DefaultTextDoubleClickStrategy : ITextDoubleClickStrategy {
|
|
36
|
|
37
|
|
38 /**
|
|
39 * Implements a character iterator that works directly on
|
|
40 * instances of <code>IDocument</code>. Used to collaborate with
|
|
41 * the break iterator.
|
|
42 *
|
|
43 * @see IDocument
|
|
44 * @since 2.0
|
|
45 */
|
|
46 static class DocumentCharacterIterator : CharacterIterator {
|
|
47
|
|
48 /** Document to iterate over. */
|
|
49 private IDocument fDocument;
|
|
50 /** Start offset of iteration. */
|
|
51 private int fOffset= -1;
|
|
52 /** End offset of iteration. */
|
|
53 private int fEndOffset= -1;
|
|
54 /** Current offset of iteration. */
|
|
55 private int fIndex= -1;
|
|
56
|
|
57 /** Creates a new document iterator. */
|
|
58 public DocumentCharacterIterator() {
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * Configures this document iterator with the document section to be visited.
|
|
63 *
|
|
64 * @param document the document to be iterated
|
|
65 * @param iteratorRange the range in the document to be iterated
|
|
66 */
|
|
67 public void setDocument(IDocument document, IRegion iteratorRange) {
|
|
68 fDocument= document;
|
|
69 fOffset= iteratorRange.getOffset();
|
|
70 fEndOffset= fOffset + iteratorRange.getLength();
|
|
71 }
|
|
72
|
|
73 /*
|
|
74 * @see CharacterIterator#first()
|
|
75 */
|
|
76 public char first() {
|
|
77 fIndex= fOffset;
|
|
78 return current();
|
|
79 }
|
|
80
|
|
81 /*
|
|
82 * @see CharacterIterator#last()
|
|
83 */
|
|
84 public char last() {
|
|
85 fIndex= fOffset < fEndOffset ? fEndOffset -1 : fEndOffset;
|
|
86 return current();
|
|
87 }
|
|
88
|
|
89 /*
|
|
90 * @see CharacterIterator#current()
|
|
91 */
|
|
92 public char current() {
|
|
93 if (fOffset <= fIndex && fIndex < fEndOffset) {
|
|
94 try {
|
|
95 return fDocument.getChar(fIndex);
|
|
96 } catch (BadLocationException x) {
|
|
97 }
|
|
98 }
|
|
99 return DONE;
|
|
100 }
|
|
101
|
|
102 /*
|
|
103 * @see CharacterIterator#next()
|
|
104 */
|
|
105 public char next() {
|
|
106 ++fIndex;
|
|
107 int end= getEndIndex();
|
|
108 if (fIndex >= end) {
|
|
109 fIndex= end;
|
|
110 return DONE;
|
|
111 }
|
|
112 return current();
|
|
113 }
|
|
114
|
|
115 /*
|
|
116 * @see CharacterIterator#previous()
|
|
117 */
|
|
118 public char previous() {
|
|
119 if (fIndex is fOffset)
|
|
120 return DONE;
|
|
121
|
|
122 if (fIndex > fOffset)
|
|
123 -- fIndex;
|
|
124
|
|
125 return current();
|
|
126 }
|
|
127
|
|
128 /*
|
|
129 * @see CharacterIterator#setIndex(int)
|
|
130 */
|
|
131 public char setIndex(int index) {
|
|
132 fIndex= index;
|
|
133 return current();
|
|
134 }
|
|
135
|
|
136 /*
|
|
137 * @see CharacterIterator#getBeginIndex()
|
|
138 */
|
|
139 public int getBeginIndex() {
|
|
140 return fOffset;
|
|
141 }
|
|
142
|
|
143 /*
|
|
144 * @see CharacterIterator#getEndIndex()
|
|
145 */
|
|
146 public int getEndIndex() {
|
|
147 return fEndOffset;
|
|
148 }
|
|
149
|
|
150 /*
|
|
151 * @see CharacterIterator#getIndex()
|
|
152 */
|
|
153 public int getIndex() {
|
|
154 return fIndex;
|
|
155 }
|
|
156
|
|
157 /*
|
|
158 * @see CharacterIterator#clone()
|
|
159 */
|
|
160 public Object clone() {
|
|
161 DocumentCharacterIterator i= new DocumentCharacterIterator();
|
|
162 i.fDocument= fDocument;
|
|
163 i.fIndex= fIndex;
|
|
164 i.fOffset= fOffset;
|
|
165 i.fEndOffset= fEndOffset;
|
|
166 return i;
|
|
167 }
|
|
168 }
|
|
169
|
|
170
|
|
171 /**
|
|
172 * The document character iterator used by this strategy.
|
|
173 * @since 2.0
|
|
174 */
|
|
175 private DocumentCharacterIterator fDocIter= new DocumentCharacterIterator();
|
|
176
|
|
177
|
|
178 /**
|
|
179 * Creates a new default text double click strategy.
|
|
180 */
|
|
181 public DefaultTextDoubleClickStrategy() {
|
|
182 super();
|
|
183 }
|
|
184
|
|
185 /*
|
|
186 * @see dwtx.jface.text.ITextDoubleClickStrategy#doubleClicked(dwtx.jface.text.ITextViewer)
|
|
187 */
|
|
188 public void doubleClicked(ITextViewer text) {
|
|
189
|
|
190 int position= text.getSelectedRange().x;
|
|
191
|
|
192 if (position < 0)
|
|
193 return;
|
|
194
|
|
195 try {
|
|
196
|
|
197 IDocument document= text.getDocument();
|
|
198 IRegion line= document.getLineInformationOfOffset(position);
|
|
199 if (position is line.getOffset() + line.getLength())
|
|
200 return;
|
|
201
|
|
202 fDocIter.setDocument(document, line);
|
|
203
|
|
204 BreakIterator breakIter= BreakIterator.getWordInstance();
|
|
205 breakIter.setText(fDocIter);
|
|
206
|
|
207 int start= breakIter.preceding(position);
|
|
208 if (start is BreakIterator.DONE)
|
|
209 start= line.getOffset();
|
|
210
|
|
211 int end= breakIter.following(position);
|
|
212 if (end is BreakIterator.DONE)
|
|
213 end= line.getOffset() + line.getLength();
|
|
214
|
|
215 if (breakIter.isBoundary(position)) {
|
|
216 if (end - position > position- start)
|
|
217 start= position;
|
|
218 else
|
|
219 end= position;
|
|
220 }
|
|
221
|
|
222 if (start !is end)
|
|
223 text.setSelectedRange(start, end - start);
|
|
224
|
|
225 } catch (BadLocationException x) {
|
|
226 }
|
|
227 }
|
|
228 }
|