KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > projection > SegmentUpdater


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 package org.eclipse.jface.text.projection;
12
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.text.BadPositionCategoryException;
17 import org.eclipse.jface.text.DefaultPositionUpdater;
18 import org.eclipse.jface.text.DocumentEvent;
19 import org.eclipse.jface.text.Position;
20
21
22 /**
23  * The position updater used to adapt the segments of a projection document to
24  * changes of the master document. Depending on the flags set on a segment, a
25  * segment is either extended to shifted if an insertion happens at a segment's
26  * offset. The last segment is extended if an insert operation happens at the
27  * end of the segment.
28  *
29  * @since 3.0
30  */

31 class SegmentUpdater extends DefaultPositionUpdater {
32
33     private Segment fNextSegment= null;
34     private boolean fIsProjectionChange= false;
35
36     /**
37      * Creates the segment updater for the given category.
38      *
39      * @param segmentCategory the position category used for managing the segments of a projection document
40      */

41     protected SegmentUpdater(String JavaDoc segmentCategory) {
42         super(segmentCategory);
43     }
44
45     /*
46      * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
47      */

48     public void update(DocumentEvent event) {
49
50         Assert.isTrue(event instanceof ProjectionDocumentEvent);
51         fIsProjectionChange= ((ProjectionDocumentEvent) event).getChangeType() == ProjectionDocumentEvent.PROJECTION_CHANGE;
52
53         try {
54
55             Position[] category= event.getDocument().getPositions(getCategory());
56
57             fOffset= event.getOffset();
58             fLength= event.getLength();
59             fReplaceLength= (event.getText() == null ? 0 : event.getText().length());
60             fDocument= event.getDocument();
61
62             for (int i= 0; i < category.length; i++) {
63
64                 fPosition= category[i];
65                 Assert.isTrue(fPosition instanceof Segment);
66
67                 if (i < category.length - 1)
68                     fNextSegment= (Segment) category[i + 1];
69                 else
70                     fNextSegment= null;
71
72                 fOriginalPosition.offset= fPosition.offset;
73                 fOriginalPosition.length= fPosition.length;
74
75                 if (notDeleted())
76                     adaptToReplace();
77
78             }
79
80         } catch (BadPositionCategoryException x) {
81             // do nothing
82
}
83     }
84
85     /*
86      * @see org.eclipse.jface.text.DefaultPositionUpdater#adaptToInsert()
87      */

88     protected void adaptToInsert() {
89
90         Segment segment= (Segment) fPosition;
91         int myStart= segment.offset;
92         int myEnd= segment.offset + segment.length - (segment.isMarkedForStretch || fNextSegment == null || isAffectingReplace() ? 0 : 1);
93         myEnd= Math.max(myStart, myEnd);
94         int yoursStart= fOffset;
95
96         try {
97
98             if (myEnd < yoursStart)
99                 return;
100
101             if (segment.isMarkedForStretch) {
102                 Assert.isTrue(fIsProjectionChange);
103                 segment.isMarkedForShift= false;
104                 if (fNextSegment != null) {
105                     fNextSegment.isMarkedForShift= true;
106                     fNextSegment.isMarkedForStretch= false;
107                 }
108             }
109
110             if (fLength <= 0) {
111
112                 if (myStart < (yoursStart + (segment.isMarkedForShift ? 0 : 1)))
113                     fPosition.length += fReplaceLength;
114                 else
115                     fPosition.offset += fReplaceLength;
116
117             } else {
118
119                 if (myStart <= yoursStart && fOriginalPosition.offset <= yoursStart)
120                     fPosition.length += fReplaceLength;
121                 else
122                     fPosition.offset += fReplaceLength;
123             }
124
125         } finally {
126             segment.clearMark();
127         }
128     }
129 }
130
Popular Tags