1 11 package org.eclipse.ui.texteditor; 12 13 import org.eclipse.jface.text.BadLocationException; 14 import org.eclipse.jface.text.BadPositionCategoryException; 15 import org.eclipse.jface.text.DefaultPositionUpdater; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.IPositionUpdater; 18 import org.eclipse.jface.text.ITextSelection; 19 import org.eclipse.jface.text.Position; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.IMemento; 25 import org.eclipse.ui.INavigationLocation; 26 import org.eclipse.ui.NavigationLocation; 27 28 29 34 public class TextSelectionNavigationLocation extends NavigationLocation { 35 36 private static final String TAG_X= "x"; private static final String TAG_Y= "y"; private static final String TAG_INFO= "info"; private static final String INFO_DELETED= "deleted"; private static final String INFO_NOT_DELETED= "not_deleted"; 43 private static final String CATEGORY= "__navigation_" + TextSelectionNavigationLocation.class.hashCode(); private static final IPositionUpdater fgPositionUpdater= new DefaultPositionUpdater(CATEGORY); 45 46 47 private Position fPosition; 48 private IDocument fDocument; 49 private Position fSavedPosition; 50 51 52 58 public TextSelectionNavigationLocation(ITextEditor part, boolean initialize) { 59 super(part); 60 61 if (initialize) { 62 63 ISelection s= part.getSelectionProvider().getSelection(); 64 if(s == null || s.isEmpty()) 65 return; 66 67 ITextSelection selection= (ITextSelection) s; 68 if(selection.getOffset() == 0 && selection.getLength() == 0) 69 return; 70 71 IDocument document= getDocument(part); 72 Position position= new Position(selection.getOffset(), selection.getLength()); 73 if (installOnDocument(document, position)) { 74 fDocument= document; 75 fPosition= position; 76 if (!part.isDirty()) 77 fSavedPosition= new Position(fPosition.offset, fPosition.length); 78 } 79 } 80 } 81 82 88 private IDocument getDocument(ITextEditor part) { 89 IDocumentProvider provider= part.getDocumentProvider(); 90 return provider.getDocument(part.getEditorInput()); 91 } 92 93 100 private boolean installOnDocument(IDocument document, Position position) { 101 102 if (document != null && position != null) { 103 104 if (!document.containsPositionCategory(CATEGORY)) { 105 document.addPositionCategory(CATEGORY); 106 document.addPositionUpdater(fgPositionUpdater); 107 } 108 109 try { 110 document.addPosition(CATEGORY, position); 111 return true; 112 } catch (BadLocationException e) { 113 } catch (BadPositionCategoryException e) { 114 } 115 } 116 117 return false; 118 } 119 120 127 private boolean uninstallFromDocument(IDocument document, Position position) { 128 129 if (document != null && position != null) { 130 try { 131 132 document.removePosition(CATEGORY, position); 133 134 Position[] category= document.getPositions(CATEGORY); 135 if (category == null || category.length == 0) { 136 document.removePositionCategory(CATEGORY); 137 document.removePositionUpdater(fgPositionUpdater); 138 } 139 return true; 140 141 } catch (BadPositionCategoryException e) { 142 } 143 } 144 145 return false; 146 } 147 148 151 public String toString() { 152 return "Selection<" + fPosition + ">"; } 154 155 162 private boolean equalsLocationOf(ITextEditor part) { 163 164 if (fPosition == null) 165 return true; 166 167 if (fPosition.isDeleted) 168 return false; 169 170 ISelectionProvider provider= part.getSite().getSelectionProvider(); 171 ISelection selection= provider.getSelection(); 172 if (selection instanceof ITextSelection) { 173 ITextSelection textSelection= (ITextSelection) selection; 174 if (textSelection.getOffset() == fPosition.offset && textSelection.getLength() == fPosition.length) { 175 String text= textSelection.getText(); 176 if (text != null) { 177 try { 178 return text.equals(fDocument.get(fPosition.offset, fPosition.length)); 179 } catch (BadLocationException e) { 180 } 181 } 182 } 183 } 184 185 return false; 186 } 187 188 public void dispose() { 189 uninstallFromDocument(fDocument, fPosition); 190 fDocument= null; 191 fPosition= null; 192 fSavedPosition= null; 193 super.dispose(); 194 } 195 196 199 public void releaseState() { 200 uninstallFromDocument(fDocument, fPosition); 202 fDocument= null; 203 fPosition= null; 204 fSavedPosition= null; 205 super.releaseState(); 206 } 207 208 214 public boolean mergeInto(INavigationLocation location) { 215 216 if (location == null) 217 return false; 218 219 if (getClass() != location.getClass()) 220 return false; 221 222 if (fPosition == null || fPosition.isDeleted) 223 return true; 224 225 TextSelectionNavigationLocation s= (TextSelectionNavigationLocation) location; 226 if (s.fPosition == null || s.fPosition.isDeleted) { 227 uninstallFromDocument(fDocument, fPosition); 228 s.fDocument= fDocument; 229 s. fPosition= fPosition; 230 s.fSavedPosition= fSavedPosition; 231 return true; 232 } 233 234 if (s.fDocument == fDocument) { 235 if (s.fPosition.overlapsWith(fPosition.offset, fPosition.length) || fPosition.offset + fPosition.length == s.fPosition.offset || s.fPosition.offset + s.fPosition.length == fPosition.offset) { 236 s.fPosition.offset= fPosition.offset; 237 s.fPosition.length= fPosition.length; 238 return true; 239 } 240 } 241 242 return false; 243 } 244 245 248 public void restoreLocation() { 249 if (fPosition == null || fPosition.isDeleted) 250 return; 251 252 IEditorPart part= getEditorPart(); 253 if (part instanceof ITextEditor) { 254 ITextEditor editor= (ITextEditor) getEditorPart(); 255 editor.selectAndReveal(fPosition.offset, fPosition.length); 256 } 257 } 258 259 264 public void restoreState(IMemento memento) { 265 266 IEditorPart part= getEditorPart(); 267 if (part instanceof ITextEditor) { 268 269 fDocument= getDocument((ITextEditor) part); 271 272 Integer offset= memento.getInteger(TAG_X); 273 Integer length= memento.getInteger(TAG_Y); 274 String deleted= memento.getString(TAG_INFO); 275 276 if (offset != null && length != null) { 277 Position p= new Position(offset.intValue(), length.intValue()); 278 if (deleted != null) 279 p.isDeleted= INFO_DELETED.equals(deleted) ? true : false; 280 281 if (installOnDocument(fDocument, p)) { 283 fPosition= p; 284 if (!part.isDirty()) 285 fSavedPosition= new Position(fPosition.offset, fPosition.length); 286 } 287 } 288 } 289 } 290 291 296 public void saveState(IMemento memento) { 297 if (fSavedPosition != null) { 298 memento.putInteger(TAG_X, fSavedPosition.offset); 299 memento.putInteger(TAG_Y, fSavedPosition.length); 300 memento.putString(TAG_INFO, (fSavedPosition.isDeleted ? INFO_DELETED : INFO_NOT_DELETED)); 301 } 302 } 303 304 309 public void partSaved(IEditorPart part) { 310 if (fPosition == null || fPosition.isDeleted()) 312 fSavedPosition= null; 313 else 314 fSavedPosition= new Position(fPosition.offset, fPosition.length); 315 } 316 317 320 public void update() { 321 IEditorPart part= getEditorPart(); 322 if (part instanceof ITextEditor) { 323 ITextEditor textEditor= (ITextEditor) getEditorPart(); 324 325 if(equalsLocationOf(textEditor)) 326 return; 327 328 ISelection s= textEditor.getSelectionProvider().getSelection(); 329 if(s == null || s.isEmpty()) 330 return; 331 332 ITextSelection selection= (ITextSelection) s; 333 if(selection.getOffset() == 0 && selection.getLength() == 0) 334 return; 335 336 fPosition.offset= selection.getOffset(); 337 fPosition.length= selection.getLength(); 338 fPosition.isDeleted= false; 339 340 if (!part.isDirty()) 341 fSavedPosition= new Position(fPosition.offset, fPosition.length); 342 } 343 } 344 } 345 | Popular Tags |