129
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.projection.FragmentUpdater;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import dwtx.jface.text.BadLocationException;
|
|
19 import dwtx.jface.text.BadPositionCategoryException;
|
|
20 import dwtx.jface.text.DefaultPositionUpdater;
|
|
21 import dwtx.jface.text.DocumentEvent;
|
|
22 import dwtx.jface.text.IDocument;
|
|
23 import dwtx.jface.text.Position;
|
|
24
|
|
25
|
|
26 /**
|
|
27 * The position updater used to adapt the fragments of a master document. If an
|
|
28 * insertion happens at a fragment's offset, the fragment is extended rather
|
|
29 * than shifted. Also, the last fragment is extended if an insert operation
|
|
30 * happens at the end of the fragment.
|
|
31 *
|
|
32 * @since 3.0
|
|
33 */
|
|
34 class FragmentUpdater : DefaultPositionUpdater {
|
|
35
|
|
36 /** Indicates whether the position being updated represents the last fragment. */
|
|
37 private bool fIsLast= false;
|
|
38
|
|
39 /**
|
|
40 * Creates the fragment updater for the given category.
|
|
41 *
|
|
42 * @param fragmentCategory the position category used for managing the fragments of a document
|
|
43 */
|
|
44 protected FragmentUpdater(String fragmentCategory) {
|
|
45 super(fragmentCategory);
|
|
46 }
|
|
47
|
|
48 /*
|
|
49 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent)
|
|
50 */
|
|
51 public void update(DocumentEvent event) {
|
|
52
|
|
53 try {
|
|
54
|
|
55 Position[] category= event.getDocument().getPositions(getCategory());
|
|
56
|
|
57 fOffset= event.getOffset();
|
|
58 fLength= event.getLength();
|
|
59 fReplaceLength= (event.getText() is 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 fIsLast= (i is category.length -1);
|
|
66
|
|
67 fOriginalPosition.offset= fPosition.offset;
|
|
68 fOriginalPosition.length= fPosition.length;
|
|
69
|
|
70 if (notDeleted())
|
|
71 adaptToReplace();
|
|
72 }
|
|
73
|
|
74 } catch (BadPositionCategoryException x) {
|
|
75 // do nothing
|
|
76 }
|
|
77 }
|
|
78
|
|
79 /*
|
|
80 * @see dwtx.jface.text.DefaultPositionUpdater#adaptToInsert()
|
|
81 */
|
|
82 protected void adaptToInsert() {
|
|
83 int myStart= fPosition.offset;
|
|
84 int myEnd= Math.max(myStart, fPosition.offset + fPosition.length - (fIsLast || isAffectingReplace() ? 0 : 1));
|
|
85
|
|
86 if (myEnd < fOffset)
|
|
87 return;
|
|
88
|
|
89 if (fLength <= 0) {
|
|
90
|
|
91 if (myStart <= fOffset)
|
|
92 fPosition.length += fReplaceLength;
|
|
93 else
|
|
94 fPosition.offset += fReplaceLength;
|
|
95
|
|
96 } else {
|
|
97
|
|
98 if (myStart <= fOffset && fOriginalPosition.offset <= fOffset)
|
|
99 fPosition.length += fReplaceLength;
|
|
100 else
|
|
101 fPosition.offset += fReplaceLength;
|
|
102 }
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Returns whether this updater considers any position affected by the given document event. A
|
|
107 * position is affected if <code>event</code> {@link Position#overlapsWith(int, int) overlaps}
|
|
108 * with it but not if the position is only shifted.
|
|
109 *
|
|
110 * @param event the event
|
|
111 * @return <code>true</code> if there is any affected position, <code>false</code> otherwise
|
|
112 */
|
|
113 public bool affectsPositions(DocumentEvent event) {
|
|
114 IDocument document= event.getDocument();
|
|
115 try {
|
|
116
|
|
117 int index= document.computeIndexInCategory(getCategory(), event.getOffset());
|
|
118 Position[] fragments= document.getPositions(getCategory());
|
|
119
|
|
120 if (0 < index) {
|
|
121 Position fragment= fragments[index - 1];
|
|
122 if (fragment.overlapsWith(event.getOffset(), event.getLength()))
|
|
123 return true;
|
|
124 if (index is fragments.length && fragment.offset + fragment.length is event.getOffset())
|
|
125 return true;
|
|
126 }
|
|
127
|
|
128 if (index < fragments.length) {
|
|
129 Position fragment= fragments[index];
|
|
130 return fragment.overlapsWith(event.getOffset(), event.getLength());
|
|
131 }
|
|
132
|
|
133 } catch (BadLocationException x) {
|
|
134 } catch (BadPositionCategoryException x) {
|
|
135 }
|
|
136
|
|
137 return false;
|
|
138 }
|
|
139 }
|