1 11 package org.eclipse.jface.text.projection; 12 13 14 import org.eclipse.jface.text.BadLocationException; 15 import org.eclipse.jface.text.BadPositionCategoryException; 16 import org.eclipse.jface.text.DefaultPositionUpdater; 17 import org.eclipse.jface.text.DocumentEvent; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.Position; 20 21 22 30 class FragmentUpdater extends DefaultPositionUpdater { 31 32 33 private boolean fIsLast= false; 34 35 40 protected FragmentUpdater(String fragmentCategory) { 41 super(fragmentCategory); 42 } 43 44 47 public void update(DocumentEvent event) { 48 49 try { 50 51 Position[] category= event.getDocument().getPositions(getCategory()); 52 53 fOffset= event.getOffset(); 54 fLength= event.getLength(); 55 fReplaceLength= (event.getText() == null ? 0 : event.getText().length()); 56 fDocument= event.getDocument(); 57 58 for (int i= 0; i < category.length; i++) { 59 60 fPosition= category[i]; 61 fIsLast= (i == category.length -1); 62 63 fOriginalPosition.offset= fPosition.offset; 64 fOriginalPosition.length= fPosition.length; 65 66 if (notDeleted()) 67 adaptToReplace(); 68 } 69 70 } catch (BadPositionCategoryException x) { 71 } 73 } 74 75 78 protected void adaptToInsert() { 79 int myStart= fPosition.offset; 80 int myEnd= Math.max(myStart, fPosition.offset + fPosition.length - (fIsLast || isAffectingReplace() ? 0 : 1)); 81 82 if (myEnd < fOffset) 83 return; 84 85 if (fLength <= 0) { 86 87 if (myStart <= fOffset) 88 fPosition.length += fReplaceLength; 89 else 90 fPosition.offset += fReplaceLength; 91 92 } else { 93 94 if (myStart <= fOffset && fOriginalPosition.offset <= fOffset) 95 fPosition.length += fReplaceLength; 96 else 97 fPosition.offset += fReplaceLength; 98 } 99 } 100 101 109 public boolean affectsPositions(DocumentEvent event) { 110 IDocument document= event.getDocument(); 111 try { 112 113 int index= document.computeIndexInCategory(getCategory(), event.getOffset()); 114 Position[] fragments= document.getPositions(getCategory()); 115 116 if (0 < index) { 117 Position fragment= fragments[index - 1]; 118 if (fragment.overlapsWith(event.getOffset(), event.getLength())) 119 return true; 120 if (index == fragments.length && fragment.offset + fragment.length == event.getOffset()) 121 return true; 122 } 123 124 if (index < fragments.length) { 125 Position fragment= fragments[index]; 126 return fragment.overlapsWith(event.getOffset(), event.getLength()); 127 } 128 129 } catch (BadLocationException x) { 130 } catch (BadPositionCategoryException x) { 131 } 132 133 return false; 134 } 135 } 136 | Popular Tags |