KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavadocDoubleClickStrategy


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.jdt.internal.ui.text.java;
13
14 import com.ibm.icu.text.BreakIterator;
15 import java.text.CharacterIterator JavaDoc;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextDoubleClickStrategy;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.Region;
23
24
25 /**
26  * Copied from DefaultTextDoubleClickStrategy. Same behavior, but also
27  * allows <code>@identifier</code> to be selected.
28  *
29  * @since 3.1
30  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

184     public void doubleClicked(ITextViewer text) {
185
186         int position= text.getSelectedRange().x;
187
188         if (position < 0)
189             return;
190
191         IRegion word= getWordRegion(text.getDocument(), position);
192
193         if (word != null)
194             text.setSelectedRange(word.getOffset(), word.getLength());
195     }
196
197     /**
198      * Returns a region describing the word around <code>position</code>.
199      *
200      * @param document the document
201      * @param position the offset around which to return the word
202      * @return the word's region, or <code>null</code> for no selection
203      */

204     private IRegion getWordRegion(IDocument document, int position) {
205         try {
206
207             IRegion line= document.getLineInformationOfOffset(position);
208             if (position == line.getOffset() + line.getLength())
209                 return null;
210
211             fDocIter.setDocument(document, line);
212
213             BreakIterator breakIter= BreakIterator.getWordInstance();
214             breakIter.setText(fDocIter);
215
216             int start= breakIter.preceding(position);
217             if (start == BreakIterator.DONE)
218                 start= line.getOffset();
219
220             int end= breakIter.following(position);
221             if (end == BreakIterator.DONE)
222                 end= line.getOffset() + line.getLength();
223
224             if (breakIter.isBoundary(position)) {
225                 if (end - position > position- start)
226                     start= position;
227                 else
228                     end= position;
229             }
230
231             if (start > 0 && document.getChar(start - 1) == '@' && Character.isJavaIdentifierPart(document.getChar(start))
232                     && (start == 1 || Character.isWhitespace(document.getChar(start - 2)) || document.getChar(start - 2) == '{')) {
233                 // double click after @ident
234
start--;
235             } else if (end == position && end == start + 1 && end < line.getOffset() + line.getLength() && document.getChar(end) == '@') {
236                 // double click before " @ident"
237
return getWordRegion(document, position + 1);
238             }
239
240             if (start == end)
241                 return null;
242             return new Region(start, end - start);
243
244         } catch (BadLocationException x) {
245             return null;
246         }
247     }
248 }
249
Popular Tags