001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.text.projection;
011:
012: import org.eclipse.jface.text.BadLocationException;
013: import org.eclipse.jface.text.BadPositionCategoryException;
014: import org.eclipse.jface.text.DefaultPositionUpdater;
015: import org.eclipse.jface.text.DocumentEvent;
016: import org.eclipse.jface.text.IDocument;
017: import org.eclipse.jface.text.Position;
018:
019: /**
020: * The position updater used to adapt the fragments of a master document. If an
021: * insertion happens at a fragment's offset, the fragment is extended rather
022: * than shifted. Also, the last fragment is extended if an insert operation
023: * happens at the end of the fragment.
024: *
025: * @since 3.0
026: */
027: class FragmentUpdater extends DefaultPositionUpdater {
028:
029: /** Indicates whether the position being updated represents the last fragment. */
030: private boolean fIsLast = false;
031:
032: /**
033: * Creates the fragment updater for the given category.
034: *
035: * @param fragmentCategory the position category used for managing the fragments of a document
036: */
037: protected FragmentUpdater(String fragmentCategory) {
038: super (fragmentCategory);
039: }
040:
041: /*
042: * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
043: */
044: public void update(DocumentEvent event) {
045:
046: try {
047:
048: Position[] category = event.getDocument().getPositions(
049: getCategory());
050:
051: fOffset = event.getOffset();
052: fLength = event.getLength();
053: fReplaceLength = (event.getText() == null ? 0 : event
054: .getText().length());
055: fDocument = event.getDocument();
056:
057: for (int i = 0; i < category.length; i++) {
058:
059: fPosition = category[i];
060: fIsLast = (i == category.length - 1);
061:
062: fOriginalPosition.offset = fPosition.offset;
063: fOriginalPosition.length = fPosition.length;
064:
065: if (notDeleted())
066: adaptToReplace();
067: }
068:
069: } catch (BadPositionCategoryException x) {
070: // do nothing
071: }
072: }
073:
074: /*
075: * @see org.eclipse.jface.text.DefaultPositionUpdater#adaptToInsert()
076: */
077: protected void adaptToInsert() {
078: int myStart = fPosition.offset;
079: int myEnd = Math.max(myStart, fPosition.offset
080: + fPosition.length
081: - (fIsLast || isAffectingReplace() ? 0 : 1));
082:
083: if (myEnd < fOffset)
084: return;
085:
086: if (fLength <= 0) {
087:
088: if (myStart <= fOffset)
089: fPosition.length += fReplaceLength;
090: else
091: fPosition.offset += fReplaceLength;
092:
093: } else {
094:
095: if (myStart <= fOffset
096: && fOriginalPosition.offset <= fOffset)
097: fPosition.length += fReplaceLength;
098: else
099: fPosition.offset += fReplaceLength;
100: }
101: }
102:
103: /**
104: * Returns whether this updater considers any position affected by the given document event. A
105: * position is affected if <code>event</code> {@link Position#overlapsWith(int, int) overlaps}
106: * with it but not if the position is only shifted.
107: *
108: * @param event the event
109: * @return <code>true</code> if there is any affected position, <code>false</code> otherwise
110: */
111: public boolean affectsPositions(DocumentEvent event) {
112: IDocument document = event.getDocument();
113: try {
114:
115: int index = document.computeIndexInCategory(getCategory(),
116: event.getOffset());
117: Position[] fragments = document.getPositions(getCategory());
118:
119: if (0 < index) {
120: Position fragment = fragments[index - 1];
121: if (fragment.overlapsWith(event.getOffset(), event
122: .getLength()))
123: return true;
124: if (index == fragments.length
125: && fragment.offset + fragment.length == event
126: .getOffset())
127: return true;
128: }
129:
130: if (index < fragments.length) {
131: Position fragment = fragments[index];
132: return fragment.overlapsWith(event.getOffset(), event
133: .getLength());
134: }
135:
136: } catch (BadLocationException x) {
137: } catch (BadPositionCategoryException x) {
138: }
139:
140: return false;
141: }
142: }
|