KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.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 /**
23  * The position updater used to adapt the fragments of a master document. If an
24  * insertion happens at a fragment's offset, the fragment is extended rather
25  * than shifted. Also, the last fragment is extended if an insert operation
26  * happens at the end of the fragment.
27  *
28  * @since 3.0
29  */

30 class FragmentUpdater extends DefaultPositionUpdater {
31
32     /** Indicates whether the position being updated represents the last fragment. */
33     private boolean fIsLast= false;
34
35     /**
36      * Creates the fragment updater for the given category.
37      *
38      * @param fragmentCategory the position category used for managing the fragments of a document
39      */

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

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             // do nothing
72
}
73     }
74
75     /*
76      * @see org.eclipse.jface.text.DefaultPositionUpdater#adaptToInsert()
77      */

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     /**
102      * Returns whether this updater considers any position affected by the given document event. A
103      * position is affected if <code>event</code> {@link Position#overlapsWith(int, int) overlaps}
104      * with it but not if the position is only shifted.
105      *
106      * @param event the event
107      * @return <code>true</code> if there is any affected position, <code>false</code> otherwise
108      */

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