KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jface.text;
13
14 import com.ibm.icu.text.BreakIterator;
15 import java.text.CharacterIterator JavaDoc;
16
17 /**
18  * Standard implementation of
19  * {@link org.eclipse.jface.text.ITextDoubleClickStrategy}.
20  * <p>
21  * Selects words using <code>java.text.BreakIterator</code> for the default
22  * locale.</p>
23  * <p>
24  * This class is not intended to be subclassed.
25  * </p>
26  *
27  * @see java.text.BreakIterator
28  */

29 public class DefaultTextDoubleClickStrategy implements ITextDoubleClickStrategy {
30
31
32     /**
33      * Implements a character iterator that works directly on
34      * instances of <code>IDocument</code>. Used to collaborate with
35      * the break iterator.
36      *
37      * @see IDocument
38      * @since 2.0
39      */

40     static class DocumentCharacterIterator implements CharacterIterator JavaDoc {
41
42         /** Document to iterate over. */
43         private IDocument fDocument;
44         /** Start offset of iteration. */
45         private int fOffset= -1;
46         /** End offset of iteration. */
47         private int fEndOffset= -1;
48         /** Current offset of iteration. */
49         private int fIndex= -1;
50
51         /** Creates a new document iterator. */
52         public DocumentCharacterIterator() {
53         }
54
55         /**
56          * Configures this document iterator with the document section to be visited.
57          *
58          * @param document the document to be iterated
59          * @param iteratorRange the range in the document to be iterated
60          */

61         public void setDocument(IDocument document, IRegion iteratorRange) {
62             fDocument= document;
63             fOffset= iteratorRange.getOffset();
64             fEndOffset= fOffset + iteratorRange.getLength();
65         }
66
67         /*
68          * @see CharacterIterator#first()
69          */

70         public char first() {
71             fIndex= fOffset;
72             return current();
73         }
74
75         /*
76          * @see CharacterIterator#last()
77          */

78         public char last() {
79             fIndex= fOffset < fEndOffset ? fEndOffset -1 : fEndOffset;
80             return current();
81         }
82
83         /*
84          * @see CharacterIterator#current()
85          */

86         public char current() {
87             if (fOffset <= fIndex && fIndex < fEndOffset) {
88                 try {
89                     return fDocument.getChar(fIndex);
90                 } catch (BadLocationException x) {
91                 }
92             }
93             return DONE;
94         }
95
96         /*
97          * @see CharacterIterator#next()
98          */

99         public char next() {
100             ++fIndex;
101             int end= getEndIndex();
102             if (fIndex >= end) {
103                 fIndex= end;
104                 return DONE;
105             }
106             return current();
107         }
108
109         /*
110          * @see CharacterIterator#previous()
111          */

112         public char previous() {
113             if (fIndex == fOffset)
114                 return DONE;
115
116             if (fIndex > fOffset)
117                 -- fIndex;
118
119             return current();
120         }
121
122         /*
123          * @see CharacterIterator#setIndex(int)
124          */

125         public char setIndex(int index) {
126             fIndex= index;
127             return current();
128         }
129
130         /*
131          * @see CharacterIterator#getBeginIndex()
132          */

133         public int getBeginIndex() {
134             return fOffset;
135         }
136
137         /*
138          * @see CharacterIterator#getEndIndex()
139          */

140         public int getEndIndex() {
141             return fEndOffset;
142         }
143
144         /*
145          * @see CharacterIterator#getIndex()
146          */

147         public int getIndex() {
148             return fIndex;
149         }
150
151         /*
152          * @see CharacterIterator#clone()
153          */

154         public Object JavaDoc clone() {
155             DocumentCharacterIterator i= new DocumentCharacterIterator();
156             i.fDocument= fDocument;
157             i.fIndex= fIndex;
158             i.fOffset= fOffset;
159             i.fEndOffset= fEndOffset;
160             return i;
161         }
162     }
163
164
165     /**
166      * The document character iterator used by this strategy.
167      * @since 2.0
168      */

169     private DocumentCharacterIterator fDocIter= new DocumentCharacterIterator();
170
171
172     /**
173      * Creates a new default text double click strategy.
174      */

175     public DefaultTextDoubleClickStrategy() {
176         super();
177     }
178
179     /*
180      * @see org.eclipse.jface.text.ITextDoubleClickStrategy#doubleClicked(org.eclipse.jface.text.ITextViewer)
181      */

182     public void doubleClicked(ITextViewer text) {
183
184         int position= text.getSelectedRange().x;
185
186         if (position < 0)
187             return;
188
189         try {
190
191             IDocument document= text.getDocument();
192             IRegion line= document.getLineInformationOfOffset(position);
193             if (position == line.getOffset() + line.getLength())
194                 return;
195
196             fDocIter.setDocument(document, line);
197
198             BreakIterator breakIter= BreakIterator.getWordInstance();
199             breakIter.setText(fDocIter);
200
201             int start= breakIter.preceding(position);
202             if (start == BreakIterator.DONE)
203                 start= line.getOffset();
204
205             int end= breakIter.following(position);
206             if (end == BreakIterator.DONE)
207                 end= line.getOffset() + line.getLength();
208
209             if (breakIter.isBoundary(position)) {
210                 if (end - position > position- start)
211                     start= position;
212                 else
213                     end= position;
214             }
215
216             if (start != end)
217                 text.setSelectedRange(start, end - start);
218
219         } catch (BadLocationException x) {
220         }
221     }
222 }
223
Popular Tags