KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > template > contentassist > InclusivePositionUpdater


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.jdt.internal.ui.text.template.contentassist;
12
13 import org.eclipse.jface.text.BadPositionCategoryException;
14 import org.eclipse.jface.text.DocumentEvent;
15 import org.eclipse.jface.text.IPositionUpdater;
16 import org.eclipse.jface.text.Position;
17
18 /**
19  * Position updater that takes any change in [position.offset, position.offset + position.length] as
20  * belonging to the position.
21  *
22  * @since 3.0
23  */

24 class InclusivePositionUpdater implements IPositionUpdater {
25
26     /** The position category. */
27     private final String JavaDoc fCategory;
28
29     /**
30      * Creates a new updater for the given <code>category</code>.
31      *
32      * @param category the new category.
33      */

34     public InclusivePositionUpdater(String JavaDoc category) {
35         fCategory= category;
36     }
37
38     /*
39      * @see org.eclipse.jface.text.IPositionUpdater#update(org.eclipse.jface.text.DocumentEvent)
40      */

41     public void update(DocumentEvent event) {
42
43         int eventOffset= event.getOffset();
44         int eventOldLength= event.getLength();
45         int eventNewLength= event.getText() == null ? 0 : event.getText().length();
46         int deltaLength= eventNewLength - eventOldLength;
47
48         try {
49             Position[] positions= event.getDocument().getPositions(fCategory);
50
51             for (int i= 0; i != positions.length; i++) {
52
53                 Position position= positions[i];
54
55                 if (position.isDeleted())
56                     continue;
57
58                 int offset= position.getOffset();
59                 int length= position.getLength();
60                 int end= offset + length;
61
62                 if (offset > eventOffset + eventOldLength)
63                     // position comes way
64
// after change - shift
65
position.setOffset(offset + deltaLength);
66                 else if (end < eventOffset) {
67                     // position comes way before change -
68
// leave alone
69
} else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
70                     // event completely internal to the position - adjust length
71
position.setLength(length + deltaLength);
72                 } else if (offset < eventOffset) {
73                     // event extends over end of position - adjust length
74
int newEnd= eventOffset + eventNewLength;
75                     position.setLength(newEnd - offset);
76                 } else if (end > eventOffset + eventOldLength) {
77                     // event extends from before position into it - adjust offset
78
// and length
79
// offset becomes end of event, length ajusted acordingly
80
// we want to recycle the overlapping part
81
position.setOffset(eventOffset);
82                     int deleted= eventOffset + eventOldLength - offset;
83                     position.setLength(length - deleted + eventNewLength);
84                 } else {
85                     // event consumes the position - delete it
86
position.delete();
87                 }
88             }
89         } catch (BadPositionCategoryException e) {
90             // ignore and return
91
}
92     }
93
94     /**
95      * Returns the position category.
96      *
97      * @return the position category
98      */

99     public String JavaDoc getCategory() {
100         return fCategory;
101     }
102
103 }
104
Popular Tags