1 11 12 package org.eclipse.jface.text; 13 14 import com.ibm.icu.text.BreakIterator; 15 import java.text.CharacterIterator ; 16 17 29 public class DefaultTextDoubleClickStrategy implements ITextDoubleClickStrategy { 30 31 32 40 static class DocumentCharacterIterator implements CharacterIterator { 41 42 43 private IDocument fDocument; 44 45 private int fOffset= -1; 46 47 private int fEndOffset= -1; 48 49 private int fIndex= -1; 50 51 52 public DocumentCharacterIterator() { 53 } 54 55 61 public void setDocument(IDocument document, IRegion iteratorRange) { 62 fDocument= document; 63 fOffset= iteratorRange.getOffset(); 64 fEndOffset= fOffset + iteratorRange.getLength(); 65 } 66 67 70 public char first() { 71 fIndex= fOffset; 72 return current(); 73 } 74 75 78 public char last() { 79 fIndex= fOffset < fEndOffset ? fEndOffset -1 : fEndOffset; 80 return current(); 81 } 82 83 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 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 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 125 public char setIndex(int index) { 126 fIndex= index; 127 return current(); 128 } 129 130 133 public int getBeginIndex() { 134 return fOffset; 135 } 136 137 140 public int getEndIndex() { 141 return fEndOffset; 142 } 143 144 147 public int getIndex() { 148 return fIndex; 149 } 150 151 154 public Object 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 169 private DocumentCharacterIterator fDocIter= new DocumentCharacterIterator(); 170 171 172 175 public DefaultTextDoubleClickStrategy() { 176 super(); 177 } 178 179 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 |